@palmares/schemas 0.1.21 → 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.
- package/CHANGELOG.md +9 -0
- package/package.json +10 -4
- package/.turbo/turbo-build$colon$watch.log +0 -24
- package/.turbo/turbo-build.log +0 -13
- package/.turbo/turbo-build:watch.log +0 -26
- package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
- package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
- package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
- package/__tests__/.drizzle/schema.ts +0 -35
- package/__tests__/drizzle.config.ts +0 -11
- package/__tests__/eslint.config.js +0 -10
- package/__tests__/manage.ts +0 -5
- package/__tests__/node_modules/.bin/drizzle-kit +0 -17
- package/__tests__/node_modules/.bin/node-gyp +0 -17
- package/__tests__/node_modules/.bin/tsc +0 -17
- package/__tests__/node_modules/.bin/tsserver +0 -17
- package/__tests__/node_modules/.bin/tsx +0 -17
- package/__tests__/package.json +0 -34
- package/__tests__/sqlite.db +0 -0
- package/__tests__/src/core/array.test.ts +0 -131
- package/__tests__/src/core/boolean.test.ts +0 -66
- package/__tests__/src/core/datetime.test.ts +0 -102
- package/__tests__/src/core/index.ts +0 -35
- package/__tests__/src/core/model.test.ts +0 -260
- package/__tests__/src/core/models.ts +0 -50
- package/__tests__/src/core/numbers.test.ts +0 -177
- package/__tests__/src/core/object.test.ts +0 -218
- package/__tests__/src/core/string.test.ts +0 -222
- package/__tests__/src/core/test.test.ts +0 -59
- package/__tests__/src/core/types.test.ts +0 -97
- package/__tests__/src/core/union.test.ts +0 -99
- package/__tests__/src/settings.ts +0 -69
- package/__tests__/tsconfig.json +0 -11
- package/src/adapter/fields/array.ts +0 -31
- package/src/adapter/fields/boolean.ts +0 -43
- package/src/adapter/fields/datetime.ts +0 -43
- package/src/adapter/fields/index.ts +0 -72
- package/src/adapter/fields/number.ts +0 -43
- package/src/adapter/fields/object.ts +0 -52
- package/src/adapter/fields/string.ts +0 -43
- package/src/adapter/fields/union.ts +0 -43
- package/src/adapter/index.ts +0 -37
- package/src/adapter/types.ts +0 -276
- package/src/compile.ts +0 -14
- package/src/conf.ts +0 -30
- package/src/constants.ts +0 -7
- package/src/domain.ts +0 -15
- package/src/exceptions.ts +0 -17
- package/src/index.ts +0 -318
- package/src/middleware.ts +0 -52
- package/src/model.ts +0 -518
- package/src/parsers/convert-from-number.ts +0 -13
- package/src/parsers/convert-from-string.ts +0 -19
- package/src/parsers/index.ts +0 -2
- package/src/schema/array.ts +0 -825
- package/src/schema/boolean.ts +0 -792
- package/src/schema/datetime.ts +0 -704
- package/src/schema/index.ts +0 -5
- package/src/schema/number.ts +0 -929
- package/src/schema/object.ts +0 -799
- package/src/schema/schema.ts +0 -1179
- package/src/schema/string.ts +0 -941
- package/src/schema/types.ts +0 -154
- package/src/schema/union.ts +0 -724
- package/src/types.ts +0 -66
- package/src/utils.ts +0 -389
- package/src/validators/array.ts +0 -183
- package/src/validators/boolean.ts +0 -52
- package/src/validators/datetime.ts +0 -121
- package/src/validators/number.ts +0 -178
- package/src/validators/object.ts +0 -56
- package/src/validators/schema.ts +0 -142
- package/src/validators/string.ts +0 -278
- package/src/validators/types.ts +0 -1
- package/src/validators/union.ts +0 -52
- package/src/validators/utils.ts +0 -226
- package/tsconfig.json +0 -9
- package/tsconfig.types.json +0 -10
package/src/adapter/types.ts
DELETED
@@ -1,276 +0,0 @@
|
|
1
|
-
import type { Schema } from '../schema/schema';
|
2
|
-
import type { SupportedSchemas } from '../types';
|
3
|
-
import type { withFallbackFactory } from '../utils';
|
4
|
-
|
5
|
-
export type NonToTranslateArgs<TType extends SupportedSchemas> = {
|
6
|
-
withFallback: ReturnType<typeof withFallbackFactory<TType>>;
|
7
|
-
};
|
8
|
-
|
9
|
-
export type AdapterToStringArgs = {
|
10
|
-
parsers: {
|
11
|
-
optional: Schema['__optional']['allow'];
|
12
|
-
nullable: Schema['__nullable']['allow'];
|
13
|
-
};
|
14
|
-
type: Schema['__type'];
|
15
|
-
optional: Schema['__optional'];
|
16
|
-
nullable: Schema['__nullable'];
|
17
|
-
};
|
18
|
-
|
19
|
-
export type AdapterTranslateArgs<TType extends SupportedSchemas = SupportedSchemas> = AdapterToStringArgs &
|
20
|
-
NonToTranslateArgs<TType>;
|
21
|
-
|
22
|
-
export type DatetimeAdapterTranslateArgs = {
|
23
|
-
allowString: boolean | undefined;
|
24
|
-
above:
|
25
|
-
| {
|
26
|
-
value: Date;
|
27
|
-
inclusive: boolean;
|
28
|
-
message: string;
|
29
|
-
}
|
30
|
-
| undefined;
|
31
|
-
below:
|
32
|
-
| {
|
33
|
-
value: Date;
|
34
|
-
inclusive: boolean;
|
35
|
-
message: string;
|
36
|
-
}
|
37
|
-
| undefined;
|
38
|
-
} & AdapterTranslateArgs<'datetime'>;
|
39
|
-
|
40
|
-
export type BooleanAdapterTranslateArgs = {
|
41
|
-
parsers: {
|
42
|
-
allowString: boolean | undefined;
|
43
|
-
allowNumber: boolean | undefined;
|
44
|
-
trueValues: any[] | undefined;
|
45
|
-
falseValues: any[] | undefined;
|
46
|
-
} & AdapterTranslateArgs<'boolean'>['parsers'];
|
47
|
-
is:
|
48
|
-
| {
|
49
|
-
value: boolean;
|
50
|
-
message: string;
|
51
|
-
}
|
52
|
-
| undefined;
|
53
|
-
} & AdapterTranslateArgs<'boolean'>;
|
54
|
-
|
55
|
-
export type ArrayAdapterTranslateArgs = {
|
56
|
-
schemas: any[];
|
57
|
-
minLength:
|
58
|
-
| {
|
59
|
-
value: number;
|
60
|
-
inclusive: boolean;
|
61
|
-
message: string;
|
62
|
-
}
|
63
|
-
| undefined;
|
64
|
-
maxLength:
|
65
|
-
| {
|
66
|
-
value: number;
|
67
|
-
inclusive: boolean;
|
68
|
-
message: string;
|
69
|
-
}
|
70
|
-
| undefined;
|
71
|
-
nonEmpty:
|
72
|
-
| {
|
73
|
-
message: string;
|
74
|
-
}
|
75
|
-
| undefined;
|
76
|
-
|
77
|
-
isTuple: boolean;
|
78
|
-
} & (
|
79
|
-
| {
|
80
|
-
isTuple: true;
|
81
|
-
schemas: [any, ...any[]];
|
82
|
-
}
|
83
|
-
| { isTuple: false; schemas: [any] }
|
84
|
-
) &
|
85
|
-
AdapterTranslateArgs<'array'>;
|
86
|
-
|
87
|
-
export type NumberAdapterTranslateArgs = {
|
88
|
-
parsers: {
|
89
|
-
allowString: boolean | undefined;
|
90
|
-
} & AdapterTranslateArgs<'number'>['parsers'];
|
91
|
-
is:
|
92
|
-
| {
|
93
|
-
value: number[];
|
94
|
-
message: string;
|
95
|
-
}
|
96
|
-
| undefined;
|
97
|
-
min:
|
98
|
-
| {
|
99
|
-
value: number;
|
100
|
-
inclusive: boolean;
|
101
|
-
message: string;
|
102
|
-
}
|
103
|
-
| undefined;
|
104
|
-
max:
|
105
|
-
| {
|
106
|
-
value: number;
|
107
|
-
inclusive: boolean;
|
108
|
-
message: string;
|
109
|
-
}
|
110
|
-
| undefined;
|
111
|
-
maxDigits:
|
112
|
-
| {
|
113
|
-
value: number;
|
114
|
-
message: string;
|
115
|
-
}
|
116
|
-
| undefined;
|
117
|
-
decimalPlaces:
|
118
|
-
| {
|
119
|
-
value: number;
|
120
|
-
message: string;
|
121
|
-
}
|
122
|
-
| undefined;
|
123
|
-
integer:
|
124
|
-
| {
|
125
|
-
message: string;
|
126
|
-
}
|
127
|
-
| undefined;
|
128
|
-
} & AdapterTranslateArgs<'number'>;
|
129
|
-
|
130
|
-
export type StringAdapterToStringArgs = Omit<StringAdapterTranslateArgs, keyof NonToTranslateArgs<'string'>>;
|
131
|
-
|
132
|
-
export type StringAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
133
|
-
StringAdapterTranslateArgs,
|
134
|
-
keyof NonToTranslateArgs<'string'>
|
135
|
-
>;
|
136
|
-
|
137
|
-
export type StringAdapterTranslateArgs = {
|
138
|
-
is:
|
139
|
-
| {
|
140
|
-
value: string[] | string;
|
141
|
-
message: string;
|
142
|
-
}
|
143
|
-
| undefined;
|
144
|
-
uuid:
|
145
|
-
| {
|
146
|
-
message: string;
|
147
|
-
}
|
148
|
-
| undefined;
|
149
|
-
email: { message: string } | undefined;
|
150
|
-
minLength:
|
151
|
-
| {
|
152
|
-
value: number;
|
153
|
-
message: string;
|
154
|
-
}
|
155
|
-
| undefined;
|
156
|
-
maxLength:
|
157
|
-
| {
|
158
|
-
value: number;
|
159
|
-
message: string;
|
160
|
-
}
|
161
|
-
| undefined;
|
162
|
-
regex:
|
163
|
-
| {
|
164
|
-
value: RegExp;
|
165
|
-
message: string;
|
166
|
-
}
|
167
|
-
| undefined;
|
168
|
-
endsWith:
|
169
|
-
| {
|
170
|
-
value: string;
|
171
|
-
message: string;
|
172
|
-
}
|
173
|
-
| undefined;
|
174
|
-
startsWith:
|
175
|
-
| {
|
176
|
-
value: string;
|
177
|
-
message: string;
|
178
|
-
}
|
179
|
-
| undefined;
|
180
|
-
includes:
|
181
|
-
| {
|
182
|
-
value: string;
|
183
|
-
message: string;
|
184
|
-
}
|
185
|
-
| undefined;
|
186
|
-
} & AdapterTranslateArgs<'string'>;
|
187
|
-
|
188
|
-
export type NumberAdapterToStringArgs = Omit<NumberAdapterTranslateArgs, keyof NonToTranslateArgs<'number'>>;
|
189
|
-
|
190
|
-
export type NumberAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
191
|
-
NumberAdapterTranslateArgs,
|
192
|
-
keyof NonToTranslateArgs<'number'>
|
193
|
-
>;
|
194
|
-
|
195
|
-
export type ObjectAdapterTranslateArgs = {
|
196
|
-
data: Record<string, any>;
|
197
|
-
} & AdapterTranslateArgs<'object'>;
|
198
|
-
|
199
|
-
export type ObjectAdapterToStringArgs = {
|
200
|
-
data: Record<string, string>;
|
201
|
-
} & AdapterToStringArgs;
|
202
|
-
|
203
|
-
export type UnionAdapterTranslateArgs<TSchemasType = any> = {
|
204
|
-
schemas: TSchemasType;
|
205
|
-
} & AdapterTranslateArgs<'union'>;
|
206
|
-
|
207
|
-
export type UnionAdapterToStringArgs = {
|
208
|
-
schemas: [string, string, ...string[]];
|
209
|
-
} & AdapterToStringArgs;
|
210
|
-
|
211
|
-
export type UnionAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
212
|
-
UnionAdapterTranslateArgs,
|
213
|
-
keyof NonToTranslateArgs<'union'>
|
214
|
-
>;
|
215
|
-
|
216
|
-
export type ObjectAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
217
|
-
ObjectAdapterTranslateArgs,
|
218
|
-
keyof NonToTranslateArgs<'object'>
|
219
|
-
>;
|
220
|
-
|
221
|
-
export type ArrayAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
222
|
-
ArrayAdapterTranslateArgs,
|
223
|
-
keyof NonToTranslateArgs<'array'>
|
224
|
-
>;
|
225
|
-
|
226
|
-
export type BooleanAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
227
|
-
BooleanAdapterTranslateArgs,
|
228
|
-
keyof NonToTranslateArgs<'boolean'>
|
229
|
-
>;
|
230
|
-
|
231
|
-
export type DatetimeAdapterTranslateArgsWithoutNonTranslateArgs = Omit<
|
232
|
-
DatetimeAdapterTranslateArgs,
|
233
|
-
keyof NonToTranslateArgs<'datetime'>
|
234
|
-
>;
|
235
|
-
export type ValidationDataBasedOnType<TType> = TType extends 'number'
|
236
|
-
? NumberAdapterTranslateArgsWithoutNonTranslateArgs
|
237
|
-
: TType extends 'union'
|
238
|
-
? UnionAdapterTranslateArgsWithoutNonTranslateArgs
|
239
|
-
: TType extends 'string'
|
240
|
-
? StringAdapterTranslateArgsWithoutNonTranslateArgs
|
241
|
-
: TType extends 'array'
|
242
|
-
? ArrayAdapterTranslateArgsWithoutNonTranslateArgs
|
243
|
-
: TType extends 'boolean'
|
244
|
-
? BooleanAdapterTranslateArgsWithoutNonTranslateArgs
|
245
|
-
: TType extends 'datetime'
|
246
|
-
? DatetimeAdapterTranslateArgsWithoutNonTranslateArgs
|
247
|
-
: ObjectAdapterTranslateArgsWithoutNonTranslateArgs;
|
248
|
-
|
249
|
-
export type ErrorCodes =
|
250
|
-
| 'max'
|
251
|
-
| 'maxDigits'
|
252
|
-
| 'decimalPlaces'
|
253
|
-
| 'min'
|
254
|
-
| 'integer'
|
255
|
-
| 'required'
|
256
|
-
| 'null'
|
257
|
-
| 'object'
|
258
|
-
| 'number'
|
259
|
-
| 'string'
|
260
|
-
| 'array'
|
261
|
-
| 'tuple'
|
262
|
-
| 'nonEmpty'
|
263
|
-
| 'datetime'
|
264
|
-
| 'is'
|
265
|
-
| 'above'
|
266
|
-
| 'below'
|
267
|
-
| 'boolean'
|
268
|
-
| 'minLength'
|
269
|
-
| 'maxLength'
|
270
|
-
| 'regex'
|
271
|
-
| 'uuid'
|
272
|
-
| 'email'
|
273
|
-
| 'includes'
|
274
|
-
| 'endsWith'
|
275
|
-
| 'startsWith'
|
276
|
-
| `customError${string}`;
|
package/src/compile.ts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
import { getDefaultStd } from '@palmares/core';
|
2
|
-
|
3
|
-
import { getDefaultAdapter } from './conf';
|
4
|
-
|
5
|
-
import type { Schema } from './schema/schema';
|
6
|
-
|
7
|
-
export async function compile(schemas: Record<string, Schema<any, any>>) {
|
8
|
-
const schemasAsEntries = Object.entries(schemas);
|
9
|
-
const adapter = getDefaultAdapter();
|
10
|
-
const std = getDefaultStd();
|
11
|
-
for (const [keyName, schema] of schemasAsEntries) {
|
12
|
-
console.log(await schema.compile(adapter));
|
13
|
-
}
|
14
|
-
}
|
package/src/conf.ts
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
import { NoAdapterFoundError } from './exceptions';
|
2
|
-
|
3
|
-
import type { SchemaAdapter } from './adapter';
|
4
|
-
|
5
|
-
declare global {
|
6
|
-
// eslint-disable-next-line no-var
|
7
|
-
var $PSchemasAdapter: SchemaAdapter | undefined;
|
8
|
-
}
|
9
|
-
|
10
|
-
/**
|
11
|
-
* Sets the default adapter to be used by all of your schemas.
|
12
|
-
*
|
13
|
-
* @param adapter - The adapter to use when you define schemas.
|
14
|
-
*/
|
15
|
-
export function setDefaultAdapter(adapter: SchemaAdapter) {
|
16
|
-
globalThis.$PSchemasAdapter = adapter;
|
17
|
-
}
|
18
|
-
|
19
|
-
/**
|
20
|
-
* Gets the default adapter to be used by all of your schemas.
|
21
|
-
*
|
22
|
-
* @throws {NoAdapterFoundError} - If no adapter has been set.
|
23
|
-
*
|
24
|
-
* @returns The default adapter.
|
25
|
-
*/
|
26
|
-
export function getDefaultAdapter(): SchemaAdapter {
|
27
|
-
if (!globalThis.$PSchemasAdapter) throw new NoAdapterFoundError();
|
28
|
-
|
29
|
-
return globalThis.$PSchemasAdapter;
|
30
|
-
}
|
package/src/constants.ts
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
export const DEFAULT_NUMBER_MAX_EXCEPTION = (max: number, inclusive: boolean) =>
|
2
|
-
`The number is greater than the allowed ${max}.${inclusive ? ` The value ${max} is accepted as well.` : ''}`;
|
3
|
-
export const DEFAULT_NUMBER_MIN_EXCEPTION = (min: number, inclusive: boolean) =>
|
4
|
-
`The number is less than the allowed ${min}.${inclusive ? ` The value ${min} is accepted as well.` : ''}`;
|
5
|
-
export const DEFAULT_NUMBER_POSITIVE_EXCEPTION = (allowZero: boolean) =>
|
6
|
-
`The number should be positive.${allowZero ? ` The value 0 is accepted as well.` : ''}`;
|
7
|
-
export const DEFAULT_NUMBER_INTEGER_EXCEPTION = () => `The number should be an integer.`;
|
package/src/domain.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import { domain } from '@palmares/core';
|
2
|
-
|
3
|
-
import { getDefaultAdapter, setDefaultAdapter } from './conf';
|
4
|
-
|
5
|
-
import type { SchemasSettingsType } from './types';
|
6
|
-
|
7
|
-
export const schemasDomain = domain('@palmares/schemas', '', {
|
8
|
-
commands: {},
|
9
|
-
// eslint-disable-next-line ts/require-await
|
10
|
-
load: async (settings: SchemasSettingsType) => {
|
11
|
-
setDefaultAdapter(new settings.schemaAdapter());
|
12
|
-
const schemaAdapter = getDefaultAdapter();
|
13
|
-
return undefined;
|
14
|
-
}
|
15
|
-
});
|
package/src/exceptions.ts
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
export class SchemaAdapterNotImplementedError extends Error {
|
2
|
-
constructor(args: { className: string; functionName: string }) {
|
3
|
-
super(`Schema adapter did not implement ${args.functionName} in ${args.className}`);
|
4
|
-
}
|
5
|
-
}
|
6
|
-
|
7
|
-
export class NoAdapterFoundError extends Error {
|
8
|
-
constructor() {
|
9
|
-
super('No adapter found, please define an adapter using setDefaultAdapter() before using any schema.');
|
10
|
-
}
|
11
|
-
}
|
12
|
-
|
13
|
-
export class TranslatableFieldNotImplementedError extends Error {
|
14
|
-
constructor(fieldName: string) {
|
15
|
-
super(`TranslatableField '${fieldName}' did not implement the 'schema' key on 'customAttributes'`);
|
16
|
-
}
|
17
|
-
}
|
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
|
-
}
|