DEV Community

Robert Devasia
Robert Devasia

Posted on

Building a PostgreSQL Library in D

I was always curious about new programming languages and its frameworks. All of these time my experience and curiosity spread over only in front-end development ( I’ve done some backend though 😉). I challenged myself to expand my skills and I found D programming language. D is in simple words is the advanced version of C and CPP.

What is D? the website say “**D is a general-purpose programming language with static typing, systems-level access, and C-like syntax. With the D Programming Language, write fast, read fast, and run fast.

I have used PostgreSQL as my database for my works, and that’s why I chose it for this library as well. PostgreSQL is one of the major open source SQL database system that companies using now and its features are expanding even more.

Why Build a Custom ORM Library?

When messing with D language I couldn’t find one package that satisfies my, either the packages are stopped maintenance or can be used as for direct query. From a JavaScript background I used Sequalize ORM. That light me an idea, How about similar one in D.

So, I did some research and I found Postgres provides library for C. Then I thought, how about use the C bindings in D and use it for developing the ORM. I found the source code from https://github.com/adamdruppe/arsd/blob/master/postgres.d for binding the C library to D.

Getting Started

Requirements:

  • PostgreSQL must be installed in your system. ( I developed for PostgreSQL 16)
  • IDE (Zed/ VSCode/Vim)
  • DMD - d language compiler

To create a new project, use the following command in your terminal:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the command:
dub init <project_name>
Enter fullscreen mode Exit fullscreen mode

This command will create a new project directory with the specified name and set up the basic structure for a D project.

  1. You'll be prompted to enter the following information:
    • Format - .sdl or .json ( I selected json )
    • Description of the project (optional)
    • Author name
    • License (e.g., MIT, BSD, etc.)
    • Copyright string
    • Add dependency (optional)
  2. After providing the information, dub will create a new directory with your project name and generate the following files:
    • dub.json: Configuration file for your project
    • source/app.d: Main source file
    • .gitignore: Git ignore file
  3. Navigate into your new project directory: cd <project_name>
  4. You can now start developing your D project!

With these steps completed, you'll have a basic D project structure set up and ready for development.

In Windows the below section needs to be added on the dub.json.

"libs": [ "pq" ],
    "lflags-windows-x86_64": [ "-LIBPATH:C:/Program Files/PostgreSQL/16/lib/" ],
    "copyFiles-windows-x86_64": [
        "C:/Program Files/PostgreSQL/16/lib/libpq.dll",
        "C:/Program Files/PostgreSQL/16/bin/libintl-9.dll",
        "C:/Program Files/PostgreSQL/16/bin/libssl-3-x64.dll",
        "C:/Program Files/PostgreSQL/16/bin/libcrypto-3-x64.dll",
        "C:/Program Files/PostgreSQL/16/bin/libwinpthread-1.dll",
        "C:/Program Files/PostgreSQL/16/bin/libiconv-2.dll"
    ],
Enter fullscreen mode Exit fullscreen mode

or

The way I did is copied all the necessary DLL files to lib ( manually created) folder and then added the below code:

"copyFiles-windows": [
        "libs/*.dll"
    ],
    "lflags-windows": [
        "/LIBPATH:$PACKAGE_DIR/libs"
    ],
    "libs": [
        "pq"
    ]
Enter fullscreen mode Exit fullscreen mode

In Linux or macOS, you need to ensure that the PostgreSQL development libraries are installed and properly linked. You can typically do this by installing the appropriate packages through your system's package manager. For example, on Ubuntu or Debian-based systems, you might use:

sudo apt-get install libpq-dev
Enter fullscreen mode Exit fullscreen mode

Once you have the necessary libraries installed and properly linked, you can proceed with setting up your D project to work with PostgreSQL.

Implementing C bindings:

Here is the C bindings for D.

module postgres.implementation.implementationc;

extern (C)
{
    struct PGconn
    {
    }

    struct PGresult
    {
    }

    void PQfinish(PGconn*);

    PGconn* PQconnectdb(const char*);

    int PQstatus(PGconn*); // FIXME check return value

    const(char*) PQerrorMessage(PGconn*);

    char* PQresultVerboseErrorMessage(const PGresult* res,
        PGVerbosity verbosity,
        PGContextVisibility show_context);
    PGresult* PQexec(PGconn*, const char*);
    void PQclear(PGresult*);

    PGresult* PQprepare(PGconn*, const char* stmtName, const char* query,
        ulong nParams, const void* paramTypes);

    PGresult* PQexecPrepared(PGconn*, const char* stmtName,
        int nParams, const char** paramValues,
        const int* paramLengths, const int* paramFormats, int resultFormat);

    int PQresultStatus(PGresult*); // FIXME check return value

    int PQnfields(PGresult*); // number of fields in a result
    const(char*) PQfname(PGresult*, int); // name of field

    int PQntuples(PGresult*); // number of rows in result
    const(char*) PQgetvalue(PGresult*, int row, int column);

    size_t PQescapeString(char* to, const char* from, size_t length);

    enum int CONNECTION_OK = 0;
    enum int PGRES_COMMAND_OK = 1;
    enum int PGRES_TUPLES_OK = 2;
    enum int PGRES_FATAL_ERROR = 7;
    enum PGContextVisibility
    {
        PQSHOW_CONTEXT_NEVER,
        PQSHOW_CONTEXT_ERRORS,
        PQSHOW_CONTEXT_ALWAYS
    }

    enum PGVerbosity
    {
        PQERRORS_TERSE,
        PQERRORS_DEFAULT,
        PQERRORS_VERBOSE,
        PQERRORS_SQLSTATE
    }

    int PQgetlength(const PGresult* res,
        int row_number,
        int column_number);
    int PQgetisnull(const PGresult* res,
        int row_number,
        int column_number);

    int PQfformat(const PGresult* res, int column_number);

    alias Oid = int;
    enum BYTEAOID = 17;
    Oid PQftype(const PGresult* res, int column_number);

    char* PQescapeByteaConn(PGconn* conn,
        const ubyte* from,
        size_t from_length,
        size_t* to_length);
    char* PQunescapeBytea(const char* from, size_t* to_length);
    void PQfreemem(void* ptr);

    char* PQcmdTuples(PGresult* res);

}

Enter fullscreen mode Exit fullscreen mode

Now we can easily use these functions in D.
This is the code for some basic exception handling:

module postgres.implementation.exception;

public:
import std.conv;

private import postgres.implementation.implementationc;

class PGSqlException : Exception
{
    string code;
    string sqlState;
    string message;
    this(PGconn* conn, PGresult* res = null)
    {
        if (res != null)
        {
            char* c = PQresultVerboseErrorMessage(res, PGVerbosity.PQERRORS_VERBOSE, PGContextVisibility
                    .PQSHOW_CONTEXT_ALWAYS);
            char* s = PQresultVerboseErrorMessage(res, PGVerbosity.PQERRORS_SQLSTATE, PGContextVisibility
                    .PQSHOW_CONTEXT_ALWAYS);
           string ss = to!string(c);
           import std.string:split;
           this.code = to!string(ss.split(':')[1]);

            this.sqlState = to!string(s);
        }
        const char* m = PQerrorMessage(conn);

        this.message = to!string(m);
        super(this.message);
    }
}

class DuplicateKeyException : Exception
{
    this(string message)
    {
        super(message);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • PGSqlException: A custom exception class that inherits from the standard D Exception class. It's designed to handle PostgreSQL-specific errors.
  • Fields:
    • code: Stores the error code
    • sqlState: Stores the SQL state
    • message: Stores the error message
  • Constructor: Takes a PGconn* (PostgreSQL connection) and an optional PGresult* (result of a query). PQresultVerboseErrorMessage and PQerrorMessage to extract detailed error information.
  • DuplicateKeyException: A simple exception class for handling duplicate key errors. It only takes a message parameter and passes it to the base Exception class.

I will be adding more exceptions and other situations as I work on this project

Now create create a implementation/core/core.d file for writing the connection code.

module postgres.implementation.core;

import std.string;

public:
import std.conv;
import std.stdio;

private import postgres.implementation.implementationc;

import postgres._internal.connection;
import postgres.implementation.exception : PGSqlException;
import std.variant;

class Postgres
{
    private PGconn* conn;

    private string connection;
    this(DatabaseConnectionOption dco)
    {
        connection = i"host=$(dco.host) 
                              port=$(dco.port) 
                              dbname=$(dco.database) 
                              user=$(dco.user)
                              password=$(dco.password) 
                              client_encoding=$(dco.client_encoding?dco.client_encoding:`utf8`) 
                              application_name=$(dco.application_name)
                              sslmode=$(dco.ssl ? `require` : `disable`)".text;
        connect(connection);
    }

    ~this()
    {
        PQfinish(this.conn);
    }

    private void connect(string connectionString)
    {
        this.conn = PQconnectdb(toStringz(connectionString));

        if (PQstatus(this.conn) != CONNECTION_OK)
        {
            throw new PGSqlException(this.conn);
        }
        else
        {
            query("SET NAMES 'utf8'");
        }
    }

    QueryResult query(string sql)
    {
        bool first_retry = true;
    retry:
        PGresult* res = PQexec(this.conn, toStringz(sql));

        int status = PQresultStatus(res);
        if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)
        {
            import std.stdio;

            QueryResult result = new QueryResult(res);
            return result;
        }
        else
        {
            if (first_retry && to!string(PQerrorMessage(conn)) == "no connection to server\n")
            {
                first_retry = false;
                // try to reconnect...
                PQfinish(conn);
                connect(connection);
                goto retry;
            }
            throw new PGSqlException(this.conn, res);
        }

    }

    QueryResult executePreparedStatement(string name, string sql, Variant[] args)
    {
        const(char)*[] argsStrings;

        for (int i = 0; i < args.length; i++)
        {
            Variant arg = args[i];
            if (!is(typeof(arg) == typeof(null))) // auto str = to!string(arg);
            {
                argsStrings ~= toStringz(to!string(arg));
            }

        }

        //  paramTypes set to null; for postgres to infer types; Need fix if project become advanceds
        PGresult* pres = PQprepare(conn, toStringz(name), toStringz(sql), argsStrings.length, null);
        int press = PQresultStatus(pres);
        if (press != PGRES_TUPLES_OK
            && press != PGRES_COMMAND_OK)
        {
            throw new PGSqlException(conn, pres);
        }
        else
        {
            PGresult* res = PQexecPrepared(conn, toStringz(name),
                argsStrings.length.to!int, argsStrings.ptr, null, null, 0);
            int ress = PQresultStatus(res);
            query(i"DEALLOCATE $(name)".text);
            if (ress != PGRES_TUPLES_OK
                && ress != PGRES_COMMAND_OK)
                throw new PGSqlException(conn, res);

            return new QueryResult(res);
        }

    }

}

class QueryResult
{

    private int rowSize;
    private int colSize;
    string[string][size_t] rows;
    private PGresult* res;

    this(PGresult* r)
    {
        this.res = r;
        rowSize = PQntuples(r);
        colSize = PQnfields(r);
        generateRows();
    }

    ~this()
    {
        PQclear(res);
    }

    private void generateRows()
    {
        import std.typecons : Tuple;

        for (int j = 0; j < rowSize; j++)
        {
            string[string] r;
            for (int i = 0; i < colSize; i++)
            {
                string a;

                if (PQgetisnull(res, j, i))
                    a = null;
                else
                {

                    switch (PQfformat(res, i))
                    {
                    case 0: // text representation
                    {
                            switch (PQftype(res, i))
                            {
                            case BYTEAOID:
                                size_t len;
                                char* c = PQunescapeBytea(PQgetvalue(res, j, i), &len);
                                a = cast(string) c[0 .. len].idup;

                                PQfreemem(c);
                                break;
                            default:
                                a = to!string(PQgetvalue(res, j, i));
                            }
                            break;
                        }
                    case 1: // binary representation
                        throw new Exception("unexpected format returned by pq");
                    default:
                        throw new Exception("unknown pq format");
                    }
                }
                // import std.stdio;
                r[to!string(PQfname(res, i))] = a;

            }
            rows[j] = r;
        }

    }
}

// struct Row {

// }

Enter fullscreen mode Exit fullscreen mode

Key points of the above code:

  • Postgres class: Represents a PostgreSQL database connection.
    • Manages connection creation, querying, and prepared statement execution.
    • Uses the C bindings defined earlier to interact with the PostgreSQL library.
  • QueryResult class: Encapsulates the result of a database query.
    • Stores query results in a structured format.
    • Handles different data types and formats returned by PostgreSQL.
  • Error handling: Implements custom exception handling for PostgreSQL errors.
  • Connection management: Includes automatic reconnection attempts on connection loss.
  • Prepared statements: Supports execution of prepared SQL statements with parameter binding.
  • Memory management: Properly frees resources using destructors (~this()).
  • UTF-8 support: Sets the connection encoding to UTF-8 by default.

This implementation provides a high-level interface for D applications to interact with PostgreSQL databases, abstracting away many of the low-level details of the C API.

You might have an IDE warning/error now that “connection module not found

Let’s create connection module:

Create _internal/connection.d file add this code:

module postgres._internal.connection;

struct DatabaseConnectionOption
{
    // STRING_CONNECTION_OPTION_MAP 
    string application_name;
    string client_encoding;
    string database;
    string host;
    string password;
    string user;
    // BOOLEAN_CONNECTION_OPTION_MAP
    bool binary = false;
    bool keepAlive = false;
    bool ssl = false;

    // NUMBER_CONNECTION_OPTION_MAP
    long port;
    long statement_timeout;
    long query_timeout;
    long keepAliveInitialDelayMillis;
    long idle_in_transaction_session_timeout;
    long connectionTimeoutMillis;
    long lock_timeout;
}

Enter fullscreen mode Exit fullscreen mode

Add constants and other options for SQL:

_internal/consts.d

module postgres._internal.consts;
import std.typecons;
import std.json;
public import std.stdio;

string[] reserveredWords = [
    "insert", "update", "delete", "findOne", "where", "findAll",
];

struct Type
{
    DataTypes type;
}

struct NotNull
{
    bool isNullable = false;
}

struct PmKey
{
    bool isPrimary = true;
}

struct DefaultValue
{
    string defaultValue;
}

struct ForeignKey
{
    string table;
    string referenceKey;
}

struct Unique
{
    bool isUnique;
}

enum DataTypes : string
{
    // Basic Types
    SERIAL = "SERIAL",
    INTEGER = "INTEGER",
    BIGINT = "BIGINT",
    SMALLINT = "SMALLINT",
    FLOAT = "FLOAT",
    REAL = "REAL",
    DOUBLE_PRECISION = "DOUBLE PRECISION",
    NUMERIC = "NUMERIC",
    BOOLEAN = "BOOLEAN",
    CHAR = "CHAR",
    VARCHAR = "VARCHAR",
    TEXT = "TEXT",
    DATE = "DATE",
    TIME = "TIME",
    TIMESTAMP = "TIMESTAMP",
    BYTEA = "BYTEA",

    // 1D Array Types
    INTEGER_ARRAY = "INTEGER[]",
    BIGINT_ARRAY = "BIGINT[]",
    SMALLINT_ARRAY = "SMALLINT[]",
    FLOAT_ARRAY = "FLOAT[]",
    REAL_ARRAY = "REAL[]",
    DOUBLE_PRECISION_ARRAY = "DOUBLE PRECISION[]",
    NUMERIC_ARRAY = "NUMERIC[]",
    BOOLEAN_ARRAY = "BOOLEAN[]",
    CHAR_ARRAY = "CHAR[]",
    VARCHAR_ARRAY = "VARCHAR[]",
    TEXT_ARRAY = "TEXT[]",
    DATE_ARRAY = "DATE[]",
    TIME_ARRAY = "TIME[]",
    TIMESTAMP_ARRAY = "TIMESTAMP[]",
    BYTEA_ARRAY = "BYTEA[]",

    // 2D Array Types
    INTEGER_ARRAY_2D = "INTEGER[][]",
    BIGINT_ARRAY_2D = "BIGINT[][]",
    SMALLINT_ARRAY_2D = "SMALLINT[][]",
    FLOAT_ARRAY_2D = "FLOAT[][]",
    REAL_ARRAY_2D = "REAL[][]",
    DOUBLE_PRECISION_ARRAY_2D = "DOUBLE PRECISION[][]",
    NUMERIC_ARRAY_2D = "NUMERIC[][]",
    BOOLEAN_ARRAY_2D = "BOOLEAN[][]",
    CHAR_ARRAY_2D = "CHAR[][]",
    VARCHAR_ARRAY_2D = "VARCHAR[][]",
    TEXT_ARRAY_2D = "TEXT[][]",
    DATE_ARRAY_2D = "DATE[][]",
    TIME_ARRAY_2D = "TIME[][]",
    TIMESTAMP_ARRAY_2D = "TIMESTAMP[][]",
    BYTEA_ARRAY_2D = "BYTEA[][]"
}

enum DefaultDateType
{
    date = "CURRENT_DATE",
    time = "CURRENT_TIME",
    timestamp = "CURRENT_TIMESTAMP"
}

import std.array : join;
import std.algorithm : map;
import std.variant;
import postgres._internal.helpers;
import phobos.sys.meta;

class WhereClause
{
    string symbol;
    string col;
    string val;
    this(string symbol, string col, string val)
    {
        this.symbol = symbol;
        this.col = col;
        this.val = val;
    }

    this()
    {

    }

    static WhereClause eq(T)(string column, T value)
    {
        import std.conv;

        return new WhereClause("=", column, to!string(value));
    }

    static WhereClause notEq(T)(string column, T value)
    {
        import std.conv;

        return new WhereClause("!=", column, to!string(value));
    }

    static WhereClause gt(T)(string column, T value)
    {
        import std.conv;

        return new WhereClause(">", column, to!string(value));
    }

    static WhereClause lt(T)(string column, T value)
    {
        import std.conv;

        return new WhereClause("<", column, to!string(value));
    }

    static WhereClause gtOrEq(T)(string column, T value)
    {
        import std.conv;

        string a;
        if (typeof(value) == string)
        {
            a = "'" ~ to!string(value) ~ "'";
        }
        else
        {
            a = to!string(value);
        }
        return new WhereClause(">=", column,);
    }

    static WhereClause ltOrEq(T)(string column, T value)
    {
        import std.conv;

        string a;
        if (typeof(value) == string)
        {
            a = "'" ~ to!string(value) ~ "'";
        }
        else
        {
            a = to!string(value);
        }

        return new WhereClause("<=", column, to!string(value));
    }

    static WhereClause like(string column, string pattern)
    {
        return new WhereClause("LIKE", column, "'" ~ pattern ~ "'");
    }

    static WhereClause notLike(string column, string pattern)
    {
        return new WhereClause("NOT LIKE", column, "'" ~ pattern ~ "'");
    }

    static WhereClause isNull(string column)
    {
        return new WhereClause("IS NULL", column, "");
    }

    static WhereClause isNotNull(string column)
    {
        return new WhereClause("IS NOT NULL", column, "");
    }

    static WhereClause inValues(T)(string column, T[] values)
    {
        import std.conv;
        import std.array;

        auto formattedValues = values.map!(v => () {
            string a;
            if (is(typeof(v) == string))
            {
                return a = "'" ~ to!string(v) ~ "'";
            }
            else
            {
                return to!string(v);
            }
        }).join(",");
        return new WhereClause("IN", column, "(" ~ formattedValues ~ ")");
    }

    static WhereClause notInValues(T)(string column, T[] values)
    {
        import std.conv;
        import std.array;

        auto formattedValues = values.map!(v => () {
            string a;
            if (is(typeof(v) == string))
            {
                return a = "'" ~ to!string(v) ~ "'";
            }
            else
            {
                return to!string(v);
            }
        }).join(",");
        return new WhereClause("NOT IN", column, "(" ~ formattedValues ~ ")");
    }

    static WhereClause between(T)(string column, T lower, T upper)
    {
        import std.conv;

        string a;
        string b;
        if (is(typeof(lower)) == string)
        {
            a = "'" ~ to!string(lower) ~ "'";
        }
        else
        {
            a = to!string(lower);
        }
        if (is(typeof(lower)) == string)
        {
            b = "'" ~ to!string(upper) ~ "'";
        }
        else
        {
            b = to!string(upper);
        }
        return new WhereClause("BETWEEN", column, lower ~ " AND " ~ upper);
    }

    static WhereClause notBetween(T)(string column, T lower, T upper)
    {
        import std.conv;

        string a;
        string b;
        if (is(typeof(lower)) == string)
        {
            a = "'" ~ to!string(lower) ~ "'";
        }
        else
        {
            a = to!string(lower);
        }
        if (is(typeof(lower)) == string)
        {
            b = "'" ~ to!string(upper) ~ "'";
        }
        else
        {
            b = to!string(upper);
        }

        return new WhereClause("NOT BETWEEN", column, lower ~ " AND " ~ upper);
    }

}

enum Seperater
{
    AND = "AND",
    OR = "OR"
}

auto generateWhereClause(WhereClause...)(string tableName, Seperater separator = Seperater.AND,
    int count = 0, WhereClause args)
{
    import std.conv;
    import std.meta : Repeat;

    string[] c;
    Tuple!(Repeat!(args.length, string)) a;

    foreach (i, condition; args)
    {
        c ~= `"`~tableName~`"`~"."~condition.col ~ " " ~ condition.symbol ~ " " ~ "$" ~ to!string(count) ~ " ";
        a[i] = condition.val;
        auto tup = condition.tupleof;

        // valuesTuple[i] = tup;
        count++;
    }

struct ModelMetaData
{
    string tableName;
    string primaryKey;
    JSONValue relations = parseJSON("[]");
    JSONValue columns;
    string[string] colValues;
    // string[string] meta = null;
    string[] autoIncrementals = [];
}

struct InsertionOptions
{
}

struct UpdateOptions
{

}

struct SelectOptions
{
    string[] cols;
    Includes[] includes;
    Seperater seperator = Seperater.AND;
    int limit;
    int offset;
    string orderBy;
    string order;
    string groupBy;
    string having;
    // SelectOptions exclude;
}

import postgres.model;

struct Includes
{
    // Schema table;
    string table;
    SelectOptions options;
}
// Tuple!() arrayToTuple(T)(T[] arr, size_t idx = 0) {
//     return tuple();
// }

Enter fullscreen mode Exit fullscreen mode

Creating Model Template

D supports template metaprogramming, a feature that allows you to write highly generic code. This means that D has templates similar to those in C++ but more powerful and flexible.
The ABC’s of Templates in D | The D Blog

Key features of D's templates:

  1. Compile-time type checking: Templates are checked at compile time, ensuring type safety.
  2. Code generation: You can use templates to generate specialized code for different types or values.
  3. Variadic templates: D supports templates that can take an arbitrary number of arguments, including types and values.
  4. Static ifs and mixins: These allow you to generate and manipulate code during compilation based on conditions or even inject string-based code (with mixin).

Now lets create a template class.

model.d

Now use the code from https://github.com/rodevasia/sequelized/blob/main/source/postgres/model.d paste to your file

Let's examine the code from the provided GitHub link:

module postgres.model;

import std.traits;
import std.meta;
import std.typecons;
import std.string;
import std.conv;
import std.algorithm;
import std.array;

import postgres.attributes;
import postgres.connection;

mixin template Model()
{
    // ... (rest of the code)
}
Enter fullscreen mode Exit fullscreen mode

This code defines a template class Model in D. Here's a breakdown of its key components:

  1. Module declaration: The code is part of the postgres.model module.
  2. Imports: Various standard D libraries and custom modules are imported for use in the class.
  3. Template class: The Model class is defined as a template with type parameter T. This allows the class to work with different types.
  4. Class methods: The class includes several methods for database operations such as save(), update(), delete(), and find().
  5. Compile-time reflection: The code uses D's compile-time features to inspect the fields of the type T and generate appropriate SQL queries.
  6. SQL query generation: Methods like getInsertQuery() and getUpdateQuery() dynamically create SQL queries based on the structure of type T.
  7. Database interaction: The class uses a Connection object to interact with a PostgreSQL database.

We written all the code for working. Let’s make this a library. add this on your dub.json

"targetType": "library"
Enter fullscreen mode Exit fullscreen mode

Using the Library:

Let create a new project:

$ dub init
Select a package recipe format:
1) sdl    2) json
#? [2]: 2
Name [planner-d]: example
Description [A minimal D application.]:
Author name []: Robert Devasia
Select or enter an SPDX license-identifier (https://spdx.org/licenses/):
 1) BSL-1.0 (Boost)               8) BSD-
 2) MIT                           9) MPL- (Mozilla)
 3) Unlicense (public domain)    10) EUPL-
 4) Apache-                      11) CC- (Creative Commons)
 5) AGPL-                        12) Zlib
 6) GPL-                         13) ISC
 7) LGPL-                        14) proprietary
? [14]:
Copyright string [Copyright ┬® 2024, Robert Devasia]:
Add dependency (leave empty to skip) []:

Enter fullscreen mode Exit fullscreen mode

add the the library as dependency in dub.json

"dependencies": {
        "sequalized": {
            "path": "<PATH_TO_LIBRARY>"
        }
    },
Enter fullscreen mode Exit fullscreen mode

app.d

import std.stdio;
import postgres._internal.connection;
import postgres.implementation.core;
import postgres.model;
import postgres._internal.consts;
import std.compiler;

void main()
{
    DatabaseConnectionOption options = DatabaseConnectionOption();
    options.host = "localhost";
    options.port = 5432;
    options.database = "postgres";
    options.user = "postgres";
    options.password = "postgres";
    Postgres pg = new Postgres(options);
    Example test = new Example(pg);
    test.sync(); // Create table
    test.textField = "Hello, World!";
    test.insert(); // inserts the record
}

class Example
{
    mixin Model;

    @Type(DataTypes.INTEGER)
    @PmKey
    @Unique
    ulong id;
    @Type(DataTypes.VARCHAR)
    string textField;
}

Enter fullscreen mode Exit fullscreen mode

Let's break down the code and explain its main components:

Imports

The code imports necessary modules from the standard library and the Sequalized library:

  • std.stdio: For basic input/output operations
  • postgres._internal.connection: Handles database connection details
  • postgres.implementation.core: Core functionality for PostgreSQL operations
  • postgres.model: Provides the Model mixin for defining database models
  • postgres._internal.consts: Contains constant values used in the library

Main Function

The main function demonstrates how to use the Sequalized library:

  • It creates a DatabaseConnectionOption object with connection details
  • Initializes a Postgres object with these options
  • Creates an instance of the Example class
  • Calls sync() to create the corresponding table in the database
  • Sets a value for the textField and inserts a record into the database

Example Class

This class defines a model for the database table:

  • It uses the Model mixin to inherit ORM functionality
  • Defines two fields: id and textField
  • Uses attributes like @Type, @PmKey, and @unique to specify field properties

I haven't included full process, and that is for you to find out :)

If you'd love to contribute to my project here is the link for the repo:
https://github.com/rodevasia/sequelized

Top comments (0)