appwrite-cli 12.0.1 → 13.0.0-rc.1

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.
Files changed (283) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +2 -2
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +145 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/client.d.ts +89 -0
  8. package/dist/lib/client.d.ts.map +1 -0
  9. package/dist/lib/client.js +227 -0
  10. package/dist/lib/client.js.map +1 -0
  11. package/dist/lib/commands/account.d.ts +379 -0
  12. package/dist/lib/commands/account.d.ts.map +1 -0
  13. package/dist/lib/commands/account.js +1228 -0
  14. package/dist/lib/commands/account.js.map +1 -0
  15. package/dist/lib/commands/console.d.ts +20 -0
  16. package/dist/lib/commands/console.d.ts.map +1 -0
  17. package/dist/lib/commands/console.js +78 -0
  18. package/dist/lib/commands/console.js.map +1 -0
  19. package/dist/lib/commands/databases.d.ts +732 -0
  20. package/dist/lib/commands/databases.d.ts.map +1 -0
  21. package/dist/lib/commands/databases.js +2196 -0
  22. package/dist/lib/commands/databases.js.map +1 -0
  23. package/dist/lib/commands/functions.d.ts +310 -0
  24. package/dist/lib/commands/functions.d.ts.map +1 -0
  25. package/dist/lib/commands/functions.js +1100 -0
  26. package/dist/lib/commands/functions.js.map +1 -0
  27. package/dist/lib/commands/generic.d.ts +17 -0
  28. package/dist/lib/commands/generic.d.ts.map +1 -0
  29. package/dist/lib/commands/generic.js +279 -0
  30. package/dist/lib/commands/generic.js.map +1 -0
  31. package/dist/lib/commands/graphql.d.ts +19 -0
  32. package/dist/lib/commands/graphql.d.ts.map +1 -0
  33. package/dist/lib/commands/graphql.js +77 -0
  34. package/dist/lib/commands/graphql.js.map +1 -0
  35. package/dist/lib/commands/health.d.ts +153 -0
  36. package/dist/lib/commands/health.d.ts.map +1 -0
  37. package/dist/lib/commands/health.js +464 -0
  38. package/dist/lib/commands/health.js.map +1 -0
  39. package/dist/lib/commands/init.d.ts +3 -0
  40. package/dist/lib/commands/init.d.ts.map +1 -0
  41. package/dist/lib/commands/init.js +518 -0
  42. package/dist/lib/commands/init.js.map +1 -0
  43. package/dist/lib/commands/locale.d.ts +53 -0
  44. package/dist/lib/commands/locale.d.ts.map +1 -0
  45. package/dist/lib/commands/locale.js +165 -0
  46. package/dist/lib/commands/locale.js.map +1 -0
  47. package/dist/lib/commands/messaging.d.ts +588 -0
  48. package/dist/lib/commands/messaging.d.ts.map +1 -0
  49. package/dist/lib/commands/messaging.js +2042 -0
  50. package/dist/lib/commands/messaging.js.map +1 -0
  51. package/dist/lib/commands/migrations.d.ts +150 -0
  52. package/dist/lib/commands/migrations.d.ts.map +1 -0
  53. package/dist/lib/commands/migrations.js +524 -0
  54. package/dist/lib/commands/migrations.js.map +1 -0
  55. package/dist/lib/commands/organizations.d.ts +11 -0
  56. package/dist/lib/commands/organizations.d.ts.map +1 -0
  57. package/dist/lib/commands/organizations.js +31 -0
  58. package/dist/lib/commands/organizations.js.map +1 -0
  59. package/dist/lib/commands/project.d.ts +53 -0
  60. package/dist/lib/commands/project.d.ts.map +1 -0
  61. package/dist/lib/commands/project.js +176 -0
  62. package/dist/lib/commands/project.js.map +1 -0
  63. package/dist/lib/commands/projects.d.ts +516 -0
  64. package/dist/lib/commands/projects.d.ts.map +1 -0
  65. package/dist/lib/commands/projects.js +1590 -0
  66. package/dist/lib/commands/projects.js.map +1 -0
  67. package/dist/lib/commands/proxy.d.ts +71 -0
  68. package/dist/lib/commands/proxy.d.ts.map +1 -0
  69. package/dist/lib/commands/proxy.js +240 -0
  70. package/dist/lib/commands/proxy.js.map +1 -0
  71. package/dist/lib/commands/pull.d.ts +8 -0
  72. package/dist/lib/commands/pull.d.ts.map +1 -0
  73. package/dist/lib/commands/pull.js +455 -0
  74. package/dist/lib/commands/pull.js.map +1 -0
  75. package/dist/lib/commands/push.d.ts +4 -0
  76. package/dist/lib/commands/push.d.ts.map +1 -0
  77. package/dist/lib/commands/push.js +2154 -0
  78. package/dist/lib/commands/push.js.map +1 -0
  79. package/dist/lib/commands/run.d.ts +3 -0
  80. package/dist/lib/commands/run.d.ts.map +1 -0
  81. package/dist/lib/commands/run.js +287 -0
  82. package/dist/lib/commands/run.js.map +1 -0
  83. package/dist/lib/commands/sites.d.ts +296 -0
  84. package/dist/lib/commands/sites.d.ts.map +1 -0
  85. package/dist/lib/commands/sites.js +1046 -0
  86. package/dist/lib/commands/sites.js.map +1 -0
  87. package/dist/lib/commands/storage.d.ts +170 -0
  88. package/dist/lib/commands/storage.d.ts.map +1 -0
  89. package/dist/lib/commands/storage.js +651 -0
  90. package/dist/lib/commands/storage.js.map +1 -0
  91. package/dist/lib/commands/tables-db.d.ts +728 -0
  92. package/dist/lib/commands/tables-db.d.ts.map +1 -0
  93. package/dist/lib/commands/tables-db.js +2198 -0
  94. package/dist/lib/commands/tables-db.js.map +1 -0
  95. package/dist/lib/commands/teams.d.ts +129 -0
  96. package/dist/lib/commands/teams.d.ts.map +1 -0
  97. package/dist/lib/commands/teams.js +403 -0
  98. package/dist/lib/commands/teams.js.map +1 -0
  99. package/dist/lib/commands/tokens.d.ts +48 -0
  100. package/dist/lib/commands/tokens.d.ts.map +1 -0
  101. package/dist/lib/commands/tokens.js +156 -0
  102. package/dist/lib/commands/tokens.js.map +1 -0
  103. package/dist/lib/commands/types.d.ts +3 -0
  104. package/dist/lib/commands/types.d.ts.map +1 -0
  105. package/dist/lib/commands/types.js +155 -0
  106. package/dist/lib/commands/types.js.map +1 -0
  107. package/dist/lib/commands/update.d.ts +3 -0
  108. package/dist/lib/commands/update.d.ts.map +1 -0
  109. package/dist/lib/commands/update.js +202 -0
  110. package/dist/lib/commands/update.js.map +1 -0
  111. package/dist/lib/commands/users.d.ts +382 -0
  112. package/dist/lib/commands/users.d.ts.map +1 -0
  113. package/dist/lib/commands/users.js +1195 -0
  114. package/dist/lib/commands/users.js.map +1 -0
  115. package/dist/lib/commands/vcs.d.ts +92 -0
  116. package/dist/lib/commands/vcs.d.ts.map +1 -0
  117. package/dist/lib/commands/vcs.js +276 -0
  118. package/dist/lib/commands/vcs.js.map +1 -0
  119. package/dist/lib/config.d.ts +118 -0
  120. package/dist/lib/config.d.ts.map +1 -0
  121. package/{lib → dist/lib}/config.js +204 -325
  122. package/dist/lib/config.js.map +1 -0
  123. package/dist/lib/emulation/docker.d.ts +15 -0
  124. package/dist/lib/emulation/docker.d.ts.map +1 -0
  125. package/dist/lib/emulation/docker.js +220 -0
  126. package/dist/lib/emulation/docker.js.map +1 -0
  127. package/dist/lib/emulation/utils.d.ts +29 -0
  128. package/dist/lib/emulation/utils.d.ts.map +1 -0
  129. package/dist/lib/emulation/utils.js +168 -0
  130. package/dist/lib/emulation/utils.js.map +1 -0
  131. package/dist/lib/exception.d.ts +8 -0
  132. package/dist/lib/exception.d.ts.map +1 -0
  133. package/dist/lib/exception.js +16 -0
  134. package/dist/lib/exception.js.map +1 -0
  135. package/dist/lib/id.d.ts +7 -0
  136. package/dist/lib/id.d.ts.map +1 -0
  137. package/dist/lib/id.js +32 -0
  138. package/dist/lib/id.js.map +1 -0
  139. package/dist/lib/paginate.d.ts +10 -0
  140. package/dist/lib/paginate.d.ts.map +1 -0
  141. package/{lib → dist/lib}/paginate.js +9 -15
  142. package/dist/lib/paginate.js.map +1 -0
  143. package/dist/lib/parser.d.ts +18 -0
  144. package/dist/lib/parser.d.ts.map +1 -0
  145. package/dist/lib/parser.js +237 -0
  146. package/dist/lib/parser.js.map +1 -0
  147. package/dist/lib/questions.d.ts +59 -0
  148. package/dist/lib/questions.d.ts.map +1 -0
  149. package/dist/lib/questions.js +995 -0
  150. package/dist/lib/questions.js.map +1 -0
  151. package/dist/lib/sdks.d.ts +4 -0
  152. package/dist/lib/sdks.d.ts.map +1 -0
  153. package/dist/lib/sdks.js +51 -0
  154. package/dist/lib/sdks.js.map +1 -0
  155. package/dist/lib/spinner.d.ts +28 -0
  156. package/dist/lib/spinner.d.ts.map +1 -0
  157. package/{lib → dist/lib}/spinner.js +34 -45
  158. package/dist/lib/spinner.js.map +1 -0
  159. package/dist/lib/type-generation/attribute.d.ts +17 -0
  160. package/dist/lib/type-generation/attribute.d.ts.map +1 -0
  161. package/dist/lib/type-generation/attribute.js +19 -0
  162. package/dist/lib/type-generation/attribute.js.map +1 -0
  163. package/dist/lib/type-generation/languages/csharp.d.ts +7 -0
  164. package/dist/lib/type-generation/languages/csharp.d.ts.map +1 -0
  165. package/dist/lib/type-generation/languages/csharp.js +180 -0
  166. package/dist/lib/type-generation/languages/csharp.js.map +1 -0
  167. package/dist/lib/type-generation/languages/dart.d.ts +8 -0
  168. package/dist/lib/type-generation/languages/dart.d.ts.map +1 -0
  169. package/dist/lib/type-generation/languages/dart.js +197 -0
  170. package/dist/lib/type-generation/languages/dart.js.map +1 -0
  171. package/dist/lib/type-generation/languages/java.d.ts +7 -0
  172. package/dist/lib/type-generation/languages/java.d.ts.map +1 -0
  173. package/dist/lib/type-generation/languages/java.js +140 -0
  174. package/dist/lib/type-generation/languages/java.js.map +1 -0
  175. package/dist/lib/type-generation/languages/javascript.d.ts +9 -0
  176. package/dist/lib/type-generation/languages/javascript.d.ts.map +1 -0
  177. package/dist/lib/type-generation/languages/javascript.js +108 -0
  178. package/dist/lib/type-generation/languages/javascript.js.map +1 -0
  179. package/dist/lib/type-generation/languages/kotlin.d.ts +7 -0
  180. package/dist/lib/type-generation/languages/kotlin.d.ts.map +1 -0
  181. package/dist/lib/type-generation/languages/kotlin.js +95 -0
  182. package/dist/lib/type-generation/languages/kotlin.js.map +1 -0
  183. package/dist/lib/type-generation/languages/language.d.ts +43 -0
  184. package/dist/lib/type-generation/languages/language.d.ts.map +1 -0
  185. package/dist/lib/type-generation/languages/language.js +82 -0
  186. package/dist/lib/type-generation/languages/language.js.map +1 -0
  187. package/dist/lib/type-generation/languages/php.d.ts +7 -0
  188. package/dist/lib/type-generation/languages/php.d.ts.map +1 -0
  189. package/dist/lib/type-generation/languages/php.js +116 -0
  190. package/dist/lib/type-generation/languages/php.js.map +1 -0
  191. package/dist/lib/type-generation/languages/swift.d.ts +7 -0
  192. package/dist/lib/type-generation/languages/swift.d.ts.map +1 -0
  193. package/dist/lib/type-generation/languages/swift.js +179 -0
  194. package/dist/lib/type-generation/languages/swift.js.map +1 -0
  195. package/dist/lib/type-generation/languages/typescript.d.ts +9 -0
  196. package/dist/lib/type-generation/languages/typescript.d.ts.map +1 -0
  197. package/dist/lib/type-generation/languages/typescript.js +112 -0
  198. package/dist/lib/type-generation/languages/typescript.js.map +1 -0
  199. package/dist/lib/types.d.ts +133 -0
  200. package/dist/lib/types.d.ts.map +1 -0
  201. package/dist/lib/types.js +3 -0
  202. package/dist/lib/types.js.map +1 -0
  203. package/dist/lib/utils.d.ts +15 -0
  204. package/dist/lib/utils.d.ts.map +1 -0
  205. package/{lib → dist/lib}/utils.js +74 -120
  206. package/dist/lib/utils.js.map +1 -0
  207. package/dist/lib/validations.d.ts +2 -0
  208. package/dist/lib/validations.d.ts.map +1 -0
  209. package/dist/lib/validations.js +20 -0
  210. package/dist/lib/validations.js.map +1 -0
  211. package/docs/examples/databases/upsert-document.md +1 -2
  212. package/index.ts +152 -0
  213. package/install.ps1 +2 -5
  214. package/install.sh +1 -2
  215. package/lib/client.ts +259 -0
  216. package/lib/commands/{account.js → account.ts} +408 -662
  217. package/lib/commands/console.ts +112 -0
  218. package/lib/commands/{databases.js → databases.ts} +818 -1136
  219. package/lib/commands/{functions.js → functions.ts} +381 -526
  220. package/lib/commands/{generic.js → generic.ts} +47 -39
  221. package/lib/commands/graphql.ts +110 -0
  222. package/lib/commands/{health.js → health.ts} +168 -284
  223. package/lib/commands/{init.js → init.ts} +68 -66
  224. package/lib/commands/{locale.js → locale.ts} +75 -121
  225. package/lib/commands/{messaging.js → messaging.ts} +699 -937
  226. package/lib/commands/{migrations.js → migrations.ts} +182 -258
  227. package/lib/commands/organizations.ts +46 -0
  228. package/lib/commands/{project.js → project.ts} +75 -111
  229. package/lib/commands/{projects.js → projects.ts} +587 -843
  230. package/lib/commands/{proxy.js → proxy.ts} +94 -140
  231. package/lib/commands/{pull.js → pull.ts} +54 -44
  232. package/lib/commands/{push.js → push.ts} +235 -191
  233. package/lib/commands/{run.js → run.ts} +61 -55
  234. package/lib/commands/{sites.js → sites.ts} +364 -504
  235. package/lib/commands/{storage.js → storage.ts} +216 -292
  236. package/lib/commands/{tables-db.js → tables-db.ts} +817 -1126
  237. package/lib/commands/{teams.js → teams.ts} +162 -236
  238. package/lib/commands/{tokens.js → tokens.ts} +70 -99
  239. package/lib/commands/{types.js → types.ts} +37 -35
  240. package/lib/commands/{update.js → update.ts} +25 -27
  241. package/lib/commands/{users.js → users.ts} +426 -644
  242. package/lib/commands/{vcs.js → vcs.ts} +118 -174
  243. package/lib/config.ts +854 -0
  244. package/lib/emulation/{docker.js → docker.ts} +39 -38
  245. package/lib/emulation/utils.ts +193 -0
  246. package/lib/exception.ts +20 -0
  247. package/lib/{id.js → id.ts} +5 -5
  248. package/lib/paginate.ts +63 -0
  249. package/lib/parser.ts +238 -0
  250. package/lib/{questions.js → questions.ts} +148 -156
  251. package/lib/sdks.ts +55 -0
  252. package/lib/spinner.ts +118 -0
  253. package/lib/type-generation/attribute.ts +17 -0
  254. package/lib/type-generation/languages/{csharp.js → csharp.ts} +59 -57
  255. package/lib/type-generation/languages/{dart.js → dart.ts} +91 -89
  256. package/lib/type-generation/languages/{java.js → java.ts} +58 -56
  257. package/lib/type-generation/languages/javascript.ts +111 -0
  258. package/lib/type-generation/languages/kotlin.ts +96 -0
  259. package/lib/type-generation/languages/language.ts +119 -0
  260. package/lib/type-generation/languages/{php.js → php.ts} +55 -53
  261. package/lib/type-generation/languages/{swift.js → swift.ts} +59 -57
  262. package/lib/type-generation/languages/typescript.ts +116 -0
  263. package/lib/types.ts +150 -0
  264. package/lib/utils.ts +322 -0
  265. package/lib/validations.ts +17 -0
  266. package/package.json +21 -12
  267. package/scoop/appwrite.config.json +3 -3
  268. package/tsconfig.json +30 -0
  269. package/index.js +0 -147
  270. package/lib/client.js +0 -254
  271. package/lib/commands/console.js +0 -127
  272. package/lib/commands/graphql.js +0 -126
  273. package/lib/commands/organizations.js +0 -48
  274. package/lib/emulation/utils.js +0 -186
  275. package/lib/exception.js +0 -9
  276. package/lib/parser.js +0 -250
  277. package/lib/sdks.js +0 -60
  278. package/lib/type-generation/attribute.js +0 -19
  279. package/lib/type-generation/languages/javascript.js +0 -111
  280. package/lib/type-generation/languages/kotlin.js +0 -94
  281. package/lib/type-generation/languages/language.js +0 -125
  282. package/lib/type-generation/languages/typescript.js +0 -116
  283. package/lib/validations.js +0 -17
package/lib/config.ts ADDED
@@ -0,0 +1,854 @@
1
+ import os = require('os');
2
+ import fs = require('fs');
3
+ import _path = require('path');
4
+ import process = require('process');
5
+ import JSONbig = require('json-bigint');
6
+ import type { ConfigData, SessionData, GlobalConfigData, ProjectConfigData } from './types';
7
+
8
+ const JSONBigInt = JSONbig({ storeAsString: false });
9
+
10
+ const KeysVars = new Set(['key', 'value']);
11
+ const KeysSite = new Set(['path', '$id', 'name', 'enabled', 'logging', 'timeout', 'framework', 'buildRuntime', 'adapter', 'installCommand', 'buildCommand', 'outputDirectory', 'fallbackFile', 'specification', 'vars']);
12
+ const KeysFunction = new Set(['path', '$id', 'execute', 'name', 'enabled', 'logging', 'runtime', 'specification', 'scopes', 'events', 'schedule', 'timeout', 'entrypoint', 'commands', 'vars']);
13
+ const KeysDatabase = new Set(['$id', 'name', 'enabled']);
14
+ const KeysCollection = new Set(['$id', '$permissions', 'databaseId', 'name', 'enabled', 'documentSecurity', 'attributes', 'indexes']);
15
+ const KeysTable = new Set(['$id', '$permissions', 'databaseId', 'name', 'enabled', 'rowSecurity', 'columns', 'indexes']);
16
+ const KeysStorage = new Set(['$id', '$permissions', 'fileSecurity', 'name', 'enabled', 'maximumFileSize', 'allowedFileExtensions', 'compression', 'encryption', 'antivirus']);
17
+ const KeysTopics = new Set(['$id', 'name', 'subscribe']);
18
+ const KeysTeams = new Set(['$id', 'name']);
19
+ const KeysAttributes = new Set([
20
+ 'key',
21
+ 'type',
22
+ 'required',
23
+ 'array',
24
+ 'size',
25
+ 'default',
26
+ // integer and float
27
+ 'min',
28
+ 'max',
29
+ // email, enum, URL, IP, and datetime
30
+ 'format',
31
+ // enum
32
+ 'elements',
33
+ // relationship
34
+ 'relatedCollection',
35
+ 'relationType',
36
+ 'twoWay',
37
+ 'twoWayKey',
38
+ 'onDelete',
39
+ 'side',
40
+ // Indexes
41
+ 'attributes',
42
+ 'orders',
43
+ // Strings
44
+ 'encrypt',
45
+ ]);
46
+ const KeysColumns = new Set([
47
+ 'key',
48
+ 'type',
49
+ 'required',
50
+ 'array',
51
+ 'size',
52
+ 'default',
53
+ // integer and float
54
+ 'min',
55
+ 'max',
56
+ // email, enum, URL, IP, and datetime
57
+ 'format',
58
+ // enum
59
+ 'elements',
60
+ // relationship
61
+ 'relatedTable',
62
+ 'relationType',
63
+ 'twoWay',
64
+ 'twoWayKey',
65
+ 'onDelete',
66
+ 'side',
67
+ // Indexes
68
+ 'columns',
69
+ 'orders',
70
+ // Strings
71
+ 'encrypt',
72
+ ]);
73
+ const KeyIndexes = new Set(['key', 'type', 'status', 'attributes', 'orders']);
74
+ const KeyIndexesColumns = new Set(['key', 'type', 'status', 'columns', 'orders']);
75
+
76
+ function whitelistKeys<T = any>(
77
+ value: T,
78
+ keys: Set<string>,
79
+ nestedKeys: Record<string, Set<string>> = {}
80
+ ): T {
81
+ if (Array.isArray(value)) {
82
+ const newValue: any[] = [];
83
+
84
+ for (const item of value) {
85
+ newValue.push(whitelistKeys(item, keys, nestedKeys));
86
+ }
87
+
88
+ return newValue as T;
89
+ }
90
+
91
+ const newValue: Record<string, any> = {};
92
+ Object.keys(value as any).forEach((key) => {
93
+ if (keys.has(key)) {
94
+ if (nestedKeys[key]) {
95
+ newValue[key] = whitelistKeys((value as any)[key], nestedKeys[key]);
96
+ } else {
97
+ newValue[key] = (value as any)[key];
98
+ }
99
+ }
100
+ });
101
+ return newValue as T;
102
+ }
103
+
104
+ class Config<T extends ConfigData = ConfigData> {
105
+ protected path: string;
106
+ protected data: T;
107
+
108
+ constructor(path: string) {
109
+ this.path = path;
110
+ this.data = {} as T;
111
+ this.read();
112
+ }
113
+
114
+ read(): void {
115
+ try {
116
+ const file = fs.readFileSync(this.path).toString();
117
+ this.data = JSONBigInt.parse(file);
118
+ } catch (e) {
119
+ this.data = {} as T;
120
+ }
121
+ }
122
+
123
+ write(): void {
124
+ const dir = _path.dirname(this.path);
125
+ if (!fs.existsSync(dir)) {
126
+ fs.mkdirSync(dir, { recursive: true });
127
+ }
128
+ fs.writeFileSync(this.path, JSONBigInt.stringify(this.data, null, 4), { mode: 0o600 });
129
+ }
130
+
131
+ get<K extends keyof T>(key: K): T[K];
132
+ get(key: string): any {
133
+ return this.data[key as keyof T];
134
+ }
135
+
136
+ set<K extends keyof T>(key: K, value: T[K]): void;
137
+ set(key: string, value: any): void {
138
+ (this.data as any)[key] = value;
139
+ this.write();
140
+ }
141
+
142
+ delete(key: string): void {
143
+ delete (this.data as any)[key];
144
+ this.write();
145
+ }
146
+
147
+ clear(): void {
148
+ this.data = {} as T;
149
+ this.write();
150
+ }
151
+
152
+ has(key: string): boolean {
153
+ return this.data[key as keyof T] !== undefined;
154
+ }
155
+
156
+ keys(): string[] {
157
+ return Object.keys(this.data);
158
+ }
159
+
160
+ values(): unknown[] {
161
+ return Object.values(this.data);
162
+ }
163
+
164
+ toString(): string {
165
+ return JSONBigInt.stringify(this.data, null, 4);
166
+ }
167
+
168
+ protected _getDBEntities(entityType: string): any[] {
169
+ if (!this.has(entityType)) {
170
+ return [];
171
+ }
172
+ return this.get(entityType);
173
+ }
174
+
175
+ protected _getDBEntity(entityType: string, $id: string): any {
176
+ if (!this.has(entityType)) {
177
+ return {};
178
+ }
179
+
180
+ const entities = this.get(entityType);
181
+ for (let i = 0; i < entities.length; i++) {
182
+ if (entities[i]['$id'] == $id) {
183
+ return entities[i];
184
+ }
185
+ }
186
+
187
+ return {};
188
+ }
189
+
190
+ protected _addDBEntity(
191
+ entityType: string,
192
+ props: any,
193
+ keysSet: Set<string>,
194
+ nestedKeys: Record<string, Set<string>> = {}
195
+ ): void {
196
+ props = whitelistKeys(props, keysSet, nestedKeys);
197
+
198
+ if (!this.has(entityType)) {
199
+ this.set(entityType, []);
200
+ }
201
+
202
+ const entities = this.get(entityType);
203
+ for (let i = 0; i < entities.length; i++) {
204
+ if (entities[i]['$id'] == props['$id']) {
205
+ entities[i] = props;
206
+ this.set(entityType, entities);
207
+ return;
208
+ }
209
+ }
210
+ entities.push(props);
211
+ this.set(entityType, entities);
212
+ }
213
+ }
214
+
215
+ class Local extends Config<ProjectConfigData> {
216
+ static CONFIG_FILE_PATH = 'appwrite.config.json';
217
+ static CONFIG_FILE_PATH_LEGACY = 'appwrite.json';
218
+ configDirectoryPath = '';
219
+
220
+ constructor(path: string = Local.CONFIG_FILE_PATH, legacyPath: string = Local.CONFIG_FILE_PATH_LEGACY) {
221
+ let absolutePath = Local.findConfigFile(path) || Local.findConfigFile(legacyPath);
222
+
223
+ if (!absolutePath) {
224
+ absolutePath = `${process.cwd()}/${path}`;
225
+ }
226
+
227
+ super(absolutePath);
228
+ this.configDirectoryPath = _path.dirname(absolutePath);
229
+ }
230
+
231
+ static findConfigFile(filename: string): string | null {
232
+ let currentPath = process.cwd();
233
+
234
+ while (true) {
235
+ const filePath = `${currentPath}/${filename}`;
236
+
237
+ if (fs.existsSync(filePath)) {
238
+ return filePath;
239
+ }
240
+
241
+ const parentDirectory = _path.dirname(currentPath);
242
+ if (parentDirectory === currentPath) {
243
+ break;
244
+ }
245
+ currentPath = parentDirectory;
246
+ }
247
+
248
+ return null;
249
+ }
250
+
251
+ getDirname(): string {
252
+ return _path.dirname(this.path);
253
+ }
254
+
255
+ getEndpoint(): string {
256
+ return this.get('endpoint' as any) || '';
257
+ }
258
+
259
+ setEndpoint(endpoint: string): void {
260
+ this.set('endpoint' as any, endpoint);
261
+ }
262
+
263
+ getSites(): any[] {
264
+ if (!this.has('sites')) {
265
+ return [];
266
+ }
267
+ return this.get('sites' as any);
268
+ }
269
+
270
+ getSite($id: string): any {
271
+ if (!this.has('sites')) {
272
+ return {};
273
+ }
274
+
275
+ const sites = this.get('sites' as any);
276
+ for (let i = 0; i < sites.length; i++) {
277
+ if (sites[i]['$id'] == $id) {
278
+ return sites[i];
279
+ }
280
+ }
281
+
282
+ return {};
283
+ }
284
+
285
+ addSite(props: any): void {
286
+ props = whitelistKeys(props, KeysSite, {
287
+ vars: KeysVars,
288
+ });
289
+
290
+ if (!this.has('sites')) {
291
+ this.set('sites' as any, []);
292
+ }
293
+
294
+ const sites = this.get('sites' as any);
295
+ for (let i = 0; i < sites.length; i++) {
296
+ if (sites[i]['$id'] == props['$id']) {
297
+ sites[i] = {
298
+ ...sites[i],
299
+ ...props,
300
+ };
301
+ this.set('sites' as any, sites);
302
+ return;
303
+ }
304
+ }
305
+
306
+ sites.push(props);
307
+ this.set('sites' as any, sites);
308
+ }
309
+
310
+ getFunctions(): any[] {
311
+ if (!this.has('functions')) {
312
+ return [];
313
+ }
314
+ return this.get('functions' as any);
315
+ }
316
+
317
+ getFunction($id: string): any {
318
+ if (!this.has('functions')) {
319
+ return {};
320
+ }
321
+
322
+ const functions = this.get('functions' as any);
323
+ for (let i = 0; i < functions.length; i++) {
324
+ if (functions[i]['$id'] == $id) {
325
+ return functions[i];
326
+ }
327
+ }
328
+
329
+ return {};
330
+ }
331
+
332
+ addFunction(props: any): void {
333
+ props = whitelistKeys(props, KeysFunction, {
334
+ vars: KeysVars,
335
+ });
336
+
337
+ if (!this.has('functions')) {
338
+ this.set('functions' as any, []);
339
+ }
340
+
341
+ const functions = this.get('functions' as any);
342
+ for (let i = 0; i < functions.length; i++) {
343
+ if (functions[i]['$id'] == props['$id']) {
344
+ functions[i] = {
345
+ ...functions[i],
346
+ ...props,
347
+ };
348
+ this.set('functions' as any, functions);
349
+ return;
350
+ }
351
+ }
352
+
353
+ functions.push(props);
354
+ this.set('functions' as any, functions);
355
+ }
356
+
357
+ getCollections(): any[] {
358
+ if (!this.has('collections')) {
359
+ return [];
360
+ }
361
+ return this.get('collections' as any);
362
+ }
363
+
364
+ getCollection($id: string): any {
365
+ if (!this.has('collections')) {
366
+ return {};
367
+ }
368
+
369
+ const collections = this.get('collections' as any);
370
+ for (let i = 0; i < collections.length; i++) {
371
+ if (collections[i]['$id'] == $id) {
372
+ return collections[i];
373
+ }
374
+ }
375
+
376
+ return {};
377
+ }
378
+
379
+ addCollection(props: any): void {
380
+ props = whitelistKeys(props, KeysCollection, {
381
+ attributes: KeysAttributes,
382
+ indexes: KeyIndexes,
383
+ });
384
+
385
+ if (!this.has('collections')) {
386
+ this.set('collections' as any, []);
387
+ }
388
+
389
+ const collections = this.get('collections' as any);
390
+ for (let i = 0; i < collections.length; i++) {
391
+ if (collections[i]['$id'] == props['$id'] && collections[i]['databaseId'] == props['databaseId']) {
392
+ collections[i] = props;
393
+ this.set('collections' as any, collections);
394
+ return;
395
+ }
396
+ }
397
+ collections.push(props);
398
+ this.set('collections' as any, collections);
399
+ }
400
+
401
+ getTables(): any[] {
402
+ if (!this.has('tables')) {
403
+ return [];
404
+ }
405
+ return this.get('tables' as any);
406
+ }
407
+
408
+ getTable($id: string): any {
409
+ if (!this.has('tables')) {
410
+ return {};
411
+ }
412
+
413
+ const tables = this.get('tables' as any);
414
+ for (let i = 0; i < tables.length; i++) {
415
+ if (tables[i]['$id'] == $id) {
416
+ return tables[i];
417
+ }
418
+ }
419
+
420
+ return {};
421
+ }
422
+
423
+ addTable(props: any): void {
424
+ props = whitelistKeys(props, KeysTable, {
425
+ columns: KeysColumns,
426
+ indexes: KeyIndexesColumns,
427
+ });
428
+
429
+ if (!this.has('tables')) {
430
+ this.set('tables' as any, []);
431
+ }
432
+
433
+ const tables = this.get('tables' as any);
434
+ for (let i = 0; i < tables.length; i++) {
435
+ if (tables[i]['$id'] == props['$id'] && tables[i]['databaseId'] == props['databaseId']) {
436
+ tables[i] = props;
437
+ this.set('tables' as any, tables);
438
+ return;
439
+ }
440
+ }
441
+ tables.push(props);
442
+ this.set('tables' as any, tables);
443
+ }
444
+
445
+ getBuckets(): any[] {
446
+ if (!this.has('buckets')) {
447
+ return [];
448
+ }
449
+ return this.get('buckets' as any);
450
+ }
451
+
452
+ getBucket($id: string): any {
453
+ if (!this.has('buckets')) {
454
+ return {};
455
+ }
456
+
457
+ const buckets = this.get('buckets' as any);
458
+ for (let i = 0; i < buckets.length; i++) {
459
+ if (buckets[i]['$id'] == $id) {
460
+ return buckets[i];
461
+ }
462
+ }
463
+
464
+ return {};
465
+ }
466
+
467
+ addBucket(props: any): void {
468
+ props = whitelistKeys(props, KeysStorage);
469
+
470
+ if (!this.has('buckets')) {
471
+ this.set('buckets' as any, []);
472
+ }
473
+
474
+ const buckets = this.get('buckets' as any);
475
+ for (let i = 0; i < buckets.length; i++) {
476
+ if (buckets[i]['$id'] == props['$id']) {
477
+ buckets[i] = props;
478
+ this.set('buckets' as any, buckets);
479
+ return;
480
+ }
481
+ }
482
+ buckets.push(props);
483
+ this.set('buckets' as any, buckets);
484
+ }
485
+
486
+ getMessagingTopics(): any[] {
487
+ if (!this.has('topics')) {
488
+ return [];
489
+ }
490
+ return this.get('topics' as any);
491
+ }
492
+
493
+ getMessagingTopic($id: string): any {
494
+ if (!this.has('topics')) {
495
+ return {};
496
+ }
497
+
498
+ const topic = this.get('topics' as any);
499
+ for (let i = 0; i < topic.length; i++) {
500
+ if (topic[i]['$id'] == $id) {
501
+ return topic[i];
502
+ }
503
+ }
504
+
505
+ return {};
506
+ }
507
+
508
+ addMessagingTopic(props: any): void {
509
+ props = whitelistKeys(props, KeysTopics);
510
+
511
+ if (!this.has('topics')) {
512
+ this.set('topics' as any, []);
513
+ }
514
+
515
+ const topics = this.get('topics' as any);
516
+ for (let i = 0; i < topics.length; i++) {
517
+ if (topics[i]['$id'] === props['$id']) {
518
+ topics[i] = props;
519
+ this.set('topics' as any, topics);
520
+ return;
521
+ }
522
+ }
523
+ topics.push(props);
524
+ this.set('topics' as any, topics);
525
+ }
526
+
527
+ getTablesDBs(): any[] {
528
+ return this._getDBEntities('tablesDB');
529
+ }
530
+
531
+ getTablesDB($id: string): any {
532
+ return this._getDBEntity('tablesDB', $id);
533
+ }
534
+
535
+ addTablesDB(props: any): void {
536
+ this._addDBEntity('tablesDB', props, KeysDatabase);
537
+ }
538
+
539
+ getDatabases(): any[] {
540
+ return this._getDBEntities('databases');
541
+ }
542
+
543
+ getDatabase($id: string): any {
544
+ return this._getDBEntity('databases', $id);
545
+ }
546
+
547
+ addDatabase(props: any): void {
548
+ this._addDBEntity('databases', props, KeysDatabase);
549
+ }
550
+
551
+ getTeams(): any[] {
552
+ if (!this.has('teams')) {
553
+ return [];
554
+ }
555
+ return this.get('teams' as any);
556
+ }
557
+
558
+ getTeam($id: string): any {
559
+ if (!this.has('teams')) {
560
+ return {};
561
+ }
562
+
563
+ const teams = this.get('teams' as any);
564
+ for (let i = 0; i < teams.length; i++) {
565
+ if (teams[i]['$id'] == $id) {
566
+ return teams[i];
567
+ }
568
+ }
569
+
570
+ return {};
571
+ }
572
+
573
+ addTeam(props: any): void {
574
+ props = whitelistKeys(props, KeysTeams);
575
+ if (!this.has('teams')) {
576
+ this.set('teams' as any, []);
577
+ }
578
+
579
+ const teams = this.get('teams' as any);
580
+ for (let i = 0; i < teams.length; i++) {
581
+ if (teams[i]['$id'] == props['$id']) {
582
+ teams[i] = props;
583
+ this.set('teams' as any, teams);
584
+ return;
585
+ }
586
+ }
587
+ teams.push(props);
588
+ this.set('teams' as any, teams);
589
+ }
590
+
591
+ getProject(): { projectId?: string; projectName?: string; projectSettings?: any } {
592
+ if (!this.has('projectId')) {
593
+ return {};
594
+ }
595
+
596
+ return {
597
+ projectId: this.get('projectId' as any),
598
+ projectName: this.get('projectName' as any),
599
+ projectSettings: this.get('settings' as any),
600
+ };
601
+ }
602
+
603
+ setProject(projectId: string, projectName: string = '', projectSettings: any = undefined): void {
604
+ this.set('projectId' as any, projectId);
605
+
606
+ if (projectName !== '') {
607
+ this.set('projectName' as any, projectName);
608
+ }
609
+
610
+ if (projectSettings === undefined) {
611
+ return;
612
+ }
613
+
614
+ this.set('settings' as any, this.createSettingsObject(projectSettings));
615
+ }
616
+
617
+ createSettingsObject(projectSettings: any): any {
618
+ return {
619
+ services: {
620
+ account: projectSettings.serviceStatusForAccount,
621
+ avatars: projectSettings.serviceStatusForAvatars,
622
+ databases: projectSettings.serviceStatusForDatabases,
623
+ locale: projectSettings.serviceStatusForLocale,
624
+ health: projectSettings.serviceStatusForHealth,
625
+ storage: projectSettings.serviceStatusForStorage,
626
+ teams: projectSettings.serviceStatusForTeams,
627
+ users: projectSettings.serviceStatusForUsers,
628
+ sites: projectSettings.serviceStatusForSites,
629
+ functions: projectSettings.serviceStatusForFunctions,
630
+ graphql: projectSettings.serviceStatusForGraphql,
631
+ messaging: projectSettings.serviceStatusForMessaging,
632
+ },
633
+ auth: {
634
+ methods: {
635
+ jwt: projectSettings.authJWT,
636
+ phone: projectSettings.authPhone,
637
+ invites: projectSettings.authInvites,
638
+ anonymous: projectSettings.authAnonymous,
639
+ 'email-otp': projectSettings.authEmailOtp,
640
+ 'magic-url': projectSettings.authUsersAuthMagicURL,
641
+ 'email-password': projectSettings.authEmailPassword,
642
+ },
643
+ security: {
644
+ duration: projectSettings.authDuration,
645
+ limit: projectSettings.authLimit,
646
+ sessionsLimit: projectSettings.authSessionsLimit,
647
+ passwordHistory: projectSettings.authPasswordHistory,
648
+ passwordDictionary: projectSettings.authPasswordDictionary,
649
+ personalDataCheck: projectSettings.authPersonalDataCheck,
650
+ sessionAlerts: projectSettings.authSessionAlerts,
651
+ mockNumbers: projectSettings.authMockNumbers,
652
+ },
653
+ },
654
+ };
655
+ }
656
+ }
657
+
658
+ class Global extends Config<GlobalConfigData> {
659
+ static CONFIG_FILE_PATH = '.appwrite/prefs.json';
660
+
661
+ static PREFERENCE_CURRENT = 'current';
662
+ static PREFERENCE_ENDPOINT = 'endpoint';
663
+ static PREFERENCE_EMAIL = 'email';
664
+ static PREFERENCE_SELF_SIGNED = 'selfSigned';
665
+ static PREFERENCE_COOKIE = 'cookie';
666
+ static PREFERENCE_PROJECT = 'project';
667
+ static PREFERENCE_KEY = 'key';
668
+ static PREFERENCE_LOCALE = 'locale';
669
+ static PREFERENCE_MODE = 'mode';
670
+
671
+ static IGNORE_ATTRIBUTES = [
672
+ Global.PREFERENCE_CURRENT,
673
+ Global.PREFERENCE_SELF_SIGNED,
674
+ Global.PREFERENCE_ENDPOINT,
675
+ Global.PREFERENCE_COOKIE,
676
+ Global.PREFERENCE_PROJECT,
677
+ Global.PREFERENCE_KEY,
678
+ Global.PREFERENCE_LOCALE,
679
+ Global.PREFERENCE_MODE,
680
+ ];
681
+
682
+ static MODE_ADMIN = 'admin';
683
+ static MODE_DEFAULT = 'default';
684
+
685
+ static PROJECT_CONSOLE = 'console';
686
+
687
+ constructor(path: string = Global.CONFIG_FILE_PATH) {
688
+ const homeDir = os.homedir();
689
+ super(`${homeDir}/${path}`);
690
+ }
691
+
692
+ getCurrentSession(): string {
693
+ if (!this.has(Global.PREFERENCE_CURRENT)) {
694
+ return '';
695
+ }
696
+ return this.get(Global.PREFERENCE_CURRENT as any);
697
+ }
698
+
699
+ setCurrentSession(session: string): void {
700
+ if (session !== undefined) {
701
+ this.set(Global.PREFERENCE_CURRENT as any, session);
702
+ }
703
+ }
704
+
705
+ getSessionIds(): string[] {
706
+ return Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key));
707
+ }
708
+
709
+ getSessions(): Array<{ id: string; endpoint: string; email: string }> {
710
+ const sessions = Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key));
711
+ const current = this.getCurrentSession();
712
+
713
+ const sessionMap = new Map<string, { id: string; endpoint: string; email: string }>();
714
+
715
+ sessions.forEach((sessionId) => {
716
+ const sessionData = (this.data as any)[sessionId];
717
+ const email = sessionData[Global.PREFERENCE_EMAIL];
718
+ const endpoint = sessionData[Global.PREFERENCE_ENDPOINT];
719
+ const key = `${email}|${endpoint}`;
720
+
721
+ if (sessionId === current || !sessionMap.has(key)) {
722
+ sessionMap.set(key, {
723
+ id: sessionId,
724
+ endpoint: sessionData[Global.PREFERENCE_ENDPOINT],
725
+ email: sessionData[Global.PREFERENCE_EMAIL],
726
+ });
727
+ }
728
+ });
729
+
730
+ return Array.from(sessionMap.values());
731
+ }
732
+
733
+ addSession(session: string, data: SessionData): void {
734
+ this.set(session as any, data as any);
735
+ }
736
+
737
+ removeSession(session: string): void {
738
+ this.delete(session);
739
+ }
740
+
741
+ getEmail(): string {
742
+ if (!this.hasFrom(Global.PREFERENCE_EMAIL)) {
743
+ return '';
744
+ }
745
+
746
+ return this.getFrom(Global.PREFERENCE_EMAIL);
747
+ }
748
+
749
+ setEmail(email: string): void {
750
+ this.setTo(Global.PREFERENCE_EMAIL, email);
751
+ }
752
+
753
+ getEndpoint(): string {
754
+ if (!this.hasFrom(Global.PREFERENCE_ENDPOINT)) {
755
+ return '';
756
+ }
757
+
758
+ return this.getFrom(Global.PREFERENCE_ENDPOINT);
759
+ }
760
+
761
+ setEndpoint(endpoint: string): void {
762
+ this.setTo(Global.PREFERENCE_ENDPOINT, endpoint);
763
+ }
764
+
765
+ getSelfSigned(): boolean {
766
+ if (!this.hasFrom(Global.PREFERENCE_SELF_SIGNED)) {
767
+ return false;
768
+ }
769
+ return this.getFrom(Global.PREFERENCE_SELF_SIGNED);
770
+ }
771
+
772
+ setSelfSigned(selfSigned: boolean): void {
773
+ this.setTo(Global.PREFERENCE_SELF_SIGNED, selfSigned);
774
+ }
775
+
776
+ getCookie(): string {
777
+ if (!this.hasFrom(Global.PREFERENCE_COOKIE)) {
778
+ return '';
779
+ }
780
+ return this.getFrom(Global.PREFERENCE_COOKIE);
781
+ }
782
+
783
+ setCookie(cookie: string): void {
784
+ this.setTo(Global.PREFERENCE_COOKIE, cookie);
785
+ }
786
+
787
+ getProject(): string {
788
+ if (!this.hasFrom(Global.PREFERENCE_PROJECT)) {
789
+ return '';
790
+ }
791
+ return this.getFrom(Global.PREFERENCE_PROJECT);
792
+ }
793
+
794
+ setProject(project: string): void {
795
+ this.setTo(Global.PREFERENCE_PROJECT, project);
796
+ }
797
+
798
+ getKey(): string {
799
+ if (!this.hasFrom(Global.PREFERENCE_KEY)) {
800
+ return '';
801
+ }
802
+ return this.getFrom(Global.PREFERENCE_KEY);
803
+ }
804
+
805
+ setKey(key: string): void {
806
+ this.setTo(Global.PREFERENCE_KEY, key);
807
+ }
808
+
809
+ hasFrom(key: string): boolean {
810
+ const current = this.getCurrentSession();
811
+
812
+ if (current) {
813
+ const config = this.get(current as any) ?? {};
814
+
815
+ return (config as any)[key] !== undefined;
816
+ }
817
+ return false;
818
+ }
819
+
820
+ getFrom(key: string): any {
821
+ const current = this.getCurrentSession();
822
+
823
+ if (current) {
824
+ const config = this.get(current as any) ?? {};
825
+
826
+ return (config as any)[key];
827
+ }
828
+ }
829
+
830
+ setTo(key: string, value: any): void {
831
+ const current = this.getCurrentSession();
832
+
833
+ if (current) {
834
+ const config = this.get(current as any);
835
+
836
+ (config as any)[key] = value;
837
+ this.write();
838
+ }
839
+ }
840
+ }
841
+
842
+ export const localConfig = new Local();
843
+ export const globalConfig = new Global();
844
+ export {
845
+ KeysAttributes,
846
+ KeysSite,
847
+ KeysFunction,
848
+ KeysTopics,
849
+ KeysStorage,
850
+ KeysTeams,
851
+ KeysCollection,
852
+ KeysTable,
853
+ whitelistKeys,
854
+ };