appwrite-utils-cli 0.0.262 → 0.0.264

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.
@@ -1,4 +1,4 @@
1
- import { ID, IndexType } from "node-appwrite";
1
+ import { ID, IndexType, Permission } from "node-appwrite";
2
2
  import { z } from "zod";
3
3
 
4
4
  const stringAttributeSchema = z.object({
@@ -376,6 +376,8 @@ export const indexSchema = z.object({
376
376
  orders: z.array(z.string()).optional(),
377
377
  });
378
378
 
379
+ export const indexesSchema = z.array(indexSchema);
380
+
379
381
  export type Index = z.infer<typeof indexSchema>;
380
382
 
381
383
  export const AttributeMappingsSchema = z.array(
@@ -510,12 +512,45 @@ export const importDefSchemas = z
510
512
  .default([])
511
513
  .describe("The import definitions for the database");
512
514
 
515
+ export const permissionSchema = z
516
+ .object({
517
+ permission: z.string(),
518
+ target: z.string(),
519
+ })
520
+ .or(
521
+ z.string().transform((val) => {
522
+ const trimmedVal = val.trim();
523
+ // Adjusted regex to match double quotes
524
+ const match = trimmedVal.match(/^(\w+)\("([^"]+)"\)$/);
525
+ if (!match) {
526
+ throw new Error(`Invalid permission format: ${trimmedVal}`);
527
+ }
528
+ return {
529
+ permission: match[1],
530
+ target: match[2],
531
+ };
532
+ })
533
+ );
534
+
535
+ export const permissionsSchema = z.array(permissionSchema).optional();
536
+
537
+ export const attributesSchema = z.array(attributeSchema).default([]);
538
+
513
539
  export const collectionSchema = z.object({
540
+ name: z.string().describe("The name of the collection"),
514
541
  $id: z
515
542
  .string()
516
543
  .optional()
517
544
  .default(ID.unique())
518
545
  .describe("The ID of the collection, auto generated if not provided"),
546
+ enabled: z
547
+ .boolean()
548
+ .default(true)
549
+ .describe("Whether the collection is enabled or not"),
550
+ documentSecurity: z
551
+ .boolean()
552
+ .default(false)
553
+ .describe("Whether document security is enabled or not"),
519
554
  $createdAt: z.string(),
520
555
  $updatedAt: z.string(),
521
556
  $permissions: z
@@ -527,19 +562,6 @@ export const collectionSchema = z.object({
527
562
  )
528
563
  .default([])
529
564
  .describe("The permissions of the collection"),
530
- databaseId: z
531
- .string()
532
- .optional()
533
- .describe("The ID of the database the collection belongs to"),
534
- name: z.string().describe("The name of the collection"),
535
- enabled: z
536
- .boolean()
537
- .default(true)
538
- .describe("Whether the collection is enabled or not"),
539
- documentSecurity: z
540
- .boolean()
541
- .default(false)
542
- .describe("Whether document security is enabled or not"),
543
565
  attributes: z
544
566
  .array(attributeSchema)
545
567
  .default([])
@@ -549,6 +571,10 @@ export const collectionSchema = z.object({
549
571
  .default([])
550
572
  .describe("The indexes of the collection"),
551
573
  importDefs: importDefSchemas,
574
+ databaseId: z
575
+ .string()
576
+ .optional()
577
+ .describe("The ID of the database the collection belongs to"),
552
578
  });
553
579
 
554
580
  export const CollectionCreateSchema = collectionSchema.omit({
@@ -7,6 +7,8 @@ import type {
7
7
  import { z } from "zod";
8
8
  import fs from "fs";
9
9
  import path from "path";
10
+ import { dump } from "js-yaml";
11
+ import { getDatabaseFromConfig } from "./afterImportActions.js";
10
12
 
11
13
  interface RelationshipDetail {
12
14
  parentCollection: string;
@@ -28,6 +30,16 @@ export class SchemaGenerator {
28
30
  this.extractRelationships();
29
31
  }
30
32
 
33
+ public updateYamlSchemas(): void {
34
+ // Output this.config to a YAML file at appwriteFolderPath/appwriteConfig.yaml
35
+ let finalConfig = this.config;
36
+ finalConfig.appwriteClient = null;
37
+ const yamlConfig = finalConfig;
38
+ const yamlPath = path.join(this.appwriteFolderPath, "appwriteConfig.yaml");
39
+ fs.writeFileSync(yamlPath, dump(yamlConfig), { encoding: "utf-8" });
40
+ console.log(`YAML written to ${yamlPath}`);
41
+ }
42
+
31
43
  private extractRelationships(): void {
32
44
  this.config.collections.forEach((collection) => {
33
45
  collection.attributes.forEach((attr) => {
@@ -63,6 +75,9 @@ export class SchemaGenerator {
63
75
  isArrayParent,
64
76
  isArrayChild
65
77
  );
78
+ console.log(
79
+ `Extracted relationship: ${attr.key}\n\t${collection.name} -> ${relationshipAttr.relatedCollection}, databaseId: ${collection.databaseId}`
80
+ );
66
81
  }
67
82
  });
68
83
  });
@@ -122,22 +137,35 @@ export class SchemaGenerator {
122
137
 
123
138
  // Use the relationshipMap to find related collections
124
139
  const relationshipDetails = this.relationshipMap.get(name) || [];
125
- const relatedCollections = relationshipDetails.map((detail) => {
126
- const relatedCollectionName = detail.isChild
127
- ? detail.parentCollection
128
- : detail.childCollection;
129
- const key = detail.isChild ? detail.childKey : detail.parentKey;
130
- const isArray = detail.isArray ? "array" : "";
131
- return [relatedCollectionName, key, isArray];
132
- });
140
+ const relatedCollections = relationshipDetails
141
+ .filter((detail, index, self) => {
142
+ const uniqueKey = `${detail.parentCollection}-${detail.childCollection}-${detail.parentKey}-${detail.childKey}`;
143
+ return (
144
+ index ===
145
+ self.findIndex(
146
+ (obj) =>
147
+ `${obj.parentCollection}-${obj.childCollection}-${obj.parentKey}-${obj.childKey}` ===
148
+ uniqueKey
149
+ )
150
+ );
151
+ })
152
+ .map((detail) => {
153
+ const relatedCollectionName = detail.isChild
154
+ ? detail.parentCollection
155
+ : detail.childCollection;
156
+ const key = detail.isChild ? detail.childKey : detail.parentKey;
157
+ const isArray = detail.isArray ? "array" : "";
158
+ return [relatedCollectionName, key, isArray];
159
+ });
133
160
 
134
161
  let relatedTypes = "";
135
162
  let relatedTypesLazy = "";
136
163
  let curNum = 0;
137
164
  let maxNum = relatedCollections.length;
138
165
  relatedCollections.forEach((relatedCollection) => {
139
- const relatedPascalName = toPascalCase(relatedCollection[0]);
140
- const relatedCamelName = toCamelCase(relatedCollection[0]);
166
+ console.log(relatedCollection);
167
+ let relatedPascalName = toPascalCase(relatedCollection[0]);
168
+ let relatedCamelName = toCamelCase(relatedCollection[0]);
141
169
  curNum++;
142
170
  let endNameTypes = relatedPascalName;
143
171
  let endNameLazy = `${relatedPascalName}Schema`;
@@ -207,10 +235,18 @@ export class SchemaGenerator {
207
235
  case "integer":
208
236
  baseSchemaCode = "z.number().int()";
209
237
  if (attribute.min !== undefined) {
210
- baseSchemaCode += `.min(${attribute.min}, "Minimum value of ${attribute.min} not met")`;
238
+ if (BigInt(attribute.min) === BigInt(-9223372036854776000)) {
239
+ delete attribute.min;
240
+ } else {
241
+ baseSchemaCode += `.min(${attribute.min}, "Minimum value of ${attribute.min} not met")`;
242
+ }
211
243
  }
212
244
  if (attribute.max !== undefined) {
213
- baseSchemaCode += `.max(${attribute.max}, "Maximum value of ${attribute.max} exceeded")`;
245
+ if (BigInt(attribute.max) === BigInt(9223372036854776000)) {
246
+ delete attribute.max;
247
+ } else {
248
+ baseSchemaCode += `.max(${attribute.max}, "Maximum value of ${attribute.max} exceeded")`;
249
+ }
214
250
  }
215
251
  if (attribute.xdefault !== undefined) {
216
252
  baseSchemaCode += `.default(${attribute.xdefault})`;
@@ -23,6 +23,7 @@ import {
23
23
  } from "./migrations/validationRules.js";
24
24
  import { ImportController } from "./migrations/importController.js";
25
25
  import _ from "lodash";
26
+ import { AppwriteToX } from "./migrations/appwriteToX.js";
26
27
 
27
28
  async function loadConfig(configPath: string) {
28
29
  if (!fs.existsSync(configPath)) {
@@ -35,6 +36,7 @@ async function loadConfig(configPath: string) {
35
36
  }
36
37
 
37
38
  export interface SetupOptions {
39
+ sync: boolean;
38
40
  runProd: boolean;
39
41
  runStaging: boolean;
40
42
  runDev: boolean;
@@ -127,6 +129,13 @@ export class UtilsController {
127
129
  throw new Error("Database or storage not initialized");
128
130
  }
129
131
 
132
+ if (options.sync) {
133
+ console.log("Starting synchronization with server...");
134
+ const appwriteToX = new AppwriteToX(this.config, this.appwriteFolderPath);
135
+ await appwriteToX.toSchemas();
136
+ console.log("Synchronization complete, YAML and Schemas updated");
137
+ }
138
+
130
139
  // Start the setup
131
140
  console.log(
132
141
  "Starting setup, this step sets up migrations, runs backup, wipes databases, and updates schemas (depending on your options)..."