cogsbox-shape 0.5.26 → 0.5.28
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/example/schema.d.ts +60 -60
- package/dist/example/user.d.ts +60 -60
- package/dist/schema.d.ts +2020 -968
- package/dist/schema.js +8 -85
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -234,14 +234,6 @@ export function manyToMany(config) {
|
|
|
234
234
|
defaultCount: config.defaultCount,
|
|
235
235
|
});
|
|
236
236
|
}
|
|
237
|
-
function isRelation(value) {
|
|
238
|
-
return (value &&
|
|
239
|
-
typeof value === "object" &&
|
|
240
|
-
"type" in value &&
|
|
241
|
-
"fromKey" in value &&
|
|
242
|
-
"toKey" in value &&
|
|
243
|
-
"schema" in value);
|
|
244
|
-
}
|
|
245
237
|
function inferDefaultFromZod(zodType, sqlConfig) {
|
|
246
238
|
if (sqlConfig?.pk) {
|
|
247
239
|
return uuidv4();
|
|
@@ -282,83 +274,6 @@ export function reference(config) {
|
|
|
282
274
|
to: config.to,
|
|
283
275
|
};
|
|
284
276
|
}
|
|
285
|
-
function createSerializableSchema(schema) {
|
|
286
|
-
const serializableSchema = {
|
|
287
|
-
_tableName: schema._tableName,
|
|
288
|
-
__schemaId: crypto.randomUUID(),
|
|
289
|
-
_syncKey: schema._syncKey
|
|
290
|
-
? {
|
|
291
|
-
toString: schema._syncKey.toString(),
|
|
292
|
-
}
|
|
293
|
-
: undefined,
|
|
294
|
-
};
|
|
295
|
-
for (const [key, value] of Object.entries(schema)) {
|
|
296
|
-
if (key === "_tableName" || key === "__schemaId")
|
|
297
|
-
continue;
|
|
298
|
-
if (typeof value === "function") {
|
|
299
|
-
const relation = value();
|
|
300
|
-
if (!isRelation(relation)) {
|
|
301
|
-
throw new Error(`Invalid relation for key ${key}`);
|
|
302
|
-
}
|
|
303
|
-
// Call the schema function to get the actual schema
|
|
304
|
-
const childSchema = createSerializableSchema(relation.schema);
|
|
305
|
-
// Get toKey value by calling the function
|
|
306
|
-
const toKeyField = relation.toKey.type === "reference"
|
|
307
|
-
? relation.toKey.to()
|
|
308
|
-
: relation.toKey;
|
|
309
|
-
const serializedToKey = {
|
|
310
|
-
sql: toKeyField.sql,
|
|
311
|
-
jsonSchema: zodToJsonSchema(toKeyField.zodClientSchema),
|
|
312
|
-
defaultValue: toKeyField.defaultValue,
|
|
313
|
-
};
|
|
314
|
-
serializableSchema[key] = {
|
|
315
|
-
type: "relation",
|
|
316
|
-
relationType: relation.type,
|
|
317
|
-
fromKey: relation.fromKey,
|
|
318
|
-
toKey: serializedToKey,
|
|
319
|
-
schema: childSchema,
|
|
320
|
-
...(relation.type === "hasMany" && {
|
|
321
|
-
defaultCount: relation.defaultCount,
|
|
322
|
-
}),
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
else {
|
|
326
|
-
// Handle regular fields or references (unchanged)
|
|
327
|
-
if (value.type === "reference") {
|
|
328
|
-
const referencedField = value.to();
|
|
329
|
-
const serializedField = {
|
|
330
|
-
sql: referencedField.sql,
|
|
331
|
-
jsonSchema: zodToJsonSchema(referencedField.zodClientSchema),
|
|
332
|
-
defaultValue: referencedField.defaultValue,
|
|
333
|
-
...(referencedField.toClient &&
|
|
334
|
-
referencedField.toDb && {
|
|
335
|
-
transforms: {
|
|
336
|
-
toClient: referencedField.toClient.toString(),
|
|
337
|
-
toDb: referencedField.toDb.toString(),
|
|
338
|
-
},
|
|
339
|
-
}),
|
|
340
|
-
};
|
|
341
|
-
serializableSchema[key] = serializedField;
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
const serializedField = {
|
|
345
|
-
sql: value.sql,
|
|
346
|
-
jsonSchema: zodToJsonSchema(value.zodClientSchema),
|
|
347
|
-
defaultValue: value.defaultValue,
|
|
348
|
-
...(value.toClient &&
|
|
349
|
-
value.toDb && {
|
|
350
|
-
transforms: {
|
|
351
|
-
toClient: value.toClient.toString(),
|
|
352
|
-
toDb: value.toDb.toString(),
|
|
353
|
-
},
|
|
354
|
-
}),
|
|
355
|
-
};
|
|
356
|
-
serializableSchema[key] = serializedField;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return serializableSchema;
|
|
361
|
-
}
|
|
362
277
|
export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
|
|
363
278
|
// If schemas are provided, use them (to avoid circular calls)
|
|
364
279
|
if (clientSchema && dbSchema) {
|
|
@@ -422,6 +337,14 @@ export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
|
|
|
422
337
|
}
|
|
423
338
|
return z.object(mixedFields);
|
|
424
339
|
}
|
|
340
|
+
function isRelation(value) {
|
|
341
|
+
return (value &&
|
|
342
|
+
typeof value === "object" &&
|
|
343
|
+
"type" in value &&
|
|
344
|
+
"fromKey" in value &&
|
|
345
|
+
"toKey" in value &&
|
|
346
|
+
"schema" in value);
|
|
347
|
+
}
|
|
425
348
|
export function createSchema(schema) {
|
|
426
349
|
const sqlFields = {};
|
|
427
350
|
const clientFields = {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.28",
|
|
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",
|