cogsbox-shape 0.5.23 → 0.5.26

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.
@@ -1,17 +1,17 @@
1
1
  import { z } from "zod";
2
- import { createSchema, hasMany, shape } from "../schema";
2
+ import { belongsTo, createSchema, hasMany, shape } from "../schema.js";
3
3
  export const userSchema = {
4
4
  _tableName: "users",
5
5
  id: shape.sql({ type: "int", pk: true }),
6
6
  firstname: shape
7
7
  .sql({ type: "varchar", length: 255 })
8
- .db(({ zod }) => zod.min(1)),
8
+ .validation(({ sql }) => sql.min(1)),
9
9
  surname: shape
10
10
  .sql({ type: "varchar", length: 255 })
11
- .db(({ zod }) => zod.min(1)),
11
+ .validation(({ sql }) => sql.min(1)),
12
12
  email: shape
13
13
  .sql({ type: "varchar", length: 255 })
14
- .db(({ zod }) => zod.email()),
14
+ .validation(({ sql }) => sql.email()),
15
15
  pets: hasMany({
16
16
  fromKey: "id",
17
17
  toKey: () => petSchema.userId,
@@ -21,22 +21,55 @@ export const userSchema = {
21
21
  };
22
22
  export const petSchema = {
23
23
  _tableName: "pets",
24
- id: shape.sql({ type: "int", pk: true }).client(({ zod }) => z.string()),
24
+ id: shape
25
+ .sql({ type: "int", pk: true })
26
+ .initialState(z.string(), () => "uuidexample")
27
+ .client(({ sql, initialState }) => z.union([sql, initialState]))
28
+ .validation(z.string())
29
+ .transform({
30
+ toClient: (dbValue) => dbValue,
31
+ toDb: (clientValue) => Number(clientValue),
32
+ }),
25
33
  name: shape.sql({ type: "varchar", length: 255 }),
26
34
  userId: shape.sql({ type: "int" }).client(z.string()),
27
35
  fluffynessScale: shape
28
36
  .sql({ type: "text" })
29
- .client(({ zod }) => z.array(z.enum(["bald", "fuzzy", "fluffy", "poof"])))
37
+ .client(({ sql }) => z.array(z.enum(["bald", "fuzzy", "fluffy", "poof"])))
30
38
  .transform({
31
39
  toClient: (value) => value.split(",").filter(Boolean),
32
40
  toDb: (value) => value.join(","),
33
41
  }),
34
42
  favourite: shape
35
43
  .sql({ type: "int" })
36
- .client(({ zod }) => z.boolean())
44
+ .client(({ sql }) => z.boolean())
37
45
  .transform({
38
46
  toClient: (dbValue) => dbValue === 1,
39
47
  toDb: (clientValue) => (clientValue ? 1 : 0),
40
48
  }),
41
49
  };
42
- export const { dbSchema, clientSchema, initialValues, serialized } = createSchema(userSchema);
50
+ // export const { dbSchema, clientSchema, initialValues, serialized } =
51
+ // createSchema(userSchema);
52
+ const testPets = {
53
+ _tableName: "users",
54
+ id: shape
55
+ .sql({ type: "int", pk: true })
56
+ .initialState(z.string().uuid(), () => "sdasdsad")
57
+ .client(({ sql, initialState }) => z.union([sql, initialState]))
58
+ .validation(({ sql }) => sql)
59
+ .transform({
60
+ toClient: (dbValue) => dbValue,
61
+ toDb: (clientValue) => Number(clientValue),
62
+ }),
63
+ email: shape
64
+ .sql({ type: "varchar", length: 255 })
65
+ .validation(({ sql }) => sql),
66
+ createdAt: shape
67
+ .sql({ type: "datetime" })
68
+ .client(({ sql }) => z.string())
69
+ .validation(({ sql }) => sql.optional())
70
+ .transform({
71
+ toClient: (date) => date.toISOString(),
72
+ toDb: (str) => new Date(str),
73
+ }),
74
+ };
75
+ const { sqlSchema, clientSchema: clSchema, defaultValues, validationSchema, } = createSchema(testPets);