This commit is contained in:
Bob Farrell 2024-09-22 20:23:31 +01:00
parent 4707fdf744
commit 3f0731baa4
7 changed files with 23 additions and 53 deletions

View File

@ -52,6 +52,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
.jetquery_migrations_path = @as([]const u8, "src/app/database/migrations"),
.jetquery_config_path = @as([]const u8, "config/database.zig"),
});
const zmd_dep = b.dependency("zmd", .{ .target = target, .optimize = optimize });
const httpz_dep = b.dependency("httpz", .{ .target = target, .optimize = optimize });

View File

@ -14,21 +14,7 @@ pub fn main() !void {
const allocator = arena.allocator();
// FIXME: Load config from app
var repo = try jetquery.Repo.init(
allocator,
.{
.adapter = .{
.postgresql = .{
.database = "jetzig_website",
.username = "postgres",
.hostname = "127.0.0.1",
.password = "password",
.port = 5432,
},
},
},
);
var repo = try jetquery.Repo.loadConfig(allocator, .{});
defer repo.deinit();
const migrate = Migrate.init(&repo);

View File

@ -70,11 +70,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
);
defer log_thread.join();
var repo = try jetzig.database.repo(
self.allocator,
jetzig.config.get(?jetzig.database.DatabaseOptions, "database"),
self,
);
var repo = try jetzig.database.repo(self.allocator, self);
defer repo.deinit();
if (self.env.detach) {

View File

@ -151,6 +151,15 @@ pub fn init(allocator: std.mem.Allocator) !Environment {
std.process.exit(1);
}
if (jetzig.jetquery.adapter == .null) {
try logger.WARN("No database configured in `config/database.zig`. Database operations are not available.", .{});
} else {
try logger.INFO(
"Using `{s}` database adapter with database: `{s}`.",
.{ @tagName(jetzig.jetquery.adapter), jetzig.jetquery.config.database.database },
);
}
return .{
.allocator = allocator,
.logger = logger,

View File

@ -13,12 +13,11 @@ pub const DatabaseOptions = struct {
pub const Schema = jetzig.get(type, "Schema");
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 },
);
pub fn Query(comptime table: std.meta.DeclEnum(jetzig.config.get(type, "Schema"))) type {
return jetzig.jetquery.Query(@field(jetzig.config.get(type, "Schema"), @tagName(table)));
}
pub fn repo(allocator: std.mem.Allocator, app: *const jetzig.App) !jetzig.jetquery.Repo {
// XXX: Is this terrible ?
const Callback = struct {
var jetzig_app: *const jetzig.App = undefined;
@ -28,24 +27,10 @@ pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions, app:
};
Callback.jetzig_app = app;
return switch (options.adapter) {
.postgresql => try jetzig.jetquery.Repo.init(
allocator,
.{
.adapter = .{
.postgresql = .{
.hostname = options.hostname,
.port = options.port,
.username = options.username,
.password = options.password,
.database = options.database,
.lazy_connect = true,
},
},
.eventCallback = Callback.callbackFn,
},
),
};
return try jetzig.jetquery.Repo.loadConfig(
allocator,
.{ .eventCallback = Callback.callbackFn, .lazy_connect = true },
);
}
fn eventCallback(event: jetzig.jetquery.events.Event, app: *const jetzig.App) !void {

View File

@ -520,15 +520,6 @@ pub fn mail(self: *Request, name: []const u8, mail_params: jetzig.mail.MailParam
};
}
pub fn query(
self: *const Request,
comptime table: std.meta.DeclEnum(jetzig.config.get(type, "Schema")),
) jetzig.jetquery.Query(@field(jetzig.config.get(type, "Schema"), @tagName(table))) {
return jetzig.jetquery.Query(
@field(jetzig.config.get(type, "Schema"), @tagName(table)),
).init(self.allocator);
}
fn extensionFormat(self: *const Request) ?jetzig.http.Request.Format {
const extension = self.path.extension orelse return null;
if (std.mem.eql(u8, extension, ".html")) {

View File

@ -324,7 +324,9 @@ fn renderView(
};
}
} else {
try self.logger.WARN("`request.render` was not invoked. Rendering empty content.", .{});
if (!request.redirected) {
try self.logger.WARN("`request.render` was not invoked. Rendering empty content.", .{});
}
request.response_data.reset();
return .{
.view = .{ .data = request.response_data, .status_code = .no_content },