@palmares/schemas 0.1.20 → 0.1.22

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/src/adapter/index.js +2 -0
  3. package/dist/cjs/src/conf.js +3 -4
  4. package/dist/cjs/src/model.js +34 -38
  5. package/dist/cjs/src/schema/array.js +1 -0
  6. package/dist/cjs/src/schema/object.js +2 -8
  7. package/dist/cjs/src/schema/schema.js +1 -0
  8. package/dist/cjs/src/utils.js +12 -14
  9. package/dist/cjs/src/validators/utils.js +1 -0
  10. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
  11. package/dist/cjs/types/adapter/index.d.ts +2 -0
  12. package/dist/cjs/types/adapter/index.d.ts.map +1 -1
  13. package/dist/cjs/types/conf.d.ts +3 -0
  14. package/dist/cjs/types/conf.d.ts.map +1 -1
  15. package/dist/cjs/types/domain.d.ts +3 -0
  16. package/dist/cjs/types/domain.d.ts.map +1 -1
  17. package/dist/cjs/types/model.d.ts +2 -2
  18. package/dist/cjs/types/model.d.ts.map +1 -1
  19. package/dist/cjs/types/schema/array.d.ts +1 -0
  20. package/dist/cjs/types/schema/array.d.ts.map +1 -1
  21. package/dist/cjs/types/schema/object.d.ts.map +1 -1
  22. package/dist/cjs/types/schema/schema.d.ts +1 -0
  23. package/dist/cjs/types/schema/schema.d.ts.map +1 -1
  24. package/dist/cjs/types/utils.d.ts +2 -1
  25. package/dist/cjs/types/utils.d.ts.map +1 -1
  26. package/dist/cjs/types/validators/utils.d.ts +1 -0
  27. package/dist/cjs/types/validators/utils.d.ts.map +1 -1
  28. package/dist/esm/src/adapter/index.js +2 -0
  29. package/dist/esm/src/conf.js +3 -4
  30. package/dist/esm/src/model.js +32 -30
  31. package/dist/esm/src/schema/array.js +1 -0
  32. package/dist/esm/src/schema/object.js +2 -1
  33. package/dist/esm/src/schema/schema.js +1 -0
  34. package/dist/esm/src/utils.js +9 -6
  35. package/dist/esm/src/validators/utils.js +1 -0
  36. package/package.json +11 -5
  37. package/.turbo/turbo-build$colon$watch.log +0 -24
  38. package/.turbo/turbo-build.log +0 -4
  39. package/.turbo/turbo-build:watch.log +0 -26
  40. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
  41. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
  42. package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
  43. package/__tests__/.drizzle/schema.ts +0 -35
  44. package/__tests__/drizzle.config.ts +0 -11
  45. package/__tests__/eslint.config.js +0 -10
  46. package/__tests__/manage.ts +0 -5
  47. package/__tests__/node_modules/.bin/drizzle-kit +0 -17
  48. package/__tests__/node_modules/.bin/tsc +0 -17
  49. package/__tests__/node_modules/.bin/tsserver +0 -17
  50. package/__tests__/node_modules/.bin/tsx +0 -17
  51. package/__tests__/package.json +0 -34
  52. package/__tests__/sqlite.db +0 -0
  53. package/__tests__/src/core/array.test.ts +0 -131
  54. package/__tests__/src/core/boolean.test.ts +0 -66
  55. package/__tests__/src/core/datetime.test.ts +0 -102
  56. package/__tests__/src/core/index.ts +0 -35
  57. package/__tests__/src/core/model.test.ts +0 -260
  58. package/__tests__/src/core/models.ts +0 -50
  59. package/__tests__/src/core/numbers.test.ts +0 -177
  60. package/__tests__/src/core/object.test.ts +0 -218
  61. package/__tests__/src/core/string.test.ts +0 -222
  62. package/__tests__/src/core/test.test.ts +0 -59
  63. package/__tests__/src/core/types.test.ts +0 -97
  64. package/__tests__/src/core/union.test.ts +0 -99
  65. package/__tests__/src/settings.ts +0 -69
  66. package/__tests__/tsconfig.json +0 -11
  67. package/src/adapter/fields/array.ts +0 -31
  68. package/src/adapter/fields/boolean.ts +0 -43
  69. package/src/adapter/fields/datetime.ts +0 -43
  70. package/src/adapter/fields/index.ts +0 -72
  71. package/src/adapter/fields/number.ts +0 -43
  72. package/src/adapter/fields/object.ts +0 -52
  73. package/src/adapter/fields/string.ts +0 -43
  74. package/src/adapter/fields/union.ts +0 -43
  75. package/src/adapter/index.ts +0 -35
  76. package/src/adapter/types.ts +0 -276
  77. package/src/compile.ts +0 -14
  78. package/src/conf.ts +0 -27
  79. package/src/constants.ts +0 -7
  80. package/src/domain.ts +0 -15
  81. package/src/exceptions.ts +0 -17
  82. package/src/index.ts +0 -318
  83. package/src/middleware.ts +0 -52
  84. package/src/model.ts +0 -507
  85. package/src/parsers/convert-from-number.ts +0 -13
  86. package/src/parsers/convert-from-string.ts +0 -19
  87. package/src/parsers/index.ts +0 -2
  88. package/src/schema/array.ts +0 -824
  89. package/src/schema/boolean.ts +0 -792
  90. package/src/schema/datetime.ts +0 -704
  91. package/src/schema/index.ts +0 -5
  92. package/src/schema/number.ts +0 -929
  93. package/src/schema/object.ts +0 -798
  94. package/src/schema/schema.ts +0 -1178
  95. package/src/schema/string.ts +0 -941
  96. package/src/schema/types.ts +0 -154
  97. package/src/schema/union.ts +0 -724
  98. package/src/types.ts +0 -66
  99. package/src/utils.ts +0 -383
  100. package/src/validators/array.ts +0 -183
  101. package/src/validators/boolean.ts +0 -52
  102. package/src/validators/datetime.ts +0 -121
  103. package/src/validators/number.ts +0 -178
  104. package/src/validators/object.ts +0 -56
  105. package/src/validators/schema.ts +0 -142
  106. package/src/validators/string.ts +0 -278
  107. package/src/validators/types.ts +0 -1
  108. package/src/validators/union.ts +0 -52
  109. package/src/validators/utils.ts +0 -225
  110. package/tsconfig.json +0 -9
  111. package/tsconfig.types.json +0 -10
package/src/index.ts DELETED
@@ -1,318 +0,0 @@
1
- import { SchemaAdapter } from './adapter';
2
- import { schemasDomain as SchemaDomain } from './domain';
3
- import { modelSchema } from './model';
4
- import { ArraySchema, array } from './schema/array';
5
- import { BooleanSchema, boolean } from './schema/boolean';
6
- import { DatetimeSchema, datetime } from './schema/datetime';
7
- import { NumberSchema, number } from './schema/number';
8
- import { ObjectSchema, object } from './schema/object';
9
- import { Schema, schema } from './schema/schema';
10
- import { StringSchema, string } from './schema/string';
11
- import { UnionSchema, union } from './schema/union';
12
-
13
- import type { DefinitionsOfSchemaType, ExtractTypeFromObjectOfSchemas } from './schema/types';
14
- import type { Narrow } from '@palmares/core';
15
- import type { Model, ModelFields } from '@palmares/databases';
16
-
17
- export { FieldAdapter, fieldAdapter } from './adapter/fields';
18
- export { NumberFieldAdapter, numberFieldAdapter } from './adapter/fields/number';
19
- export { ObjectFieldAdapter, objectFieldAdapter } from './adapter/fields/object';
20
- export { UnionFieldAdapter, unionFieldAdapter } from './adapter/fields/union';
21
- export { StringFieldAdapter, stringFieldAdapter } from './adapter/fields/string';
22
- export { ArrayFieldAdapter, arrayFieldAdapter } from './adapter/fields/array';
23
- export { BooleanFieldAdapter, booleanFieldAdapter } from './adapter/fields/boolean';
24
- export { DatetimeFieldAdapter, datetimeFieldAdapter } from './adapter/fields/datetime';
25
-
26
- export type { Infer as infer } from './types';
27
- export { setDefaultAdapter, getDefaultAdapter } from './conf';
28
- export * from './adapter/types';
29
- export * from './schema';
30
- export {
31
- SchemaAdapter,
32
- NumberSchema,
33
- ObjectSchema,
34
- UnionSchema,
35
- StringSchema,
36
- ArraySchema,
37
- BooleanSchema,
38
- DatetimeSchema,
39
- Schema
40
- };
41
- export { schema, number, object, union, string, array, datetime, boolean };
42
- export { compile } from './compile';
43
- export { schemaHandler } from './middleware';
44
-
45
- export { modelSchema };
46
- export { SchemaDomain };
47
-
48
- export default SchemaDomain;
49
-
50
- export function getSchemasWithDefaultAdapter<TAdapter extends SchemaAdapter>() {
51
- return {
52
- number: () => NumberSchema.new<{ schemaAdapter: TAdapter; schemaType: 'number'; hasSave: false }>(),
53
- string: () => StringSchema.new<{ schemaAdapter: TAdapter; schemaType: 'string'; hasSave: false }>(),
54
- array: <TSchemas extends readonly [Schema, ...Schema[]] | [[Schema]]>(...schemas: TSchemas) =>
55
- array<TSchemas, { schemaAdapter: TAdapter; schemaType: 'array'; hasSave: false }>(...schemas),
56
- boolean: () => BooleanSchema.new<{ schemaAdapter: TAdapter; schemaType: 'boolean'; hasSave: false }>(),
57
- object: <TData extends Record<any, Schema<any, any>>>(data: TData) =>
58
- ObjectSchema.new<TData, { schemaAdapter: TAdapter; schemaType: 'object'; hasSave: false }>(data),
59
- union: <TSchemas extends readonly [Schema<any, any>, Schema<any, any>, ...Schema<any, any>[]]>(
60
- ...schemas: Narrow<TSchemas>
61
- ) => UnionSchema.new<TSchemas, { schemaAdapter: TAdapter; schemaType: 'union'; hasSave: false }>(schemas),
62
- datetime: () => DatetimeSchema.new<{ schemaAdapter: TAdapter; schemaType: 'datetime'; hasSave: false }>(),
63
- /**
64
- * Different from other models, this function is a factory function that returns either an ObjectSchema or
65
- * an ArraySchema.
66
- * The idea is to build the schema of a model dynamically based on its fields.
67
- *
68
- * Another feature is that it can automatically add the foreign key relation to the schema,
69
- * but for that you need to define the fields of the related model in the fields object.
70
- *
71
- * For example: A User model have a field `companyId` that is a ForeignKeyField to the Company model.
72
- * The `relationName` is the direct relation from the User model to the Company model, and the `relatedName`
73
- * is the reverse relation from the Company model to the User model. If you define the fieldName as either
74
- * the relatedName or the relationName it will fetch the data automatically.
75
- *
76
- * **Important**: We build the schema dynamically but also lazily, if you don't try to parse or validate the
77
- * schema, it won't be built. After the first time it's built, it's cached and never built again.
78
- *
79
- * **Important 2**: If you want to use the automatic relation feature, you need to define guarantee that the
80
- * foreignKey field fieldName exists on `show` array, or that it doesn't exist on `omit` array.
81
- *
82
- * Like: `{ options: { show: ['id', 'name', 'companyId'] }}` or `{ options: { omit: ['id'] }}` it **will work**.
83
- *
84
- * If you do `{ options: { show: ['id', 'name'] }}` or `{ options: { omit: ['companyId']} }` it **won't work**.
85
- *
86
- * **Important 3**: If you want to return an array instead of an object, you need to pass the `many` option as true.
87
- *
88
- * @example
89
- * ```typescript
90
- * import { auto, choice, foreignKey, Model, define } from '@palmares/databases';
91
- * import * as p from '@palmares/schemas';
92
- *
93
- * const Company = define('Company', {
94
- * fields: {
95
- * id: auto(),
96
- * name: text(),
97
- * },
98
- * options: {
99
- * tableName: 'company',
100
- * }
101
- * });
102
- *
103
- * class User extends Model<User>() {
104
- * fields = {
105
- * id: auto(),
106
- * type: choice({ choices: ['user', 'admin'] }),
107
- * companyId: foreignKey({
108
- * relatedTo: Company,
109
- * relationName: 'company',
110
- * relatedName: 'usersOfCompany',
111
- * toField: 'id',
112
- * onDelete: 'CASCADE',
113
- * }),
114
- * }
115
- *
116
- * options = {
117
- * tableName: 'user',
118
- * }
119
- * }
120
- *
121
- * const userSchema = p.modelSchema(User, {
122
- * fields: {
123
- * company: p.modelSchema(Company).optional({ outputOnly: true });
124
- * },
125
- * show: ['type', 'companyId']
126
- * });
127
- *
128
- * const companySchema = p.modelSchema(Company, {
129
- * fields: {
130
- * usersOfCompany: p.modelSchema(User, { many: true }).optional({ outputOnly: true });
131
- * },
132
- * show: ['id', 'type']
133
- * });
134
- *```
135
- * @param model - The model that you want to build the schema from.
136
- * @param options - The options to build the schema.
137
- * @param options.ignoreExtraneousFields - If you want to ignore extraneous fields set this to true.
138
- * @param options.engineInstance - What engine instance you want to use to fetch the data.
139
- * Defaults to the first one.
140
- * @param options.fields - Extra fields that you want to add to the schema. If it has the same name as the
141
- * model field, We will not create a schema for that field and use the one you have defined here.
142
- * @param options.omit - Fields that you want to omit from the schema. If that is defined, we ignore `show` option.
143
- * @param options.show - Fields that you want to show on the schema. If that is defined, we ignore `omit` option.
144
- * @param options.many - If you want to return an array instead of an object, set this to true. With that we create
145
- * an ArraySchema instead of an ObjectSchema.
146
- *
147
- * @returns - If you pass the `many` option as true, we return an ArraySchema, otherwise we return an ObjectSchema.
148
- */
149
- modelSchema: <
150
- TModel extends ReturnType<typeof Model>,
151
- const TOmit extends readonly (keyof ModelFields<InstanceType<TModel>>)[] | undefined[] = undefined[],
152
- const TShow extends readonly (keyof ModelFields<InstanceType<TModel>>)[] | undefined[] = undefined[],
153
- TMany extends boolean = false,
154
- TFields extends Record<any, Schema<any, DefinitionsOfSchemaType>> | undefined = undefined,
155
- TAllModelFields = ModelFields<InstanceType<TModel>>,
156
- TFieldsOnModel = TOmit extends undefined[]
157
- ? TShow extends undefined[]
158
- ? TAllModelFields
159
- : Pick<TAllModelFields, TShow[number] extends keyof TAllModelFields ? TShow[number] : never>
160
- : Omit<TAllModelFields, TOmit[number] extends keyof TAllModelFields ? TOmit[number] : never>,
161
- TReturnType extends {
162
- input: any;
163
- output: any;
164
- validate: any;
165
- internal: any;
166
- representation: any;
167
- } = {
168
- input: TFields extends undefined
169
- ? TFieldsOnModel
170
- : Omit<
171
- TFieldsOnModel,
172
- keyof ExtractTypeFromObjectOfSchemas<
173
- // eslint-disable-next-line ts/ban-types
174
- TFields extends undefined ? {} : TFields,
175
- 'input'
176
- >
177
- > &
178
- ExtractTypeFromObjectOfSchemas<
179
- // eslint-disable-next-line ts/ban-types
180
- TFields extends undefined ? {} : TFields,
181
- 'input'
182
- >;
183
- output: TFields extends undefined
184
- ? TFieldsOnModel
185
- : Omit<
186
- TFieldsOnModel,
187
- keyof ExtractTypeFromObjectOfSchemas<
188
- // eslint-disable-next-line ts/ban-types
189
- TFields extends undefined ? {} : TFields,
190
- 'output'
191
- >
192
- > &
193
- ExtractTypeFromObjectOfSchemas<
194
- // eslint-disable-next-line ts/ban-types
195
- TFields extends undefined ? {} : TFields,
196
- 'output'
197
- >;
198
- internal: TFields extends undefined
199
- ? TFieldsOnModel
200
- : Omit<
201
- TFieldsOnModel,
202
- keyof ExtractTypeFromObjectOfSchemas<
203
- // eslint-disable-next-line ts/ban-types
204
- TFields extends undefined ? {} : TFields,
205
- 'internal'
206
- >
207
- > &
208
- ExtractTypeFromObjectOfSchemas<
209
- // eslint-disable-next-line ts/ban-types
210
- TFields extends undefined ? {} : TFields,
211
- 'internal'
212
- >;
213
- representation: TFields extends undefined
214
- ? TFieldsOnModel
215
- : Omit<
216
- TFieldsOnModel,
217
- keyof ExtractTypeFromObjectOfSchemas<
218
- // eslint-disable-next-line ts/ban-types
219
- TFields extends Record<any, Schema<any, DefinitionsOfSchemaType>> ? TFields : {},
220
- 'representation'
221
- >
222
- > &
223
- ExtractTypeFromObjectOfSchemas<
224
- // eslint-disable-next-line ts/ban-types
225
- TFields extends Record<any, Schema<any, DefinitionsOfSchemaType>> ? TFields : {},
226
- 'representation'
227
- >;
228
- validate: TFields extends undefined
229
- ? TFieldsOnModel
230
- : Omit<
231
- TFieldsOnModel,
232
- keyof ExtractTypeFromObjectOfSchemas<
233
- // eslint-disable-next-line ts/ban-types
234
- TFields extends Record<any, Schema<any, DefinitionsOfSchemaType>> ? TFields : {},
235
- 'validate'
236
- >
237
- > &
238
- ExtractTypeFromObjectOfSchemas<
239
- // eslint-disable-next-line ts/ban-types
240
- TFields extends Record<any, Schema<any, DefinitionsOfSchemaType>> ? TFields : {},
241
- 'validate'
242
- >;
243
- }
244
- >(
245
- model: TModel,
246
- options?: {
247
- ignoreExtraneousFields?: boolean;
248
- engineInstance?: string;
249
- fields?: TFields;
250
- omit?: TOmit;
251
- omitRelation?: readonly (keyof TFields)[];
252
- show?: TShow;
253
- many?: TMany;
254
- }
255
- ): TMany extends true
256
- ? ArraySchema<
257
- {
258
- input: TReturnType['input'][];
259
- output: TReturnType['output'][];
260
- internal: TReturnType['internal'][];
261
- representation: TReturnType['representation'][];
262
- validate: TReturnType['validate'][];
263
- },
264
- {
265
- schemaAdapter: TAdapter;
266
- schemaType: 'object';
267
- hasSave: false;
268
- },
269
- [
270
- ObjectSchema<
271
- {
272
- input: TReturnType['input'];
273
- output: TReturnType['output'];
274
- internal: TReturnType['internal'];
275
- representation: TReturnType['representation'];
276
- validate: TReturnType['validate'];
277
- },
278
- {
279
- schemaAdapter: TAdapter;
280
- schemaType: 'object';
281
- hasSave: false;
282
- },
283
- Record<any, any>
284
- >
285
- ]
286
- >
287
- : ObjectSchema<
288
- {
289
- input: TReturnType['input'];
290
- output: TReturnType['output'];
291
- internal: TReturnType['internal'];
292
- representation: TReturnType['representation'];
293
- validate: TReturnType['validate'];
294
- },
295
- {
296
- schemaAdapter: TAdapter;
297
- schemaType: 'object';
298
- hasSave: false;
299
- },
300
- Record<any, any>
301
- > =>
302
- modelSchema<
303
- TModel,
304
- TOmit,
305
- TShow,
306
- TMany,
307
- TFields,
308
- TAllModelFields,
309
- {
310
- schemaAdapter: TAdapter;
311
- schemaType: 'object';
312
- hasSave: false;
313
- },
314
- TFieldsOnModel,
315
- TReturnType
316
- >(model, options)
317
- };
318
- }
package/src/middleware.ts DELETED
@@ -1,52 +0,0 @@
1
- import { Response } from '@palmares/server';
2
-
3
- import type { Schema } from './schema/schema';
4
- import type { Infer } from './types';
5
- import type { Request } from '@palmares/server';
6
-
7
- /**
8
- * Validates the request body and returns a response automatically, don't need to do anything else.
9
- */
10
- export function schemaHandler<
11
- TInput extends Schema<any, any>,
12
- TOutput extends Schema<
13
- { input: Infer<TInput, 'output'>; internal: any; output: any; representation: any; validate: any },
14
- any
15
- > = TInput
16
- >(input: TInput, output?: TOutput) {
17
- return async (
18
- request: Request<
19
- any,
20
- {
21
- body: Infer<TInput, 'input'>;
22
- headers: unknown & {
23
- 'content-type': 'application/json';
24
- };
25
- }
26
- >
27
- ) => {
28
- const data = await request.json();
29
- const validatedData = await input.validate(data, { request });
30
- if (validatedData.isValid) {
31
- const savedData = (await validatedData.save()) as Infer<TOutput, 'representation'>;
32
- const status = request.method === 'POST' ? 201 : 200;
33
- if (output) return Response.json(await output.data(savedData), { status: status });
34
- return Response.json<
35
- Infer<TOutput, 'representation'>,
36
- {
37
- status: 200 | 201;
38
- headers: object & {
39
- 'content-type': 'application/json';
40
- };
41
- }
42
- >(savedData, {
43
- status,
44
- headers: {
45
- 'content-type': 'application/json'
46
- }
47
- });
48
- }
49
-
50
- return Response.json({ errors: validatedData.errors }, { status: 400 });
51
- };
52
- }