cogsbox-shape 0.5.192 → 0.5.194
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/README.md +126 -37
- package/cogsbox-shape-db/dist/connect.d.ts +39 -14
- package/cogsbox-shape-db/dist/connect.js +9 -5
- package/cogsbox-shape-db/dist/sqlite/sqlite-driver.d.ts +1 -1
- package/cogsbox-shape-db/dist/table-db.d.ts +10 -2
- package/cogsbox-shape-db/dist/table-db.js +7 -0
- package/cogsbox-shape-db/dist/types.d.ts +11 -6
- package/dist/generateSQL.js +148 -67
- package/dist/schema.d.ts +62 -8
- package/dist/schema.js +115 -42
- package/dist/vitest/fullSchema.test.d.ts +1 -0
- package/dist/vitest/fullSchema.test.js +1367 -0
- package/dist/vitest/generateSQL.test.d.ts +1 -0
- package/dist/vitest/generateSQL.test.js +70 -0
- package/dist/vitest/packageExports.test.d.ts +1 -0
- package/dist/vitest/packageExports.test.js +15 -0
- package/package.json +2 -1
package/dist/generateSQL.js
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
1
|
import fs from "fs/promises";
|
|
2
|
-
const sqlTypeMap = {
|
|
3
|
-
int: "INTEGER",
|
|
4
|
-
varchar: (length = 255) => `VARCHAR(${length})`,
|
|
5
|
-
char: (length = 1) => `CHAR(${length})`,
|
|
6
|
-
text: "TEXT",
|
|
7
|
-
longtext: "LONGTEXT",
|
|
8
|
-
boolean: "TINYINT(1)",
|
|
9
|
-
date: "DATE",
|
|
10
|
-
datetime: "DATETIME",
|
|
11
|
-
};
|
|
12
2
|
function isWrappedSchema(input) {
|
|
13
3
|
return (input !== null &&
|
|
14
4
|
typeof input === "object" &&
|
|
@@ -16,6 +6,107 @@ function isWrappedSchema(input) {
|
|
|
16
6
|
input.schemas !== null &&
|
|
17
7
|
typeof input.schemas === "object");
|
|
18
8
|
}
|
|
9
|
+
function escapeSqlString(value) {
|
|
10
|
+
return value.replace(/'/g, "''");
|
|
11
|
+
}
|
|
12
|
+
function quoteEnumValues(values) {
|
|
13
|
+
return values.map((value) => `'${escapeSqlString(value)}'`).join(", ");
|
|
14
|
+
}
|
|
15
|
+
function columnName(fieldName, sqlConfig) {
|
|
16
|
+
return sqlConfig.field ?? fieldName;
|
|
17
|
+
}
|
|
18
|
+
function assertDialect(current, next, tableName) {
|
|
19
|
+
if (current && current !== next) {
|
|
20
|
+
throw new Error(`Mixed SQL dialects in table "${tableName}": "${current}" and "${next}".`);
|
|
21
|
+
}
|
|
22
|
+
return next;
|
|
23
|
+
}
|
|
24
|
+
function sqlType(dialect, fieldName, tableName, config) {
|
|
25
|
+
switch (dialect) {
|
|
26
|
+
case "sqlite":
|
|
27
|
+
switch (config.type) {
|
|
28
|
+
case "int":
|
|
29
|
+
return "INTEGER";
|
|
30
|
+
case "boolean":
|
|
31
|
+
return "INTEGER";
|
|
32
|
+
case "varchar":
|
|
33
|
+
case "char":
|
|
34
|
+
case "text":
|
|
35
|
+
case "longtext":
|
|
36
|
+
case "enum":
|
|
37
|
+
return "TEXT";
|
|
38
|
+
case "date":
|
|
39
|
+
case "datetime":
|
|
40
|
+
case "timestamp":
|
|
41
|
+
return "TEXT";
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
case "postgres":
|
|
45
|
+
switch (config.type) {
|
|
46
|
+
case "int":
|
|
47
|
+
return "INTEGER";
|
|
48
|
+
case "boolean":
|
|
49
|
+
return "BOOLEAN";
|
|
50
|
+
case "varchar":
|
|
51
|
+
return `VARCHAR(${config.length ?? 255})`;
|
|
52
|
+
case "char":
|
|
53
|
+
return `CHAR(${config.length ?? 1})`;
|
|
54
|
+
case "text":
|
|
55
|
+
case "longtext":
|
|
56
|
+
return "TEXT";
|
|
57
|
+
case "enum":
|
|
58
|
+
if (!config.name) {
|
|
59
|
+
throw new Error(`Postgres enum field "${tableName}.${fieldName}" requires a name.`);
|
|
60
|
+
}
|
|
61
|
+
return config.name;
|
|
62
|
+
case "date":
|
|
63
|
+
return "DATE";
|
|
64
|
+
case "datetime":
|
|
65
|
+
case "timestamp":
|
|
66
|
+
return "TIMESTAMP";
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "mysql":
|
|
70
|
+
switch (config.type) {
|
|
71
|
+
case "int":
|
|
72
|
+
return "INTEGER";
|
|
73
|
+
case "boolean":
|
|
74
|
+
return "TINYINT(1)";
|
|
75
|
+
case "varchar":
|
|
76
|
+
return `VARCHAR(${config.length ?? 255})`;
|
|
77
|
+
case "char":
|
|
78
|
+
return `CHAR(${config.length ?? 1})`;
|
|
79
|
+
case "text":
|
|
80
|
+
return "TEXT";
|
|
81
|
+
case "longtext":
|
|
82
|
+
return "LONGTEXT";
|
|
83
|
+
case "enum":
|
|
84
|
+
return `ENUM(${quoteEnumValues(config.values ?? [])})`;
|
|
85
|
+
case "date":
|
|
86
|
+
return "DATE";
|
|
87
|
+
case "datetime":
|
|
88
|
+
return "DATETIME";
|
|
89
|
+
case "timestamp":
|
|
90
|
+
return "TIMESTAMP";
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
throw new Error(`Unknown ${dialect} SQL type "${config.type}" for field "${tableName}.${fieldName}".`);
|
|
95
|
+
}
|
|
96
|
+
function defaultSql(value) {
|
|
97
|
+
if (value === "CURRENT_TIMESTAMP")
|
|
98
|
+
return "CURRENT_TIMESTAMP";
|
|
99
|
+
if (typeof value === "string")
|
|
100
|
+
return `'${escapeSqlString(value)}'`;
|
|
101
|
+
if (value instanceof Date)
|
|
102
|
+
return `'${value.toISOString()}'`;
|
|
103
|
+
return String(value);
|
|
104
|
+
}
|
|
105
|
+
function enumCheck(dialect, fieldName, config) {
|
|
106
|
+
if (dialect !== "sqlite" || config.type !== "enum")
|
|
107
|
+
return undefined;
|
|
108
|
+
return `CHECK (${fieldName} IN (${quoteEnumValues(config.values ?? [])}))`;
|
|
109
|
+
}
|
|
19
110
|
export async function generateSQL(input, outputPath = "cogsbox-shape-sql.sql", options = { includeForeignKeys: true }) {
|
|
20
111
|
if (!input) {
|
|
21
112
|
throw new Error("No schema input provided");
|
|
@@ -24,7 +115,8 @@ export async function generateSQL(input, outputPath = "cogsbox-shape-sql.sql", o
|
|
|
24
115
|
if (!schemas || typeof schemas !== "object") {
|
|
25
116
|
throw new Error("Invalid schemas input");
|
|
26
117
|
}
|
|
27
|
-
const
|
|
118
|
+
const statements = [];
|
|
119
|
+
const postgresEnums = new Map();
|
|
28
120
|
for (const [name, schema] of Object.entries(schemas)) {
|
|
29
121
|
const tableName = schema._tableName;
|
|
30
122
|
if (!tableName) {
|
|
@@ -33,91 +125,80 @@ export async function generateSQL(input, outputPath = "cogsbox-shape-sql.sql", o
|
|
|
33
125
|
}
|
|
34
126
|
const fields = [];
|
|
35
127
|
const foreignKeys = [];
|
|
128
|
+
let tableDialect;
|
|
36
129
|
for (const [fieldName, field] of Object.entries(schema)) {
|
|
37
|
-
|
|
38
|
-
const f = field; // Just cast once
|
|
39
|
-
console.log(`Processing field: ${fieldName}`, f);
|
|
40
|
-
// Skip metadata fields
|
|
130
|
+
const f = field;
|
|
41
131
|
if (fieldName === "_tableName" ||
|
|
42
132
|
fieldName === "SchemaWrapperBrand" ||
|
|
43
133
|
fieldName.startsWith("__") ||
|
|
44
134
|
typeof f !== "object" ||
|
|
45
|
-
!f)
|
|
135
|
+
!f) {
|
|
46
136
|
continue;
|
|
47
|
-
|
|
137
|
+
}
|
|
48
138
|
if (f.type === "reference" && f.to) {
|
|
49
139
|
const referencedField = f.to();
|
|
50
140
|
const targetTableName = referencedField.__parentTableType._tableName;
|
|
51
141
|
const targetFieldName = referencedField.__meta._key;
|
|
52
|
-
console.log(`Found reference field: ${fieldName} -> ${targetTableName}.${targetFieldName}`);
|
|
53
142
|
fields.push(` ${fieldName} INTEGER NOT NULL`);
|
|
54
143
|
if (options.includeForeignKeys) {
|
|
55
144
|
foreignKeys.push(` FOREIGN KEY (${fieldName}) REFERENCES ${targetTableName}(${targetFieldName})`);
|
|
56
145
|
}
|
|
57
146
|
continue;
|
|
58
147
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (sqlConfig.type === "belongsTo" &&
|
|
72
|
-
sqlConfig.fromKey &&
|
|
73
|
-
sqlConfig.schema) {
|
|
74
|
-
fields.push(` ${sqlConfig.fromKey} INTEGER`);
|
|
75
|
-
if (options.includeForeignKeys) {
|
|
76
|
-
const targetSchema = sqlConfig.schema();
|
|
77
|
-
foreignKeys.push(` FOREIGN KEY (${sqlConfig.fromKey}) REFERENCES ${targetSchema._tableName}(id)`);
|
|
78
|
-
}
|
|
148
|
+
const fieldDef = f.__meta?._fieldType ?? f;
|
|
149
|
+
const sqlConfig = fieldDef?.config?.sql;
|
|
150
|
+
if (!sqlConfig)
|
|
151
|
+
continue;
|
|
152
|
+
if (["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
|
|
153
|
+
if (sqlConfig.type === "belongsTo" &&
|
|
154
|
+
sqlConfig.fromKey &&
|
|
155
|
+
sqlConfig.schema) {
|
|
156
|
+
fields.push(` ${sqlConfig.fromKey} INTEGER`);
|
|
157
|
+
if (options.includeForeignKeys) {
|
|
158
|
+
const targetSchema = sqlConfig.schema();
|
|
159
|
+
foreignKeys.push(` FOREIGN KEY (${sqlConfig.fromKey}) REFERENCES ${targetSchema._tableName}(id)`);
|
|
79
160
|
}
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
// Handle regular SQL types
|
|
83
|
-
const { type, nullable, pk, length, default: defaultValue } = sqlConfig;
|
|
84
|
-
if (!sqlTypeMap[type]) {
|
|
85
|
-
console.warn(`Unknown SQL type: ${type} for field ${fieldName}`);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const sqlType = typeof sqlTypeMap[type] === "function"
|
|
89
|
-
? sqlTypeMap[type](length)
|
|
90
|
-
: sqlTypeMap[type];
|
|
91
|
-
let fieldDefStr = ` ${fieldName} ${sqlType}`;
|
|
92
|
-
if (pk)
|
|
93
|
-
fieldDefStr += " PRIMARY KEY AUTO_INCREMENT";
|
|
94
|
-
if (!nullable && !pk)
|
|
95
|
-
fieldDefStr += " NOT NULL";
|
|
96
|
-
// Handle defaults
|
|
97
|
-
if (defaultValue !== undefined &&
|
|
98
|
-
defaultValue !== "CURRENT_TIMESTAMP") {
|
|
99
|
-
fieldDefStr += ` DEFAULT ${typeof defaultValue === "string" ? `'${defaultValue}'` : defaultValue}`;
|
|
100
161
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const dialect = sqlConfig.dialect;
|
|
165
|
+
if (!dialect) {
|
|
166
|
+
throw new Error(`Field "${tableName}.${fieldName}" is missing a SQL dialect.`);
|
|
167
|
+
}
|
|
168
|
+
tableDialect = assertDialect(tableDialect, dialect, tableName);
|
|
169
|
+
if (dialect === "postgres" && sqlConfig.type === "enum") {
|
|
170
|
+
postgresEnums.set(sqlConfig.name, sqlConfig.values);
|
|
171
|
+
}
|
|
172
|
+
const dbFieldName = columnName(fieldName, sqlConfig);
|
|
173
|
+
const parts = [
|
|
174
|
+
dbFieldName,
|
|
175
|
+
sqlType(dialect, fieldName, tableName, sqlConfig),
|
|
176
|
+
];
|
|
177
|
+
if (sqlConfig.pk) {
|
|
178
|
+
parts.push(dialect === "mysql" ? "PRIMARY KEY AUTO_INCREMENT" : "PRIMARY KEY");
|
|
179
|
+
}
|
|
180
|
+
if (!sqlConfig.nullable && !sqlConfig.pk)
|
|
181
|
+
parts.push("NOT NULL");
|
|
182
|
+
if (sqlConfig.default !== undefined) {
|
|
183
|
+
parts.push(`DEFAULT ${defaultSql(sqlConfig.default)}`);
|
|
105
184
|
}
|
|
185
|
+
const check = enumCheck(dialect, dbFieldName, sqlConfig);
|
|
186
|
+
if (check)
|
|
187
|
+
parts.push(check);
|
|
188
|
+
fields.push(` ${parts.join(" ")}`);
|
|
106
189
|
}
|
|
107
|
-
// Combine fields and foreign keys based on option
|
|
108
190
|
const allFields = options.includeForeignKeys
|
|
109
191
|
? [...fields, ...foreignKeys]
|
|
110
192
|
: fields;
|
|
111
|
-
// Create table SQL
|
|
112
193
|
if (allFields.length > 0) {
|
|
113
|
-
|
|
194
|
+
statements.push(`CREATE TABLE ${tableName} (\n${allFields.join(",\n")}\n);`);
|
|
114
195
|
}
|
|
115
196
|
else {
|
|
116
197
|
console.warn(`Warning: Table ${tableName} has no fields`);
|
|
117
198
|
}
|
|
118
199
|
}
|
|
119
|
-
|
|
120
|
-
const sqlContent =
|
|
200
|
+
const enumStatements = Array.from(postgresEnums.entries()).map(([name, values]) => `CREATE TYPE ${name} AS ENUM (${quoteEnumValues(values)});`);
|
|
201
|
+
const sqlContent = [...enumStatements, ...statements].join("\n\n");
|
|
121
202
|
await fs.writeFile(outputPath, sqlContent, "utf-8");
|
|
122
203
|
return sqlContent;
|
|
123
204
|
}
|
package/dist/schema.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ type CurrentTimestampConfig = {
|
|
|
9
9
|
export declare const isFunction: (fn: unknown) => fn is Function;
|
|
10
10
|
export declare function currentTimeStamp(): CurrentTimestampConfig;
|
|
11
11
|
type DbConfig = SQLType | RelationConfig<any> | null;
|
|
12
|
-
export type
|
|
12
|
+
export type SQLDialect = "sqlite" | "postgres" | "mysql";
|
|
13
|
+
type SQLTypeConfig = ({
|
|
13
14
|
type: "int";
|
|
14
15
|
nullable?: boolean;
|
|
15
16
|
default?: number;
|
|
@@ -31,16 +32,33 @@ export type SQLType = ({
|
|
|
31
32
|
nullable?: boolean;
|
|
32
33
|
length?: number;
|
|
33
34
|
default?: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: "enum";
|
|
37
|
+
values: readonly [string, ...string[]];
|
|
38
|
+
nullable?: boolean;
|
|
39
|
+
default?: string;
|
|
40
|
+
name?: string;
|
|
34
41
|
}) & BaseConfig;
|
|
42
|
+
export type SQLType = SQLTypeConfig & {
|
|
43
|
+
dialect: SQLDialect;
|
|
44
|
+
};
|
|
45
|
+
type SQLTypeInput = SQLTypeConfig;
|
|
46
|
+
type WithDialect<T extends SQLTypeInput, TDialect extends SQLDialect> = SQLType & T & {
|
|
47
|
+
dialect: TDialect;
|
|
48
|
+
};
|
|
35
49
|
type BaseConfig = {
|
|
36
50
|
nullable?: boolean;
|
|
37
51
|
pk?: true;
|
|
38
52
|
field?: string;
|
|
39
53
|
sqlOnly?: true;
|
|
40
54
|
};
|
|
41
|
-
type SQLToZodType<T extends
|
|
55
|
+
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
|
+
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 {
|
|
42
58
|
default: "CURRENT_TIMESTAMP";
|
|
43
|
-
} ? 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 "
|
|
59
|
+
} ? 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
|
+
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 {
|
|
44
62
|
default: "CURRENT_TIMESTAMP";
|
|
45
63
|
} ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
|
|
46
64
|
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;
|
|
@@ -164,7 +182,9 @@ interface ShapeAPI {
|
|
|
164
182
|
clientInput: <const TValue>(value: TValue | ((tools: {
|
|
165
183
|
uuid: () => string;
|
|
166
184
|
}) => TValue)) => Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
|
|
167
|
-
|
|
185
|
+
sqlite: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "sqlite">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
|
|
186
|
+
postgres: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "postgres">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
|
|
187
|
+
mysql: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "mysql">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
|
|
168
188
|
reference: <TGetter extends () => any>(getter: TGetter) => Reference<TGetter>;
|
|
169
189
|
hasMany: <T extends HasManyDefault>(config?: T) => PlaceholderRelation<"hasMany">;
|
|
170
190
|
hasOne: (config?: HasOneDefault) => PlaceholderRelation<"hasOne">;
|
|
@@ -185,6 +205,11 @@ export type EnrichFields<T extends ShapeSchema> = {
|
|
|
185
205
|
[K in keyof T]: K extends "_tableName" ? T[K] : K extends string ? EnrichedField<K, T[K], T> : T[K];
|
|
186
206
|
};
|
|
187
207
|
export declare const SchemaWrapperBrand: unique symbol;
|
|
208
|
+
export type RefinementError = {
|
|
209
|
+
path: string[];
|
|
210
|
+
message: string;
|
|
211
|
+
};
|
|
212
|
+
export type RefinementFn<T> = (row: T) => RefinementError | RefinementError[] | undefined | null;
|
|
188
213
|
type PickPrimaryKeys<T extends ShapeSchema> = {
|
|
189
214
|
[K in keyof T as T[K] extends {
|
|
190
215
|
config: {
|
|
@@ -217,6 +242,10 @@ type SchemaBuilder<T extends ShapeSchema> = Prettify<EnrichFields<T>> & {
|
|
|
217
242
|
forClient?: Record<string, (row: any) => any>;
|
|
218
243
|
forDb?: Record<string, (row: any) => any>;
|
|
219
244
|
};
|
|
245
|
+
__refinements?: {
|
|
246
|
+
server?: RefinementFn<InferClientRow<T>>;
|
|
247
|
+
client?: RefinementFn<z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<EnrichFields<T>, "zodClientInputSchema">>>>>;
|
|
248
|
+
};
|
|
220
249
|
primaryKeySQL: (definer: (pkFields: PickPrimaryKeys<T>) => string) => SchemaBuilder<T>;
|
|
221
250
|
derive: (derivers: {
|
|
222
251
|
forClient?: {
|
|
@@ -226,6 +255,10 @@ type SchemaBuilder<T extends ShapeSchema> = Prettify<EnrichFields<T>> & {
|
|
|
226
255
|
[K in PickDbFieldKeys<T>]?: (row: InferClientRow<T>) => any;
|
|
227
256
|
};
|
|
228
257
|
}) => SchemaBuilder<T>;
|
|
258
|
+
refine: (refinements: {
|
|
259
|
+
server?: RefinementFn<InferClientRow<T>>;
|
|
260
|
+
client?: RefinementFn<z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<EnrichFields<T>, "zodClientInputSchema">>>>>;
|
|
261
|
+
}) => SchemaBuilder<T>;
|
|
229
262
|
};
|
|
230
263
|
export declare function schema<T extends string, U extends ShapeSchema<T>>(schema: U): SchemaBuilder<U>;
|
|
231
264
|
export type RelationType = "hasMany" | "hasOne" | "manyToMany";
|
|
@@ -245,7 +278,7 @@ export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>
|
|
|
245
278
|
__schemaId?: string;
|
|
246
279
|
[key: string]: T[keyof T] | string | ((id: number) => string) | true | undefined;
|
|
247
280
|
};
|
|
248
|
-
type ValidShapeField = ReturnType<typeof s.
|
|
281
|
+
type ValidShapeField = ReturnType<typeof s.sqlite> | ReturnType<typeof s.postgres> | ReturnType<typeof s.mysql>;
|
|
249
282
|
export type ShapeSchema<T extends string = string> = {
|
|
250
283
|
_tableName: T;
|
|
251
284
|
[SchemaWrapperBrand]?: true;
|
|
@@ -265,6 +298,10 @@ export declare function createSchema<T extends {
|
|
|
265
298
|
pk: string[] | null;
|
|
266
299
|
clientPk: string[] | null;
|
|
267
300
|
deriveDependencies: Record<string, string[]>;
|
|
301
|
+
refineDependencies: {
|
|
302
|
+
server: string[];
|
|
303
|
+
client: string[];
|
|
304
|
+
};
|
|
268
305
|
isClientRecord: (record: any) => boolean;
|
|
269
306
|
sqlSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodSqlSchema">>>;
|
|
270
307
|
clientInputSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodClientInputSchema">>>;
|
|
@@ -346,6 +383,10 @@ type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders
|
|
|
346
383
|
};
|
|
347
384
|
pk: string[] | null;
|
|
348
385
|
clientPk: string[] | null;
|
|
386
|
+
refineDependencies: {
|
|
387
|
+
server: string[];
|
|
388
|
+
client: string[];
|
|
389
|
+
};
|
|
349
390
|
isClientRecord: (record: any) => boolean;
|
|
350
391
|
generateDefaults: () => Prettify<DeriveDefaults<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>>>;
|
|
351
392
|
};
|
|
@@ -492,6 +533,10 @@ type RegistryShape = Record<string, {
|
|
|
492
533
|
defaultValues: any;
|
|
493
534
|
stateType: any;
|
|
494
535
|
deriveDependencies: Record<string, string[]>;
|
|
536
|
+
refineDependencies: {
|
|
537
|
+
server: string[];
|
|
538
|
+
client: string[];
|
|
539
|
+
};
|
|
495
540
|
};
|
|
496
541
|
transforms: {
|
|
497
542
|
toClient: (dbObject: any) => any;
|
|
@@ -503,6 +548,10 @@ type RegistryShape = Record<string, {
|
|
|
503
548
|
pk: string[] | null;
|
|
504
549
|
clientPk: string[] | null;
|
|
505
550
|
deriveDependencies: Record<string, string[]>;
|
|
551
|
+
refineDependencies: {
|
|
552
|
+
server: string[];
|
|
553
|
+
client: string[];
|
|
554
|
+
};
|
|
506
555
|
isClientRecord: (record: any) => boolean;
|
|
507
556
|
generateDefaults: () => any;
|
|
508
557
|
}>;
|
|
@@ -529,6 +578,11 @@ type CreateSchemaBoxReturn<S extends Record<string, SchemaWithPlaceholders>, R e
|
|
|
529
578
|
generateDefaults: () => Resolved[K]["zodSchemas"]["defaultValues"];
|
|
530
579
|
pk: string[] | null;
|
|
531
580
|
clientPk: string[] | null;
|
|
581
|
+
deriveDependencies: Record<string, string[]>;
|
|
582
|
+
refineDependencies: {
|
|
583
|
+
server: string[];
|
|
584
|
+
client: string[];
|
|
585
|
+
};
|
|
532
586
|
isClientRecord: (record: any) => boolean;
|
|
533
587
|
nav: NavigationProxy<K & string, Resolved>;
|
|
534
588
|
RelationSelection: NavigationToSelection<NavigationProxy<K & string, Resolved>>;
|
|
@@ -554,7 +608,7 @@ type GetDbKey<K, Field> = Field extends Reference<infer TGetter> ? ReturnType<TG
|
|
|
554
608
|
};
|
|
555
609
|
} ? string extends F ? K : F : K;
|
|
556
610
|
type DeriveSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientInputSchema" | "zodClientSchema" | "zodValidationSchema", Depth extends any[] = []> = Depth["length"] extends 10 ? any : {
|
|
557
|
-
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" ? never : K extends keyof T ? T[K] extends {
|
|
611
|
+
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" | "refine" | "__refinements" ? never : K extends keyof T ? T[K] extends {
|
|
558
612
|
config: {
|
|
559
613
|
sql: {
|
|
560
614
|
sqlOnly: true;
|
|
@@ -581,7 +635,7 @@ type DeriveSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientInputSchema" |
|
|
|
581
635
|
} ? ZodSchema : never;
|
|
582
636
|
};
|
|
583
637
|
type DeriveDefaults<T, Depth extends any[] = []> = Prettify<Depth["length"] extends 10 ? any : {
|
|
584
|
-
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" ? never : K extends keyof T ? T[K] extends {
|
|
638
|
+
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" | "refine" | "__refinements" ? never : K extends keyof T ? T[K] extends {
|
|
585
639
|
config: {
|
|
586
640
|
sql: {
|
|
587
641
|
sqlOnly: true;
|
|
@@ -604,7 +658,7 @@ type DeriveDefaults<T, Depth extends any[] = []> = Prettify<Depth["length"] exte
|
|
|
604
658
|
} ? z.infer<TClient> : never;
|
|
605
659
|
}>;
|
|
606
660
|
type DeriveStateType<T, Depth extends any[] = []> = Prettify<Depth["length"] extends 10 ? any : {
|
|
607
|
-
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" ? never : K extends keyof T ? T[K] extends {
|
|
661
|
+
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "primaryKeySQL" | "derive" | "__derives" | "refine" | "__refinements" ? never : K extends keyof T ? T[K] extends {
|
|
608
662
|
config: {
|
|
609
663
|
sql: {
|
|
610
664
|
sqlOnly: true;
|