rorm-orm / dorm

D frontend for rorm
MIT License
15 stars 1 forks source link

DORM

License Dub downloads Integration Tests

A sophisticated D ORM using rorm-lib as a backend.

Works standalone using multi-threading or with vibe-core.

The following databases are currently supported:

Documentation

Take a look at rorm-orm/docs or just use the deployed documentation: rorm.rs.

Auto-generated source code documentation is available on https://dorm.dpldocs.info/.

Installation

dub add dorm

When first compiling, DORM will automatically download pre-compiled library binaries into the package location when it hasn't been downloaded yet. You can also manually put the librorm.a (Posix) or rorm.lib (Windows) file into the DUB package to not make it download anything.

You can use dub run dorm to run the supporting CLI tool, which will be downloaded at the same time. rorm-cli is used for creating migrations, which you check-in into your project source and to apply migrations both on development machines as well as on production machines.

Example

For a more detailed walkthrough see rorm.rs

module models;

import dorm.design;

mixin RegisterModels;

class User : Model
{
    @Id long id;

    @maxLength(255)
    string username;

    @maxLength(255)
    string password;

    @autoCreateTime
    SysTime createdAt;

    @columnName("admin")
    bool isAdmin;

    @constructValue!(() => Clock.currTime + 24.hours)
    SysTime tempPasswordTime;
}
import models;

import faked, std.datetime.systime, std.random, std.stdio;

import dorm.api.db;

mixin SetupDormRuntime;

void main() {
    @DormPatch!User
    struct UserSelection {
        long id;
        string username;
        SysTime createdAt;
    }

    auto appConfig = parseTomlConfig!BareConfiguration("database.toml");
    auto db = DormDB(appConfig.database);

    auto f = new Faker_de(uniform!int);

    foreach (i; 0 .. 2) {
        @DormPatch!User
        struct UserInsert {
            string username;
            string password;
            bool isAdmin;
        }

        UserInsert user;
        user.username = f.nameName;
        user.password = "123456";
        db.insert(user);
    }

    auto oldestUsers = db.select!UserSelection
        .condition(u => u.not.isAdmin)
        .orderBy(u => u.createdAt.asc)
        .limit(5)
        .stream();

    writeln("Oldest 5 Users:");
    foreach (i, user; oldestUsers) {
        writefln!"#%d %s\tcreated at %s"(i + 1, user.username, user.createdAt);

        // delete first user
        if (i == 0)
            db.remove(user);
    }
}

For a more detailed walkthrough see rorm.rs

Contribution

Before contribution, see the development guidelines.