This commit is contained in:
Bob Farrell 2024-10-28 21:59:14 +00:00
parent f32c5227c6
commit defd4b8b87
8 changed files with 23 additions and 27 deletions

View File

@ -17,7 +17,8 @@
.path = "../jetquery",
},
.jetcommon = .{
.path = "../jetcommon",
.url = "https://github.com/jetzig-framework/jetcommon/archive/5946df967d3cf2f843035464a6b8a17cb573afd3.tar.gz",
.hash = "12206a50c39a1766d24abcaa9f8fbeaaa20c853f987c667e0b8325e53d9c7ede7d24",
},
.args = .{
.url = "https://github.com/ikskuh/zig-args/archive/0abdd6947a70e6d8cc83b66228cea614aa856206.tar.gz",

View File

@ -1,8 +1,8 @@
const std = @import("std");
const jetquery = @import("jetquery");
const Migrate = @import("jetquery_migrate");
// const migrations = @import("migrations").migrations;
const Migrate = @import("jetquery_migrate").Migrate;
const MigrateSchema = @import("jetquery_migrate").MigrateSchema;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -14,9 +14,12 @@ pub fn main() !void {
const allocator = arena.allocator();
var repo = try jetquery.Repo.loadConfig(allocator, .{});
var repo = try jetquery.Repo(
jetquery.config.database.adapter,
MigrateSchema,
).loadConfig(allocator, .{});
defer repo.deinit();
const migrate = Migrate.init(&repo);
const migrate = Migrate(jetquery.config.database.adapter).init(&repo);
try migrate.run();
}

View File

@ -151,12 +151,12 @@ pub fn init(allocator: std.mem.Allocator) !Environment {
std.process.exit(1);
}
if (jetzig.jetquery.adapter == .null) {
if (jetzig.database.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 },
.{ @tagName(jetzig.database.adapter), jetzig.jetquery.config.database.database },
);
}

View File

@ -97,9 +97,6 @@ pub const job_worker_threads: usize = 1;
/// milliseconds.
pub const job_worker_sleep_interval_ms: usize = 10;
/// Database configuration.
pub const database: ?db.DatabaseOptions = null;
/// Database Schema.
pub const Schema: type = struct {
pub const _null = struct {}; // https://github.com/ziglang/zig/pull/21331

View File

@ -2,22 +2,15 @@ const std = @import("std");
const jetzig = @import("../jetzig.zig");
pub const DatabaseOptions = struct {
adapter: enum { postgresql },
hostname: []const u8,
port: u16,
username: []const u8,
password: []const u8,
database: []const u8,
};
pub const Schema = jetzig.config.get(type, "Schema");
pub const adapter = jetzig.jetquery.config.database.adapter;
pub const Repo = jetzig.jetquery.Repo(jetzig.jetquery.config.database.adapter, Schema);
pub fn Query(comptime table: jetzig.jetquery.DeclEnum(jetzig.config.get(type, "Schema"))) type {
return jetzig.jetquery.Query(Schema, table);
pub fn Query(comptime model: anytype) type {
return jetzig.jetquery.Query(adapter, Schema, model);
}
pub fn repo(allocator: std.mem.Allocator, app: *const jetzig.App) !jetzig.jetquery.Repo {
pub fn repo(allocator: std.mem.Allocator, app: *const jetzig.App) !Repo {
// XXX: Is this terrible ?
const Callback = struct {
var jetzig_app: *const jetzig.App = undefined;
@ -27,7 +20,7 @@ pub fn repo(allocator: std.mem.Allocator, app: *const jetzig.App) !jetzig.jetque
};
Callback.jetzig_app = app;
return try jetzig.jetquery.Repo.loadConfig(
return try Repo.loadConfig(
allocator,
.{ .eventCallback = Callback.callbackFn, .lazy_connect = true },
);

View File

@ -17,6 +17,8 @@ resource_id: []const u8,
extension: ?[]const u8,
query: ?[]const u8,
// TODO: Fix edge case `/foo/bar/` <-- strip trailing slash.
const Self = @This();
/// Initialize a new HTTP Path.

View File

@ -43,7 +43,7 @@ rendered_view: ?jetzig.views.View = null,
start_time: i128,
store: RequestStore,
cache: RequestStore,
repo: *jetzig.jetquery.Repo,
repo: *jetzig.database.Repo,
global: *jetzig.Global,
/// Wrapper for KV store that uses the request's arena allocator for fetching values.
@ -105,7 +105,7 @@ pub fn init(
httpz_request: *httpz.Request,
httpz_response: *httpz.Response,
response: *jetzig.http.Response,
repo: *jetzig.jetquery.Repo,
repo: *jetzig.database.Repo,
) !Request {
const method = switch (httpz_request.method) {
.DELETE => Method.DELETE,

View File

@ -19,7 +19,7 @@ initialized: bool = false,
store: *jetzig.kv.Store,
job_queue: *jetzig.kv.Store,
cache: *jetzig.kv.Store,
repo: *jetzig.jetquery.Repo,
repo: *jetzig.database.Repo,
global: *anyopaque,
decoded_static_route_params: []*jetzig.data.Value = &.{},
@ -36,7 +36,7 @@ pub fn init(
store: *jetzig.kv.Store,
job_queue: *jetzig.kv.Store,
cache: *jetzig.kv.Store,
repo: *jetzig.jetquery.Repo,
repo: *jetzig.database.Repo,
global: *anyopaque,
) Server {
return .{