Skip to content

Commit

Permalink
zig- db- add create methods to Remote and User
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Oct 22, 2021
1 parent 84af863 commit 88c36af
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/db/Remote.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const _internal = @import("./_internal.zig");
const db = &_internal.db;

pub const Remote = struct {
id: u64,
id: u64 = 0,
uuid: ulid.ULID,
type: Type,
domain: string,
Expand Down Expand Up @@ -43,4 +43,15 @@ pub const Remote = struct {
if (all_remotes.len > 0) return all_remotes;
return db.collect(alloc, Remote, "select * from " ++ table_name, .{});
}

pub fn create(alloc: *std.mem.Allocator, ty: Type, domain: string) !Remote {
const held = db.mutex.acquire();
defer held.release();

return _internal.insert(alloc, &Remote{
.uuid = _internal.factory.newULID(),
.type = ty,
.domain = domain,
});
}
};
4 changes: 4 additions & 0 deletions src/db/Time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ pub const Time = struct {
// RFC1123
return try self.formatAlloc(alloc, "ddd, DD MMM YYY HH:mm:ss z");
}

pub fn now() Self {
return .{ .ctx = time.DateTime.now() };
}
};
15 changes: 14 additions & 1 deletion src/db/User.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const _internal = @import("./_internal.zig");
const db = &_internal.db;

pub const User = struct {
id: u64,
id: u64 = 0,
uuid: ulid.ULID,
provider: u64,
snowflake: string,
Expand All @@ -20,6 +20,19 @@ pub const User = struct {

pub const table_name = "users";

pub fn create(alloc: *std.mem.Allocator, provider: u64, snowflake: string, name: string) !User {
const held = db.mutex.acquire();
defer held.release();

return try _internal.insert(alloc, &User{
.uuid = _internal.factory.newULID(),
.provider = provider,
.snowflake = snowflake,
.name = name,
.joined_on = Time.now(),
});
}

usingnamespace _internal.ByKeyGen(User);

pub const findPackageBy = _internal.FindByGen(User, Package, .owner, .uuid).first;
Expand Down

0 comments on commit 88c36af

Please sign in to comment.