cogsbox-shape 0.5.50 → 0.5.52
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 +47 -30
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -4784,54 +4784,13 @@ 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): {
|
|
4831
|
-
sqlSchema: z.ZodObject<
|
|
4832
|
-
clientSchema: z.ZodObject<
|
|
4833
|
-
validationSchema: z.ZodObject<
|
|
4834
|
-
defaultValues:
|
|
4790
|
+
sqlSchema: z.ZodObject<any>;
|
|
4791
|
+
clientSchema: z.ZodObject<any>;
|
|
4792
|
+
validationSchema: z.ZodObject<any>;
|
|
4793
|
+
defaultValues: any;
|
|
4835
4794
|
};
|
|
4836
4795
|
export type InferSchemaTypes<T extends {
|
|
4837
4796
|
_tableName: string;
|
package/dist/schema.js
CHANGED
|
@@ -345,58 +345,75 @@ 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 = {};
|
|
351
353
|
const validationFields = {};
|
|
352
354
|
const defaultValues = {};
|
|
355
|
+
// Store the functions to be executed in a second pass
|
|
356
|
+
const deferredFields = [];
|
|
357
|
+
// --- PASS 1: Process all non-function fields ---
|
|
353
358
|
for (const key in schema) {
|
|
354
359
|
if (key === "_tableName" || key.startsWith("__"))
|
|
355
360
|
continue;
|
|
356
361
|
const definition = schema[key];
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
definition.type === "manyToMany")) {
|
|
365
|
-
const relation = definition; // It's already the object we need.
|
|
366
|
-
const childSchemaResult = createSchema(relation.schema());
|
|
367
|
-
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
368
|
-
validationFields[key] = z
|
|
369
|
-
.array(childSchemaResult.validationSchema)
|
|
370
|
-
.optional();
|
|
371
|
-
clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
|
|
372
|
-
sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
|
|
373
|
-
defaultValues[key] = Array.from({ length: relation.defaultCount || 0 }, () => childSchemaResult.defaultValues);
|
|
374
|
-
}
|
|
375
|
-
else {
|
|
376
|
-
// hasOne or belongsTo
|
|
377
|
-
validationFields[key] = childSchemaResult.validationSchema.optional();
|
|
378
|
-
clientFields[key] = childSchemaResult.clientSchema.optional();
|
|
379
|
-
sqlFields[key] = childSchemaResult.sqlSchema.optional();
|
|
380
|
-
defaultValues[key] = childSchemaResult.defaultValues;
|
|
381
|
-
}
|
|
362
|
+
if (typeof definition === "function") {
|
|
363
|
+
// It's a hasMany, belongsTo, etc. Defer it.
|
|
364
|
+
deferredFields.push({ key, definition });
|
|
365
|
+
}
|
|
366
|
+
else if (definition && definition.type === "reference") {
|
|
367
|
+
// It's a reference. Defer it.
|
|
368
|
+
deferredFields.push({ key, definition });
|
|
382
369
|
}
|
|
383
|
-
// Case 2: Is it a standard field builder object?
|
|
384
370
|
else if (definition && typeof definition.config === "object") {
|
|
371
|
+
// It's a standard field builder. Process it now.
|
|
385
372
|
sqlFields[key] = definition.config.zodSqlSchema;
|
|
386
373
|
clientFields[key] = definition.config.zodClientSchema;
|
|
387
374
|
validationFields[key] = definition.config.zodValidationSchema;
|
|
388
375
|
defaultValues[key] = definition.config.initialValue;
|
|
389
376
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
377
|
+
}
|
|
378
|
+
// --- PASS 2: Process all deferred references and relations ---
|
|
379
|
+
// Now we can safely call the functions because the schemas they refer to exist.
|
|
380
|
+
for (const { key, definition } of deferredFields) {
|
|
381
|
+
let resolvedDefinition = definition;
|
|
382
|
+
// If it's a relation like hasMany, call the outer function to get the config object
|
|
383
|
+
if (typeof resolvedDefinition === "function") {
|
|
384
|
+
resolvedDefinition = resolvedDefinition();
|
|
385
|
+
}
|
|
386
|
+
if (resolvedDefinition && resolvedDefinition.type === "reference") {
|
|
387
|
+
const referencedField = resolvedDefinition.to(); // This is now safe to call
|
|
388
|
+
if (!referencedField || !referencedField.config) {
|
|
389
|
+
throw new Error(`Could not resolve reference for key "${key}"`);
|
|
390
|
+
}
|
|
393
391
|
sqlFields[key] = referencedField.config.zodSqlSchema;
|
|
394
392
|
clientFields[key] = referencedField.config.zodClientSchema;
|
|
395
393
|
validationFields[key] = referencedField.config.zodValidationSchema;
|
|
396
394
|
defaultValues[key] = referencedField.config.initialValue;
|
|
397
395
|
}
|
|
396
|
+
else if (resolvedDefinition &&
|
|
397
|
+
["hasMany", "manyToMany", "hasOne", "belongsTo"].includes(resolvedDefinition.type)) {
|
|
398
|
+
const relation = resolvedDefinition;
|
|
399
|
+
const childSchemaResult = createSchema(relation.schema()); // Recursive call
|
|
400
|
+
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
401
|
+
sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
|
|
402
|
+
clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
|
|
403
|
+
validationFields[key] = z
|
|
404
|
+
.array(childSchemaResult.validationSchema)
|
|
405
|
+
.optional();
|
|
406
|
+
defaultValues[key] = Array.from({ length: relation.defaultCount || 0 }, () => childSchemaResult.defaultValues);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
// hasOne or belongsTo
|
|
410
|
+
sqlFields[key] = childSchemaResult.sqlSchema.optional();
|
|
411
|
+
clientFields[key] = childSchemaResult.clientSchema.optional();
|
|
412
|
+
validationFields[key] = childSchemaResult.validationSchema.optional();
|
|
413
|
+
defaultValues[key] = childSchemaResult.defaultValues;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
398
416
|
}
|
|
399
|
-
// Return the final, correctly typed Zod objects
|
|
400
417
|
return {
|
|
401
418
|
sqlSchema: z.object(sqlFields),
|
|
402
419
|
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.52",
|
|
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",
|