diff --git a/build.zig b/build.zig index 3cdae76..45bfab8 100644 --- a/build.zig +++ b/build.zig @@ -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("zmpl", zmpl_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 path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b)); diff --git a/build.zig.zon b/build.zig.zon index fdc4128..dbd707c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,8 +7,8 @@ .hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163", }, .zmpl = .{ - .url = "https://github.com/jetzig-framework/zmpl/archive/84d16cc7da6739de6f8c581d9f2e4b216fba3351.tar.gz", - .hash = "122032bd0acbd20b682edcb43fd91ef340bbff0e751d2deb3086abab8decf2c6ace5", + .url = "https://github.com/jetzig-framework/zmpl/archive/a88a06cf43ece8b303a87f52d611b874ad06af9c.tar.gz", + .hash = "12202af84e839619429496900c3280fbfb7c3a3ce80ac0c2e70b99080c480fb22aa3", }, .jetkv = .{ .url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz", diff --git a/src/compile_static_routes.zig b/src/compile_static_routes.zig index 323d244..ca15648 100644 --- a/src/compile_static_routes.zig +++ b/src/compile_static_routes.zig @@ -8,6 +8,12 @@ pub const Global = if (@hasDecl(@import("main"), "Global")) else jetzig.DefaultGlobal; +pub const database_lazy_connect = true; + +pub const jetzig_options = struct { + pub const Schema = @import("Schema"); +}; + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(gpa.deinit() == .ok); diff --git a/src/jetzig/App.zig b/src/jetzig/App.zig index 73d426c..0f3d847 100644 --- a/src/jetzig/App.zig +++ b/src/jetzig/App.zig @@ -11,6 +11,7 @@ env: jetzig.Environment, allocator: std.mem.Allocator, custom_routes: std.ArrayList(jetzig.views.Route), initHook: ?*const fn (*App) anyerror!void, +server: *jetzig.http.Server = undefined, pub fn deinit(self: *const App) void { @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( self.allocator, jetzig.config.get(?jetzig.database.DatabaseOptions, "database"), + self, ); defer repo.deinit(); @@ -104,6 +106,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void { &repo, options.global, ); + @constCast(self).server = &server; var mutex = std.Thread.Mutex{}; var worker_pool = jetzig.jobs.Pool.init( diff --git a/src/jetzig/config.zig b/src/jetzig/config.zig index f84e27e..917228a 100644 --- a/src/jetzig/config.zig +++ b/src/jetzig/config.zig @@ -103,14 +103,6 @@ pub const database: ?db.DatabaseOptions = null; /// Database Schema. pub const Schema: type = struct { 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. diff --git a/src/jetzig/database.zig b/src/jetzig/database.zig index edb5cfa..92ff274 100644 --- a/src/jetzig/database.zig +++ b/src/jetzig/database.zig @@ -13,12 +13,21 @@ pub const DatabaseOptions = struct { 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( allocator, .{ .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) { .postgresql => try jetzig.jetquery.Repo.init( allocator, @@ -30,9 +39,15 @@ pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions) !jetz .username = options.username, .password = options.password, .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}); +}