cogsbox-shape 0.5.208 → 0.5.209
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generateSQL.js +6 -0
- package/dist/schema.d.ts +6 -2
- package/dist/schema.js +5 -0
- package/dist/vitest/fullSchema.test.js +11 -0
- package/dist/vitest/generateSQL.test.js +28 -0
- package/package.json +1 -1
package/dist/generateSQL.js
CHANGED
|
@@ -27,6 +27,8 @@ function sqlType(dialect, fieldName, tableName, config) {
|
|
|
27
27
|
switch (config.type) {
|
|
28
28
|
case "int":
|
|
29
29
|
return "INTEGER";
|
|
30
|
+
case "real":
|
|
31
|
+
return "REAL";
|
|
30
32
|
case "boolean":
|
|
31
33
|
return "INTEGER";
|
|
32
34
|
case "varchar":
|
|
@@ -45,6 +47,8 @@ function sqlType(dialect, fieldName, tableName, config) {
|
|
|
45
47
|
switch (config.type) {
|
|
46
48
|
case "int":
|
|
47
49
|
return "INTEGER";
|
|
50
|
+
case "real":
|
|
51
|
+
return "REAL";
|
|
48
52
|
case "boolean":
|
|
49
53
|
return "BOOLEAN";
|
|
50
54
|
case "varchar":
|
|
@@ -70,6 +74,8 @@ function sqlType(dialect, fieldName, tableName, config) {
|
|
|
70
74
|
switch (config.type) {
|
|
71
75
|
case "int":
|
|
72
76
|
return "INTEGER";
|
|
77
|
+
case "real":
|
|
78
|
+
return "DOUBLE";
|
|
73
79
|
case "boolean":
|
|
74
80
|
return "TINYINT(1)";
|
|
75
81
|
case "varchar":
|
package/dist/schema.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ type SQLTypeConfig = ({
|
|
|
14
14
|
type: "int";
|
|
15
15
|
nullable?: boolean;
|
|
16
16
|
default?: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: "real";
|
|
19
|
+
nullable?: boolean;
|
|
20
|
+
default?: number;
|
|
17
21
|
} | {
|
|
18
22
|
type: "boolean";
|
|
19
23
|
nullable?: boolean;
|
|
@@ -54,11 +58,11 @@ type BaseConfig = {
|
|
|
54
58
|
};
|
|
55
59
|
type SQLToZodType<T extends SQLTypeInput, TDefault extends boolean> = T["pk"] extends true ? TDefault extends true ? z.ZodString : z.ZodNumber : T["nullable"] extends true ? T["type"] extends "varchar" | "char" | "text" | "longtext" ? z.ZodNullable<z.ZodString> : T["type"] extends "enum" ? T extends {
|
|
56
60
|
values: infer TValues extends readonly [string, ...string[]];
|
|
57
|
-
} ? z.ZodNullable<z.ZodType<TValues[number]>> : never : T["type"] extends "int" ? z.ZodNullable<z.ZodNumber> : T["type"] extends "boolean" ? z.ZodNullable<z.ZodNumber> : T["type"] extends "date" | "datetime" | "timestamp" ? T extends {
|
|
61
|
+
} ? z.ZodNullable<z.ZodType<TValues[number]>> : never : T["type"] extends "int" | "real" ? z.ZodNullable<z.ZodNumber> : T["type"] extends "boolean" ? z.ZodNullable<z.ZodNumber> : T["type"] extends "date" | "datetime" | "timestamp" ? T extends {
|
|
58
62
|
default: "CURRENT_TIMESTAMP";
|
|
59
63
|
} ? TDefault extends true ? never : z.ZodNullable<z.ZodDate> : z.ZodNullable<z.ZodDate> : never : T["type"] extends "varchar" | "char" | "text" | "longtext" ? z.ZodString : T["type"] extends "enum" ? T extends {
|
|
60
64
|
values: infer TValues extends readonly [string, ...string[]];
|
|
61
|
-
} ? z.ZodType<TValues[number]> : never : T["type"] extends "int" ? z.ZodNumber : T["type"] extends "boolean" ? z.ZodNumber : T["type"] extends "date" | "datetime" | "timestamp" ? T extends {
|
|
65
|
+
} ? z.ZodType<TValues[number]> : never : T["type"] extends "int" | "real" ? z.ZodNumber : T["type"] extends "boolean" ? z.ZodNumber : T["type"] extends "date" | "datetime" | "timestamp" ? T extends {
|
|
62
66
|
default: "CURRENT_TIMESTAMP";
|
|
63
67
|
} ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
|
|
64
68
|
type ZodTypeFromPrimitive<T> = T extends string ? z.ZodString : T extends number ? z.ZodNumber : T extends boolean ? z.ZodBoolean : T extends Date ? z.ZodDate : z.ZodAny;
|
package/dist/schema.js
CHANGED
|
@@ -18,6 +18,9 @@ function createSqlBuilder(dialect, sqlConfig) {
|
|
|
18
18
|
case "int":
|
|
19
19
|
baseType = z.number();
|
|
20
20
|
break;
|
|
21
|
+
case "real":
|
|
22
|
+
baseType = z.number();
|
|
23
|
+
break;
|
|
21
24
|
case "boolean":
|
|
22
25
|
baseType = z.number();
|
|
23
26
|
break;
|
|
@@ -467,6 +470,8 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
467
470
|
return sqlTypeConfig.default ?? sqlTypeConfig.values[0];
|
|
468
471
|
case "int":
|
|
469
472
|
return 0;
|
|
473
|
+
case "real":
|
|
474
|
+
return 0;
|
|
470
475
|
case "boolean":
|
|
471
476
|
return false;
|
|
472
477
|
case "date":
|
|
@@ -31,6 +31,17 @@ describe("Schema Builder Type Tests (with expect-type)", () => {
|
|
|
31
31
|
expect(statusField.config.zodSqlSchema.parse("draft")).toBe("draft");
|
|
32
32
|
expect(() => statusField.config.zodSqlSchema.parse("deleted")).toThrow();
|
|
33
33
|
});
|
|
34
|
+
it("should correctly type a real (float) field", () => {
|
|
35
|
+
const tempField = s.sqlite({ type: "real" });
|
|
36
|
+
expectTypeOf(tempField.config.zodSqlSchema).toEqualTypeOf();
|
|
37
|
+
expect(tempField.config.zodSqlSchema.parse(3.14)).toBe(3.14);
|
|
38
|
+
});
|
|
39
|
+
it("should correctly type a nullable real field", () => {
|
|
40
|
+
const nullableReal = s.sqlite({ type: "real", nullable: true });
|
|
41
|
+
expectTypeOf(nullableReal.config.zodClientCheckedSchema).toEqualTypeOf();
|
|
42
|
+
expect(nullableReal.config.zodSqlSchema.parse(null)).toBeNull();
|
|
43
|
+
expect(nullableReal.config.zodSqlSchema.parse(2.5)).toBe(2.5);
|
|
44
|
+
});
|
|
34
45
|
});
|
|
35
46
|
describe("Chainable Methods", () => {
|
|
36
47
|
it("should create a union type when .client provides a different type", () => {
|
|
@@ -67,4 +67,32 @@ describe("generateSQL dialect columns", () => {
|
|
|
67
67
|
});
|
|
68
68
|
await expect(withOutputFile((path) => generateSQL({ posts }, path))).rejects.toThrow(/Mixed SQL dialects/);
|
|
69
69
|
});
|
|
70
|
+
it("generates REAL column for SQLite real type", async () => {
|
|
71
|
+
const measurements = schema({
|
|
72
|
+
_tableName: "measurements",
|
|
73
|
+
id: s.sqlite({ type: "int", pk: true }),
|
|
74
|
+
temperature: s.sqlite({ type: "real" }),
|
|
75
|
+
humidity: s.sqlite({ type: "real", nullable: true }),
|
|
76
|
+
});
|
|
77
|
+
const sql = await withOutputFile((path) => generateSQL({ measurements }, path));
|
|
78
|
+
expect(sql).toContain("id INTEGER PRIMARY KEY");
|
|
79
|
+
expect(sql).toContain("temperature REAL NOT NULL");
|
|
80
|
+
expect(sql).toContain("humidity REAL");
|
|
81
|
+
});
|
|
82
|
+
it("generates REAL for Postgres and DOUBLE for MySQL real type", async () => {
|
|
83
|
+
const pgData = schema({
|
|
84
|
+
_tableName: "readings",
|
|
85
|
+
id: s.postgres({ type: "int", pk: true }),
|
|
86
|
+
value: s.postgres({ type: "real" }),
|
|
87
|
+
});
|
|
88
|
+
const pgSql = await withOutputFile((path) => generateSQL({ pgData }, path));
|
|
89
|
+
expect(pgSql).toContain("value REAL NOT NULL");
|
|
90
|
+
const myData = schema({
|
|
91
|
+
_tableName: "readings",
|
|
92
|
+
id: s.mysql({ type: "int", pk: true }),
|
|
93
|
+
value: s.mysql({ type: "real" }),
|
|
94
|
+
});
|
|
95
|
+
const mySql = await withOutputFile((path) => generateSQL({ myData }, path));
|
|
96
|
+
expect(mySql).toContain("value DOUBLE NOT NULL");
|
|
97
|
+
});
|
|
70
98
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.209",
|
|
4
4
|
"description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|