Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

3 Commits

Repository files navigation

zig-orm

This is a work in progress. Crashes are expected to happen.

A database ORM. Currently only supports PostgreSQL but the goal is to let other database drivers be easily written. The current PostgreSQL driver uses libpq.

Getting Started

First, a model needs to be defined. A model requires two definitions, Table which denotes the table name, and Allocator which specifies the allocator used on a select query.

constUser=struct {
// required declarations used by the ormpubconstTable="test_table";
pubconstAllocator=std.testing.allocator;
test_value: []constu8,
test_num: u32,
test_bool: bool,
};

Next, a database connection needs to be established.

constPqDatabase=Database(PqDriver);
vardb=PqDatabase.init(std.testing.allocator);
trydb.connect("postgres://testuser:testpassword@localhost:5432/testdb");

Now, we can try to insert a User.

varnew_user=User{ .test_value="foo",
.test_num=42,
.test_bool=true
};
trydb.insert(User, new_user).send();

Or, we can select a User and even specify "where" conditions.

if (trydb.select(User).where(.{ .test_value="foo" }).send()) |model| {
// required to clean up the select resultdeferdb.deinitModel(model);
std.testing.expect(std.mem.eql(u8, model.test_value, "foo"));
}

You can also specify the select query to return an array of models

if (trydb.select([]UserModel).send()) |models| {
// required to clean up the select resultdeferdb.deinitModel(models);
}

About

A database ORM

Resources

Stars

28 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages