This commit is contained in:
Bob Farrell 2024-09-08 18:11:41 +01:00
parent 198754eef2
commit b179007344
6 changed files with 35 additions and 11 deletions

View File

@ -203,6 +203,14 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
exe_static_routes.root_module.addImport("jetzig", jetzig_module); exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module); exe_static_routes.root_module.addImport("zmpl", zmpl_module);
exe_static_routes.root_module.addImport("main", main_module); exe_static_routes.root_module.addImport("main", main_module);
// TODO: Prevent failure if schema file not present.
const schema_module = b.createModule(.{ .root_source_file = b.path("src/app/database/Schema.zig") });
schema_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("routes", routes_module);
exe_static_routes.root_module.addImport("jetzig", jetzig_module);
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
exe_static_routes.root_module.addImport("Schema", schema_module);
const markdown_fragments_write_files = b.addWriteFiles(); const markdown_fragments_write_files = b.addWriteFiles();
const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b)); const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b));

View File

@ -7,8 +7,8 @@
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163", .hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163",
}, },
.zmpl = .{ .zmpl = .{
.url = "https://github.com/jetzig-framework/zmpl/archive/84d16cc7da6739de6f8c581d9f2e4b216fba3351.tar.gz", .url = "https://github.com/jetzig-framework/zmpl/archive/a88a06cf43ece8b303a87f52d611b874ad06af9c.tar.gz",
.hash = "122032bd0acbd20b682edcb43fd91ef340bbff0e751d2deb3086abab8decf2c6ace5", .hash = "12202af84e839619429496900c3280fbfb7c3a3ce80ac0c2e70b99080c480fb22aa3",
}, },
.jetkv = .{ .jetkv = .{
.url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz", .url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz",

View File

@ -8,6 +8,12 @@ pub const Global = if (@hasDecl(@import("main"), "Global"))
else else
jetzig.DefaultGlobal; jetzig.DefaultGlobal;
pub const database_lazy_connect = true;
pub const jetzig_options = struct {
pub const Schema = @import("Schema");
};
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok); defer std.debug.assert(gpa.deinit() == .ok);

View File

@ -11,6 +11,7 @@ env: jetzig.Environment,
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
custom_routes: std.ArrayList(jetzig.views.Route), custom_routes: std.ArrayList(jetzig.views.Route),
initHook: ?*const fn (*App) anyerror!void, initHook: ?*const fn (*App) anyerror!void,
server: *jetzig.http.Server = undefined,
pub fn deinit(self: *const App) void { pub fn deinit(self: *const App) void {
@constCast(self).custom_routes.deinit(); @constCast(self).custom_routes.deinit();
@ -72,6 +73,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
var repo = try jetzig.database.repo( var repo = try jetzig.database.repo(
self.allocator, self.allocator,
jetzig.config.get(?jetzig.database.DatabaseOptions, "database"), jetzig.config.get(?jetzig.database.DatabaseOptions, "database"),
self,
); );
defer repo.deinit(); defer repo.deinit();
@ -104,6 +106,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
&repo, &repo,
options.global, options.global,
); );
@constCast(self).server = &server;
var mutex = std.Thread.Mutex{}; var mutex = std.Thread.Mutex{};
var worker_pool = jetzig.jobs.Pool.init( var worker_pool = jetzig.jobs.Pool.init(

View File

@ -103,14 +103,6 @@ pub const database: ?db.DatabaseOptions = null;
/// Database Schema. /// Database Schema.
pub const Schema: type = struct { pub const Schema: type = struct {
pub const _null = struct {}; // https://github.com/ziglang/zig/pull/21331 pub const _null = struct {}; // https://github.com/ziglang/zig/pull/21331
pub const Blogs = struct {
pub const table_name = "blogs";
pub const Definition = struct {
id: []const u8,
title: []const u8,
content: []const u8,
};
};
}; };
/// Key-value store options. Set backend to `.file` to use a file-based store. /// Key-value store options. Set backend to `.file` to use a file-based store.

View File

@ -13,12 +13,21 @@ pub const DatabaseOptions = struct {
pub const Schema = jetzig.get(type, "Schema"); pub const Schema = jetzig.get(type, "Schema");
pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions) !jetzig.jetquery.Repo { pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions, app: *const jetzig.App) !jetzig.jetquery.Repo {
const options = maybe_options orelse return try jetzig.jetquery.Repo.init( const options = maybe_options orelse return try jetzig.jetquery.Repo.init(
allocator, allocator,
.{ .adapter = .null }, .{ .adapter = .null },
); );
// XXX: Is this terrible ?
const Callback = struct {
var jetzig_app: *const jetzig.App = undefined;
pub fn callbackFn(event: jetzig.jetquery.events.Event) !void {
try eventCallback(event, jetzig_app);
}
};
Callback.jetzig_app = app;
return switch (options.adapter) { return switch (options.adapter) {
.postgresql => try jetzig.jetquery.Repo.init( .postgresql => try jetzig.jetquery.Repo.init(
allocator, allocator,
@ -30,9 +39,15 @@ pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions) !jetz
.username = options.username, .username = options.username,
.password = options.password, .password = options.password,
.database = options.database, .database = options.database,
.lazy_connect = @hasField(@import("root"), "database_lazy_connect"),
}, },
}, },
.eventCallback = Callback.callbackFn,
}, },
), ),
}; };
} }
fn eventCallback(event: jetzig.jetquery.events.Event, app: *const jetzig.App) !void {
try app.server.logger.INFO("[database] {?s}", .{event.sql});
}