cogsbox-shape 0.5.41 → 0.5.42
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.d.ts +4 -45
- package/dist/schema.js +21 -17
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -4784,56 +4784,15 @@ export declare function reference<TField extends object>(config: TField): {
|
|
|
4784
4784
|
to: TField;
|
|
4785
4785
|
};
|
|
4786
4786
|
export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
|
|
4787
|
-
type SchemaDefinition = {
|
|
4788
|
-
_tableName: string;
|
|
4789
|
-
[key: string]: any;
|
|
4790
|
-
};
|
|
4791
|
-
type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema"> = {
|
|
4792
|
-
[K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
|
|
4793
|
-
config: {
|
|
4794
|
-
[P in Key]: infer S extends z.ZodTypeAny;
|
|
4795
|
-
};
|
|
4796
|
-
} ? S : T[K] extends {
|
|
4797
|
-
type: "reference";
|
|
4798
|
-
to: () => {
|
|
4799
|
-
config: {
|
|
4800
|
-
[P in Key]: infer S extends z.ZodTypeAny;
|
|
4801
|
-
};
|
|
4802
|
-
};
|
|
4803
|
-
} ? S : T[K] extends () => {
|
|
4804
|
-
type: "hasMany" | "manyToMany";
|
|
4805
|
-
schema: infer S extends SchemaDefinition;
|
|
4806
|
-
} ? z.ZodArray<z.ZodObject<Prettify<InferSchemaByKey<S, Key>>>> : T[K] extends () => {
|
|
4807
|
-
type: "hasOne" | "belongsTo";
|
|
4808
|
-
schema: infer S extends SchemaDefinition;
|
|
4809
|
-
} ? z.ZodObject<Prettify<InferSchemaByKey<S, Key>>> : never;
|
|
4810
|
-
};
|
|
4811
|
-
type InferSqlSchema<T> = InferSchemaByKey<T, "zodSqlSchema">;
|
|
4812
|
-
type InferClientSchema<T> = InferSchemaByKey<T, "zodClientSchema">;
|
|
4813
|
-
type InferValidationSchema<T> = InferSchemaByKey<T, "zodValidationSchema">;
|
|
4814
|
-
type InferDefaultValues2<T> = {
|
|
4815
|
-
[K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
|
|
4816
|
-
config: {
|
|
4817
|
-
initialValue: infer D;
|
|
4818
|
-
};
|
|
4819
|
-
} ? D : T[K] extends () => {
|
|
4820
|
-
type: "hasMany" | "manyToMany";
|
|
4821
|
-
schema: infer S extends SchemaDefinition;
|
|
4822
|
-
defaultCount?: number;
|
|
4823
|
-
} ? Array<Prettify<InferDefaultValues2<S>>> : T[K] extends () => {
|
|
4824
|
-
type: "hasOne" | "belongsTo";
|
|
4825
|
-
schema: infer S extends SchemaDefinition;
|
|
4826
|
-
} ? Prettify<InferDefaultValues2<S>> : never;
|
|
4827
|
-
};
|
|
4828
4787
|
export declare function createSchema<T extends {
|
|
4829
4788
|
_tableName: string;
|
|
4830
4789
|
}>(schema: T extends {
|
|
4831
4790
|
_tableName: string;
|
|
4832
4791
|
} ? T : never): {
|
|
4833
|
-
sqlSchema:
|
|
4834
|
-
clientSchema:
|
|
4835
|
-
validationSchema:
|
|
4836
|
-
defaultValues:
|
|
4792
|
+
sqlSchema: any;
|
|
4793
|
+
clientSchema: any;
|
|
4794
|
+
validationSchema: any;
|
|
4795
|
+
defaultValues: any;
|
|
4837
4796
|
};
|
|
4838
4797
|
export type InferSchemaTypes<T extends {
|
|
4839
4798
|
_tableName: string;
|
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.42",
|
|
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",
|