cogsbox-shape 0.5.33 → 0.5.35
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/example/schema.d.ts +3 -6
- package/dist/example/user.d.ts +1887 -1890
- package/dist/example/user.js +21 -21
- package/dist/schema.d.ts +9 -10
- package/dist/schema.js +7 -17
- package/package.json +1 -1
package/dist/example/user.js
CHANGED
|
@@ -1,25 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { createSchema, hasMany, reference, shape, } from "../schema.js";
|
|
3
|
-
export const userSchema = {
|
|
4
|
-
_tableName: "users",
|
|
5
|
-
id: shape.sql({ type: "int", pk: true }),
|
|
6
|
-
firstname: shape
|
|
7
|
-
.sql({ type: "varchar", length: 255 })
|
|
8
|
-
.initialState(() => "test")
|
|
9
|
-
.validation(({ sql }) => sql.min(1)),
|
|
10
|
-
surname: shape
|
|
11
|
-
.sql({ type: "varchar", length: 255 })
|
|
12
|
-
.validation(({ sql }) => sql.min(1)),
|
|
13
|
-
email: shape
|
|
14
|
-
.sql({ type: "varchar", length: 255 })
|
|
15
|
-
.validation(({ sql }) => sql.email()),
|
|
16
|
-
pets: hasMany({
|
|
17
|
-
fromKey: "id",
|
|
18
|
-
toKey: () => petSchema.userId,
|
|
19
|
-
schema: () => petSchema,
|
|
20
|
-
defaultCount: 1,
|
|
21
|
-
}),
|
|
22
|
-
};
|
|
23
3
|
export const petSchema = {
|
|
24
4
|
_tableName: "pets",
|
|
25
5
|
id: shape
|
|
@@ -32,7 +12,7 @@ export const petSchema = {
|
|
|
32
12
|
toDb: (clientValue) => Number(clientValue),
|
|
33
13
|
}),
|
|
34
14
|
name: shape.sql({ type: "varchar", length: 255 }),
|
|
35
|
-
userId: reference(
|
|
15
|
+
userId: reference(() => userSchema.id),
|
|
36
16
|
fluffynessScale: shape
|
|
37
17
|
.sql({ type: "text" })
|
|
38
18
|
.client(({ sql }) => z.array(z.enum(["bald", "fuzzy", "fluffy", "poof"])))
|
|
@@ -50,6 +30,26 @@ export const petSchema = {
|
|
|
50
30
|
};
|
|
51
31
|
// export const { dbSchema, clientSchema, initialValues, serialized } =
|
|
52
32
|
// createSchema(userSchema);
|
|
33
|
+
export const userSchema = {
|
|
34
|
+
_tableName: "users",
|
|
35
|
+
id: shape.sql({ type: "int", pk: true }),
|
|
36
|
+
firstname: shape
|
|
37
|
+
.sql({ type: "varchar", length: 255 })
|
|
38
|
+
.initialState(() => "test")
|
|
39
|
+
.validation(({ sql }) => sql.min(1)),
|
|
40
|
+
surname: shape
|
|
41
|
+
.sql({ type: "varchar", length: 255 })
|
|
42
|
+
.validation(({ sql }) => sql.min(1)),
|
|
43
|
+
email: shape
|
|
44
|
+
.sql({ type: "varchar", length: 255 })
|
|
45
|
+
.validation(({ sql }) => sql.email()),
|
|
46
|
+
pets: hasMany({
|
|
47
|
+
fromKey: "id",
|
|
48
|
+
toKey: () => petSchema.userId,
|
|
49
|
+
schema: () => petSchema,
|
|
50
|
+
defaultCount: 1,
|
|
51
|
+
}),
|
|
52
|
+
};
|
|
53
53
|
const testPets = {
|
|
54
54
|
_tableName: "users",
|
|
55
55
|
id: shape
|
package/dist/schema.d.ts
CHANGED
|
@@ -4734,9 +4734,8 @@ type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
|
4734
4734
|
};
|
|
4735
4735
|
type AnyFieldDefinition = ReturnType<typeof shape.sql>;
|
|
4736
4736
|
type ReferenceField<TField extends AnyFieldDefinition> = {
|
|
4737
|
-
field: TField;
|
|
4738
4737
|
type: "reference";
|
|
4739
|
-
to: () =>
|
|
4738
|
+
to: () => TField;
|
|
4740
4739
|
};
|
|
4741
4740
|
type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T> | ReferenceField<AnyFieldDefinition>;
|
|
4742
4741
|
export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>> = {
|
|
@@ -4780,13 +4779,9 @@ export type InferDBSchema<T> = {
|
|
|
4780
4779
|
} ? DbType : never;
|
|
4781
4780
|
}> : never;
|
|
4782
4781
|
};
|
|
4783
|
-
export declare function reference<TField extends object
|
|
4784
|
-
to: TField;
|
|
4785
|
-
field: Zod;
|
|
4786
|
-
}): {
|
|
4787
|
-
field: Zod;
|
|
4782
|
+
export declare function reference<TField extends object>(config: TField): {
|
|
4788
4783
|
type: "reference";
|
|
4789
|
-
to:
|
|
4784
|
+
to: TField;
|
|
4790
4785
|
};
|
|
4791
4786
|
export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
|
|
4792
4787
|
type SchemaDefinition = {
|
|
@@ -4800,8 +4795,12 @@ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodVa
|
|
|
4800
4795
|
};
|
|
4801
4796
|
} ? S : T[K] extends {
|
|
4802
4797
|
type: "reference";
|
|
4803
|
-
|
|
4804
|
-
|
|
4798
|
+
to: () => {
|
|
4799
|
+
config: {
|
|
4800
|
+
[P in Key]: infer S extends z.ZodTypeAny;
|
|
4801
|
+
};
|
|
4802
|
+
};
|
|
4803
|
+
} ? S : T[K] extends () => {
|
|
4805
4804
|
type: "hasMany" | "manyToMany";
|
|
4806
4805
|
schema: infer S extends SchemaDefinition;
|
|
4807
4806
|
} ? z.ZodArray<z.ZodObject<Prettify<InferSchemaByKey<S, Key>>>> : T[K] extends () => {
|
package/dist/schema.js
CHANGED
|
@@ -118,7 +118,7 @@ function createBuilder(config) {
|
|
|
118
118
|
: config.sqlZod; // Keep SQL type if just setting default
|
|
119
119
|
const finalDefaultValue = hasTypeParam
|
|
120
120
|
? defaultValue()
|
|
121
|
-
: schemaOrDefault
|
|
121
|
+
: schemaOrDefault;
|
|
122
122
|
const newCompletedStages = new Set(completedStages);
|
|
123
123
|
newCompletedStages.add("new");
|
|
124
124
|
const newClientZod = hasTypeParam
|
|
@@ -267,9 +267,8 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
267
267
|
}
|
|
268
268
|
export function reference(config) {
|
|
269
269
|
return {
|
|
270
|
-
field: config.field,
|
|
271
270
|
type: "reference",
|
|
272
|
-
to:
|
|
271
|
+
to: config,
|
|
273
272
|
};
|
|
274
273
|
}
|
|
275
274
|
export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
|
|
@@ -380,20 +379,11 @@ export function createSchema(schema) {
|
|
|
380
379
|
else if (field &&
|
|
381
380
|
typeof field === "object" &&
|
|
382
381
|
field.type === "reference") {
|
|
383
|
-
|
|
384
|
-
sqlFields[key] =
|
|
385
|
-
clientFields[key] =
|
|
386
|
-
validationFields[key] =
|
|
387
|
-
|
|
388
|
-
if (field.field instanceof z.ZodNumber) {
|
|
389
|
-
defaultValues[key] = 0;
|
|
390
|
-
}
|
|
391
|
-
else if (field.field instanceof z.ZodString) {
|
|
392
|
-
defaultValues[key] = "";
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
defaultValues[key] = inferDefaultFromZod(field.field);
|
|
396
|
-
}
|
|
382
|
+
const referencedField = field.to();
|
|
383
|
+
sqlFields[key] = referencedField.config.zodSqlSchema;
|
|
384
|
+
clientFields[key] = referencedField.config.zodClientSchema;
|
|
385
|
+
validationFields[key] = referencedField.config.zodValidationSchema;
|
|
386
|
+
defaultValues[key] = referencedField.config.initialValue;
|
|
397
387
|
}
|
|
398
388
|
else if (field && typeof field === "object" && "config" in field) {
|
|
399
389
|
sqlFields[key] = field.config.zodSqlSchema;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.35",
|
|
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",
|