postgres: make sure to store id as varchar if uuid

This commit is contained in:
dswbx
2025-06-07 09:59:50 +02:00
parent 5b318ce485
commit e66e05b2b0
3 changed files with 51 additions and 3 deletions

View File

@@ -26,7 +26,7 @@
}, },
"app": { "app": {
"name": "bknd", "name": "bknd",
"version": "0.13.1-rc.0", "version": "0.14.0-rc.0",
"bin": "./dist/cli/index.js", "bin": "./dist/cli/index.js",
"dependencies": { "dependencies": {
"@cfworker/json-schema": "^4.1.1", "@cfworker/json-schema": "^4.1.1",
@@ -45,6 +45,7 @@
"bcryptjs": "^3.0.2", "bcryptjs": "^3.0.2",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"fast-xml-parser": "^5.0.8", "fast-xml-parser": "^5.0.8",
"hono": "^4.7.11",
"json-schema-form-react": "^0.0.2", "json-schema-form-react": "^0.0.2",
"json-schema-library": "10.0.0-rc7", "json-schema-library": "10.0.0-rc7",
"json-schema-to-ts": "^3.1.1", "json-schema-to-ts": "^3.1.1",
@@ -82,7 +83,6 @@
"autoprefixer": "^10.4.21", "autoprefixer": "^10.4.21",
"clsx": "^2.1.1", "clsx": "^2.1.1",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"hono": "4.7.11",
"jotai": "^2.12.2", "jotai": "^2.12.2",
"jsdom": "^26.0.0", "jsdom": "^26.0.0",
"jsonv-ts": "^0.1.0", "jsonv-ts": "^0.1.0",

View File

@@ -32,7 +32,13 @@ export abstract class PostgresConnection<DB = any> extends Connection<DB> {
override getFieldSchema(spec: FieldSpec): SchemaResponse { override getFieldSchema(spec: FieldSpec): SchemaResponse {
this.validateFieldSpecType(spec.type); this.validateFieldSpecType(spec.type);
let type: ColumnDataType = spec.primary ? "serial" : spec.type; let type: ColumnDataType = spec.type;
if (spec.primary) {
if (spec.type === "integer") {
type = "serial";
}
}
switch (spec.type) { switch (spec.type) {
case "blob": case "blob":

View File

@@ -151,5 +151,47 @@ export function testSuite(config: TestSuiteConfig) {
expect(result[0].comments[0].content).toBe("Hello"); expect(result[0].comments[0].content).toBe("Hello");
expect(result[1].comments.length).toBe(0); expect(result[1].comments.length).toBe(0);
}); });
it("should support uuid", async () => {
const schema = proto.em(
{
posts: proto.entity(
"posts",
{
title: proto.text().required(),
content: proto.text(),
},
{
primary_format: "uuid",
},
),
comments: proto.entity("comments", {
content: proto.text(),
}),
},
(fns, s) => {
fns.relation(s.comments).manyToOne(s.posts);
fns.index(s.posts).on(["title"], true);
},
);
const app = createApp({
connection,
initialConfig: {
data: schema.toJSON(),
},
});
await app.build();
const config = app.toJSON();
// @ts-expect-error
expect(config.data.entities?.posts.fields?.id.config?.format).toBe("uuid");
const em = app.em;
const mutator = em.mutator(em.entity("posts"));
const data = await mutator.insertOne({ title: "Hello", content: "World" });
expect(data.data.id).toBeString();
expect(String(data.data.id).length).toBe(36);
});
}); });
} }