@@ -6,6 +6,8 @@ const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollect
66const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
77const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment } = require('./functions');
88const {
9+ databasesGet,
10+ databasesCreate,
911 databasesCreateBooleanAttribute,
1012 databasesGetCollection,
1113 databasesCreateCollection,
@@ -23,19 +25,18 @@ const {
2325 databasesDeleteIndex
2426} = require("./databases");
2527
26- // TODO: Add databaseId everywhere
27-
2828const POOL_DEBOUNCE = 2000; // in milliseconds
2929const POOL_MAX_DEBOUNCES = 30;
3030
3131const awaitPools = {
32- wipeAttributes: async (collectionId, iteration = 1) => {
32+ wipeAttributes: async (databaseId, collectionId, iteration = 1) => {
3333 if (iteration > POOL_MAX_DEBOUNCES) {
3434 return false;
3535 }
3636
3737 // TODO: Pagination?
38- const { attributes: remoteAttributes } = await databaseListAttributes({
38+ const { attributes: remoteAttributes } = await databasesListAttributes({
39+ databaseId,
3940 collectionId,
4041 limit: 100,
4142 parseOutput: false
@@ -46,15 +47,16 @@ const awaitPools = {
4647 }
4748
4849 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);
5051 },
51- wipeIndexes: async (collectionId, iteration = 1) => {
52+ wipeIndexes: async (databaseId, collectionId, iteration = 1) => {
5253 if (iteration > POOL_MAX_DEBOUNCES) {
5354 return false;
5455 }
5556
5657 // TODO: Pagination?
57- const { indexes: remoteIndexes } = await databaseListIndexes({
58+ const { indexes: remoteIndexes } = await databasesListIndexes({
59+ databaseId,
5860 collectionId,
5961 limit: 100,
6062 parseOutput: false
@@ -65,15 +67,16 @@ const awaitPools = {
6567 }
6668
6769 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);
6971 },
70- expectAttributes: async (collectionId, attributeKeys, iteration = 1) => {
72+ expectAttributes: async (databaseId, collectionId, attributeKeys, iteration = 1) => {
7173 if (iteration > POOL_MAX_DEBOUNCES) {
7274 return false;
7375 }
7476
7577 // TODO: Pagination?
76- const { attributes: remoteAttributes } = await databaseListAttributes({
78+ const { attributes: remoteAttributes } = await databasesListAttributes({
79+ databaseId,
7780 collectionId,
7881 limit: 100,
7982 parseOutput: false
@@ -96,15 +99,16 @@ const awaitPools = {
9699 }
97100
98101 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);
100103 },
101- expectIndexes: async (collectionId, indexKeys, iteration = 1) => {
104+ expectIndexes: async (databaseId, collectionId, indexKeys, iteration = 1) => {
102105 if (iteration > POOL_MAX_DEBOUNCES) {
103106 return false;
104107 }
105108
106109 // TODO: Pagination?
107- const { indexes: remoteIndexes } = await databaseListIndexes({
110+ const { indexes: remoteIndexes } = await databasesListIndexes({
111+ databaseId,
108112 collectionId,
109113 limit: 100,
110114 parseOutput: false
@@ -127,7 +131,7 @@ const awaitPools = {
127131 }
128132
129133 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);
131135 },
132136}
133137
@@ -162,7 +166,12 @@ const deployFunction = async ({ functionId, all } = {}) => {
162166 let functions = functionIds.map((id) => {
163167 const functions = localConfig.getFunctions();
164168 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;
166175 });
167176
168177 for (let func of functions) {
@@ -244,12 +253,13 @@ const deployFunction = async ({ functionId, all } = {}) => {
244253 }
245254}
246255
247- const createAttribute = async (collectionId, attribute) => {
256+ const createAttribute = async (databaseId, collectionId, attribute) => {
248257 switch (attribute.type) {
249258 case 'string':
250259 switch (attribute.format) {
251260 case 'email':
252- return await databaseCreateEmailAttribute({
261+ return await databasesCreateEmailAttribute({
262+ databaseId,
253263 collectionId,
254264 key: attribute.key,
255265 required: attribute.required,
@@ -258,7 +268,8 @@ const createAttribute = async (collectionId, attribute) => {
258268 parseOutput: false
259269 })
260270 case 'url':
261- return await databaseCreateUrlAttribute({
271+ return await databasesCreateUrlAttribute({
272+ databaseId,
262273 collectionId,
263274 key: attribute.key,
264275 required: attribute.required,
@@ -267,7 +278,8 @@ const createAttribute = async (collectionId, attribute) => {
267278 parseOutput: false
268279 })
269280 case 'ip':
270- return await databaseCreateIpAttribute({
281+ return await databasesCreateIpAttribute({
282+ databaseId,
271283 collectionId,
272284 key: attribute.key,
273285 required: attribute.required,
@@ -276,7 +288,8 @@ const createAttribute = async (collectionId, attribute) => {
276288 parseOutput: false
277289 })
278290 case 'enum':
279- return await databaseCreateEnumAttribute({
291+ return await databasesCreateEnumAttribute({
292+ databaseId,
280293 collectionId,
281294 key: attribute.key,
282295 elements: attribute.elements,
@@ -286,7 +299,8 @@ const createAttribute = async (collectionId, attribute) => {
286299 parseOutput: false
287300 })
288301 default:
289- return await databaseCreateStringAttribute({
302+ return await databasesCreateStringAttribute({
303+ databaseId,
290304 collectionId,
291305 key: attribute.key,
292306 size: attribute.size,
@@ -298,7 +312,8 @@ const createAttribute = async (collectionId, attribute) => {
298312
299313 }
300314 case 'integer':
301- return await databaseCreateIntegerAttribute({
315+ return await databasesCreateIntegerAttribute({
316+ databaseId,
302317 collectionId,
303318 key: attribute.key,
304319 required: attribute.required,
@@ -309,7 +324,8 @@ const createAttribute = async (collectionId, attribute) => {
309324 parseOutput: false
310325 })
311326 case 'double':
312- return databaseCreateFloatAttribute({
327+ return databasesCreateFloatAttribute({
328+ databaseId,
313329 collectionId,
314330 key: attribute.key,
315331 required: attribute.required,
@@ -320,7 +336,9 @@ const createAttribute = async (collectionId, attribute) => {
320336 parseOutput: false
321337 })
322338 case 'boolean':
323- return databaseCreateBooleanAttribute({
339+ return databasesCreateBooleanAttribute({
340+ databaseId,
341+ databaseId,
324342 collectionId,
325343 key: attribute.key,
326344 required: attribute.required,
@@ -331,15 +349,55 @@ const createAttribute = async (collectionId, attribute) => {
331349 }
332350}
333351
334- const deployCollection = async () => {
352+ const deployCollection = async ({ all } = {} ) => {
335353 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+ }
338376
339377 for (let collection of collections) {
340378 log(`Deploying collection ${collection.name} ( ${collection['$id']} )`)
379+
380+ let databaseId;
381+
341382 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,
343401 collectionId: collection['$id'],
344402 parseOutput: false,
345403 })
@@ -354,51 +412,55 @@ const deployCollection = async () => {
354412 log(`Updating attributes ... `);
355413
356414 // TODO: Pagination?
357- const { indexes: remoteIndexes } = await databaseListIndexes({
415+ const { indexes: remoteIndexes } = await databasesListIndexes({
416+ databaseId,
358417 collectionId: collection['$id'],
359418 limit: 100,
360419 parseOutput: false
361420 });
362421
363422 await Promise.all(remoteIndexes.map(async index => {
364- await databaseDeleteIndex({
423+ await databasesDeleteIndex({
424+ databaseId,
365425 collectionId: collection['$id'],
366426 key: index.key,
367427 parseOutput: false
368428 });
369429 }));
370430
371- const deleteIndexesPoolStatus = await awaitPools.wipeIndexes(collection['$id']);
431+ const deleteIndexesPoolStatus = await awaitPools.wipeIndexes(databaseId, collection['$id']);
372432 if (!deleteIndexesPoolStatus) {
373433 throw new Error("Index deletion did not finish for too long.");
374434 }
375435
376436 // TODO: Pagination?
377- const { attributes: remoteAttributes } = await databaseListAttributes({
437+ const { attributes: remoteAttributes } = await databasesListAttributes({
438+ databaseId,
378439 collectionId: collection['$id'],
379440 limit: 100,
380441 parseOutput: false
381442 });
382443
383444 await Promise.all(remoteAttributes.map(async attribute => {
384- await databaseDeleteAttribute({
445+ await databasesDeleteAttribute({
446+ databaseId,
385447 collectionId: collection['$id'],
386448 key: attribute.key,
387449 parseOutput: false
388450 });
389451 }));
390452
391- const deleteAttributesPoolStatus = await awaitPools.wipeAttributes(collection['$id']);
453+ const deleteAttributesPoolStatus = await awaitPools.wipeAttributes(databaseId, collection['$id']);
392454 if (!deleteAttributesPoolStatus) {
393455 throw new Error("Attribute deletion did not finish for too long.");
394456 }
395457
396458 await Promise.all(collection.attributes.map(async attribute => {
397- await createAttribute(collection['$id'], attribute);
459+ await createAttribute(databaseId, collection['$id'], attribute);
398460 }));
399461
400462 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);
402464 if (!createPoolStatus) {
403465 throw new Error("Attribute creation did not finish for too long.");
404466 }
@@ -407,7 +469,8 @@ const deployCollection = async () => {
407469
408470 log(`Creating indexes ...`)
409471 await Promise.all(collection.indexes.map(async index => {
410- await databaseCreateIndex({
472+ await databasesCreateIndex({
473+ databaseId,
411474 collectionId: collection['$id'],
412475 key: index.key,
413476 type: index.type,
@@ -418,7 +481,7 @@ const deployCollection = async () => {
418481 }));
419482
420483 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);
422485 if (!indexPoolStatus) {
423486 throw new Error("Index creation did not finish for too long.");
424487 }
@@ -427,7 +490,8 @@ const deployCollection = async () => {
427490 } catch (e) {
428491 if (e.code == 404) {
429492 log(`Collection ${collection.name} does not exist in the project. Creating ... `);
430- response = await databaseCreateCollection({
493+ response = await databasesCreateCollection({
494+ databaseId,
431495 collectionId: collection['$id'],
432496 name: collection.name,
433497 permission: collection.permission,
@@ -438,11 +502,11 @@ const deployCollection = async () => {
438502
439503 log(`Creating attributes ... `);
440504 await Promise.all(collection.attributes.map(async attribute => {
441- await createAttribute(collection['$id'], attribute);
505+ await createAttribute(databaseId, collection['$id'], attribute);
442506 }));
443507
444508 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);
446510 if (!attributePoolStatus) {
447511 throw new Error("Attribute creation did not finish for too long.");
448512 }
@@ -451,7 +515,8 @@ const deployCollection = async () => {
451515
452516 log(`Creating indexes ...`);
453517 await Promise.all(collection.indexes.map(async index => {
454- await databaseCreateIndex({
518+ await databasesCreateIndex({
519+ databaseId,
455520 collectionId: collection['$id'],
456521 key: index.key,
457522 type: index.type,
@@ -462,7 +527,7 @@ const deployCollection = async () => {
462527 }));
463528
464529 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);
466531 if (!indexPoolStatus) {
467532 throw new Error("Index creation did not finish for too long.");
468533 }
@@ -487,6 +552,7 @@ deploy
487552deploy
488553 .command("collection")
489554 .description("Deploy collections in the current project.")
555+ .option(`--all`, `Flag to deploy all functions`)
490556 .action(actionRunner(deployCollection));
491557
492558module.exports = {
0 commit comments