Database

Brian Zou edited this page Dec 22, 2018 · 4 revisions

Config

config file in hunt:

config/application.conf

set values:

hunt.database.default.driver=postgresql
hunt.database.default.host=127.0.0.1
hunt.database.default.port=5432
hunt.database.default.database=test
hunt.database.default.username=root
hunt.database.default.password=
hunt.database.default.charset=utf8

Create model

source file:

source/app/model/User.d

source code:

module app.model.User;

import entity;

@Table("users")
class User
{
    mixin MakeModel;

    @AutoIncrement
    @PrimaryKey
    int id;

    string name;

    string email;
}

Create Repository

Easy query result, we need to create UserRepository. source file:

source/app/repository/UserRepository.d

source code:

module app.repository.UserRepository;

import app.model.User;

import entity;

class UsersRepository : EntityRepository!(User, int)
{
}

Query database result

easy to query.

auto repository = new UserRepository;
auto user = repository.findById( id );

easy to delete.

auto repository = new UserRepository;
repository.remove( id );

easy to insert.

auto repository = new UserRepository;
User user = new User;
user.name = "Brian";
user.email = "zoujiaqing@gmail.com";
auto user = repository.save( user );

logDebug(user.id);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.