cogsbox-shape 0.5.43 → 0.5.44
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 +21 -17
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -349,11 +349,11 @@ export function createSchema(schema) {
|
|
|
349
349
|
const validationFields = {};
|
|
350
350
|
const defaultValues = {};
|
|
351
351
|
for (const key in schema) {
|
|
352
|
-
if (key === "_tableName")
|
|
352
|
+
if (key === "_tableName" || key.startsWith("__"))
|
|
353
353
|
continue;
|
|
354
354
|
const field = schema[key];
|
|
355
355
|
// Case 1: Handle relation functions (hasMany, hasOne, etc.)
|
|
356
|
-
if (
|
|
356
|
+
if (isFunction(field)) {
|
|
357
357
|
const relation = field();
|
|
358
358
|
if (!isRelation(relation)) {
|
|
359
359
|
continue;
|
|
@@ -362,34 +362,36 @@ export function createSchema(schema) {
|
|
|
362
362
|
const childSchemaResult = createSchema(relation.schema);
|
|
363
363
|
// For to-many relations, wrap schemas in z.array()
|
|
364
364
|
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
validationFields[key] =
|
|
368
|
-
|
|
365
|
+
const arraySchema = z.array(childSchemaResult.validationSchema);
|
|
366
|
+
// Relations are often not present on creation, so they should be optional.
|
|
367
|
+
validationFields[key] = arraySchema.optional();
|
|
368
|
+
clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
|
|
369
|
+
sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
|
|
369
370
|
const count = relation.defaultCount || 0;
|
|
370
371
|
defaultValues[key] = Array.from({ length: count }, () => childSchemaResult.defaultValues);
|
|
371
372
|
}
|
|
372
373
|
else {
|
|
373
|
-
//
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
374
|
+
// hasOne or belongsTo
|
|
375
|
+
// Relations are often not present on creation, so they should be optional.
|
|
376
|
+
validationFields[key] = childSchemaResult.validationSchema.optional();
|
|
377
|
+
clientFields[key] = childSchemaResult.clientSchema.optional();
|
|
378
|
+
sqlFields[key] = childSchemaResult.sqlSchema.optional();
|
|
377
379
|
defaultValues[key] = childSchemaResult.defaultValues;
|
|
378
380
|
}
|
|
379
381
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
field.type === "reference") {
|
|
382
|
+
// Case 2: Handle reference() objects
|
|
383
|
+
else if (field && field.type === "reference") {
|
|
383
384
|
const referencedField = field.to();
|
|
384
|
-
sqlFields[key] = referencedField.config.zodSqlSchema;
|
|
385
|
-
clientFields[key] = referencedField.config.zodClientSchema;
|
|
386
385
|
validationFields[key] = referencedField.config.zodValidationSchema;
|
|
386
|
+
clientFields[key] = referencedField.config.zodClientSchema;
|
|
387
|
+
sqlFields[key] = referencedField.config.zodSqlSchema;
|
|
387
388
|
defaultValues[key] = referencedField.config.initialValue;
|
|
388
389
|
}
|
|
390
|
+
// Case 3: Handle standard shape.sql() fields
|
|
389
391
|
else if (field && typeof field === "object" && "config" in field) {
|
|
390
|
-
sqlFields[key] = field.config.zodSqlSchema;
|
|
391
|
-
clientFields[key] = field.config.zodClientSchema;
|
|
392
392
|
validationFields[key] = field.config.zodValidationSchema;
|
|
393
|
+
clientFields[key] = field.config.zodClientSchema;
|
|
394
|
+
sqlFields[key] = field.config.zodSqlSchema;
|
|
393
395
|
defaultValues[key] = field.config.initialValue;
|
|
394
396
|
}
|
|
395
397
|
}
|
|
@@ -404,6 +406,7 @@ export function createSchema(schema) {
|
|
|
404
406
|
/**
|
|
405
407
|
* (This is the smart function from the last answer that resolves `toKey` functions)
|
|
406
408
|
*/
|
|
409
|
+
// In your cogsbox-shape file, replace the entire `serializeSchemaMetadata` function.
|
|
407
410
|
function serializeSchemaMetadata(schema) {
|
|
408
411
|
const fields = {};
|
|
409
412
|
const relations = {};
|
|
@@ -436,6 +439,7 @@ function serializeSchemaMetadata(schema) {
|
|
|
436
439
|
console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
|
|
437
440
|
throw e;
|
|
438
441
|
}
|
|
442
|
+
// This is the critical part: ADD the processed relation to the relations object.
|
|
439
443
|
relations[key] = {
|
|
440
444
|
type: "relation",
|
|
441
445
|
relationType: relation.type,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.44",
|
|
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",
|