Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533const std = @import("std");const Allocator = std.mem.Allocator;const Writer = std.Io.Writer;const ArenaAllocator = std.heap.ArenaAllocator;
const jetquery = @import("jetquery");
const util = @import("util.zig");
pub const ReflectOptions = struct { header: []const u8 = "", import_jetquery: []const u8 = \\@import("jetquery") ,};
pub fn Reflect(adapter_name: jetquery.adapters.Name, Schema: type) type { return struct { const Self = @This(); const AdaptedRepo = jetquery.Repo(adapter_name, Schema); allocator: std.mem.Allocator, repo: *AdaptedRepo, options: ReflectOptions,
pub fn init(allocator: Allocator, repo: *AdaptedRepo, options: ReflectOptions) Self { return .{ .repo = repo, .allocator = allocator, .options = options }; }
pub fn generateSchema(self: Self) ![]const u8 { var arena: ArenaAllocator = .init(self.allocator); defer arena.deinit();
const allocator = arena.allocator();
var buf: Writer.Allocating = .init(allocator); defer buf.deinit();
const writer = &buf.writer;
try writer.print( \\{s} \\const jetquery = {s}; \\ , .{ self.options.header, self.options.import_jetquery, });
const reflection = try self.repo.adapter.reflect(allocator, self.repo); const map = try reflection.tableMap(allocator); var written = std.BufSet.init(allocator);
// Write tables already defined in the schema first to preserve schema order if // edited by user ... inline for (comptime std.meta.declarations(Schema)) |decl| { if (map.get(@field(Schema, decl.name).name)) |table| { try writeModel(allocator, Schema, reflection, table, writer); try written.insert(table.name); } }
// ... then write any remaining tables. for (reflection.tables) |table| { if (written.contains(table.name)) continue; try writeModel(allocator, Schema, reflection, table, writer); }
return try jetquery.util.zig( self.repo.io, self.allocator, buf.writer.buffered(), "Found errors in generated schema.", ); } };}
fn writeModel( allocator: Allocator, comptime schema: type, reflection: jetquery.Reflection, table: jetquery.Reflection.TableInfo, writer: anytype,) !void { try writer.print( \\ \\pub const {s} = jetquery.Model( \\@This(), \\"{s}", \\struct {{ \\ , .{ try translateTableName(allocator, schema, table.name), try util.zigEscape(allocator, .string, table.name), });
for (reflection.columns) |column| { if (!std.mem.eql(u8, column.table, table.name)) continue; try writer.print( \\{s}: {s}, \\ , .{ try util.zigEscape(allocator, .id, column.name), column.zigType(), }, ); }
try writer.print( \\}}, \\{s} \\); \\ , .{try stringifyOptions(allocator, schema, reflection, table.name)});}
fn stringifyOptions( allocator: std.mem.Allocator, comptime schema: type, reflection: jetquery.Reflection, table_name: []const u8,) ![]const u8 { inline for (comptime std.meta.declarations(schema)) |decl| { const table = @field(schema, decl.name); if (std.mem.eql(u8, table.name, table_name)) { return try stringifyModelOptions(allocator, table); } }
var relations_buf: Writer.Allocating = .init(allocator); defer relations_buf.deinit();
const relations_writer = &relations_buf.writer; // TODO: If there are multiple foreign keys to the same table, we don't try to guess the // relation, e.g. `foo_id` and `other_foo_id`. We could guess `foo` and `other_foo` for // `belongsTo` relations, but `hasMany` would require pluralization which is a bit awkward // so, for now, we just use the table name (singularized for `belongsTo`) and users can // adjust - we can do pluralization later but it starts to feel a bit overengineered for just // generating an initial schema when a new table is discovered. Users are expected to do at // least a bit of work here. for (reflection.foreign_keys) |foreign_key| { // belongsTo if (std.mem.eql(u8, foreign_key.table, table_name)) { const model_name = try util.modelize( allocator, foreign_key.foreign_table, ); const singularized = try util.singularize(allocator, foreign_key.foreign_table); try relations_writer.print( \\.{s} = jetquery.belongsTo(.{s}, .{{ , .{ singularized, model_name }, ); const expected_foreign_key = try std.mem.concat( allocator, u8, &.{ singularized, "_id" }, ); if (!std.mem.eql(u8, foreign_key.foreign_column, "id")) { try relations_writer.print( \\.primary_key = "{s}", , .{foreign_key.foreign_column}); } if (!std.mem.eql(u8, foreign_key.column, expected_foreign_key)) { try relations_writer.print( \\.foreign_key = "{s}", , .{foreign_key.column}); } try relations_writer.writeAll("}),"); }
// hasMany if (std.mem.eql(u8, foreign_key.foreign_table, table_name)) { const model_name = try util.modelize( allocator, foreign_key.table, ); const singularized = try util.singularize(allocator, foreign_key.foreign_table); try relations_writer.print( \\.{s} = jetquery.hasMany(.{s}, .{{ , .{ foreign_key.table, model_name }, ); if (!std.mem.eql(u8, foreign_key.foreign_column, "id")) { try relations_writer.print( \\.primary_key = "{s}", , .{foreign_key.foreign_column}); } const expected_foreign_key = try std.mem.concat( allocator, u8, &.{ singularized, "_id" }, ); if (!std.mem.eql(u8, foreign_key.column, expected_foreign_key)) { try relations_writer.print( \\.foreign_key = "{s}", , .{foreign_key.column}); } try relations_writer.writeAll("}),"); } }
if (relations_buf.writer.buffered().len == 0) return ".{}";
return try std.fmt.allocPrint(allocator, \\.{{ .relations = .{{ {s} }}, }}, , .{relations_buf.writer.buffered()});}
fn stringifyModelOptions(allocator: std.mem.Allocator, model: type) ![]const u8 { var buf: Writer.Allocating = .init(allocator); defer buf.deinit(); const writer = &buf.writer;
if (comptime hasDefaultOptions(model)) return ".{}";
try writer.print( \\.{{ \\ , .{});
if (comptime !isDefaultPrimaryKey(model)) { try writer.print( \\.primary_key = "{s}", \\ , .{try util.zigEscape(allocator, .string, model.primary_key)}); }
const relation_fields = jetquery.fields.structFields(@TypeOf(model.relations));
if (relation_fields.len > 0) { try writer.print( \\.relations = .{{ \\ , .{}); inline for (relation_fields) |field| { const relation = @field(model.relations, field.name); try writer.print( \\.{s} = {s}(.{s}, {s}), \\ , .{ try util.zigEscape(allocator, .id, field.name), switch (relation.relation_type) { .belongs_to => "jetquery.belongsTo", .has_many => "jetquery.hasMany", }, try util.zigEscape(allocator, .id, relation.relation_model_name), try stringifyRelationOptions(allocator, relation), }); }
try writer.print( \\}}, \\ , .{}); }
try writer.print( \\}}, \\ , .{});
return try buf.toOwnedSlice();}
fn stringifyRelationOptions(allocator: std.mem.Allocator, comptime relation: type) ![]const u8 { if (comptime relation.options.primary_key == null and relation.options.foreign_key == null) return ".{}";
var buf: Writer.Allocating = .init(allocator); defer buf.deinit(); const writer = &buf.writer;
try writer.print(".{{", .{}); if (comptime relation.options.primary_key != null) { try writer.print( \\.primary_key = "{s}"{s} \\ , .{ try util.zigEscape(allocator, .string, relation.options.primary_key.?), if (comptime relation.options.foreign_key != null) ", " else "", }); }
if (comptime relation.options.foreign_key != null) { try writer.print( \\.foreign_key = "{s}" \\ , .{ try util.zigEscape(allocator, .string, relation.options.foreign_key.?), }); } try writer.print("}}", .{});
return try buf.toOwnedSlice();}
fn hasDefaultOptions(comptime model: type) bool { comptime { if (isDefaultPrimaryKey(model)) return false; if (jetquery.fields.structFields(@TypeOf(model.relations)).len > 0) return false;
return true; }}
fn isDefaultPrimaryKey(comptime model: type) bool { comptime { return std.mem.eql(u8, model.primary_key, "id"); }}
fn isDefaultForeignKey(comptime model: type) bool { comptime { return std.mem.eql(u8, model.foreign_key, "_id"); }}
fn translateTableName( allocator: std.mem.Allocator, comptime schema: type, name: []const u8,) ![]const u8 { // First try finding an existing model in the current schema. inline for (comptime std.meta.declarations(schema)) |decl| { if (std.mem.eql(u8, @field(schema, decl.name).name, name)) return decl.name; }
// Then try translating the table name into a model name. This is the only time we do any // magic with plural nouns etc. - if the user modifies the default generated value, we use // that value going forward as it will now be in the schema. var it = std.mem.tokenizeScalar(u8, name, '_'); var buf = std.array_list.Managed([]const u8).init(allocator); while (it.next()) |token| { var dup = try allocator.dupe(u8, token); dup[0] = std.ascii.toUpper(dup[0]); try buf.append(dup); } return try util.singularize(allocator, try std.mem.join(allocator, "", buf.items));}
test "reflect" { var admin_repo = try jetquery.Repo(.postgresql, void).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "postgres", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer admin_repo.deinit(); try admin_repo.dropDatabase("reflection_test", .{ .if_exists = true }); try admin_repo.createDatabase("reflection_test", .{});
const Schema = struct { pub const Human = jetquery.Model( @This(), "humans", struct { id: i32, name: []const u8 }, .{ .relations = .{ .cats = jetquery.relation.hasMany(.Cat, .{ .foreign_key = "custom_foreign_key" }), }, }, );
pub const Cat = jetquery.Model( @This(), "cats", struct { id: i32, name: []const u8, human_id: i32, paws: i32 }, .{ .relations = .{ .human = jetquery.relation.belongsTo(.Human, .{}), }, .primary_key = "custom_primary_key", }, ); };
var repo = try jetquery.Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "reflection_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable("humans", &.{ jetquery.schema.table.primaryKey("id", .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.timestamps(.{}), }, .{}); try repo.createTable("cats", &.{ jetquery.schema.table.primaryKey("id", .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column( "human_id", .integer, .{ .optional = true, .reference = .{ "humans", "id" } }, ), jetquery.schema.table.timestamps(.{}), }, .{}); try repo.createTable("kennels", &.{ jetquery.schema.table.primaryKey("id", .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.timestamps(.{}), }, .{}); try repo.createTable("dogs", &.{ jetquery.schema.table.primaryKey("id", .{ .type = .bigint }), jetquery.schema.table.column( "kennel_id", .integer, .{ .reference = .{ "kennels", "id" } }, ), jetquery.schema.table.column( "other_kennel_id", .integer, .{ .reference = .{ "kennels", "id" } }, ), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("is_woofy", .boolean, .{}), jetquery.schema.table.column("description", .text, .{}), jetquery.schema.table.column("bark_rating", .float, .{ .optional = true }), jetquery.schema.table.column("food_budget", .decimal, .{ .optional = true }), jetquery.schema.table.column("paws", .smallint, .{}), jetquery.schema.table.column("teeth", .bigint, .{}), jetquery.schema.table.column("more_precise_bark_rating", .double_precision, .{}), jetquery.schema.table.timestamps(.{}), }, .{});
const reflect = Reflect(.postgresql, Schema).init(std.testing.allocator, &repo, .{}); const schema = try reflect.generateSchema(); defer std.testing.allocator.free(schema); try std.testing.expectEqualStrings( \\const jetquery = @import("jetquery"); \\ \\pub const Human = jetquery.Model( \\ @This(), \\ "humans", \\ struct { \\ id: i32, \\ name: []const u8, \\ created_at: jetquery.DateTime, \\ updated_at: jetquery.DateTime, \\ }, \\ .{ \\ .relations = .{ \\ .cats = jetquery.hasMany(.Cat, .{ .foreign_key = "custom_foreign_key" }), \\ }, \\ }, \\); \\ \\pub const Cat = jetquery.Model( \\ @This(), \\ "cats", \\ struct { \\ id: i32, \\ name: []const u8, \\ human_id: ?i32, \\ created_at: jetquery.DateTime, \\ updated_at: jetquery.DateTime, \\ }, \\ .{ \\ .primary_key = "custom_primary_key", \\ .relations = .{ \\ .human = jetquery.belongsTo(.Human, .{}), \\ }, \\ }, \\); \\ \\pub const Dog = jetquery.Model( \\ @This(), \\ "dogs", \\ struct { \\ id: i64, \\ kennel_id: i32, \\ other_kennel_id: i32, \\ name: []const u8, \\ is_woofy: bool, \\ description: []const u8, \\ bark_rating: ?f32, \\ food_budget: ?[]const u8, \\ paws: i16, \\ teeth: i64, \\ more_precise_bark_rating: f64, \\ created_at: jetquery.DateTime, \\ updated_at: jetquery.DateTime, \\ }, \\ .{ \\ .relations = .{ \\ .kennel = jetquery.belongsTo(.Kennel, .{}), \\ .kennel = jetquery.belongsTo(.Kennel, .{ \\ .foreign_key = "other_kennel_id", \\ }), \\ }, \\ }, \\); \\ \\pub const Kennel = jetquery.Model( \\ @This(), \\ "kennels", \\ struct { \\ id: i32, \\ name: []const u8, \\ created_at: jetquery.DateTime, \\ updated_at: jetquery.DateTime, \\ }, \\ .{ \\ .relations = .{ \\ .dogs = jetquery.hasMany(.Dog, .{}), \\ .dogs = jetquery.hasMany(.Dog, .{ \\ .foreign_key = "other_kennel_id", \\ }), \\ }, \\ }, \\); \\ , schema);}