@@ -6,6 +6,8 @@ const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollect
6
6
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
7
7
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment } = require('./functions');
8
8
const {
9
+ databasesGet,
10
+ databasesCreate,
9
11
databasesCreateBooleanAttribute,
10
12
databasesGetCollection,
11
13
databasesCreateCollection,
@@ -23,19 +25,18 @@ const {
23
25
databasesDeleteIndex
24
26
} = require("./databases");
25
27
26
- // TODO: Add databaseId everywhere
27
-
28
28
const POOL_DEBOUNCE = 2000; // in milliseconds
29
29
const POOL_MAX_DEBOUNCES = 30;
30
30
31
31
const awaitPools = {
32
- wipeAttributes: async (collectionId, iteration = 1) => {
32
+ wipeAttributes: async (databaseId, collectionId, iteration = 1) => {
33
33
if (iteration > POOL_MAX_DEBOUNCES) {
34
34
return false;
35
35
}
36
36
37
37
// TODO: Pagination?
38
- const { attributes: remoteAttributes } = await databaseListAttributes({
38
+ const { attributes: remoteAttributes } = await databasesListAttributes({
39
+ databaseId,
39
40
collectionId,
40
41
limit: 100,
41
42
parseOutput: false
@@ -46,15 +47,16 @@ const awaitPools = {
46
47
}
47
48
48
49
await new Promise(resolve => setTimeout(resolve, POOL_DEBOUNCE));
49
- return await awaitPools.wipeAttributes(collectionId, iteration + 1);
50
+ return await awaitPools.wipeAttributes(databaseId, collectionId, iteration + 1);
50
51
},
51
- wipeIndexes: async (collectionId, iteration = 1) => {
52
+ wipeIndexes: async (databaseId, collectionId, iteration = 1) => {
52
53
if (iteration > POOL_MAX_DEBOUNCES) {
53
54
return false;
54
55
}
55
56
56
57
// TODO: Pagination?
57
- const { indexes: remoteIndexes } = await databaseListIndexes({
58
+ const { indexes: remoteIndexes } = await databasesListIndexes({
59
+ databaseId,
58
60
collectionId,
59
61
limit: 100,
60
62
parseOutput: false
@@ -65,15 +67,16 @@ const awaitPools = {
65
67
}
66
68
67
69
await new Promise(resolve => setTimeout(resolve, POOL_DEBOUNCE));
68
- return await awaitPools.wipeIndexes(collectionId, iteration + 1);
70
+ return await awaitPools.wipeIndexes(databaseId, collectionId, iteration + 1);
69
71
},
70
- expectAttributes: async (collectionId, attributeKeys, iteration = 1) => {
72
+ expectAttributes: async (databaseId, collectionId, attributeKeys, iteration = 1) => {
71
73
if (iteration > POOL_MAX_DEBOUNCES) {
72
74
return false;
73
75
}
74
76
75
77
// TODO: Pagination?
76
- const { attributes: remoteAttributes } = await databaseListAttributes({
78
+ const { attributes: remoteAttributes } = await databasesListAttributes({
79
+ databaseId,
77
80
collectionId,
78
81
limit: 100,
79
82
parseOutput: false
@@ -96,15 +99,16 @@ const awaitPools = {
96
99
}
97
100
98
101
await new Promise(resolve => setTimeout(resolve, POOL_DEBOUNCE));
99
- return await awaitPools.expectAttributes(collectionId, attributeKeys, iteration + 1);
102
+ return await awaitPools.expectAttributes(databaseId, collectionId, attributeKeys, iteration + 1);
100
103
},
101
- expectIndexes: async (collectionId, indexKeys, iteration = 1) => {
104
+ expectIndexes: async (databaseId, collectionId, indexKeys, iteration = 1) => {
102
105
if (iteration > POOL_MAX_DEBOUNCES) {
103
106
return false;
104
107
}
105
108
106
109
// TODO: Pagination?
107
- const { indexes: remoteIndexes } = await databaseListIndexes({
110
+ const { indexes: remoteIndexes } = await databasesListIndexes({
111
+ databaseId,
108
112
collectionId,
109
113
limit: 100,
110
114
parseOutput: false
@@ -127,7 +131,7 @@ const awaitPools = {
127
131
}
128
132
129
133
await new Promise(resolve => setTimeout(resolve, POOL_DEBOUNCE));
130
- return await awaitPools.expectIndexes(collectionId, indexKeys, iteration + 1);
134
+ return await awaitPools.expectIndexes(databaseId, collectionId, indexKeys, iteration + 1);
131
135
},
132
136
}
133
137
@@ -162,7 +166,12 @@ const deployFunction = async ({ functionId, all } = {}) => {
162
166
let functions = functionIds.map((id) => {
163
167
const functions = localConfig.getFunctions();
164
168
const func = functions.find((f) => f.$id === id);
165
- return JSONbig.stringify(func);
169
+
170
+ if(!func) {
171
+ throw new Error("Function '" + id + "' not found.")
172
+ }
173
+
174
+ return func;
166
175
});
167
176
168
177
for (let func of functions) {
@@ -244,12 +253,13 @@ const deployFunction = async ({ functionId, all } = {}) => {
244
253
}
245
254
}
246
255
247
- const createAttribute = async (collectionId, attribute) => {
256
+ const createAttribute = async (databaseId, collectionId, attribute) => {
248
257
switch (attribute.type) {
249
258
case 'string':
250
259
switch (attribute.format) {
251
260
case 'email':
252
- return await databaseCreateEmailAttribute({
261
+ return await databasesCreateEmailAttribute({
262
+ databaseId,
253
263
collectionId,
254
264
key: attribute.key,
255
265
required: attribute.required,
@@ -258,7 +268,8 @@ const createAttribute = async (collectionId, attribute) => {
258
268
parseOutput: false
259
269
})
260
270
case 'url':
261
- return await databaseCreateUrlAttribute({
271
+ return await databasesCreateUrlAttribute({
272
+ databaseId,
262
273
collectionId,
263
274
key: attribute.key,
264
275
required: attribute.required,
@@ -267,7 +278,8 @@ const createAttribute = async (collectionId, attribute) => {
267
278
parseOutput: false
268
279
})
269
280
case 'ip':
270
- return await databaseCreateIpAttribute({
281
+ return await databasesCreateIpAttribute({
282
+ databaseId,
271
283
collectionId,
272
284
key: attribute.key,
273
285
required: attribute.required,
@@ -276,7 +288,8 @@ const createAttribute = async (collectionId, attribute) => {
276
288
parseOutput: false
277
289
})
278
290
case 'enum':
279
- return await databaseCreateEnumAttribute({
291
+ return await databasesCreateEnumAttribute({
292
+ databaseId,
280
293
collectionId,
281
294
key: attribute.key,
282
295
elements: attribute.elements,
@@ -286,7 +299,8 @@ const createAttribute = async (collectionId, attribute) => {
286
299
parseOutput: false
287
300
})
288
301
default:
289
- return await databaseCreateStringAttribute({
302
+ return await databasesCreateStringAttribute({
303
+ databaseId,
290
304
collectionId,
291
305
key: attribute.key,
292
306
size: attribute.size,
@@ -298,7 +312,8 @@ const createAttribute = async (collectionId, attribute) => {
298
312
299
313
}
300
314
case 'integer':
301
- return await databaseCreateIntegerAttribute({
315
+ return await databasesCreateIntegerAttribute({
316
+ databaseId,
302
317
collectionId,
303
318
key: attribute.key,
304
319
required: attribute.required,
@@ -309,7 +324,8 @@ const createAttribute = async (collectionId, attribute) => {
309
324
parseOutput: false
310
325
})
311
326
case 'double':
312
- return databaseCreateFloatAttribute({
327
+ return databasesCreateFloatAttribute({
328
+ databaseId,
313
329
collectionId,
314
330
key: attribute.key,
315
331
required: attribute.required,
@@ -320,7 +336,9 @@ const createAttribute = async (collectionId, attribute) => {
320
336
parseOutput: false
321
337
})
322
338
case 'boolean':
323
- return databaseCreateBooleanAttribute({
339
+ return databasesCreateBooleanAttribute({
340
+ databaseId,
341
+ databaseId,
324
342
collectionId,
325
343
key: attribute.key,
326
344
required: attribute.required,
@@ -331,15 +349,55 @@ const createAttribute = async (collectionId, attribute) => {
331
349
}
332
350
}
333
351
334
- const deployCollection = async () => {
352
+ const deployCollection = async ({ all } = {} ) => {
335
353
let response = {};
336
- let answers = await inquirer.prompt(questionsDeployCollections[0])
337
- let collections = answers.collections.map((collection) => JSONbig.parse(collection));
354
+
355
+ let collectionIds = [];
356
+ const configCollections = localConfig.getCollections();
357
+
358
+ if(all) {
359
+ if (configCollections.length === 0) {
360
+ throw new Error("No collections found in the current directory. Run `{{ language .params .executableName }} init collection` to fetch all your collections.");
361
+ }
362
+ collectionIds.push(...configCollections.map((c) => c.$id));
363
+ }
364
+
365
+ if(collectionIds.length < = 0) {
366
+ let answers = await inquirer.prompt(questionsDeployCollections[0])
367
+ collectionIds.push(...answers.collections);
368
+ }
369
+
370
+ let collections = [];
371
+
372
+ for(const collectionId of collectionIds) {
373
+ const idCollections = configCollections.filter((c) => c.$id === collectionId);
374
+ collections.push(...idCollections);
375
+ }
338
376
339
377
for (let collection of collections) {
340
378
log(`Deploying collection ${collection.name} ( ${collection['$id']} )`)
379
+
380
+ let databaseId;
381
+
341
382
try {
342
- response = await databaseGetCollection({
383
+ const database = await databasesGet({
384
+ databaseId: collection.databaseId,
385
+ parseOutput: false,
386
+ });
387
+ databaseId = database.$id;
388
+ } catch(err) {
389
+ log(`Database ${collection.databaseId} not found. Creating it now...`);
390
+ const database = await databasesCreate({
391
+ databaseId: collection.databaseId,
392
+ name: collection.databaseId,
393
+ parseOutput: false,
394
+ });
395
+ databaseId = database.$id;
396
+ }
397
+
398
+ try {
399
+ response = await databasesGetCollection({
400
+ databaseId,
343
401
collectionId: collection['$id'],
344
402
parseOutput: false,
345
403
})
@@ -354,51 +412,55 @@ const deployCollection = async () => {
354
412
log(`Updating attributes ... `);
355
413
356
414
// TODO: Pagination?
357
- const { indexes: remoteIndexes } = await databaseListIndexes({
415
+ const { indexes: remoteIndexes } = await databasesListIndexes({
416
+ databaseId,
358
417
collectionId: collection['$id'],
359
418
limit: 100,
360
419
parseOutput: false
361
420
});
362
421
363
422
await Promise.all(remoteIndexes.map(async index => {
364
- await databaseDeleteIndex({
423
+ await databasesDeleteIndex({
424
+ databaseId,
365
425
collectionId: collection['$id'],
366
426
key: index.key,
367
427
parseOutput: false
368
428
});
369
429
}));
370
430
371
- const deleteIndexesPoolStatus = await awaitPools.wipeIndexes(collection['$id']);
431
+ const deleteIndexesPoolStatus = await awaitPools.wipeIndexes(databaseId, collection['$id']);
372
432
if (!deleteIndexesPoolStatus) {
373
433
throw new Error("Index deletion did not finish for too long.");
374
434
}
375
435
376
436
// TODO: Pagination?
377
- const { attributes: remoteAttributes } = await databaseListAttributes({
437
+ const { attributes: remoteAttributes } = await databasesListAttributes({
438
+ databaseId,
378
439
collectionId: collection['$id'],
379
440
limit: 100,
380
441
parseOutput: false
381
442
});
382
443
383
444
await Promise.all(remoteAttributes.map(async attribute => {
384
- await databaseDeleteAttribute({
445
+ await databasesDeleteAttribute({
446
+ databaseId,
385
447
collectionId: collection['$id'],
386
448
key: attribute.key,
387
449
parseOutput: false
388
450
});
389
451
}));
390
452
391
- const deleteAttributesPoolStatus = await awaitPools.wipeAttributes(collection['$id']);
453
+ const deleteAttributesPoolStatus = await awaitPools.wipeAttributes(databaseId, collection['$id']);
392
454
if (!deleteAttributesPoolStatus) {
393
455
throw new Error("Attribute deletion did not finish for too long.");
394
456
}
395
457
396
458
await Promise.all(collection.attributes.map(async attribute => {
397
- await createAttribute(collection['$id'], attribute);
459
+ await createAttribute(databaseId, collection['$id'], attribute);
398
460
}));
399
461
400
462
const attributeKeys = collection.attributes.map(attribute => attribute.key);
401
- const createPoolStatus = await awaitPools.expectAttributes(collection['$id'], attributeKeys);
463
+ const createPoolStatus = await awaitPools.expectAttributes(databaseId, collection['$id'], attributeKeys);
402
464
if (!createPoolStatus) {
403
465
throw new Error("Attribute creation did not finish for too long.");
404
466
}
@@ -407,7 +469,8 @@ const deployCollection = async () => {
407
469
408
470
log(`Creating indexes ...`)
409
471
await Promise.all(collection.indexes.map(async index => {
410
- await databaseCreateIndex({
472
+ await databasesCreateIndex({
473
+ databaseId,
411
474
collectionId: collection['$id'],
412
475
key: index.key,
413
476
type: index.type,
@@ -418,7 +481,7 @@ const deployCollection = async () => {
418
481
}));
419
482
420
483
const indexKeys = collection.indexes.map(attribute => attribute.key);
421
- const indexPoolStatus = await awaitPools.expectIndexes(collection['$id'], indexKeys);
484
+ const indexPoolStatus = await awaitPools.expectIndexes(databaseId, collection['$id'], indexKeys);
422
485
if (!indexPoolStatus) {
423
486
throw new Error("Index creation did not finish for too long.");
424
487
}
@@ -427,7 +490,8 @@ const deployCollection = async () => {
427
490
} catch (e) {
428
491
if (e.code == 404) {
429
492
log(`Collection ${collection.name} does not exist in the project. Creating ... `);
430
- response = await databaseCreateCollection({
493
+ response = await databasesCreateCollection({
494
+ databaseId,
431
495
collectionId: collection['$id'],
432
496
name: collection.name,
433
497
permission: collection.permission,
@@ -438,11 +502,11 @@ const deployCollection = async () => {
438
502
439
503
log(`Creating attributes ... `);
440
504
await Promise.all(collection.attributes.map(async attribute => {
441
- await createAttribute(collection['$id'], attribute);
505
+ await createAttribute(databaseId, collection['$id'], attribute);
442
506
}));
443
507
444
508
const attributeKeys = collection.attributes.map(attribute => attribute.key);
445
- const attributePoolStatus = await awaitPools.expectAttributes(collection['$id'], attributeKeys);
509
+ const attributePoolStatus = await awaitPools.expectAttributes(databaseId, collection['$id'], attributeKeys);
446
510
if (!attributePoolStatus) {
447
511
throw new Error("Attribute creation did not finish for too long.");
448
512
}
@@ -451,7 +515,8 @@ const deployCollection = async () => {
451
515
452
516
log(`Creating indexes ...`);
453
517
await Promise.all(collection.indexes.map(async index => {
454
- await databaseCreateIndex({
518
+ await databasesCreateIndex({
519
+ databaseId,
455
520
collectionId: collection['$id'],
456
521
key: index.key,
457
522
type: index.type,
@@ -462,7 +527,7 @@ const deployCollection = async () => {
462
527
}));
463
528
464
529
const indexKeys = collection.indexes.map(attribute => attribute.key);
465
- const indexPoolStatus = await awaitPools.expectIndexes(collection['$id'], indexKeys);
530
+ const indexPoolStatus = await awaitPools.expectIndexes(databaseId, collection['$id'], indexKeys);
466
531
if (!indexPoolStatus) {
467
532
throw new Error("Index creation did not finish for too long.");
468
533
}
@@ -487,6 +552,7 @@ deploy
487
552
deploy
488
553
.command("collection")
489
554
.description("Deploy collections in the current project.")
555
+ .option(`--all`, `Flag to deploy all functions`)
490
556
.action(actionRunner(deployCollection));
491
557
492
558
module.exports = {
0 commit comments