Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.
/ zorm Public archive

Lightweight and efficient object-relational mapping

License

Notifications You must be signed in to change notification settings

ziglibs/zorm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Abstract

Notice
This library has moved over to Snowflake.

zorm is not your traditional ORM tool. Unlike other ORMs, zorm is designed to provide types for making object-relational mapping within your scripts simple and not reliant on database table paradigms.

In zorm, objects are cast on manually defined fields that can be compared:

Object mapping

const std = @import("std");

// In this example, we'll be mapping our object from JSON.
// For this very specific use case, zorm will handle assuming types to be tight and memory-bound.
const dumb_payload =
    \\{
    \\   "foo": "hello, world",
    \\   "bar": 420
    \\}
;

const zorm = @import("zorm");

// Objects are defined as a sort of "factory method." The inner argument is a comptime-known
// anonymous struct literal containing fields.
const Foo = zorm.Object(.{
    // (comptime T: type, data: ?FieldMetadata)
    // Fields can have a name, description and default value.
    zorm.Field(?[]const u8, .{ .name = "foo", .default = undefined }),
    zorm.Field(usize, .{ .name = "bar" })
});

pub fn main() !void {
    // With an object defined, we can now generate our own from our payload.
    // (comptime T: type, data: anytype)
    const myFoo: zorm.Object = try zorm.create(Foo, .{ .JsonString = dumb_payload });

    // Accessing data is now done through the newly created object.
    std.debug.print("{any}\n", .{myFoo.get("foo")});
}

Using datatypes

zorm provides you a set of datatypes. An example of a datatype is Date:

pub fn main() !void {
    // We can build a date from a string, useful for defaults.
    const date = try zorm.Date.fromString("2002-07-23", .@"YYYY-MM-DD");

    // This is an ISO 8601 format, which zorm will intelligently determine
    const payload =
        \\{
        \\    "date": "1999-12-31"
        \\}
    ;
    const NewTable = try zorm.create(
        zorm.Object(.{
            // Default values must either be undefined or part of the type specified in the field,
            // as expected when working with structs.
            zorm.Field(?zorm.Date, .{ .name = "date", .default = date })
        }),
        .{ .JsonString = payload }
    );

    // 1999 will be returned instead of 2002.
    std.debug.print("{?}\n", .{NewTable.get("date").f_type.year});
}

Building

zorm runs on 0.10.0-dev and higher versions of Zig.

It is recommended to install and build from source:

$ git clone --recursive https://github.com/ziglibs/zorm
$ cd ./zorm
$ zig build

Sponsor this project

 

Languages