@palmares/schemas 0.1.25 → 0.1.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.
- package/CHANGELOG.md +10 -0
- package/dist/src/adapter/fields/array.cjs +96 -0
- package/dist/src/adapter/fields/array.d.ts.map +1 -0
- package/dist/src/adapter/fields/array.js +70 -0
- package/dist/src/adapter/fields/boolean.cjs +102 -0
- package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
- package/dist/src/adapter/fields/boolean.js +76 -0
- package/dist/src/adapter/fields/datetime.cjs +102 -0
- package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
- package/dist/src/adapter/fields/datetime.js +76 -0
- package/dist/src/adapter/fields/index.cjs +87 -0
- package/dist/src/adapter/fields/index.d.ts.map +1 -0
- package/dist/src/adapter/fields/index.js +61 -0
- package/dist/src/adapter/fields/number.cjs +102 -0
- package/dist/src/adapter/fields/number.d.ts.map +1 -0
- package/dist/src/adapter/fields/number.js +76 -0
- package/dist/src/adapter/fields/object.cjs +108 -0
- package/dist/src/adapter/fields/object.d.ts.map +1 -0
- package/dist/src/adapter/fields/object.js +82 -0
- package/dist/src/adapter/fields/string.cjs +102 -0
- package/dist/src/adapter/fields/string.d.ts.map +1 -0
- package/dist/src/adapter/fields/string.js +76 -0
- package/dist/src/adapter/fields/union.cjs +102 -0
- package/dist/src/adapter/fields/union.d.ts.map +1 -0
- package/dist/src/adapter/fields/union.js +76 -0
- package/dist/src/adapter/index.cjs +65 -0
- package/dist/src/adapter/index.d.ts.map +1 -0
- package/dist/src/adapter/index.js +40 -0
- package/dist/src/adapter/types.cjs +18 -0
- package/dist/src/adapter/types.d.ts.map +1 -0
- package/dist/src/adapter/types.js +0 -0
- package/dist/src/compile.cjs +59 -0
- package/dist/src/compile.d.ts.map +1 -0
- package/dist/src/compile.js +36 -0
- package/dist/src/conf.cjs +53 -0
- package/dist/src/conf.d.ts.map +1 -0
- package/dist/src/conf.js +27 -0
- package/dist/src/constants.cjs +40 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +14 -0
- package/dist/src/domain.cjs +63 -0
- package/dist/src/domain.d.ts.map +1 -0
- package/dist/src/domain.js +40 -0
- package/dist/src/exceptions.cjs +58 -0
- package/dist/src/exceptions.d.ts.map +1 -0
- package/dist/src/exceptions.js +33 -0
- package/dist/src/index.cjs +6520 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6456 -0
- package/dist/src/middleware.cjs +58 -0
- package/dist/src/middleware.d.ts.map +1 -0
- package/dist/src/middleware.js +35 -0
- package/dist/src/model.cjs +6003 -0
- package/dist/src/model.d.ts.map +1 -0
- package/dist/src/model.js +5978 -0
- package/dist/src/parsers/convert-from-number.cjs +34 -0
- package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
- package/dist/src/parsers/convert-from-number.js +11 -0
- package/dist/src/parsers/convert-from-string.cjs +40 -0
- package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
- package/dist/src/parsers/convert-from-string.js +17 -0
- package/dist/src/parsers/index.cjs +50 -0
- package/dist/src/parsers/index.d.ts.map +1 -0
- package/dist/src/parsers/index.js +24 -0
- package/dist/src/schema/array.cjs +1863 -0
- package/dist/src/schema/array.d.ts.map +1 -0
- package/dist/src/schema/array.js +1837 -0
- package/dist/src/schema/boolean.cjs +1800 -0
- package/dist/src/schema/boolean.d.ts.map +1 -0
- package/dist/src/schema/boolean.js +1774 -0
- package/dist/src/schema/datetime.cjs +1789 -0
- package/dist/src/schema/datetime.d.ts.map +1 -0
- package/dist/src/schema/datetime.js +1763 -0
- package/dist/src/schema/index.cjs +2026 -0
- package/dist/src/schema/index.d.ts.map +1 -0
- package/dist/src/schema/index.js +2001 -0
- package/dist/src/schema/number.cjs +2026 -0
- package/dist/src/schema/number.d.ts.map +1 -0
- package/dist/src/schema/number.js +2000 -0
- package/dist/src/schema/object.cjs +1795 -0
- package/dist/src/schema/object.d.ts.map +1 -0
- package/dist/src/schema/object.js +1769 -0
- package/dist/src/schema/schema.cjs +844 -0
- package/dist/src/schema/schema.d.ts.map +1 -0
- package/dist/src/schema/schema.js +818 -0
- package/dist/src/schema/string.cjs +2126 -0
- package/dist/src/schema/string.d.ts.map +1 -0
- package/dist/src/schema/string.js +2100 -0
- package/dist/src/schema/types.cjs +18 -0
- package/dist/src/schema/types.d.ts.map +1 -0
- package/dist/src/schema/types.js +0 -0
- package/dist/src/schema/union.cjs +1737 -0
- package/dist/src/schema/union.d.ts.map +1 -0
- package/dist/src/schema/union.js +1711 -0
- package/dist/src/types.cjs +18 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +0 -0
- package/dist/src/utils.cjs +477 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +445 -0
- package/dist/src/validators/array.cjs +176 -0
- package/dist/src/validators/array.d.ts.map +1 -0
- package/dist/src/validators/array.js +150 -0
- package/dist/src/validators/boolean.cjs +71 -0
- package/dist/src/validators/boolean.d.ts.map +1 -0
- package/dist/src/validators/boolean.js +47 -0
- package/dist/src/validators/datetime.cjs +129 -0
- package/dist/src/validators/datetime.d.ts.map +1 -0
- package/dist/src/validators/datetime.js +103 -0
- package/dist/src/validators/number.cjs +194 -0
- package/dist/src/validators/number.d.ts.map +1 -0
- package/dist/src/validators/number.js +166 -0
- package/dist/src/validators/object.cjs +69 -0
- package/dist/src/validators/object.d.ts.map +1 -0
- package/dist/src/validators/object.js +46 -0
- package/dist/src/validators/schema.cjs +158 -0
- package/dist/src/validators/schema.d.ts.map +1 -0
- package/dist/src/validators/schema.js +132 -0
- package/dist/src/validators/string.cjs +255 -0
- package/dist/src/validators/string.d.ts.map +1 -0
- package/dist/src/validators/string.js +224 -0
- package/dist/src/validators/types.cjs +18 -0
- package/dist/src/validators/types.d.ts.map +1 -0
- package/dist/src/validators/types.js +0 -0
- package/dist/src/validators/union.cjs +69 -0
- package/dist/src/validators/union.d.ts.map +1 -0
- package/dist/src/validators/union.js +46 -0
- package/dist/src/validators/utils.cjs +165 -0
- package/dist/src/validators/utils.d.ts.map +1 -0
- package/dist/src/validators/utils.js +142 -0
- package/dist/tsconfig.types.tsbuildinfo +1 -0
- package/package.json +15 -23
- package/dist/cjs/src/adapter/fields/array.js +0 -152
- package/dist/cjs/src/adapter/fields/boolean.js +0 -162
- package/dist/cjs/src/adapter/fields/datetime.js +0 -162
- package/dist/cjs/src/adapter/fields/index.js +0 -311
- package/dist/cjs/src/adapter/fields/number.js +0 -162
- package/dist/cjs/src/adapter/fields/object.js +0 -171
- package/dist/cjs/src/adapter/fields/string.js +0 -162
- package/dist/cjs/src/adapter/fields/union.js +0 -162
- package/dist/cjs/src/adapter/index.js +0 -201
- package/dist/cjs/src/adapter/types.js +0 -4
- package/dist/cjs/src/compile.js +0 -262
- package/dist/cjs/src/conf.js +0 -26
- package/dist/cjs/src/constants.js +0 -36
- package/dist/cjs/src/domain.js +0 -156
- package/dist/cjs/src/exceptions.js +0 -168
- package/dist/cjs/src/index.js +0 -317
- package/dist/cjs/src/middleware.js +0 -213
- package/dist/cjs/src/model.js +0 -628
- package/dist/cjs/src/parsers/convert-from-number.js +0 -15
- package/dist/cjs/src/parsers/convert-from-string.js +0 -19
- package/dist/cjs/src/parsers/index.js +0 -20
- package/dist/cjs/src/schema/array.js +0 -1014
- package/dist/cjs/src/schema/boolean.js +0 -883
- package/dist/cjs/src/schema/datetime.js +0 -840
- package/dist/cjs/src/schema/index.js +0 -12
- package/dist/cjs/src/schema/number.js +0 -976
- package/dist/cjs/src/schema/object.js +0 -1052
- package/dist/cjs/src/schema/schema.js +0 -1840
- package/dist/cjs/src/schema/string.js +0 -1038
- package/dist/cjs/src/schema/types.js +0 -4
- package/dist/cjs/src/schema/union.js +0 -1070
- package/dist/cjs/src/types.js +0 -4
- package/dist/cjs/src/utils.js +0 -647
- package/dist/cjs/src/validators/array.js +0 -462
- package/dist/cjs/src/validators/boolean.js +0 -201
- package/dist/cjs/src/validators/datetime.js +0 -291
- package/dist/cjs/src/validators/number.js +0 -375
- package/dist/cjs/src/validators/object.js +0 -291
- package/dist/cjs/src/validators/schema.js +0 -322
- package/dist/cjs/src/validators/string.js +0 -467
- package/dist/cjs/src/validators/types.js +0 -4
- package/dist/cjs/src/validators/union.js +0 -233
- package/dist/cjs/src/validators/utils.js +0 -499
- package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
- package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
- package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
- package/dist/cjs/types/adapter/index.d.ts.map +0 -1
- package/dist/cjs/types/adapter/types.d.ts.map +0 -1
- package/dist/cjs/types/compile.d.ts.map +0 -1
- package/dist/cjs/types/conf.d.ts.map +0 -1
- package/dist/cjs/types/constants.d.ts.map +0 -1
- package/dist/cjs/types/domain.d.ts.map +0 -1
- package/dist/cjs/types/exceptions.d.ts.map +0 -1
- package/dist/cjs/types/index.d.ts.map +0 -1
- package/dist/cjs/types/middleware.d.ts.map +0 -1
- package/dist/cjs/types/model.d.ts.map +0 -1
- package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
- package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
- package/dist/cjs/types/parsers/index.d.ts.map +0 -1
- package/dist/cjs/types/schema/array.d.ts.map +0 -1
- package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
- package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
- package/dist/cjs/types/schema/index.d.ts.map +0 -1
- package/dist/cjs/types/schema/number.d.ts.map +0 -1
- package/dist/cjs/types/schema/object.d.ts.map +0 -1
- package/dist/cjs/types/schema/schema.d.ts.map +0 -1
- package/dist/cjs/types/schema/string.d.ts.map +0 -1
- package/dist/cjs/types/schema/types.d.ts.map +0 -1
- package/dist/cjs/types/schema/union.d.ts.map +0 -1
- package/dist/cjs/types/types.d.ts.map +0 -1
- package/dist/cjs/types/utils.d.ts.map +0 -1
- package/dist/cjs/types/validators/array.d.ts.map +0 -1
- package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
- package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
- package/dist/cjs/types/validators/number.d.ts.map +0 -1
- package/dist/cjs/types/validators/object.d.ts.map +0 -1
- package/dist/cjs/types/validators/schema.d.ts.map +0 -1
- package/dist/cjs/types/validators/string.d.ts.map +0 -1
- package/dist/cjs/types/validators/types.d.ts.map +0 -1
- package/dist/cjs/types/validators/union.d.ts.map +0 -1
- package/dist/cjs/types/validators/utils.d.ts.map +0 -1
- package/dist/esm/src/adapter/fields/array.js +0 -13
- package/dist/esm/src/adapter/fields/boolean.js +0 -20
- package/dist/esm/src/adapter/fields/datetime.js +0 -20
- package/dist/esm/src/adapter/fields/index.js +0 -37
- package/dist/esm/src/adapter/fields/number.js +0 -20
- package/dist/esm/src/adapter/fields/object.js +0 -26
- package/dist/esm/src/adapter/fields/string.js +0 -20
- package/dist/esm/src/adapter/fields/union.js +0 -20
- package/dist/esm/src/adapter/index.js +0 -21
- package/dist/esm/src/adapter/types.js +0 -1
- package/dist/esm/src/compile.js +0 -10
- package/dist/esm/src/conf.js +0 -18
- package/dist/esm/src/constants.js +0 -4
- package/dist/esm/src/domain.js +0 -11
- package/dist/esm/src/exceptions.js +0 -15
- package/dist/esm/src/index.js +0 -126
- package/dist/esm/src/middleware.js +0 -29
- package/dist/esm/src/model.js +0 -264
- package/dist/esm/src/parsers/convert-from-number.js +0 -8
- package/dist/esm/src/parsers/convert-from-string.js +0 -14
- package/dist/esm/src/parsers/index.js +0 -2
- package/dist/esm/src/schema/array.js +0 -487
- package/dist/esm/src/schema/boolean.js +0 -520
- package/dist/esm/src/schema/datetime.js +0 -477
- package/dist/esm/src/schema/index.js +0 -3
- package/dist/esm/src/schema/number.js +0 -610
- package/dist/esm/src/schema/object.js +0 -521
- package/dist/esm/src/schema/schema.js +0 -767
- package/dist/esm/src/schema/string.js +0 -668
- package/dist/esm/src/schema/types.js +0 -1
- package/dist/esm/src/schema/union.js +0 -468
- package/dist/esm/src/types.js +0 -14
- package/dist/esm/src/utils.js +0 -207
- package/dist/esm/src/validators/array.js +0 -140
- package/dist/esm/src/validators/boolean.js +0 -37
- package/dist/esm/src/validators/datetime.js +0 -89
- package/dist/esm/src/validators/number.js +0 -148
- package/dist/esm/src/validators/object.js +0 -39
- package/dist/esm/src/validators/schema.js +0 -118
- package/dist/esm/src/validators/string.js +0 -202
- package/dist/esm/src/validators/types.js +0 -1
- package/dist/esm/src/validators/union.js +0 -39
- package/dist/esm/src/validators/utils.js +0 -147
- /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/compile.d.ts +0 -0
- /package/dist/{cjs/types → src}/conf.d.ts +0 -0
- /package/dist/{cjs/types → src}/constants.d.ts +0 -0
- /package/dist/{cjs/types → src}/domain.d.ts +0 -0
- /package/dist/{cjs/types → src}/exceptions.d.ts +0 -0
- /package/dist/{cjs/types → src}/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
- /package/dist/{cjs/types → src}/model.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
- /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/utils.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
- /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
package/dist/esm/src/model.js
DELETED
@@ -1,264 +0,0 @@
|
|
1
|
-
import { TranslatableFieldNotImplementedError } from './exceptions';
|
2
|
-
import { number } from './schema';
|
3
|
-
import { ArraySchema } from './schema/array';
|
4
|
-
import { boolean } from './schema/boolean';
|
5
|
-
import { datetime } from './schema/datetime';
|
6
|
-
import { ObjectSchema } from './schema/object';
|
7
|
-
import { string } from './schema/string';
|
8
|
-
import { union } from './schema/union';
|
9
|
-
async function getSchemaFromModelField(model, field, parent, definedFields, engineInstanceName, options) {
|
10
|
-
const fieldAsAny = field;
|
11
|
-
let schema = undefined;
|
12
|
-
if (fieldAsAny?.['$$type'] === '$PAutoField' || fieldAsAny?.['$$type'] === '$PBigAutoField') schema = number().integer().optional();
|
13
|
-
else if (fieldAsAny?.['$$type'] === '$PDecimalField') schema = number().decimalPlaces(field.decimalPlaces).maxDigits(field.maxDigits);
|
14
|
-
else if (fieldAsAny?.['$$type'] === '$PIntegerField') schema = number().integer();
|
15
|
-
else if (fieldAsAny?.['$$type'] === '$PBooleanField') schema = boolean();
|
16
|
-
else if (fieldAsAny?.['$$type'] === '$PTextField' || fieldAsAny?.['$$type'] === '$PCharField' || fieldAsAny?.['$$type'] === '$PUuidField') {
|
17
|
-
schema = string();
|
18
|
-
if (field.allowBlank === false) schema = schema.minLength(1);
|
19
|
-
if (fieldAsAny?.['$$type'] === '$PCharField' && typeof field.maxLength === 'number') schema = schema.maxLength(field.maxLength);
|
20
|
-
if (fieldAsAny?.['$$type'] === '$PUuidField') {
|
21
|
-
schema = schema.uuid();
|
22
|
-
// eslint-disable-next-line ts/no-unnecessary-condition
|
23
|
-
if (field.autoGenerate) schema = schema.optional();
|
24
|
-
}
|
25
|
-
} else if (fieldAsAny?.['$$type'] === '$PDateField') {
|
26
|
-
schema = datetime().allowString();
|
27
|
-
// eslint-disable-next-line ts/no-unnecessary-condition
|
28
|
-
if (field.autoNow || field.autoNowAdd) schema = schema.optional();
|
29
|
-
} else if (fieldAsAny?.['$$type'] === '$PEnumField') {
|
30
|
-
const allChoicesOfTypeStrings = field.choices.filter((choice)=>typeof choice === 'string');
|
31
|
-
const allChoicesOfTypeNumbers = field.choices.filter((choice)=>typeof choice === 'number');
|
32
|
-
let schemaForChoicesAsStrings = undefined;
|
33
|
-
let schemaForChoicesAsNumbers = undefined;
|
34
|
-
if (allChoicesOfTypeStrings.length > 0) schemaForChoicesAsStrings = string().is([
|
35
|
-
...allChoicesOfTypeStrings
|
36
|
-
]);
|
37
|
-
if (allChoicesOfTypeNumbers.length > 0) schemaForChoicesAsNumbers = number().is([
|
38
|
-
...allChoicesOfTypeNumbers
|
39
|
-
]);
|
40
|
-
if (schemaForChoicesAsStrings && schemaForChoicesAsNumbers) schema = union([
|
41
|
-
schemaForChoicesAsStrings,
|
42
|
-
schemaForChoicesAsNumbers
|
43
|
-
]);
|
44
|
-
else if (schemaForChoicesAsStrings) schema = schemaForChoicesAsStrings;
|
45
|
-
else if (schemaForChoicesAsNumbers) schema = schemaForChoicesAsNumbers;
|
46
|
-
} else if (fieldAsAny?.['$$type'] === '$PForeignKeyField') {
|
47
|
-
const fieldAsForeignKey = field;
|
48
|
-
const doesADefinedFieldExistWithRelatedName = parent && fieldAsForeignKey.relatedName && parent.__data?.[fieldAsForeignKey.relatedName];
|
49
|
-
const doesADefinedFieldExistWithRelationName = definedFields && fieldAsForeignKey.relationName && definedFields[fieldAsForeignKey.relationName];
|
50
|
-
const fieldWithRelatedName = doesADefinedFieldExistWithRelatedName ? parent.__data?.[fieldAsForeignKey.relatedName] : undefined;
|
51
|
-
const fieldWithRelationName = doesADefinedFieldExistWithRelationName ? definedFields[fieldAsForeignKey.relationName] : undefined;
|
52
|
-
const isFieldWithRelatedNameAModelField = fieldWithRelatedName?.['$$type'] === '$PSchema' && fieldWithRelatedName.__model !== undefined;
|
53
|
-
const isFieldWithRelationNameAModelField = fieldWithRelationName?.['$$type'] === '$PSchema' && fieldWithRelationName.__model !== undefined;
|
54
|
-
const relatedToModel = fieldAsForeignKey.relatedTo;
|
55
|
-
const toField = fieldAsForeignKey.toField;
|
56
|
-
if (Object.keys(model._initialized).length > 0) {
|
57
|
-
const engineInstance = await model.default.getEngineInstance(engineInstanceName);
|
58
|
-
const relatedToModelInstance = engineInstance.__modelsOfEngine[relatedToModel];
|
59
|
-
const modelFieldsOfRelatedModel = relatedToModelInstance.__cachedFields[toField];
|
60
|
-
if (isFieldWithRelatedNameAModelField) {
|
61
|
-
if (typeof options !== 'object') options = {};
|
62
|
-
options.foreignKeyRelation = {
|
63
|
-
schema: parent,
|
64
|
-
isArray: fieldWithRelatedName['$$type'] === '$PArraySchema',
|
65
|
-
model: fieldWithRelatedName.__model,
|
66
|
-
fieldToSearchOnModel: fieldAsForeignKey.fieldName,
|
67
|
-
fieldToGetFromData: fieldAsForeignKey.toField,
|
68
|
-
relationOrRelatedName: fieldAsForeignKey.relatedName
|
69
|
-
};
|
70
|
-
} else if (isFieldWithRelationNameAModelField) {
|
71
|
-
if (typeof options !== 'object') options = {};
|
72
|
-
options.foreignKeyRelation = {
|
73
|
-
isArray: fieldWithRelationName['$$type'] === '$PArraySchema',
|
74
|
-
model: fieldWithRelationName.__model,
|
75
|
-
fieldToSearchOnModel: fieldAsForeignKey.toField,
|
76
|
-
fieldToGetFromData: field.fieldName,
|
77
|
-
relationOrRelatedName: fieldAsForeignKey.relationName
|
78
|
-
};
|
79
|
-
}
|
80
|
-
return getSchemaFromModelField(relatedToModelInstance, modelFieldsOfRelatedModel, parent, definedFields, engineInstanceName, options);
|
81
|
-
}
|
82
|
-
} else if (fieldAsAny?.['$$type'] === '$PTranslatableField' && field.customAttributes.schema) {
|
83
|
-
if (field.customAttributes.schema?.['$$type'] === '$PSchema' === false) throw new TranslatableFieldNotImplementedError(field.fieldName);
|
84
|
-
schema = field.customAttributes.schema;
|
85
|
-
}
|
86
|
-
if (field.allowNull && schema) schema = schema.nullable().optional();
|
87
|
-
if (field.defaultValue && schema) schema = schema.default(field.defaultValue);
|
88
|
-
return schema || string();
|
89
|
-
}
|
90
|
-
/**
|
91
|
-
* Different from other schemas, this function is a factory function that returns either an ObjectSchema or an
|
92
|
-
* ArraySchema. The idea is to build the schema of a model dynamically based on its fields.
|
93
|
-
*
|
94
|
-
* Another feature is that it can automatically add the foreign key relation to the schema, but for that you need to
|
95
|
-
* define the fields of the related model in the fields object.
|
96
|
-
*
|
97
|
-
* For example: A User model have a field `companyId` that is a ForeignKeyField to the Company model. The `relationName`
|
98
|
-
* is the direct relation from the User model to the Company model, and the `relatedName` is the reverse relation from
|
99
|
-
* the Company model to the User model. If you define the fieldName as either the relatedName or the relationName it
|
100
|
-
* will fetch the data automatically.
|
101
|
-
*
|
102
|
-
* **Important**: We build the schema dynamically but also lazily, if you don't try to parse or validate the schema, it
|
103
|
-
* won't be built. After the first time it's built, it's cached and never built again.
|
104
|
-
*
|
105
|
-
* **Important 2**: If you want to use the automatic relation feature, you need to define guarantee that the foreignKey
|
106
|
-
* field fieldName exists on `show` array, or that it doesn't exist on `omit` array.
|
107
|
-
*
|
108
|
-
* Like: `{ options: { show: ['id', 'name', 'companyId'] }}` or `{ options: { omit: ['id'] }}` it **will work**.
|
109
|
-
*
|
110
|
-
* If you do `{ options: { show: ['id', 'name'] }}` or `{ options: { omit: ['companyId']} }` it **won't work**.
|
111
|
-
*
|
112
|
-
* **Important 3**: If you want to return an array instead of an object, you need to pass the `many` option as true.
|
113
|
-
*
|
114
|
-
* @example
|
115
|
-
* ```typescript
|
116
|
-
* import { auto, choice, foreignKey, Model, define } from '@palmares/databases';
|
117
|
-
* import * as p from '@palmares/schemas';
|
118
|
-
*
|
119
|
-
* const Company = define('Company', {
|
120
|
-
* fields: {
|
121
|
-
* id: auto(),
|
122
|
-
* name: text(),
|
123
|
-
* },
|
124
|
-
* options: {
|
125
|
-
* tableName: 'company',
|
126
|
-
* }
|
127
|
-
* });
|
128
|
-
*
|
129
|
-
* class User extends Model<User>() {
|
130
|
-
* fields = {
|
131
|
-
* id: auto(),
|
132
|
-
* type: choice({ choices: ['user', 'admin'] }),
|
133
|
-
* companyId: foreignKey({
|
134
|
-
* relatedTo: Company,
|
135
|
-
* relationName: 'company',
|
136
|
-
* relatedName: 'usersOfCompany',
|
137
|
-
* toField: 'id',
|
138
|
-
* onDelete: 'CASCADE',
|
139
|
-
* }),
|
140
|
-
* }
|
141
|
-
*
|
142
|
-
* options = {
|
143
|
-
* tableName: 'user',
|
144
|
-
* }
|
145
|
-
* }
|
146
|
-
*
|
147
|
-
* const userSchema = p.modelSchema(User, {
|
148
|
-
* fields: {
|
149
|
-
* company: p.modelSchema(Company).optional({ outputOnly: true });
|
150
|
-
* },
|
151
|
-
* show: ['type', 'companyId'], // 'companyId' is required for the automatic relation to work, otherwise it won't show
|
152
|
-
* omitRelation: ['company']
|
153
|
-
* });
|
154
|
-
*
|
155
|
-
* const companySchema = p.modelSchema(Company, {
|
156
|
-
* fields: {
|
157
|
-
* usersOfCompany: p.modelSchema(User, { many: true }).optional({ outputOnly: true });
|
158
|
-
* },
|
159
|
-
* // The `companyId` field on the 'User' model is tied to the `id` field on the 'Company' model so 'id' is required.
|
160
|
-
* show: ['id', 'type'] * });
|
161
|
-
*```
|
162
|
-
* @param model - The model that you want to build the schema from.
|
163
|
-
* @param options - The options to build the schema.
|
164
|
-
* @param options.ignoreExtraneousFields - If you want to ignore extraneous fields set this to true.
|
165
|
-
* @param options.engineInstance - What engine instance you want to use to fetch the data. Defaults to the first one.
|
166
|
-
* @param options.omitRelation - Fields that you want to omit from the relation. For example, on the example above, on
|
167
|
-
* the `userSchema` you can omit the `companyId` field from the relation by just passing `['company']`, on the
|
168
|
-
* `companySchema` you can omit the `id` field from company by passing `['usersOfCompany']`.
|
169
|
-
*
|
170
|
-
* @param options.fields - Extra fields that you want to add to the schema. If it has the same name as the model field,
|
171
|
-
* We will not create a schema for that field and use the one you have defined here.
|
172
|
-
* @param options.omit - Fields that you want to omit from the schema. If that is defined, we ignore `show` option.
|
173
|
-
* @param options.show - Fields that you want to show on the schema. If that is defined, we ignore `omit` option.
|
174
|
-
* @param options.many - If you want to return an array instead of an object, set this to true. With that we create
|
175
|
-
* an ArraySchema instead of an ObjectSchema.
|
176
|
-
*
|
177
|
-
* @returns - If you pass the `many` option as true, we return an ArraySchema, otherwise we return an ObjectSchema.
|
178
|
-
*/ export function modelSchema(model, options) {
|
179
|
-
const lazyModelSchema = ObjectSchema.new({});
|
180
|
-
const parentSchema = options?.many === true ? ArraySchema.new([
|
181
|
-
lazyModelSchema
|
182
|
-
]) : lazyModelSchema;
|
183
|
-
const omitRelationAsSet = new Set(options?.omitRelation || []);
|
184
|
-
const omitAsSet = new Set(options?.omit || []);
|
185
|
-
const showAsSet = new Set(options?.show || []);
|
186
|
-
const fieldsAsObject = options?.fields || {};
|
187
|
-
const customFieldValues = Object.values(fieldsAsObject);
|
188
|
-
// We need to add it to the instance to be able to access it on the `toRepresentation` callback
|
189
|
-
lazyModelSchema.__omitRelation = omitRelationAsSet;
|
190
|
-
parentSchema.__model = model;
|
191
|
-
lazyModelSchema.__model = model;
|
192
|
-
// Add this callback to transform the model fields
|
193
|
-
parentSchema.__runBeforeParseAndData = async ()=>{
|
194
|
-
const promise = new Promise((resolve)=>{
|
195
|
-
const fieldsOfModels = model._fields();
|
196
|
-
const fieldsAsEntries = Object.entries(fieldsOfModels);
|
197
|
-
const fieldsWithAutomaticRelations = new Map();
|
198
|
-
const fields = fieldsAsObject;
|
199
|
-
Promise.all(fieldsAsEntries.map(async ([key, value])=>{
|
200
|
-
if (omitAsSet.has(key)) return;
|
201
|
-
if (showAsSet.size > 0 && !showAsSet.has(key)) return;
|
202
|
-
let schema = fieldsAsObject[key];
|
203
|
-
const optionsForForeignKeyRelation = {};
|
204
|
-
if (!schema || value?.['$$type'] === '$PForeignKeyField') {
|
205
|
-
const newSchema = await getSchemaFromModelField(model, value, parentSchema?.__getParent?.(), options?.fields, options?.engineInstance, optionsForForeignKeyRelation);
|
206
|
-
if (!schema) schema = newSchema;
|
207
|
-
}
|
208
|
-
// Appends the foreign key relation to the schema automatically.
|
209
|
-
if (optionsForForeignKeyRelation.foreignKeyRelation) {
|
210
|
-
const rootSchema = optionsForForeignKeyRelation?.foreignKeyRelation?.schema || lazyModelSchema;
|
211
|
-
const existingRelations = fieldsWithAutomaticRelations.get(rootSchema) || [];
|
212
|
-
existingRelations.push({
|
213
|
-
relationOrRelatedName: optionsForForeignKeyRelation.foreignKeyRelation.relationOrRelatedName,
|
214
|
-
isArray: optionsForForeignKeyRelation.foreignKeyRelation.isArray,
|
215
|
-
model: optionsForForeignKeyRelation.foreignKeyRelation.model,
|
216
|
-
fieldToSearchOnModel: optionsForForeignKeyRelation.foreignKeyRelation.fieldToSearchOnModel,
|
217
|
-
fieldToGetFromData: optionsForForeignKeyRelation.foreignKeyRelation.fieldToGetFromData
|
218
|
-
});
|
219
|
-
fieldsWithAutomaticRelations.set(rootSchema, existingRelations);
|
220
|
-
}
|
221
|
-
fieldsAsObject[key] = schema;
|
222
|
-
return fieldsAsObject;
|
223
|
-
})).then(async ()=>{
|
224
|
-
if (fieldsWithAutomaticRelations.size > 0) {
|
225
|
-
// This way we can get all of the relations concurrently with Promise.all
|
226
|
-
for (const [schema, relations] of fieldsWithAutomaticRelations.entries()){
|
227
|
-
schema.toRepresentation(async (data)=>{
|
228
|
-
const allData = Array.isArray(data) ? data : [
|
229
|
-
data
|
230
|
-
];
|
231
|
-
// since we are changing the data by reference, just return the data itself.
|
232
|
-
await Promise.all(allData.map(async (data)=>Promise.all(relations.map(async (relation)=>{
|
233
|
-
// Ignore if the data of the relation already exists
|
234
|
-
if (relation.relationOrRelatedName in data) return;
|
235
|
-
let relationData = await relation.model.default.get({
|
236
|
-
search: {
|
237
|
-
[relation.fieldToSearchOnModel]: data[relation.fieldToGetFromData]
|
238
|
-
}
|
239
|
-
});
|
240
|
-
if (relation.isArray !== true) relationData = relationData[0];
|
241
|
-
data[relation.relationOrRelatedName] = relationData;
|
242
|
-
if (schema.__omitRelation.has(relation.relationOrRelatedName)) delete data[relation.fieldToGetFromData];
|
243
|
-
}))));
|
244
|
-
return data;
|
245
|
-
}, {
|
246
|
-
after: true
|
247
|
-
});
|
248
|
-
}
|
249
|
-
}
|
250
|
-
lazyModelSchema.__data = fields;
|
251
|
-
await Promise.all(customFieldValues.map(async (schema)=>{
|
252
|
-
schema['__getParent'] = ()=>lazyModelSchema;
|
253
|
-
if (schema['__runBeforeParseAndData']) await schema['__runBeforeParseAndData'](schema);
|
254
|
-
}));
|
255
|
-
resolve(undefined);
|
256
|
-
});
|
257
|
-
});
|
258
|
-
if (parentSchema.__alreadyAppliedModel) return parentSchema.__alreadyAppliedModel;
|
259
|
-
parentSchema.__alreadyAppliedModel = promise;
|
260
|
-
return promise;
|
261
|
-
};
|
262
|
-
if (options?.ignoreExtraneousFields !== true) lazyModelSchema.removeExtraneous();
|
263
|
-
return parentSchema;
|
264
|
-
}
|
@@ -1,14 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* This will convert a value from a string to any other type.
|
3
|
-
*
|
4
|
-
* @param callback
|
5
|
-
* @returns
|
6
|
-
*/ export function convertFromStringBuilder(callback) {
|
7
|
-
return (value)=>{
|
8
|
-
if (typeof value === 'string') return callback(value);
|
9
|
-
return {
|
10
|
-
value,
|
11
|
-
preventNextParsers: false
|
12
|
-
};
|
13
|
-
};
|
14
|
-
}
|