@palmares/schemas 0.0.1 → 0.1.0
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/.turbo/turbo-build$colon$watch.log +12 -410
- package/CHANGELOG.md +17 -0
- package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +22 -0
- package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +156 -0
- package/__tests__/.drizzle/migrations/meta/_journal.json +13 -0
- package/__tests__/.drizzle/schema.ts +35 -0
- package/__tests__/drizzle.config.ts +11 -0
- package/__tests__/eslint.config.js +10 -0
- package/__tests__/manage.ts +5 -0
- package/__tests__/node_modules/.bin/drizzle-kit +17 -0
- package/__tests__/node_modules/.bin/esbuild +14 -0
- package/__tests__/node_modules/.bin/tsc +17 -0
- package/__tests__/node_modules/.bin/tsserver +17 -0
- package/__tests__/node_modules/.bin/tsx +17 -0
- package/__tests__/package.json +36 -0
- package/__tests__/sqlite.db +0 -0
- package/__tests__/src/core/array.test.ts +130 -0
- package/__tests__/src/core/boolean.test.ts +66 -0
- package/__tests__/src/core/datetime.test.ts +102 -0
- package/__tests__/src/core/index.ts +35 -0
- package/__tests__/src/core/model.test.ts +260 -0
- package/__tests__/src/core/models.ts +50 -0
- package/__tests__/src/core/numbers.test.ts +177 -0
- package/__tests__/src/core/object.test.ts +198 -0
- package/__tests__/src/core/string.test.ts +222 -0
- package/__tests__/src/core/test.test.ts +59 -0
- package/__tests__/src/core/types.test.ts +97 -0
- package/__tests__/src/core/union.test.ts +99 -0
- package/__tests__/src/settings.ts +71 -0
- package/__tests__/tsconfig.json +11 -0
- package/dist/cjs/src/adapter/fields/index.js +2 -2
- package/dist/cjs/src/adapter/fields/object.js +9 -0
- package/dist/cjs/src/adapter/index.js +1 -0
- package/dist/cjs/src/constants.js +1 -7
- package/dist/cjs/src/domain.js +146 -1
- package/dist/cjs/src/index.js +69 -74
- package/dist/cjs/src/model.js +206 -206
- package/dist/cjs/src/schema/array.js +185 -58
- package/dist/cjs/src/schema/boolean.js +105 -44
- package/dist/cjs/src/schema/datetime.js +104 -38
- package/dist/cjs/src/schema/number.js +134 -114
- package/dist/cjs/src/schema/object.js +106 -43
- package/dist/cjs/src/schema/schema.js +123 -75
- package/dist/cjs/src/schema/string.js +152 -58
- package/dist/cjs/src/schema/union.js +412 -290
- package/dist/cjs/src/utils.js +42 -15
- package/dist/cjs/src/validators/array.js +6 -1
- package/dist/cjs/src/validators/boolean.js +2 -0
- package/dist/cjs/src/validators/datetime.js +4 -0
- package/dist/cjs/src/validators/number.js +12 -40
- package/dist/cjs/src/validators/object.js +1 -0
- package/dist/cjs/src/validators/schema.js +5 -1
- package/dist/cjs/src/validators/string.js +30 -2
- package/dist/cjs/src/validators/union.js +5 -4
- package/dist/cjs/src/validators/utils.js +99 -27
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
- package/dist/cjs/types/adapter/fields/array.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/index.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/object.d.ts +2 -1
- package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -1
- package/dist/cjs/types/adapter/index.d.ts +1 -0
- package/dist/cjs/types/adapter/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/types.d.ts +28 -18
- package/dist/cjs/types/adapter/types.d.ts.map +1 -1
- package/dist/cjs/types/constants.d.ts +0 -1
- package/dist/cjs/types/constants.d.ts.map +1 -1
- package/dist/cjs/types/domain.d.ts +5 -4
- package/dist/cjs/types/domain.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +78 -55
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/model.d.ts +17 -17
- package/dist/cjs/types/model.d.ts.map +1 -1
- package/dist/cjs/types/schema/array.d.ts +168 -47
- package/dist/cjs/types/schema/array.d.ts.map +1 -1
- package/dist/cjs/types/schema/boolean.d.ts +103 -44
- package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
- package/dist/cjs/types/schema/datetime.d.ts +90 -30
- package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
- package/dist/cjs/types/schema/number.d.ts +133 -125
- package/dist/cjs/types/schema/number.d.ts.map +1 -1
- package/dist/cjs/types/schema/object.d.ts +104 -35
- package/dist/cjs/types/schema/object.d.ts.map +1 -1
- package/dist/cjs/types/schema/schema.d.ts +62 -44
- package/dist/cjs/types/schema/schema.d.ts.map +1 -1
- package/dist/cjs/types/schema/string.d.ts +152 -65
- package/dist/cjs/types/schema/string.d.ts.map +1 -1
- package/dist/cjs/types/schema/types.d.ts +11 -2
- package/dist/cjs/types/schema/types.d.ts.map +1 -1
- package/dist/cjs/types/schema/union.d.ts +133 -40
- package/dist/cjs/types/schema/union.d.ts.map +1 -1
- package/dist/cjs/types/types.d.ts +35 -0
- package/dist/cjs/types/types.d.ts.map +1 -1
- package/dist/cjs/types/utils.d.ts +41 -27
- package/dist/cjs/types/utils.d.ts.map +1 -1
- package/dist/cjs/types/validators/array.d.ts.map +1 -1
- package/dist/cjs/types/validators/boolean.d.ts.map +1 -1
- package/dist/cjs/types/validators/datetime.d.ts.map +1 -1
- package/dist/cjs/types/validators/number.d.ts +5 -6
- package/dist/cjs/types/validators/number.d.ts.map +1 -1
- package/dist/cjs/types/validators/object.d.ts.map +1 -1
- package/dist/cjs/types/validators/schema.d.ts +2 -2
- package/dist/cjs/types/validators/schema.d.ts.map +1 -1
- package/dist/cjs/types/validators/string.d.ts +9 -9
- package/dist/cjs/types/validators/string.d.ts.map +1 -1
- package/dist/cjs/types/validators/utils.d.ts +44 -27
- package/dist/cjs/types/validators/utils.d.ts.map +1 -1
- package/dist/esm/src/adapter/fields/index.js +2 -2
- package/dist/esm/src/adapter/fields/object.js +6 -0
- package/dist/esm/src/adapter/index.js +1 -0
- package/dist/esm/src/constants.js +1 -2
- package/dist/esm/src/domain.js +11 -1
- package/dist/esm/src/index.js +38 -73
- package/dist/esm/src/model.js +83 -78
- package/dist/esm/src/schema/array.js +136 -54
- package/dist/esm/src/schema/boolean.js +98 -44
- package/dist/esm/src/schema/datetime.js +91 -38
- package/dist/esm/src/schema/number.js +127 -110
- package/dist/esm/src/schema/object.js +98 -43
- package/dist/esm/src/schema/schema.js +102 -67
- package/dist/esm/src/schema/string.js +147 -59
- package/dist/esm/src/schema/union.js +119 -40
- package/dist/esm/src/types.js +14 -1
- package/dist/esm/src/utils.js +56 -27
- package/dist/esm/src/validators/array.js +6 -1
- package/dist/esm/src/validators/boolean.js +2 -0
- package/dist/esm/src/validators/datetime.js +4 -0
- package/dist/esm/src/validators/number.js +9 -23
- package/dist/esm/src/validators/object.js +1 -0
- package/dist/esm/src/validators/schema.js +5 -1
- package/dist/esm/src/validators/string.js +30 -2
- package/dist/esm/src/validators/union.js +5 -4
- package/dist/esm/src/validators/utils.js +62 -36
- package/package.json +3 -3
- package/src/adapter/fields/array.ts +2 -2
- package/src/adapter/fields/boolean.ts +3 -8
- package/src/adapter/fields/datetime.ts +3 -9
- package/src/adapter/fields/index.ts +11 -11
- package/src/adapter/fields/number.ts +3 -9
- package/src/adapter/fields/object.ts +13 -10
- package/src/adapter/fields/string.ts +3 -9
- package/src/adapter/fields/union.ts +3 -9
- package/src/adapter/index.ts +1 -0
- package/src/adapter/types.ts +60 -45
- package/src/constants.ts +1 -3
- package/src/domain.ts +15 -1
- package/src/index.ts +189 -211
- package/src/model.ts +119 -115
- package/src/schema/array.ts +274 -90
- package/src/schema/boolean.ts +145 -60
- package/src/schema/datetime.ts +133 -49
- package/src/schema/number.ts +210 -173
- package/src/schema/object.ts +167 -74
- package/src/schema/schema.ts +205 -126
- package/src/schema/string.ts +221 -94
- package/src/schema/types.ts +44 -16
- package/src/schema/union.ts +193 -68
- package/src/types.ts +53 -0
- package/src/utils.ts +115 -57
- package/src/validators/array.ts +46 -27
- package/src/validators/boolean.ts +13 -7
- package/src/validators/datetime.ts +24 -16
- package/src/validators/number.ts +53 -63
- package/src/validators/object.ts +6 -5
- package/src/validators/schema.ts +33 -25
- package/src/validators/string.ts +122 -59
- package/src/validators/union.ts +8 -8
- package/src/validators/utils.ts +67 -42
package/src/model.ts
CHANGED
@@ -138,22 +138,22 @@ async function getSchemaFromModelField(
|
|
138
138
|
}
|
139
139
|
|
140
140
|
/**
|
141
|
-
* Different from other schemas, this function is a factory function that returns either an ObjectSchema or an
|
142
|
-
* The idea is to build the schema of a model dynamically based on its fields.
|
141
|
+
* Different from other schemas, this function is a factory function that returns either an ObjectSchema or an
|
142
|
+
* ArraySchema. The idea is to build the schema of a model dynamically based on its fields.
|
143
143
|
*
|
144
|
-
* Another feature is that it can automatically add the foreign key relation to the schema, but for that you need to
|
145
|
-
* the fields of the related model in the fields object.
|
144
|
+
* Another feature is that it can automatically add the foreign key relation to the schema, but for that you need to
|
145
|
+
* define the fields of the related model in the fields object.
|
146
146
|
*
|
147
147
|
* For example: A User model have a field `companyId` that is a ForeignKeyField to the Company model. The `relationName`
|
148
|
-
* is the direct relation from the User model to the Company model, and the `relatedName` is the reverse relation from
|
149
|
-
* Company model to the User model. If you define the fieldName as either the relatedName or the relationName it
|
150
|
-
* the data automatically.
|
148
|
+
* is the direct relation from the User model to the Company model, and the `relatedName` is the reverse relation from
|
149
|
+
* the Company model to the User model. If you define the fieldName as either the relatedName or the relationName it
|
150
|
+
* will fetch the data automatically.
|
151
151
|
*
|
152
|
-
* **Important**: We build the schema dynamically but also lazily, if you don't try to parse or validate the schema, it
|
153
|
-
* After the first time it's built, it's cached and never built again.
|
152
|
+
* **Important**: We build the schema dynamically but also lazily, if you don't try to parse or validate the schema, it
|
153
|
+
* won't be built. After the first time it's built, it's cached and never built again.
|
154
154
|
*
|
155
|
-
* **Important 2**: If you want to use the automatic relation feature, you need to define guarantee that the foreignKey
|
156
|
-
* exists on `show` array, or that it doesn't exist on `omit` array.
|
155
|
+
* **Important 2**: If you want to use the automatic relation feature, you need to define guarantee that the foreignKey
|
156
|
+
* field fieldName exists on `show` array, or that it doesn't exist on `omit` array.
|
157
157
|
*
|
158
158
|
* Like: `{ options: { show: ['id', 'name', 'companyId'] }}` or `{ options: { omit: ['id'] }}` it **will work**.
|
159
159
|
*
|
@@ -206,16 +206,16 @@ async function getSchemaFromModelField(
|
|
206
206
|
* fields: {
|
207
207
|
* usersOfCompany: p.modelSchema(User, { many: true }).optional({ outputOnly: true });
|
208
208
|
* },
|
209
|
-
*
|
210
|
-
* });
|
209
|
+
* // The `companyId` field on the 'User' model is tied to the `id` field on the 'Company' model so 'id' is required.
|
210
|
+
* show: ['id', 'type'] * });
|
211
211
|
*```
|
212
212
|
* @param model - The model that you want to build the schema from.
|
213
213
|
* @param options - The options to build the schema.
|
214
214
|
* @param options.ignoreExtraneousFields - If you want to ignore extraneous fields set this to true.
|
215
215
|
* @param options.engineInstance - What engine instance you want to use to fetch the data. Defaults to the first one.
|
216
|
-
* @param options.omitRelation - Fields that you want to omit from the relation. For example, on the example above, on
|
217
|
-
* `userSchema` you can omit the `companyId` field from the relation by just passing `['company']`, on the
|
218
|
-
* you can omit the `id` field from company by passing `['usersOfCompany']`.
|
216
|
+
* @param options.omitRelation - Fields that you want to omit from the relation. For example, on the example above, on
|
217
|
+
* the `userSchema` you can omit the `companyId` field from the relation by just passing `['company']`, on the
|
218
|
+
* `companySchema` you can omit the `id` field from company by passing `['usersOfCompany']`.
|
219
219
|
*
|
220
220
|
* @param options.fields - Extra fields that you want to add to the schema. If it has the same name as the model field,
|
221
221
|
* We will not create a schema for that field and use the one you have defined here.
|
@@ -354,7 +354,7 @@ export function modelSchema<
|
|
354
354
|
},
|
355
355
|
TDefinitionsOfSchemaType,
|
356
356
|
Record<any, any>
|
357
|
-
>
|
357
|
+
>
|
358
358
|
]
|
359
359
|
>
|
360
360
|
: ObjectSchema<
|
@@ -385,117 +385,121 @@ export function modelSchema<
|
|
385
385
|
|
386
386
|
// Add this callback to transform the model fields
|
387
387
|
parentSchema.__runBeforeParseAndData = async () => {
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
>();
|
388
|
+
const promise = new Promise((resolve) => {
|
389
|
+
const fieldsOfModels = (model as unknown as typeof InternalModelClass_DoNotUse)._fields();
|
390
|
+
const fieldsAsEntries = Object.entries(fieldsOfModels);
|
391
|
+
const fieldsWithAutomaticRelations = new Map<
|
392
|
+
Schema<any, any>,
|
393
|
+
{
|
394
|
+
relationOrRelatedName: string;
|
395
|
+
isArray: boolean;
|
396
|
+
model: ReturnType<typeof Model>;
|
397
|
+
fieldToSearchOnModel: string;
|
398
|
+
fieldToGetFromData: string;
|
399
|
+
}[]
|
400
|
+
>();
|
402
401
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
402
|
+
const fields = fieldsAsObject as Record<any, Schema<any, any>>;
|
403
|
+
Promise.all(
|
404
|
+
fieldsAsEntries.map(async ([key, value]) => {
|
405
|
+
if (omitAsSet.has(key as any)) return;
|
406
|
+
if (showAsSet.size > 0 && !showAsSet.has(key as any)) return;
|
407
407
|
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
408
|
+
let schema = (fieldsAsObject as any)[key as any];
|
409
|
+
const optionsForForeignKeyRelation: any = {};
|
410
|
+
if (!schema || value instanceof ForeignKeyField) {
|
411
|
+
const newSchema = await getSchemaFromModelField(
|
412
|
+
model,
|
413
|
+
value,
|
414
|
+
parentSchema?.__getParent?.(),
|
415
|
+
options?.fields,
|
416
|
+
options?.engineInstance,
|
417
|
+
optionsForForeignKeyRelation
|
418
|
+
);
|
419
|
+
if (!schema) schema = newSchema;
|
420
|
+
}
|
421
421
|
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
422
|
+
// Appends the foreign key relation to the schema automatically.
|
423
|
+
if (optionsForForeignKeyRelation.foreignKeyRelation) {
|
424
|
+
const rootSchema = optionsForForeignKeyRelation?.foreignKeyRelation?.schema || lazyModelSchema;
|
425
|
+
const existingRelations =
|
426
|
+
fieldsWithAutomaticRelations.get(rootSchema) ||
|
427
|
+
([] as {
|
428
|
+
relationOrRelatedName: string;
|
429
|
+
isArray: boolean;
|
430
|
+
model: ReturnType<typeof Model>;
|
431
|
+
fieldToSearchOnModel: string;
|
432
|
+
fieldToGetFromData: string;
|
433
|
+
}[]);
|
434
|
+
existingRelations.push({
|
435
|
+
relationOrRelatedName: optionsForForeignKeyRelation.foreignKeyRelation.relationOrRelatedName,
|
436
|
+
isArray: optionsForForeignKeyRelation.foreignKeyRelation.isArray,
|
437
|
+
model: optionsForForeignKeyRelation.foreignKeyRelation.model,
|
438
|
+
fieldToSearchOnModel: optionsForForeignKeyRelation.foreignKeyRelation.fieldToSearchOnModel,
|
439
|
+
fieldToGetFromData: optionsForForeignKeyRelation.foreignKeyRelation.fieldToGetFromData
|
440
|
+
});
|
441
|
+
fieldsWithAutomaticRelations.set(rootSchema, existingRelations);
|
442
|
+
}
|
443
443
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
444
|
+
(fieldsAsObject as any)[key] = schema;
|
445
|
+
return fieldsAsObject;
|
446
|
+
})
|
447
|
+
).then(async () => {
|
448
|
+
if (fieldsWithAutomaticRelations.size > 0) {
|
449
|
+
// This way we can get all of the relations concurrently with Promise.all
|
450
|
+
for (const [schema, relations] of fieldsWithAutomaticRelations.entries()) {
|
451
|
+
schema.toRepresentation(
|
452
|
+
async (data: any | any[]) => {
|
453
|
+
const allData = Array.isArray(data) ? data : [data];
|
454
|
+
// since we are changing the data by reference, just return the data itself.
|
455
|
+
await Promise.all(
|
456
|
+
allData.map(async (data) =>
|
457
|
+
Promise.all(
|
458
|
+
relations.map(async (relation) => {
|
459
|
+
// Ignore if the data of the relation already exists
|
460
|
+
if (relation.relationOrRelatedName in data) return;
|
450
461
|
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
await Promise.all(
|
459
|
-
allData.map(async (data) =>
|
460
|
-
Promise.all(
|
461
|
-
relations.map(async (relation) => {
|
462
|
-
// Ignore if the data of the relation already exists
|
463
|
-
if (relation.relationOrRelatedName in data) return;
|
462
|
+
let relationData: any | any[] = await relation.model.default.get({
|
463
|
+
search: {
|
464
|
+
[relation.fieldToSearchOnModel]: data[relation.fieldToGetFromData]
|
465
|
+
}
|
466
|
+
});
|
467
|
+
if (relation.isArray !== true) relationData = relationData[0];
|
468
|
+
data[relation.relationOrRelatedName] = relationData;
|
464
469
|
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
data[relation.relationOrRelatedName] = relationData;
|
470
|
+
if ((schema as any).__omitRelation.has(relation.relationOrRelatedName as any))
|
471
|
+
delete data[relation.fieldToGetFromData];
|
472
|
+
})
|
473
|
+
)
|
474
|
+
)
|
475
|
+
);
|
472
476
|
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
477
|
+
return data;
|
478
|
+
},
|
479
|
+
{
|
480
|
+
after: true
|
481
|
+
}
|
478
482
|
);
|
479
|
-
|
480
|
-
return data;
|
481
|
-
},
|
482
|
-
{
|
483
|
-
after: true
|
484
483
|
}
|
485
|
-
|
486
|
-
}
|
487
|
-
}
|
484
|
+
}
|
488
485
|
|
489
|
-
|
486
|
+
(lazyModelSchema as any).__data = fields as any;
|
490
487
|
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
488
|
+
await Promise.all(
|
489
|
+
customFieldValues.map(async (schema: any) => {
|
490
|
+
schema['__getParent'] = () => lazyModelSchema;
|
491
|
+
if (schema['__runBeforeParseAndData']) await schema['__runBeforeParseAndData'](schema);
|
492
|
+
})
|
493
|
+
);
|
494
|
+
resolve(undefined);
|
495
|
+
});
|
496
|
+
});
|
497
|
+
if (parentSchema.__alreadyAppliedModel) return parentSchema.__alreadyAppliedModel;
|
498
|
+
parentSchema.__alreadyAppliedModel = promise;
|
499
|
+
return promise;
|
497
500
|
};
|
498
501
|
|
499
502
|
if (options?.ignoreExtraneousFields !== true) lazyModelSchema.removeExtraneous();
|
503
|
+
|
500
504
|
return parentSchema;
|
501
505
|
}
|