cogsbox-shape 0.5.47 → 0.5.49
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 +22 -22
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -343,6 +343,8 @@ function isRelation(value) {
|
|
|
343
343
|
"toKey" in value &&
|
|
344
344
|
"schema" in value);
|
|
345
345
|
}
|
|
346
|
+
// In your cogsbox-shape file...
|
|
347
|
+
// Replace the entire existing createSchema function with this SINGLE, CORRECT version.
|
|
346
348
|
export function createSchema(schema) {
|
|
347
349
|
const sqlFields = {};
|
|
348
350
|
const clientFields = {};
|
|
@@ -351,20 +353,18 @@ export function createSchema(schema) {
|
|
|
351
353
|
for (const key in schema) {
|
|
352
354
|
if (key === "_tableName" || key.startsWith("__"))
|
|
353
355
|
continue;
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
//
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
356
|
+
const definition = schema[key];
|
|
357
|
+
// --- THIS IS THE CORRECT, SIMPLE LOGIC ---
|
|
358
|
+
// Case 1: Is it a relation object?
|
|
359
|
+
// A relation object will have a `type` like "hasMany" and a `schema` property.
|
|
360
|
+
if (definition &&
|
|
361
|
+
(definition.type === "hasMany" ||
|
|
362
|
+
definition.type === "hasOne" ||
|
|
363
|
+
definition.type === "belongsTo" ||
|
|
364
|
+
definition.type === "manyToMany")) {
|
|
365
|
+
const relation = definition; // It's already the object we need.
|
|
364
366
|
const childSchemaResult = createSchema(relation.schema());
|
|
365
367
|
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
366
|
-
// CORRECTLY ADD THE RELATION TO THE LIST OF VALIDATION FIELDS
|
|
367
|
-
// Make it optional, as relations are often not included on create/update.
|
|
368
368
|
validationFields[key] = z
|
|
369
369
|
.array(childSchemaResult.validationSchema)
|
|
370
370
|
.optional();
|
|
@@ -380,21 +380,21 @@ export function createSchema(schema) {
|
|
|
380
380
|
defaultValues[key] = childSchemaResult.defaultValues;
|
|
381
381
|
}
|
|
382
382
|
}
|
|
383
|
-
// Case 2:
|
|
384
|
-
else if (
|
|
385
|
-
|
|
383
|
+
// Case 2: Is it a standard field builder object?
|
|
384
|
+
else if (definition && typeof definition.config === "object") {
|
|
385
|
+
sqlFields[key] = definition.config.zodSqlSchema;
|
|
386
|
+
clientFields[key] = definition.config.zodClientSchema;
|
|
387
|
+
validationFields[key] = definition.config.zodValidationSchema;
|
|
388
|
+
defaultValues[key] = definition.config.initialValue;
|
|
389
|
+
}
|
|
390
|
+
// Case 3: Is it a reference object?
|
|
391
|
+
else if (definition && definition.type === "reference") {
|
|
392
|
+
const referencedField = definition.to();
|
|
386
393
|
sqlFields[key] = referencedField.config.zodSqlSchema;
|
|
387
394
|
clientFields[key] = referencedField.config.zodClientSchema;
|
|
388
395
|
validationFields[key] = referencedField.config.zodValidationSchema;
|
|
389
396
|
defaultValues[key] = referencedField.config.initialValue;
|
|
390
397
|
}
|
|
391
|
-
// Case 3: Handle standard shape.sql() fields
|
|
392
|
-
else if (field && typeof field === "object" && "config" in field) {
|
|
393
|
-
sqlFields[key] = field.config.zodSqlSchema;
|
|
394
|
-
clientFields[key] = field.config.zodClientSchema;
|
|
395
|
-
validationFields[key] = field.config.zodValidationSchema;
|
|
396
|
-
defaultValues[key] = field.config.initialValue;
|
|
397
|
-
}
|
|
398
398
|
}
|
|
399
399
|
// Return the final, correctly typed Zod objects
|
|
400
400
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.49",
|
|
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",
|