appwrite-utils-cli 0.10.81 → 0.10.83
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/README.md +2 -0
- package/dist/collections/attributes.js +86 -41
- package/dist/collections/methods.d.ts +6 -0
- package/dist/collections/methods.js +159 -24
- package/dist/migrations/appwriteToX.js +1 -1
- package/dist/migrations/attributes.js +0 -1
- package/dist/migrations/converters.js +3 -4
- package/dist/migrations/dataLoader.js +3 -3
- package/dist/migrations/importController.js +0 -1
- package/dist/migrations/queue.js +1 -2
- package/dist/migrations/relationships.js +1 -1
- package/dist/migrations/users.js +5 -4
- package/dist/migrations/validationRules.js +30 -30
- package/package.json +2 -2
- package/src/collections/attributes.ts +126 -50
- package/src/collections/methods.ts +259 -28
- package/src/migrations/appwriteToX.ts +4 -4
- package/src/migrations/attributes.ts +0 -1
- package/src/migrations/converters.ts +3 -5
- package/src/migrations/dataLoader.ts +4 -4
- package/src/migrations/importController.ts +0 -1
- package/src/migrations/queue.ts +1 -2
- package/src/migrations/relationships.ts +1 -1
- package/src/migrations/users.ts +8 -7
- package/src/migrations/validationRules.ts +56 -31
- package/src/migrations/collections.ts +0 -545
@@ -10,8 +10,15 @@ import type { AppwriteConfig, CollectionCreate, Indexes } from "appwrite-utils";
|
|
10
10
|
import { nameToIdMapping, processQueue } from "../migrations/queue.js";
|
11
11
|
import { createUpdateCollectionAttributes } from "./attributes.js";
|
12
12
|
import { createOrUpdateIndexes } from "./indexes.js";
|
13
|
-
import _, { initial } from "lodash";
|
14
13
|
import { SchemaGenerator } from "../migrations/schemaStrings.js";
|
14
|
+
import {
|
15
|
+
isNull,
|
16
|
+
isUndefined,
|
17
|
+
isNil,
|
18
|
+
isPlainObject,
|
19
|
+
isString,
|
20
|
+
isJSONValue,
|
21
|
+
} from "es-toolkit";
|
15
22
|
import { delay, tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
16
23
|
|
17
24
|
export const documentExists = async (
|
@@ -20,46 +27,50 @@ export const documentExists = async (
|
|
20
27
|
targetCollectionId: string,
|
21
28
|
toCreateObject: any
|
22
29
|
): Promise<Models.Document | null> => {
|
23
|
-
// Had to do this because kept running into issues with type checking arrays so, sorry 40ms
|
24
30
|
const collection = await db.getCollection(dbId, targetCollectionId);
|
25
31
|
const attributes = collection.attributes as any[];
|
26
32
|
let arrayTypeAttributes = attributes
|
27
33
|
.filter((attribute: any) => attribute.array === true)
|
28
34
|
.map((attribute: any) => attribute.key);
|
29
|
-
|
35
|
+
|
30
36
|
const isJsonString = (str: string) => {
|
31
37
|
try {
|
32
38
|
const json = JSON.parse(str);
|
33
|
-
return typeof json === "object" && json !== null;
|
39
|
+
return typeof json === "object" && json !== null;
|
34
40
|
} catch (e) {
|
35
41
|
return false;
|
36
42
|
}
|
37
43
|
};
|
38
44
|
|
39
|
-
//
|
40
|
-
const
|
41
|
-
|
42
|
-
(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
45
|
+
// Convert object to entries and filter
|
46
|
+
const validEntries = Object.entries(toCreateObject).filter(
|
47
|
+
([key, value]) =>
|
48
|
+
!arrayTypeAttributes.includes(key) &&
|
49
|
+
!key.startsWith("$") &&
|
50
|
+
!isNull(value) &&
|
51
|
+
!isUndefined(value) &&
|
52
|
+
!isNil(value) &&
|
53
|
+
!isPlainObject(value) &&
|
54
|
+
!Array.isArray(value) &&
|
55
|
+
!(isString(value) && isJsonString(value)) &&
|
56
|
+
(isString(value) ? value.length < 4096 && value.length > 0 : true)
|
57
|
+
);
|
58
|
+
|
59
|
+
// Map and filter valid entries
|
60
|
+
const validMappedEntries = validEntries
|
61
|
+
.map(([key, value]) => [
|
62
|
+
key,
|
63
|
+
isString(value) || typeof value === "number" || typeof value === "boolean"
|
55
64
|
? value
|
56
|
-
: null
|
57
|
-
)
|
58
|
-
.
|
59
|
-
.
|
60
|
-
|
61
|
-
|
62
|
-
|
65
|
+
: null,
|
66
|
+
])
|
67
|
+
.filter(([key, value]) => !isNull(value) && isString(key))
|
68
|
+
.slice(0, 25);
|
69
|
+
|
70
|
+
// Convert to Query parameters
|
71
|
+
const validQueryParams = validMappedEntries.map(([key, value]) =>
|
72
|
+
Query.equal(key as string, value as any)
|
73
|
+
);
|
63
74
|
|
64
75
|
// Execute the query with the validated and prepared parameters
|
65
76
|
const result = await db.listDocuments(
|
@@ -380,12 +391,18 @@ export const createOrUpdateCollections = async (
|
|
380
391
|
// Add delay after creating attributes
|
381
392
|
await delay(250);
|
382
393
|
|
394
|
+
const indexesToUse =
|
395
|
+
indexes.length > 0
|
396
|
+
? indexes
|
397
|
+
: config.collections?.find((c) => c.$id === collectionToUse!.$id)
|
398
|
+
?.indexes ?? [];
|
399
|
+
|
383
400
|
console.log("Creating Indexes");
|
384
401
|
await createOrUpdateIndexes(
|
385
402
|
databaseId,
|
386
403
|
database,
|
387
404
|
collectionToUse!.$id,
|
388
|
-
|
405
|
+
indexesToUse as Indexes
|
389
406
|
);
|
390
407
|
|
391
408
|
// Add delay after creating indexes
|
@@ -444,3 +461,217 @@ export const fetchAllCollections = async (
|
|
444
461
|
console.log(`Fetched a total of ${collections.length} collections.`);
|
445
462
|
return collections;
|
446
463
|
};
|
464
|
+
|
465
|
+
/**
|
466
|
+
* Transfers all documents from one collection to another in a different database
|
467
|
+
* within the same Appwrite Project
|
468
|
+
*/
|
469
|
+
export const transferDocumentsBetweenDbsLocalToLocal = async (
|
470
|
+
db: Databases,
|
471
|
+
fromDbId: string,
|
472
|
+
toDbId: string,
|
473
|
+
fromCollId: string,
|
474
|
+
toCollId: string
|
475
|
+
) => {
|
476
|
+
let fromCollDocs = await tryAwaitWithRetry(async () =>
|
477
|
+
db.listDocuments(fromDbId, fromCollId, [Query.limit(50)])
|
478
|
+
);
|
479
|
+
let totalDocumentsTransferred = 0;
|
480
|
+
|
481
|
+
if (fromCollDocs.documents.length === 0) {
|
482
|
+
console.log(`No documents found in collection ${fromCollId}`);
|
483
|
+
return;
|
484
|
+
} else if (fromCollDocs.documents.length < 50) {
|
485
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
486
|
+
const toCreateObject: Partial<typeof doc> = {
|
487
|
+
...doc,
|
488
|
+
};
|
489
|
+
delete toCreateObject.$databaseId;
|
490
|
+
delete toCreateObject.$collectionId;
|
491
|
+
delete toCreateObject.$createdAt;
|
492
|
+
delete toCreateObject.$updatedAt;
|
493
|
+
delete toCreateObject.$id;
|
494
|
+
delete toCreateObject.$permissions;
|
495
|
+
return tryAwaitWithRetry(
|
496
|
+
async () =>
|
497
|
+
await db.createDocument(
|
498
|
+
toDbId,
|
499
|
+
toCollId,
|
500
|
+
doc.$id,
|
501
|
+
toCreateObject,
|
502
|
+
doc.$permissions
|
503
|
+
)
|
504
|
+
);
|
505
|
+
});
|
506
|
+
await Promise.all(batchedPromises);
|
507
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
508
|
+
} else {
|
509
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
510
|
+
const toCreateObject: Partial<typeof doc> = {
|
511
|
+
...doc,
|
512
|
+
};
|
513
|
+
delete toCreateObject.$databaseId;
|
514
|
+
delete toCreateObject.$collectionId;
|
515
|
+
delete toCreateObject.$createdAt;
|
516
|
+
delete toCreateObject.$updatedAt;
|
517
|
+
delete toCreateObject.$id;
|
518
|
+
delete toCreateObject.$permissions;
|
519
|
+
return tryAwaitWithRetry(async () =>
|
520
|
+
db.createDocument(
|
521
|
+
toDbId,
|
522
|
+
toCollId,
|
523
|
+
doc.$id,
|
524
|
+
toCreateObject,
|
525
|
+
doc.$permissions
|
526
|
+
)
|
527
|
+
);
|
528
|
+
});
|
529
|
+
await Promise.all(batchedPromises);
|
530
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
531
|
+
while (fromCollDocs.documents.length === 50) {
|
532
|
+
fromCollDocs = await tryAwaitWithRetry(
|
533
|
+
async () =>
|
534
|
+
await db.listDocuments(fromDbId, fromCollId, [
|
535
|
+
Query.limit(50),
|
536
|
+
Query.cursorAfter(
|
537
|
+
fromCollDocs.documents[fromCollDocs.documents.length - 1].$id
|
538
|
+
),
|
539
|
+
])
|
540
|
+
);
|
541
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
542
|
+
const toCreateObject: Partial<typeof doc> = {
|
543
|
+
...doc,
|
544
|
+
};
|
545
|
+
delete toCreateObject.$databaseId;
|
546
|
+
delete toCreateObject.$collectionId;
|
547
|
+
delete toCreateObject.$createdAt;
|
548
|
+
delete toCreateObject.$updatedAt;
|
549
|
+
delete toCreateObject.$id;
|
550
|
+
delete toCreateObject.$permissions;
|
551
|
+
return tryAwaitWithRetry(
|
552
|
+
async () =>
|
553
|
+
await db.createDocument(
|
554
|
+
toDbId,
|
555
|
+
toCollId,
|
556
|
+
doc.$id,
|
557
|
+
toCreateObject,
|
558
|
+
doc.$permissions
|
559
|
+
)
|
560
|
+
);
|
561
|
+
});
|
562
|
+
await Promise.all(batchedPromises);
|
563
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
564
|
+
}
|
565
|
+
}
|
566
|
+
|
567
|
+
console.log(
|
568
|
+
`Transferred ${totalDocumentsTransferred} documents from database ${fromDbId} to database ${toDbId} -- collection ${fromCollId} to collection ${toCollId}`
|
569
|
+
);
|
570
|
+
};
|
571
|
+
|
572
|
+
export const transferDocumentsBetweenDbsLocalToRemote = async (
|
573
|
+
localDb: Databases,
|
574
|
+
endpoint: string,
|
575
|
+
projectId: string,
|
576
|
+
apiKey: string,
|
577
|
+
fromDbId: string,
|
578
|
+
toDbId: string,
|
579
|
+
fromCollId: string,
|
580
|
+
toCollId: string
|
581
|
+
) => {
|
582
|
+
const client = new Client()
|
583
|
+
.setEndpoint(endpoint)
|
584
|
+
.setProject(projectId)
|
585
|
+
.setKey(apiKey);
|
586
|
+
let totalDocumentsTransferred = 0;
|
587
|
+
const remoteDb = new Databases(client);
|
588
|
+
let fromCollDocs = await tryAwaitWithRetry(async () =>
|
589
|
+
localDb.listDocuments(fromDbId, fromCollId, [Query.limit(50)])
|
590
|
+
);
|
591
|
+
|
592
|
+
if (fromCollDocs.documents.length === 0) {
|
593
|
+
console.log(`No documents found in collection ${fromCollId}`);
|
594
|
+
return;
|
595
|
+
} else if (fromCollDocs.documents.length < 50) {
|
596
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
597
|
+
const toCreateObject: Partial<typeof doc> = {
|
598
|
+
...doc,
|
599
|
+
};
|
600
|
+
delete toCreateObject.$databaseId;
|
601
|
+
delete toCreateObject.$collectionId;
|
602
|
+
delete toCreateObject.$createdAt;
|
603
|
+
delete toCreateObject.$updatedAt;
|
604
|
+
delete toCreateObject.$id;
|
605
|
+
delete toCreateObject.$permissions;
|
606
|
+
return tryAwaitWithRetry(async () =>
|
607
|
+
remoteDb.createDocument(
|
608
|
+
toDbId,
|
609
|
+
toCollId,
|
610
|
+
doc.$id,
|
611
|
+
toCreateObject,
|
612
|
+
doc.$permissions
|
613
|
+
)
|
614
|
+
);
|
615
|
+
});
|
616
|
+
await Promise.all(batchedPromises);
|
617
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
618
|
+
} else {
|
619
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
620
|
+
const toCreateObject: Partial<typeof doc> = {
|
621
|
+
...doc,
|
622
|
+
};
|
623
|
+
delete toCreateObject.$databaseId;
|
624
|
+
delete toCreateObject.$collectionId;
|
625
|
+
delete toCreateObject.$createdAt;
|
626
|
+
delete toCreateObject.$updatedAt;
|
627
|
+
delete toCreateObject.$id;
|
628
|
+
delete toCreateObject.$permissions;
|
629
|
+
return tryAwaitWithRetry(async () =>
|
630
|
+
remoteDb.createDocument(
|
631
|
+
toDbId,
|
632
|
+
toCollId,
|
633
|
+
doc.$id,
|
634
|
+
toCreateObject,
|
635
|
+
doc.$permissions
|
636
|
+
)
|
637
|
+
);
|
638
|
+
});
|
639
|
+
await Promise.all(batchedPromises);
|
640
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
641
|
+
while (fromCollDocs.documents.length === 50) {
|
642
|
+
fromCollDocs = await tryAwaitWithRetry(async () =>
|
643
|
+
localDb.listDocuments(fromDbId, fromCollId, [
|
644
|
+
Query.limit(50),
|
645
|
+
Query.cursorAfter(
|
646
|
+
fromCollDocs.documents[fromCollDocs.documents.length - 1].$id
|
647
|
+
),
|
648
|
+
])
|
649
|
+
);
|
650
|
+
const batchedPromises = fromCollDocs.documents.map((doc) => {
|
651
|
+
const toCreateObject: Partial<typeof doc> = {
|
652
|
+
...doc,
|
653
|
+
};
|
654
|
+
delete toCreateObject.$databaseId;
|
655
|
+
delete toCreateObject.$collectionId;
|
656
|
+
delete toCreateObject.$createdAt;
|
657
|
+
delete toCreateObject.$updatedAt;
|
658
|
+
delete toCreateObject.$id;
|
659
|
+
delete toCreateObject.$permissions;
|
660
|
+
return tryAwaitWithRetry(async () =>
|
661
|
+
remoteDb.createDocument(
|
662
|
+
toDbId,
|
663
|
+
toCollId,
|
664
|
+
doc.$id,
|
665
|
+
toCreateObject,
|
666
|
+
doc.$permissions
|
667
|
+
)
|
668
|
+
);
|
669
|
+
});
|
670
|
+
await Promise.all(batchedPromises);
|
671
|
+
totalDocumentsTransferred += fromCollDocs.documents.length;
|
672
|
+
}
|
673
|
+
}
|
674
|
+
console.log(
|
675
|
+
`Total documents transferred from database ${fromDbId} to database ${toDbId} -- collection ${fromCollId} to collection ${toCollId}: ${totalDocumentsTransferred}`
|
676
|
+
);
|
677
|
+
};
|
@@ -8,7 +8,7 @@ import {
|
|
8
8
|
type Models,
|
9
9
|
type Permission,
|
10
10
|
} from "node-appwrite";
|
11
|
-
import { fetchAllCollections } from "
|
11
|
+
import { fetchAllCollections } from "../collections/methods.js";
|
12
12
|
import { fetchAllDatabases } from "./databases.js";
|
13
13
|
import {
|
14
14
|
CollectionSchema,
|
@@ -138,7 +138,7 @@ export class AppwriteToX {
|
|
138
138
|
.map((attr: any) => {
|
139
139
|
return parseAttribute(attr);
|
140
140
|
})
|
141
|
-
.filter((attribute) =>
|
141
|
+
.filter((attribute: Attribute) =>
|
142
142
|
attribute.type === "relationship"
|
143
143
|
? attribute.side !== "child"
|
144
144
|
: true
|
@@ -169,10 +169,10 @@ export class AppwriteToX {
|
|
169
169
|
}
|
170
170
|
}
|
171
171
|
this.collToAttributeMap.set(collection.name, collAttributes);
|
172
|
-
const finalIndexes = collection.indexes.map((index) => {
|
172
|
+
const finalIndexes = collection.indexes.map((index: Models.Index) => {
|
173
173
|
return {
|
174
174
|
...index,
|
175
|
-
orders: index.orders?.filter((order) => {
|
175
|
+
orders: index.orders?.filter((order: string) => {
|
176
176
|
return order !== null && order;
|
177
177
|
}),
|
178
178
|
};
|
@@ -1,7 +1,5 @@
|
|
1
|
-
import _ from "lodash";
|
2
1
|
import { converterFunctions, type AttributeMappings } from "appwrite-utils";
|
3
|
-
|
4
|
-
const { cloneDeep, isObject } = _;
|
2
|
+
import { cloneDeep, isPlainObject } from "es-toolkit";
|
5
3
|
|
6
4
|
/**
|
7
5
|
* Deeply converts all properties of an object (or array) to strings.
|
@@ -11,7 +9,7 @@ const { cloneDeep, isObject } = _;
|
|
11
9
|
export const deepAnyToString = (data: any): any => {
|
12
10
|
if (Array.isArray(data)) {
|
13
11
|
return data.map((item) => deepAnyToString(item));
|
14
|
-
} else if (
|
12
|
+
} else if (isPlainObject(data)) {
|
15
13
|
return Object.keys(data).reduce((acc, key) => {
|
16
14
|
acc[key] = deepAnyToString(data[key as keyof typeof data]);
|
17
15
|
return acc;
|
@@ -34,7 +32,7 @@ export const deepConvert = <T>(
|
|
34
32
|
): any => {
|
35
33
|
if (Array.isArray(data)) {
|
36
34
|
return data.map((item) => deepConvert(item, convertFn));
|
37
|
-
} else if (
|
35
|
+
} else if (isPlainObject(data)) {
|
38
36
|
return Object.keys(data).reduce((acc: Record<string, T>, key: string) => {
|
39
37
|
acc[key] = deepConvert(data[key as keyof typeof data], convertFn);
|
40
38
|
return acc;
|
@@ -16,14 +16,14 @@ import path from "path";
|
|
16
16
|
import fs from "fs";
|
17
17
|
import { convertObjectByAttributeMappings } from "./converters.js";
|
18
18
|
import { z } from "zod";
|
19
|
-
import { checkForCollection } from "
|
19
|
+
import { checkForCollection } from "../collections/methods.js";
|
20
20
|
import { ID, Users, type Databases } from "node-appwrite";
|
21
21
|
import { logger } from "./logging.js";
|
22
22
|
import { findOrCreateOperation, updateOperation } from "./migrationHelper.js";
|
23
23
|
import { AuthUserCreateSchema } from "../schemas/authUser.js";
|
24
|
-
import _ from "lodash";
|
25
24
|
import { UsersController } from "./users.js";
|
26
25
|
import { finalizeByAttributeMap } from "../utils/helperFunctions.js";
|
26
|
+
import { isEmpty } from "es-toolkit/compat";
|
27
27
|
|
28
28
|
// Define a schema for the structure of collection import data using Zod for validation
|
29
29
|
export const CollectionImportDataSchema = z.object({
|
@@ -315,7 +315,7 @@ export class DataLoader {
|
|
315
315
|
// Find or create an import operation for the collection
|
316
316
|
const collectionImportOperation = await findOrCreateOperation(
|
317
317
|
this.database,
|
318
|
-
collection.$id
|
318
|
+
collection.$id!,
|
319
319
|
"importData"
|
320
320
|
);
|
321
321
|
// Store the operation ID in the map
|
@@ -556,7 +556,7 @@ export class DataLoader {
|
|
556
556
|
// Skip if value to match is missing or empty
|
557
557
|
if (
|
558
558
|
!sourceValue ||
|
559
|
-
|
559
|
+
isEmpty(sourceValue) ||
|
560
560
|
sourceValue === null
|
561
561
|
)
|
562
562
|
continue;
|
@@ -13,7 +13,6 @@ import type {
|
|
13
13
|
AttributeMappings,
|
14
14
|
} from "appwrite-utils";
|
15
15
|
import type { ImportDataActions } from "./importDataActions.js";
|
16
|
-
import _ from "lodash";
|
17
16
|
import { areCollectionNamesSame, tryAwaitWithRetry } from "../utils/index.js";
|
18
17
|
import type { SetupOptions } from "../utilsController.js";
|
19
18
|
import { resolveAndUpdateRelationships } from "./relationships.js";
|
package/src/migrations/queue.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import { Query, type Databases, type Models } from "node-appwrite";
|
2
2
|
import type { Attribute } from "appwrite-utils";
|
3
3
|
import { createOrUpdateAttribute } from "./attributes.js";
|
4
|
-
import
|
5
|
-
import { fetchAndCacheCollectionByName } from "./collections.js";
|
4
|
+
import { fetchAndCacheCollectionByName } from "../collections/methods.js";
|
6
5
|
import { tryAwaitWithRetry } from "../utils/helperFunctions.js";
|
7
6
|
|
8
7
|
export interface QueuedOperation {
|
package/src/migrations/users.ts
CHANGED
@@ -12,13 +12,14 @@ import {
|
|
12
12
|
type AuthUser,
|
13
13
|
type AuthUserCreate,
|
14
14
|
} from "../schemas/authUser.js";
|
15
|
-
import _ from "lodash";
|
16
15
|
import { logger } from "./logging.js";
|
17
16
|
import { splitIntoBatches } from "./migrationHelper.js";
|
18
17
|
import {
|
19
18
|
getAppwriteClient,
|
20
19
|
tryAwaitWithRetry,
|
21
20
|
} from "../utils/helperFunctions.js";
|
21
|
+
import { isUndefined } from "es-toolkit/compat";
|
22
|
+
import { isEmpty } from "es-toolkit/compat";
|
22
23
|
|
23
24
|
export class UsersController {
|
24
25
|
private config: AppwriteConfig;
|
@@ -123,17 +124,17 @@ export class UsersController {
|
|
123
124
|
`changeMe${item.email?.toLowerCase()}` || `changeMePlease`,
|
124
125
|
item.name || undefined
|
125
126
|
);
|
126
|
-
|
127
|
+
|
127
128
|
if (item.labels) {
|
128
129
|
await this.users.updateLabels(createdUser.$id, item.labels);
|
129
130
|
}
|
130
131
|
if (item.prefs) {
|
131
132
|
await this.users.updatePrefs(createdUser.$id, item.prefs);
|
132
133
|
}
|
133
|
-
|
134
|
+
|
134
135
|
return createdUser;
|
135
136
|
}); // Set throwError to true since we want to handle errors
|
136
|
-
|
137
|
+
|
137
138
|
return user;
|
138
139
|
} catch (e) {
|
139
140
|
if (e instanceof Error) {
|
@@ -181,8 +182,8 @@ export class UsersController {
|
|
181
182
|
if (
|
182
183
|
item.email &&
|
183
184
|
item.email !== userToReturn.email &&
|
184
|
-
!
|
185
|
-
!
|
185
|
+
!isEmpty(item.email) &&
|
186
|
+
!isUndefined(item.email)
|
186
187
|
) {
|
187
188
|
const emailExists = await this.users.list([
|
188
189
|
Query.equal("email", item.email),
|
@@ -213,7 +214,7 @@ export class UsersController {
|
|
213
214
|
item.phone !== userToReturn.phone &&
|
214
215
|
item.phone.length < 15 &&
|
215
216
|
item.phone.startsWith("+") &&
|
216
|
-
(
|
217
|
+
(isUndefined(userToReturn.phone) || isEmpty(userToReturn.phone))
|
217
218
|
) {
|
218
219
|
const userFoundWithPhone = await this.users.list([
|
219
220
|
Query.equal("phone", item.phone),
|
@@ -1,17 +1,42 @@
|
|
1
|
-
import
|
2
|
-
|
1
|
+
import {
|
2
|
+
isNumber,
|
3
|
+
isString,
|
4
|
+
isBoolean,
|
5
|
+
isArray,
|
6
|
+
isPlainObject,
|
7
|
+
isNull,
|
8
|
+
isUndefined,
|
9
|
+
isDate,
|
10
|
+
isEmpty,
|
11
|
+
isInteger,
|
12
|
+
isArrayLike,
|
13
|
+
isArrayLikeObject,
|
14
|
+
isFunction,
|
15
|
+
isLength,
|
16
|
+
isMap,
|
17
|
+
isSet,
|
18
|
+
isRegExp,
|
19
|
+
isSymbol,
|
20
|
+
isObjectLike,
|
21
|
+
isSafeInteger,
|
22
|
+
isTypedArray,
|
23
|
+
isEqual,
|
24
|
+
isMatch,
|
25
|
+
has,
|
26
|
+
get,
|
27
|
+
} from "es-toolkit/compat";
|
3
28
|
export interface ValidationRules {
|
4
29
|
[key: string]: (value: any, ...args: any[]) => boolean;
|
5
30
|
}
|
6
31
|
|
7
32
|
export const validationRules = {
|
8
|
-
isNumber: (value: any): boolean =>
|
9
|
-
isString: (value: any): boolean =>
|
10
|
-
isBoolean: (value: any): boolean =>
|
11
|
-
isArray: (value: any): boolean =>
|
33
|
+
isNumber: (value: any): boolean => isNumber(value),
|
34
|
+
isString: (value: any): boolean => isString(value),
|
35
|
+
isBoolean: (value: any): boolean => isBoolean(value),
|
36
|
+
isArray: (value: any): boolean => isArray(value),
|
12
37
|
isObject: (value: any): boolean =>
|
13
|
-
|
14
|
-
isNull: (value: any): boolean =>
|
38
|
+
isPlainObject(value) && !isArray(value) && !isFunction(value),
|
39
|
+
isNull: (value: any): boolean => isNull(value),
|
15
40
|
isValidEmail: (value: string): boolean =>
|
16
41
|
value.match(/^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$/) !== null,
|
17
42
|
isValidPhone: (value: string): boolean =>
|
@@ -35,29 +60,29 @@ export const validationRules = {
|
|
35
60
|
value.match(/^\d{4}-\d{2}-\d{2}$/) !== null,
|
36
61
|
isValidTime: (value: string): boolean =>
|
37
62
|
value.match(/^\d{2}:\d{2}(:\d{2})?$/) !== null,
|
38
|
-
isNullish: (value: any): boolean =>
|
39
|
-
isUndefined: (value: any): boolean =>
|
63
|
+
isNullish: (value: any): boolean => isNull(value) || isUndefined(value),
|
64
|
+
isUndefined: (value: any): boolean => isUndefined(value),
|
40
65
|
isDefined: (value: any): boolean =>
|
41
|
-
!
|
42
|
-
isDate: (value: any): boolean =>
|
43
|
-
isEmpty: (value: any): boolean =>
|
44
|
-
isInteger: (value: any): boolean =>
|
45
|
-
isFloat: (value: any): boolean =>
|
46
|
-
isArrayLike: (value: any): boolean =>
|
47
|
-
isArrayLikeObject: (value: any): boolean =>
|
48
|
-
isFunction: (value: any): boolean =>
|
49
|
-
isLength: (value: any): boolean =>
|
50
|
-
isMap: (value: any): boolean =>
|
51
|
-
isSet: (value: any): boolean =>
|
52
|
-
isRegExp: (value: any): boolean =>
|
53
|
-
isSymbol: (value: any): boolean =>
|
54
|
-
isObjectLike: (value: any): boolean =>
|
55
|
-
isPlainObject: (value: any): boolean =>
|
56
|
-
isSafeInteger: (value: any): boolean =>
|
57
|
-
isTypedArray: (value: any): boolean =>
|
58
|
-
isEqual: (value: any, other: any): boolean =>
|
59
|
-
isMatch: (object: any, source: any): boolean =>
|
60
|
-
has: (object: any, path: string): boolean =>
|
66
|
+
!isUndefined(value) && !isNull(value) && !isEmpty(value),
|
67
|
+
isDate: (value: any): boolean => isDate(value),
|
68
|
+
isEmpty: (value: any): boolean => isEmpty(value),
|
69
|
+
isInteger: (value: any): boolean => isInteger(value),
|
70
|
+
isFloat: (value: any): boolean => isNumber(value) && !isInteger(value),
|
71
|
+
isArrayLike: (value: any): boolean => isArrayLike(value),
|
72
|
+
isArrayLikeObject: (value: any): boolean => isArrayLikeObject(value),
|
73
|
+
isFunction: (value: any): boolean => isFunction(value),
|
74
|
+
isLength: (value: any): boolean => isLength(value),
|
75
|
+
isMap: (value: any): boolean => isMap(value),
|
76
|
+
isSet: (value: any): boolean => isSet(value),
|
77
|
+
isRegExp: (value: any): boolean => isRegExp(value),
|
78
|
+
isSymbol: (value: any): boolean => isSymbol(value),
|
79
|
+
isObjectLike: (value: any): boolean => isObjectLike(value),
|
80
|
+
isPlainObject: (value: any): boolean => isPlainObject(value),
|
81
|
+
isSafeInteger: (value: any): boolean => isSafeInteger(value),
|
82
|
+
isTypedArray: (value: any): boolean => isTypedArray(value),
|
83
|
+
isEqual: (value: any, other: any): boolean => isEqual(value, other),
|
84
|
+
isMatch: (object: any, source: any): boolean => isMatch(object, source),
|
85
|
+
has: (object: any, path: string): boolean => has(object, path),
|
61
86
|
get: (object: any, path: string, defaultValue: any): any =>
|
62
|
-
|
87
|
+
get(object, path, defaultValue),
|
63
88
|
};
|