appwrite-utils-cli 0.0.274 → 0.0.276
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 +4 -39
- package/dist/init.d.ts +2 -0
- package/dist/init.js +57 -0
- package/dist/main.js +62 -100
- package/dist/migrations/afterImportActions.d.ts +1 -4
- package/dist/migrations/afterImportActions.js +1 -0
- package/dist/migrations/appwriteToX.d.ts +46 -46
- package/dist/migrations/appwriteToX.js +6 -2
- package/dist/migrations/attributes.d.ts +1 -1
- package/dist/migrations/attributes.js +97 -71
- package/dist/migrations/backup.d.ts +240 -240
- package/dist/migrations/backup.js +1 -1
- package/dist/migrations/collections.d.ts +1 -1
- package/dist/migrations/collections.js +4 -4
- package/dist/migrations/converters.d.ts +9 -127
- package/dist/migrations/converters.js +1 -504
- package/dist/migrations/dataLoader.d.ts +470 -453
- package/dist/migrations/dataLoader.js +19 -1
- package/dist/migrations/dbHelpers.d.ts +1 -1
- package/dist/migrations/dbHelpers.js +3 -0
- package/dist/migrations/importController.d.ts +1 -1
- package/dist/migrations/importController.js +4 -7
- package/dist/migrations/importDataActions.d.ts +4 -6
- package/dist/migrations/importDataActions.js +6 -4
- package/dist/migrations/indexes.d.ts +1 -1
- package/dist/migrations/indexes.js +1 -1
- package/dist/migrations/migrationHelper.d.ts +29 -29
- package/dist/migrations/migrationHelper.js +1 -1
- package/dist/migrations/openapi.d.ts +1 -1
- package/dist/migrations/openapi.js +4 -1
- package/dist/migrations/queue.d.ts +1 -1
- package/dist/migrations/relationships.d.ts +5 -5
- package/dist/migrations/relationships.js +3 -0
- package/dist/migrations/schemaStrings.d.ts +2 -2
- package/dist/migrations/schemaStrings.js +93 -8
- package/dist/migrations/setupDatabase.d.ts +1 -1
- package/dist/migrations/setupDatabase.js +1 -1
- package/dist/migrations/storage.d.ts +1 -1
- package/dist/migrations/users.d.ts +1 -1
- package/dist/schemas/authUser.d.ts +12 -10
- package/dist/types.d.ts +0 -5
- package/dist/types.js +0 -2
- package/dist/utils/helperFunctions.d.ts +2 -3
- package/dist/utils/loadConfigs.d.ts +13 -0
- package/dist/utils/loadConfigs.js +47 -0
- package/dist/utils/setupFiles.d.ts +1 -0
- package/dist/utils/setupFiles.js +98 -223
- package/dist/utilsController.d.ts +1 -3
- package/dist/utilsController.js +14 -18
- package/package.json +9 -2
- package/src/init.ts +64 -0
- package/src/main.ts +73 -98
- package/src/migrations/afterImportActions.ts +1 -5
- package/src/migrations/appwriteToX.ts +6 -2
- package/src/migrations/attributes.ts +198 -150
- package/src/migrations/backup.ts +1 -1
- package/src/migrations/collections.ts +5 -11
- package/src/migrations/converters.ts +1 -540
- package/src/migrations/dataLoader.ts +19 -2
- package/src/migrations/dbHelpers.ts +4 -1
- package/src/migrations/importController.ts +5 -15
- package/src/migrations/importDataActions.ts +10 -14
- package/src/migrations/indexes.ts +1 -1
- package/src/migrations/migrationHelper.ts +1 -1
- package/src/migrations/openapi.ts +4 -1
- package/src/migrations/queue.ts +1 -1
- package/src/migrations/relationships.ts +4 -1
- package/src/migrations/schemaStrings.ts +106 -9
- package/src/migrations/setupDatabase.ts +1 -1
- package/src/migrations/storage.ts +1 -1
- package/src/migrations/users.ts +1 -1
- package/src/types.ts +0 -5
- package/src/utils/helperFunctions.ts +2 -3
- package/src/utils/loadConfigs.ts +55 -0
- package/src/utils/setupFiles.ts +114 -225
- package/src/utilsController.ts +27 -35
- package/src/migrations/schema.ts +0 -748
@@ -1,4 +1,4 @@
|
|
1
|
-
import { AttributeMappingsSchema, CollectionCreateSchema, importDefSchema, } from "
|
1
|
+
import { AttributeMappingsSchema, CollectionCreateSchema, importDefSchema, } from "appwrite-utils";
|
2
2
|
import path from "path";
|
3
3
|
import fs from "fs";
|
4
4
|
import { convertObjectByAttributeMappings } from "./converters.js";
|
@@ -165,6 +165,9 @@ export class DataLoader {
|
|
165
165
|
if (db.$id !== dbId) {
|
166
166
|
continue;
|
167
167
|
}
|
168
|
+
if (!this.config.collections) {
|
169
|
+
continue;
|
170
|
+
}
|
168
171
|
for (let index = 0; index < this.config.collections.length; index++) {
|
169
172
|
const collectionConfig = this.config.collections[index];
|
170
173
|
let collection = CollectionCreateSchema.parse(collectionConfig);
|
@@ -222,6 +225,9 @@ export class DataLoader {
|
|
222
225
|
if (db.$id !== dbId) {
|
223
226
|
continue;
|
224
227
|
}
|
228
|
+
if (!this.config.collections) {
|
229
|
+
continue;
|
230
|
+
}
|
225
231
|
// Iterate over the configured collections to process each
|
226
232
|
for (const collectionConfig of this.config.collections) {
|
227
233
|
const collection = collectionConfig;
|
@@ -269,6 +275,9 @@ export class DataLoader {
|
|
269
275
|
async dealWithMergedUsers() {
|
270
276
|
const usersCollectionKey = this.getCollectionKey(this.config.usersCollectionName);
|
271
277
|
const usersCollectionPrimaryKeyFields = new Set();
|
278
|
+
if (!this.config.collections) {
|
279
|
+
return;
|
280
|
+
}
|
272
281
|
// Collect primary key fields from the users collection definitions
|
273
282
|
this.config.collections.forEach((collection) => {
|
274
283
|
if (this.getCollectionKey(collection.name) === usersCollectionKey) {
|
@@ -306,6 +315,9 @@ export class DataLoader {
|
|
306
315
|
});
|
307
316
|
}
|
308
317
|
async updateOldReferencesForNew() {
|
318
|
+
if (!this.config.collections) {
|
319
|
+
return;
|
320
|
+
}
|
309
321
|
for (const collectionConfig of this.config.collections) {
|
310
322
|
const collectionKey = this.getCollectionKey(collectionConfig.name);
|
311
323
|
const collectionData = this.importMap.get(collectionKey);
|
@@ -360,6 +372,9 @@ export class DataLoader {
|
|
360
372
|
}
|
361
373
|
}
|
362
374
|
async updateReferencesInRelatedCollections() {
|
375
|
+
if (!this.config.collections) {
|
376
|
+
return;
|
377
|
+
}
|
363
378
|
// Iterate over each collection configuration
|
364
379
|
for (const collectionConfig of this.config.collections) {
|
365
380
|
const collectionKey = this.getCollectionKey(collectionConfig.name);
|
@@ -839,6 +854,9 @@ export class DataLoader {
|
|
839
854
|
}
|
840
855
|
}
|
841
856
|
updateReferencesBasedOnAttributeMappings() {
|
857
|
+
if (!this.config.collections) {
|
858
|
+
return;
|
859
|
+
}
|
842
860
|
this.config.collections.forEach((collectionConfig) => {
|
843
861
|
const collectionName = collectionConfig.name;
|
844
862
|
const collectionData = this.importMap.get(this.getCollectionKey(collectionName));
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { AppwriteConfig, Attribute } from "
|
1
|
+
import type { AppwriteConfig, Attribute } from "appwrite-utils";
|
2
2
|
export declare const categorizeCollectionByRelationshipSide: (attributes: Attribute[]) => "parent" | "mixed" | "child";
|
3
3
|
export declare const getDependencies: (attributes: Attribute[]) => string[];
|
4
4
|
export declare const sortCollections: (configCollections: AppwriteConfig["collections"]) => AppwriteConfig["collections"];
|
@@ -26,6 +26,9 @@ export const getDependencies = (attributes) => {
|
|
26
26
|
};
|
27
27
|
// Function to sort collections based on dependencies and relationship sides
|
28
28
|
export const sortCollections = (configCollections) => {
|
29
|
+
if (!configCollections) {
|
30
|
+
return [];
|
31
|
+
}
|
29
32
|
// Categorize collections based on their relationship sides
|
30
33
|
const parentCollections = configCollections.filter(({ attributes }) => categorizeCollectionByRelationshipSide(attributes) === "parent");
|
31
34
|
const mixedCollections = configCollections.filter(({ attributes }) => categorizeCollectionByRelationshipSide(attributes) === "mixed");
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { type Databases, type Storage } from "node-appwrite";
|
2
|
-
import type { AppwriteConfig, ConfigDatabase } from "
|
2
|
+
import type { AppwriteConfig, ConfigDatabase } from "appwrite-utils";
|
3
3
|
import type { ImportDataActions } from "./importDataActions.js";
|
4
4
|
import type { SetupOptions } from "../utilsController.js";
|
5
5
|
import { DataLoader } from "./dataLoader.js";
|
@@ -1,16 +1,10 @@
|
|
1
1
|
import { ID, Query, } from "node-appwrite";
|
2
|
-
import { checkForCollection } from "./collections.js";
|
3
|
-
import path from "path";
|
4
|
-
import fs from "fs";
|
5
|
-
import { convertObjectByAttributeMappings } from "./converters.js";
|
6
2
|
import _ from "lodash";
|
7
|
-
import { documentExists } from "./collections.js";
|
8
3
|
import { areCollectionNamesSame } from "../utils/index.js";
|
9
4
|
import { resolveAndUpdateRelationships } from "./relationships.js";
|
10
|
-
import { AuthUserCreateSchema } from "../types.js";
|
11
5
|
import { UsersController } from "./users.js";
|
12
6
|
import { logger } from "./logging.js";
|
13
|
-
import {
|
7
|
+
import { updateOperation } from "./migrationHelper.js";
|
14
8
|
import { BatchSchema, OperationCreateSchema, OperationSchema, } from "./backup.js";
|
15
9
|
import { DataLoader } from "./dataLoader.js";
|
16
10
|
export class ImportController {
|
@@ -69,6 +63,9 @@ export class ImportController {
|
|
69
63
|
}
|
70
64
|
}
|
71
65
|
async importCollections(db, dataLoader) {
|
66
|
+
if (!this.config.collections) {
|
67
|
+
return;
|
68
|
+
}
|
72
69
|
for (const collection of this.config.collections) {
|
73
70
|
let isUsersCollection = dataLoader.getCollectionKey(this.config.usersCollectionName) ===
|
74
71
|
dataLoader.getCollectionKey(collection.name);
|
@@ -1,9 +1,8 @@
|
|
1
1
|
import { type Databases, type Storage } from "node-appwrite";
|
2
|
-
import type { AppwriteConfig } from "
|
3
|
-
import { type ValidationRules } from "
|
4
|
-
import { type ConverterFunctions } from "
|
5
|
-
import { type AfterImportActions } from "
|
6
|
-
type AttributeMappings = AppwriteConfig["collections"][number]["importDefs"][number]["attributeMappings"];
|
2
|
+
import type { AppwriteConfig } from "appwrite-utils";
|
3
|
+
import { type ValidationRules, type AttributeMappings } from "appwrite-utils";
|
4
|
+
import { type ConverterFunctions } from "appwrite-utils";
|
5
|
+
import { type AfterImportActions } from "appwrite-utils";
|
7
6
|
export declare class ImportDataActions {
|
8
7
|
private db;
|
9
8
|
private storage;
|
@@ -41,4 +40,3 @@ export declare class ImportDataActions {
|
|
41
40
|
[key: string]: any;
|
42
41
|
}, item: any): any;
|
43
42
|
}
|
44
|
-
export {};
|
@@ -1,7 +1,9 @@
|
|
1
1
|
import { ID, InputFile, Query, } from "node-appwrite";
|
2
|
-
import { validationRules } from "
|
3
|
-
import { converterFunctions
|
4
|
-
import {
|
2
|
+
import { validationRules, } from "appwrite-utils";
|
3
|
+
import { converterFunctions } from "appwrite-utils";
|
4
|
+
import { convertObjectBySchema } from "./converters.js";
|
5
|
+
import {} from "appwrite-utils";
|
6
|
+
import { afterImportActions } from "./afterImportActions.js";
|
5
7
|
import { logger } from "./logging.js";
|
6
8
|
export class ImportDataActions {
|
7
9
|
db;
|
@@ -21,7 +23,7 @@ export class ImportDataActions {
|
|
21
23
|
runConverterFunctions(item, attributeMappings) {
|
22
24
|
const conversionSchema = attributeMappings.reduce((schema, mapping) => {
|
23
25
|
schema[mapping.targetKey] = (originalValue) => {
|
24
|
-
return mapping.converters
|
26
|
+
return mapping.converters?.reduce((value, converterName) => {
|
25
27
|
let shouldProcessAsArray = false;
|
26
28
|
if ((converterName.includes("[Arr]") ||
|
27
29
|
converterName.includes("[arr]")) &&
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type Index } from "
|
1
|
+
import { type Index } from "appwrite-utils";
|
2
2
|
import { Databases, type Models } from "node-appwrite";
|
3
3
|
export declare const createOrUpdateIndex: (dbId: string, db: Databases, collectionId: string, index: Index) => Promise<Models.Index>;
|
4
4
|
export declare const createOrUpdateIndexes: (dbId: string, db: Databases, collectionId: string, indexes: Index[]) => Promise<void>;
|
@@ -29,8 +29,8 @@ export declare const ContextObject: z.ZodObject<{
|
|
29
29
|
path: string;
|
30
30
|
name: string;
|
31
31
|
}>>;
|
32
|
-
converters: z.ZodDefault<z.ZodArray<z.ZodString, "many"
|
33
|
-
validationActions: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
32
|
+
converters: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodString, "many">>>;
|
33
|
+
validationActions: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodObject<{
|
34
34
|
action: z.ZodString;
|
35
35
|
params: z.ZodArray<z.ZodString, "many">;
|
36
36
|
}, "strip", z.ZodTypeAny, {
|
@@ -39,8 +39,8 @@ export declare const ContextObject: z.ZodObject<{
|
|
39
39
|
}, {
|
40
40
|
params: string[];
|
41
41
|
action: string;
|
42
|
-
}>, "many"
|
43
|
-
postImportActions: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
42
|
+
}>, "many">>>;
|
43
|
+
postImportActions: z.ZodOptional<z.ZodDefault<z.ZodArray<z.ZodObject<{
|
44
44
|
action: z.ZodString;
|
45
45
|
params: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodRecord<z.ZodString, z.ZodAny>]>, "many">;
|
46
46
|
}, "strip", z.ZodTypeAny, {
|
@@ -49,24 +49,24 @@ export declare const ContextObject: z.ZodObject<{
|
|
49
49
|
}, {
|
50
50
|
params: (string | Record<string, any>)[];
|
51
51
|
action: string;
|
52
|
-
}>, "many"
|
52
|
+
}>, "many">>>;
|
53
53
|
}, "strip", z.ZodTypeAny, {
|
54
54
|
targetKey: string;
|
55
|
-
converters: string[];
|
56
|
-
validationActions: {
|
57
|
-
params: string[];
|
58
|
-
action: string;
|
59
|
-
}[];
|
60
|
-
postImportActions: {
|
61
|
-
params: (string | Record<string, any>)[];
|
62
|
-
action: string;
|
63
|
-
}[];
|
64
55
|
oldKey?: string | undefined;
|
65
56
|
oldKeys?: string[] | undefined;
|
66
57
|
fileData?: {
|
67
58
|
path: string;
|
68
59
|
name: string;
|
69
60
|
} | undefined;
|
61
|
+
converters?: string[] | undefined;
|
62
|
+
validationActions?: {
|
63
|
+
params: string[];
|
64
|
+
action: string;
|
65
|
+
}[] | undefined;
|
66
|
+
postImportActions?: {
|
67
|
+
params: (string | Record<string, any>)[];
|
68
|
+
action: string;
|
69
|
+
}[] | undefined;
|
70
70
|
}, {
|
71
71
|
targetKey: string;
|
72
72
|
oldKey?: string | undefined;
|
@@ -87,29 +87,31 @@ export declare const ContextObject: z.ZodObject<{
|
|
87
87
|
}>, "many">;
|
88
88
|
context: z.ZodAny;
|
89
89
|
}, "strip", z.ZodTypeAny, {
|
90
|
+
collectionId: string;
|
91
|
+
dbId: string;
|
90
92
|
attributeMappings: {
|
91
93
|
targetKey: string;
|
92
|
-
converters: string[];
|
93
|
-
validationActions: {
|
94
|
-
params: string[];
|
95
|
-
action: string;
|
96
|
-
}[];
|
97
|
-
postImportActions: {
|
98
|
-
params: (string | Record<string, any>)[];
|
99
|
-
action: string;
|
100
|
-
}[];
|
101
94
|
oldKey?: string | undefined;
|
102
95
|
oldKeys?: string[] | undefined;
|
103
96
|
fileData?: {
|
104
97
|
path: string;
|
105
98
|
name: string;
|
106
99
|
} | undefined;
|
100
|
+
converters?: string[] | undefined;
|
101
|
+
validationActions?: {
|
102
|
+
params: string[];
|
103
|
+
action: string;
|
104
|
+
}[] | undefined;
|
105
|
+
postImportActions?: {
|
106
|
+
params: (string | Record<string, any>)[];
|
107
|
+
action: string;
|
108
|
+
}[] | undefined;
|
107
109
|
}[];
|
108
|
-
collectionId: string;
|
109
|
-
dbId: string;
|
110
110
|
finalItem?: any;
|
111
111
|
context?: any;
|
112
112
|
}, {
|
113
|
+
collectionId: string;
|
114
|
+
dbId: string;
|
113
115
|
attributeMappings: {
|
114
116
|
targetKey: string;
|
115
117
|
oldKey?: string | undefined;
|
@@ -128,8 +130,6 @@ export declare const ContextObject: z.ZodObject<{
|
|
128
130
|
action: string;
|
129
131
|
}[] | undefined;
|
130
132
|
}[];
|
131
|
-
collectionId: string;
|
132
|
-
dbId: string;
|
133
133
|
finalItem?: any;
|
134
134
|
context?: any;
|
135
135
|
}>;
|
@@ -138,10 +138,10 @@ export declare const createOrFindAfterImportOperation: (database: Databases, col
|
|
138
138
|
export declare const addBatch: (database: Databases, data: string) => Promise<string>;
|
139
139
|
export declare const getAfterImportOperations: (database: Databases, collectionId: string) => Promise<{
|
140
140
|
error: string;
|
141
|
-
status: "error" | "pending" | "ready" | "in_progress" | "completed" | "cancelled";
|
142
141
|
$id: string;
|
143
142
|
$createdAt: string;
|
144
143
|
$updatedAt: string;
|
144
|
+
status: "error" | "pending" | "ready" | "in_progress" | "completed" | "cancelled";
|
145
145
|
collectionId: string;
|
146
146
|
operationType: string;
|
147
147
|
progress: number;
|
@@ -151,10 +151,10 @@ export declare const getAfterImportOperations: (database: Databases, collectionI
|
|
151
151
|
}[]>;
|
152
152
|
export declare const findOrCreateOperation: (database: Databases, collectionId: string, operationType: string, additionalQueries?: string[]) => Promise<{
|
153
153
|
error: string;
|
154
|
-
status: "error" | "pending" | "ready" | "in_progress" | "completed" | "cancelled";
|
155
154
|
$id: string;
|
156
155
|
$createdAt: string;
|
157
156
|
$updatedAt: string;
|
157
|
+
status: "error" | "pending" | "ready" | "in_progress" | "completed" | "cancelled";
|
158
158
|
collectionId: string;
|
159
159
|
operationType: string;
|
160
160
|
progress: number;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { ID, Query } from "node-appwrite";
|
2
2
|
import { BatchSchema, OperationSchema } from "./backup.js";
|
3
|
-
import { AttributeMappingsSchema } from "
|
3
|
+
import { AttributeMappingsSchema } from "appwrite-utils";
|
4
4
|
import { z } from "zod";
|
5
5
|
import { logger } from "./logging.js";
|
6
6
|
/**
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type AppwriteConfig } from "
|
1
|
+
import { type AppwriteConfig } from "appwrite-utils";
|
2
2
|
import { z } from "zod";
|
3
3
|
export declare const generateOpenApi: (config: AppwriteConfig) => Promise<void>;
|
4
4
|
export declare function transformTypeToOpenApi<T extends z.ZodTypeAny>(schema: T): z.infer<T>;
|
@@ -1,9 +1,12 @@
|
|
1
1
|
import { OpenAPIRegistry, OpenApiGeneratorV3, OpenApiGeneratorV31, } from "@asteasolutions/zod-to-openapi";
|
2
|
-
import { attributeSchema, collectionSchema, } from "
|
2
|
+
import { attributeSchema, collectionSchema, } from "appwrite-utils";
|
3
3
|
import { z } from "zod";
|
4
4
|
import { writeFileSync } from "fs";
|
5
5
|
const registry = new OpenAPIRegistry();
|
6
6
|
export const generateOpenApi = async (config) => {
|
7
|
+
if (!config.collections) {
|
8
|
+
return;
|
9
|
+
}
|
7
10
|
for (const collection of config.collections) {
|
8
11
|
// Transform and register each attribute schema
|
9
12
|
const attributeSchemas = collection.attributes.map((attribute) => {
|
@@ -1,13 +1,11 @@
|
|
1
1
|
import { Databases } from "node-appwrite";
|
2
|
-
import type { AppwriteConfig } from "
|
2
|
+
import type { AppwriteConfig } from "appwrite-utils";
|
3
3
|
/**
|
4
4
|
* Finds collections that have defined relationship attributes.
|
5
5
|
*/
|
6
6
|
export declare const findCollectionsWithRelationships: (config: AppwriteConfig) => Map<string, {
|
7
|
-
key: string;
|
8
7
|
type: "relationship";
|
9
|
-
|
10
|
-
required: boolean;
|
8
|
+
key: string;
|
11
9
|
relatedCollection: string;
|
12
10
|
relationType: "oneToMany" | "manyToOne" | "oneToOne" | "manyToMany";
|
13
11
|
twoWay: boolean;
|
@@ -15,10 +13,12 @@ export declare const findCollectionsWithRelationships: (config: AppwriteConfig)
|
|
15
13
|
onDelete: "setNull" | "cascade" | "restrict";
|
16
14
|
side: "parent" | "child";
|
17
15
|
array?: boolean | undefined;
|
16
|
+
error?: string | undefined;
|
17
|
+
required?: boolean | undefined;
|
18
|
+
description?: string | Record<string, string> | null | undefined;
|
18
19
|
importMapping?: {
|
19
20
|
originalIdField: string;
|
20
21
|
targetField?: string | undefined;
|
21
22
|
} | undefined;
|
22
|
-
description?: string | Record<string, string> | null | undefined;
|
23
23
|
}[]>;
|
24
24
|
export declare function resolveAndUpdateRelationships(dbId: string, database: Databases, config: AppwriteConfig): Promise<void>;
|
@@ -6,6 +6,9 @@ import { logger } from "./logging.js";
|
|
6
6
|
*/
|
7
7
|
export const findCollectionsWithRelationships = (config) => {
|
8
8
|
const toReturn = new Map();
|
9
|
+
if (!config.collections) {
|
10
|
+
return toReturn;
|
11
|
+
}
|
9
12
|
for (const collection of config.collections) {
|
10
13
|
if (collection.attributes) {
|
11
14
|
for (const attribute of collection.attributes) {
|
@@ -1,10 +1,10 @@
|
|
1
|
-
import type { AppwriteConfig, Attribute } from "
|
1
|
+
import type { AppwriteConfig, Attribute } from "appwrite-utils";
|
2
2
|
export declare class SchemaGenerator {
|
3
3
|
private relationshipMap;
|
4
4
|
private config;
|
5
5
|
private appwriteFolderPath;
|
6
6
|
constructor(config: AppwriteConfig, appwriteFolderPath: string);
|
7
|
-
|
7
|
+
updateTsSchemas(): void;
|
8
8
|
private extractRelationships;
|
9
9
|
private addRelationship;
|
10
10
|
generateSchemas(): void;
|
@@ -13,16 +13,98 @@ export class SchemaGenerator {
|
|
13
13
|
this.appwriteFolderPath = appwriteFolderPath;
|
14
14
|
this.extractRelationships();
|
15
15
|
}
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
const
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
updateTsSchemas() {
|
17
|
+
const collections = this.config.collections;
|
18
|
+
delete this.config.collections;
|
19
|
+
const configPath = path.join(this.appwriteFolderPath, "appwriteConfig.ts");
|
20
|
+
const configContent = `import { AppwriteConfig } from "appwrite-utils";
|
21
|
+
|
22
|
+
const appwriteConfig: AppwriteConfig = {
|
23
|
+
appwriteEndpoint: "${this.config.appwriteEndpoint}",
|
24
|
+
appwriteProject: "${this.config.appwriteProject}",
|
25
|
+
appwriteKey: "${this.config.appwriteKey}",
|
26
|
+
enableDevDatabase: ${this.config.enableDevDatabase},
|
27
|
+
enableBackups: ${this.config.enableBackups},
|
28
|
+
backupInterval: ${this.config.backupInterval},
|
29
|
+
backupRetention: ${this.config.backupRetention},
|
30
|
+
enableBackupCleanup: ${this.config.enableBackupCleanup},
|
31
|
+
enableMockData: ${this.config.enableMockData},
|
32
|
+
enableWipeOtherDatabases: ${this.config.enableWipeOtherDatabases},
|
33
|
+
documentBucketId: "${this.config.documentBucketId}",
|
34
|
+
usersCollectionName: "${this.config.usersCollectionName}",
|
35
|
+
databases: ${JSON.stringify(this.config.databases)}
|
36
|
+
};
|
37
|
+
|
38
|
+
export default appwriteConfig;
|
39
|
+
`;
|
40
|
+
fs.writeFileSync(configPath, configContent, { encoding: "utf-8" });
|
41
|
+
const collectionsFolderPath = path.join(this.appwriteFolderPath, "collections");
|
42
|
+
if (!fs.existsSync(collectionsFolderPath)) {
|
43
|
+
fs.mkdirSync(collectionsFolderPath, { recursive: true });
|
44
|
+
}
|
45
|
+
collections?.forEach((collection) => {
|
46
|
+
const { databaseId, ...collectionWithoutDbId } = collection; // Destructure to exclude databaseId
|
47
|
+
const collectionFilePath = path.join(collectionsFolderPath, `${collection.name}.ts`);
|
48
|
+
const collectionContent = `import { CollectionCreate } from "appwrite-utils";
|
49
|
+
|
50
|
+
const ${collection.name}Config: Partial<CollectionCreate> = {
|
51
|
+
name: "${collection.name}",
|
52
|
+
$id: "${collection.$id}",
|
53
|
+
enabled: ${collection.enabled},
|
54
|
+
documentSecurity: ${collection.documentSecurity},
|
55
|
+
$permissions: [
|
56
|
+
${collection.$permissions
|
57
|
+
.map((permission) => `{ permission: "${permission.permission}", target: "${permission.target}" }`)
|
58
|
+
.join(",\n ")}
|
59
|
+
],
|
60
|
+
attributes: [
|
61
|
+
${collection.attributes
|
62
|
+
.map((attr) => {
|
63
|
+
return `{ ${Object.entries(attr)
|
64
|
+
.map(([key, value]) => {
|
65
|
+
// Check the type of the value and format it accordingly
|
66
|
+
if (typeof value === "string") {
|
67
|
+
// If the value is a string, wrap it in quotes
|
68
|
+
return `${key}: "${value.replace(/"/g, '\\"')}"`; // Escape existing quotes in the string
|
69
|
+
}
|
70
|
+
else if (Array.isArray(value)) {
|
71
|
+
// If the value is an array, join it with commas
|
72
|
+
return `${key}: [${value
|
73
|
+
.map((item) => `"${item}"`)
|
74
|
+
.join(", ")}]`;
|
75
|
+
}
|
76
|
+
else {
|
77
|
+
// If the value is not a string (e.g., boolean or number), output it directly
|
78
|
+
return `${key}: ${value}`;
|
79
|
+
}
|
80
|
+
})
|
81
|
+
.join(", ")} }`;
|
82
|
+
})
|
83
|
+
.join(",\n ")}
|
84
|
+
],
|
85
|
+
indexes: [
|
86
|
+
${(collection.indexes?.map((index) => {
|
87
|
+
// Map each attribute to ensure it is properly quoted
|
88
|
+
const formattedAttributes = index.attributes
|
89
|
+
.map((attr) => `"${attr}"`)
|
90
|
+
.join(", ");
|
91
|
+
return `{ key: "${index.key}", type: "${index.type}", attributes: [${formattedAttributes}] }`;
|
92
|
+
}) ?? []).join(",\n ")}
|
93
|
+
]
|
94
|
+
};
|
95
|
+
|
96
|
+
export default ${collection.name}Config;
|
97
|
+
`;
|
98
|
+
fs.writeFileSync(collectionFilePath, collectionContent, {
|
99
|
+
encoding: "utf-8",
|
100
|
+
});
|
101
|
+
console.log(`Collection schema written to ${collectionFilePath}`);
|
102
|
+
});
|
24
103
|
}
|
25
104
|
extractRelationships() {
|
105
|
+
if (!this.config.collections) {
|
106
|
+
return;
|
107
|
+
}
|
26
108
|
this.config.collections.forEach((collection) => {
|
27
109
|
collection.attributes.forEach((attr) => {
|
28
110
|
if (attr.type === "relationship" && attr.twoWay && attr.twoWayKey) {
|
@@ -78,6 +160,9 @@ export class SchemaGenerator {
|
|
78
160
|
this.relationshipMap.set(parentCollection, relationshipsParent);
|
79
161
|
}
|
80
162
|
generateSchemas() {
|
163
|
+
if (!this.config.collections) {
|
164
|
+
return;
|
165
|
+
}
|
81
166
|
this.config.collections.forEach((collection) => {
|
82
167
|
const schemaString = this.createSchemaString(collection.name, collection.attributes);
|
83
168
|
const camelCaseName = toCamelCase(collection.name);
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Databases, Storage } from "node-appwrite";
|
2
|
-
import { type AppwriteConfig } from "
|
2
|
+
import { type AppwriteConfig } from "appwrite-utils";
|
3
3
|
import type { SetupOptions } from "../utilsController.js";
|
4
4
|
export declare const setupMigrationDatabase: (config: AppwriteConfig) => Promise<void>;
|
5
5
|
export declare const ensureDatabasesExist: (config: AppwriteConfig) => Promise<void>;
|
@@ -4,7 +4,7 @@ import { createOrUpdateCollections, generateSchemas, wipeDatabase, } from "./col
|
|
4
4
|
import { getMigrationCollectionSchemas } from "./backup.js";
|
5
5
|
import { areCollectionNamesSame, toCamelCase } from "../utils/index.js";
|
6
6
|
import { backupDatabase, initOrGetBackupStorage, initOrGetDocumentStorage, wipeDocumentStorage, } from "./storage.js";
|
7
|
-
import {} from "
|
7
|
+
import {} from "appwrite-utils";
|
8
8
|
import { nameToIdMapping } from "./queue.js";
|
9
9
|
import { UsersController } from "./users.js";
|
10
10
|
export const setupMigrationDatabase = async (config) => {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Storage, Databases, type Models } from "node-appwrite";
|
2
2
|
import { type OperationCreate } from "./backup.js";
|
3
|
-
import type { AppwriteConfig } from "
|
3
|
+
import type { AppwriteConfig } from "appwrite-utils";
|
4
4
|
export declare const logOperation: (db: Databases, dbId: string, operationDetails: OperationCreate, operationId?: string) => Promise<Models.Document>;
|
5
5
|
export declare const initOrGetBackupStorage: (storage: Storage) => Promise<Models.Bucket>;
|
6
6
|
export declare const initOrGetDocumentStorage: (storage: Storage, config: AppwriteConfig, dbName: string) => Promise<Models.Bucket | undefined>;
|
@@ -28,35 +28,37 @@ export declare const AuthUserSchema: z.ZodObject<{
|
|
28
28
|
labels?: string[] | undefined;
|
29
29
|
}>;
|
30
30
|
export type AuthUser = z.infer<typeof AuthUserSchema>;
|
31
|
-
export declare const AuthUserCreateSchema: z.ZodObject<{
|
32
|
-
|
33
|
-
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
31
|
+
export declare const AuthUserCreateSchema: z.ZodObject<z.objectUtil.extendShape<Omit<{
|
32
|
+
$id: z.ZodString;
|
34
33
|
$createdAt: z.ZodOptional<z.ZodString>;
|
35
34
|
$updatedAt: z.ZodOptional<z.ZodString>;
|
35
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
36
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
36
37
|
phone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
37
38
|
prefs: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
|
38
39
|
labels: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
|
40
|
+
}, "$id">, {
|
39
41
|
userId: z.ZodOptional<z.ZodString>;
|
40
42
|
password: z.ZodOptional<z.ZodString>;
|
41
|
-
}
|
43
|
+
}>, "strip", z.ZodTypeAny, {
|
42
44
|
prefs: Record<string, string>;
|
43
45
|
labels: string[];
|
44
|
-
name?: string | null | undefined;
|
45
|
-
email?: string | null | undefined;
|
46
46
|
$createdAt?: string | undefined;
|
47
47
|
$updatedAt?: string | undefined;
|
48
|
+
name?: string | null | undefined;
|
49
|
+
password?: string | undefined;
|
50
|
+
email?: string | null | undefined;
|
48
51
|
phone?: string | null | undefined;
|
49
52
|
userId?: string | undefined;
|
50
|
-
password?: string | undefined;
|
51
53
|
}, {
|
52
|
-
name?: string | null | undefined;
|
53
|
-
email?: string | null | undefined;
|
54
54
|
$createdAt?: string | undefined;
|
55
55
|
$updatedAt?: string | undefined;
|
56
|
+
name?: string | null | undefined;
|
57
|
+
password?: string | undefined;
|
58
|
+
email?: string | null | undefined;
|
56
59
|
phone?: string | null | undefined;
|
57
60
|
prefs?: Record<string, string> | undefined;
|
58
61
|
labels?: string[] | undefined;
|
59
62
|
userId?: string | undefined;
|
60
|
-
password?: string | undefined;
|
61
63
|
}>;
|
62
64
|
export type AuthUserCreate = z.infer<typeof AuthUserCreateSchema>;
|
package/dist/types.d.ts
CHANGED
@@ -1,9 +1,4 @@
|
|
1
|
-
export type { AppwriteConfig } from "./migrations/schema.js";
|
2
|
-
export type { ConverterFunctions } from "./migrations/converters.js";
|
3
1
|
export type { ValidationRules } from "./migrations/validationRules.js";
|
4
|
-
export type { AfterImportActions } from "./migrations/afterImportActions.js";
|
5
2
|
export { type AuthUserCreate, AuthUserCreateSchema, type AuthUser, AuthUserSchema, } from "./schemas/authUser.js";
|
6
|
-
export { getFileViewUrl, getFileDownloadUrl } from "./utils/helperFunctions.js";
|
7
|
-
export { converterFunctions } from "./migrations/converters.js";
|
8
3
|
export { validationRules } from "./migrations/validationRules.js";
|
9
4
|
export { afterImportActions } from "./migrations/afterImportActions.js";
|
package/dist/types.js
CHANGED
@@ -1,5 +1,3 @@
|
|
1
1
|
export { AuthUserCreateSchema, AuthUserSchema, } from "./schemas/authUser.js";
|
2
|
-
export { getFileViewUrl, getFileDownloadUrl } from "./utils/helperFunctions.js";
|
3
|
-
export { converterFunctions } from "./migrations/converters.js";
|
4
2
|
export { validationRules } from "./migrations/validationRules.js";
|
5
3
|
export { afterImportActions } from "./migrations/afterImportActions.js";
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import type { Models } from "node-appwrite";
|
2
|
-
import type { CollectionImportData } from "
|
3
|
-
import type { ConfigCollection } from "src/migrations/schema.js";
|
2
|
+
import type { CollectionImportData } from "../migrations/dataLoader.js";
|
4
3
|
export declare const toPascalCase: (str: string) => string;
|
5
4
|
export declare const toCamelCase: (str: string) => string;
|
6
5
|
export declare const ensureDirectoryExistence: (filePath: string) => true | undefined;
|
@@ -34,4 +33,4 @@ export declare const getFileViewUrl: (endpoint: string, projectId: string, bucke
|
|
34
33
|
* @return {string} The complete download URL for the file.
|
35
34
|
*/
|
36
35
|
export declare const getFileDownloadUrl: (endpoint: string, projectId: string, bucketId: string, fileId: string, jwt?: Models.Jwt) => string;
|
37
|
-
export declare const finalizeByAttributeMap: (appwriteFolderPath: string, collection:
|
36
|
+
export declare const finalizeByAttributeMap: (appwriteFolderPath: string, collection: Collections, item: CollectionImportData["data"][number]) => Promise<any>;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { type AppwriteConfig } from "appwrite-utils";
|
2
|
+
/**
|
3
|
+
* Recursively searches for a file named 'appwriteConfig.ts' starting from the given directory.
|
4
|
+
* @param dir The directory to start the search from.
|
5
|
+
* @returns The path to the file if found, or null if not found.
|
6
|
+
*/
|
7
|
+
export declare const findAppwriteConfig: (dir: string) => string | null;
|
8
|
+
/**
|
9
|
+
* Loads the Appwrite configuration and all collection configurations from a specified directory.
|
10
|
+
* @param configDir The directory containing the appwriteConfig.ts and collections folder.
|
11
|
+
* @returns The loaded Appwrite configuration including collections.
|
12
|
+
*/
|
13
|
+
export declare const loadConfig: (configDir: string) => Promise<AppwriteConfig>;
|