cogsbox-shape 0.5.49 → 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 +35 -18
- 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),
|
|
@@ -417,25 +423,36 @@ function serializeSchemaMetadata(schema) {
|
|
|
417
423
|
if (key === "_tableName" || key.startsWith("__"))
|
|
418
424
|
continue;
|
|
419
425
|
const definition = schema[key];
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
426
|
+
// Case 1: Is it a relation object? Check the `type` property.
|
|
427
|
+
if (definition &&
|
|
428
|
+
(definition.type === "hasMany" ||
|
|
429
|
+
definition.type === "hasOne" ||
|
|
430
|
+
definition.type === "belongsTo" ||
|
|
431
|
+
definition.type === "manyToMany")) {
|
|
432
|
+
const relation = definition;
|
|
424
433
|
let toKeyName = null;
|
|
425
434
|
try {
|
|
426
435
|
let targetFieldDefinition = relation.toKey();
|
|
427
436
|
if (targetFieldDefinition &&
|
|
428
437
|
targetFieldDefinition.type === "reference") {
|
|
429
|
-
targetFieldDefinition = targetFieldDefinition.to
|
|
438
|
+
targetFieldDefinition = targetFieldDefinition.to;
|
|
430
439
|
}
|
|
431
|
-
|
|
432
|
-
|
|
440
|
+
const targetSchemaObject = relation.schema();
|
|
441
|
+
for (const targetKey in targetSchemaObject) {
|
|
442
|
+
if (targetSchemaObject[targetKey] === targetFieldDefinition) {
|
|
433
443
|
toKeyName = targetKey;
|
|
434
444
|
break;
|
|
435
445
|
}
|
|
436
446
|
}
|
|
437
447
|
if (!toKeyName)
|
|
438
|
-
throw new Error(`Could not find field name for relation target
|
|
448
|
+
throw new Error(`Could not find field name for relation target.`);
|
|
449
|
+
relations[key] = {
|
|
450
|
+
type: "relation",
|
|
451
|
+
relationType: relation.type,
|
|
452
|
+
fromKey: relation.fromKey,
|
|
453
|
+
toKey: toKeyName,
|
|
454
|
+
schema: serializeSchemaMetadata(targetSchemaObject),
|
|
455
|
+
};
|
|
439
456
|
}
|
|
440
457
|
catch (e) {
|
|
441
458
|
console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
|
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",
|