cogsbox-shape 0.5.50 → 0.5.51
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/schema.js +16 -10
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -345,6 +345,8 @@ function isRelation(value) {
|
|
|
345
345
|
}
|
|
346
346
|
// In your cogsbox-shape file...
|
|
347
347
|
// Replace the entire existing createSchema function with this SINGLE, CORRECT version.
|
|
348
|
+
// In your cogsbox-shape file...
|
|
349
|
+
// Replace the entire existing createSchema function with this SINGLE, CORRECT version.
|
|
348
350
|
export function createSchema(schema) {
|
|
349
351
|
const sqlFields = {};
|
|
350
352
|
const clientFields = {};
|
|
@@ -353,30 +355,35 @@ export function createSchema(schema) {
|
|
|
353
355
|
for (const key in schema) {
|
|
354
356
|
if (key === "_tableName" || key.startsWith("__"))
|
|
355
357
|
continue;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
358
|
+
let definition = schema[key];
|
|
359
|
+
if (typeof definition === "function") {
|
|
360
|
+
const potentialRelation = definition();
|
|
361
|
+
if (potentialRelation &&
|
|
362
|
+
["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(potentialRelation.type)) {
|
|
363
|
+
definition = potentialRelation;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
360
366
|
if (definition &&
|
|
361
367
|
(definition.type === "hasMany" ||
|
|
362
368
|
definition.type === "hasOne" ||
|
|
363
369
|
definition.type === "belongsTo" ||
|
|
364
370
|
definition.type === "manyToMany")) {
|
|
365
|
-
const relation = definition; // It's
|
|
371
|
+
const relation = definition; // It's now the object we need.
|
|
372
|
+
// Recursively create the schema for the related table
|
|
366
373
|
const childSchemaResult = createSchema(relation.schema());
|
|
367
374
|
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
375
|
+
sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
|
|
376
|
+
clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
|
|
368
377
|
validationFields[key] = z
|
|
369
378
|
.array(childSchemaResult.validationSchema)
|
|
370
379
|
.optional();
|
|
371
|
-
clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
|
|
372
|
-
sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
|
|
373
380
|
defaultValues[key] = Array.from({ length: relation.defaultCount || 0 }, () => childSchemaResult.defaultValues);
|
|
374
381
|
}
|
|
375
382
|
else {
|
|
376
383
|
// hasOne or belongsTo
|
|
377
|
-
validationFields[key] = childSchemaResult.validationSchema.optional();
|
|
378
|
-
clientFields[key] = childSchemaResult.clientSchema.optional();
|
|
379
384
|
sqlFields[key] = childSchemaResult.sqlSchema.optional();
|
|
385
|
+
clientFields[key] = childSchemaResult.clientSchema.optional();
|
|
386
|
+
validationFields[key] = childSchemaResult.validationSchema.optional();
|
|
380
387
|
defaultValues[key] = childSchemaResult.defaultValues;
|
|
381
388
|
}
|
|
382
389
|
}
|
|
@@ -396,7 +403,6 @@ export function createSchema(schema) {
|
|
|
396
403
|
defaultValues[key] = referencedField.config.initialValue;
|
|
397
404
|
}
|
|
398
405
|
}
|
|
399
|
-
// Return the final, correctly typed Zod objects
|
|
400
406
|
return {
|
|
401
407
|
sqlSchema: z.object(sqlFields),
|
|
402
408
|
clientSchema: z.object(clientFields),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.51",
|
|
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",
|