@@ -2,7 +2,7 @@ const inquirer = require("inquirer");
22const JSONbig = require("json-bigint")({ storeAsString: false });
33const { Command } = require("commander");
44const { localConfig } = require("../config");
5- const { questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
5+ const { questionsDeployBuckets, questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
77const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88const {
@@ -23,8 +23,12 @@ const {
2323 databasesDeleteAttribute,
2424 databasesListAttributes,
2525 databasesListIndexes,
26- databasesDeleteIndex
26+ databasesDeleteIndex,
27+ databasesUpdateCollection
2728} = require("./databases");
29+ const {
30+ storageGetBucket, storageUpdateBucket, storageCreateBucket
31+ } = require("./storage");
2832const {
2933 teamsGet,
3034 teamsUpdate,
@@ -437,17 +441,27 @@ const deployCollection = async ({ all, yes } = {}) => {
437441
438442 let databaseId;
439443
444+ const localDatabase = localConfig.getDatabase(collection.databaseId);
445+
440446 try {
441447 const database = await databasesGet({
442448 databaseId: collection.databaseId,
443449 parseOutput: false,
444450 });
445451 databaseId = database.$id;
452+
453+ await databasesUpdate({
454+ databaseId: collection.databaseId,
455+ name: localDatabase.name ?? collection.databaseId,
456+ parseOutput: false
457+ })
458+
459+ success(`Updated ${localDatabase.name} ( ${collection.databaseId} )`);
446460 } catch(err) {
447461 log(`Database ${collection.databaseId} not found. Creating it now...`);
448462 const database = await databasesCreate({
449463 databaseId: collection.databaseId,
450- name: collection.databaseId,
464+ name: localDatabase.name ?? collection.databaseId,
451465 parseOutput: false,
452466 });
453467 databaseId = database.$id;
@@ -547,6 +561,18 @@ const deployCollection = async ({ all, yes } = {}) => {
547561 }
548562
549563 success(`Created ${collection.indexes.length} indexes`);
564+
565+ await databasesUpdateCollection({
566+ databaseId,
567+ collectionId: collection['$id'],
568+ name: collection.name,
569+ documentSecurity: collection.documentSecurity,
570+ permissions: collection['$permissions'],
571+ enabled: collection.enabled,
572+ parseOutput: false
573+ })
574+
575+ success(`Deployed ${collection.name} ( ${collection['$id']} )`);
550576 } catch (e) {
551577 if (e.code == 404) {
552578 log(`Collection ${collection.name} does not exist in the project. Creating ... `);
@@ -601,6 +627,93 @@ const deployCollection = async ({ all, yes } = {}) => {
601627 }
602628}
603629
630+ const deployBucket = async ({ all, yes } = {}) => {
631+ let response = {};
632+
633+ let bucketIds = [];
634+ const configBuckets = localConfig.getBuckets();
635+
636+ if(all) {
637+ if (configBuckets.length === 0) {
638+ throw new Error("No buckets found in the current directory. Run `appwrite init bucket` to fetch all your buckets.");
639+ }
640+ bucketIds.push(...configBuckets.map((b) => b.$id));
641+ }
642+
643+ if(bucketIds.length === 0) {
644+ let answers = await inquirer.prompt(questionsDeployBuckets[0])
645+ bucketIds.push(...answers.buckets);
646+ }
647+
648+ let buckets = [];
649+
650+ for(const bucketId of bucketIds) {
651+ const idBuckets = configBuckets.filter((b) => b.$id === bucketId);
652+ buckets.push(...idBuckets);
653+ }
654+
655+ for (let bucket of buckets) {
656+ log(`Deploying bucket ${bucket.name} ( ${bucket['$id']} )`)
657+
658+ try {
659+ response = await storageGetBucket({
660+ bucketId: bucket['$id'],
661+ parseOutput: false,
662+ })
663+ log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
664+
665+ if(!yes) {
666+ answers = await inquirer.prompt(questionsDeployBuckets[1])
667+ if (answers.override !== "YES") {
668+ log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
669+ continue;
670+ }
671+ }
672+
673+ log(`Updating bucket ...`)
674+
675+ await storageUpdateBucket({
676+ bucketId: bucket['$id'],
677+ name: bucket.name,
678+ permissions: bucket['$permissions'],
679+ fileSecurity: bucket.fileSecurity,
680+ enabled: bucket.enabled,
681+ maximumFileSize: bucket.maximumFileSize,
682+ allowedFileExtensions: bucket.allowedFileExtensions,
683+ compression: bucket.compression,
684+ encryption: bucket.encryption,
685+ antivirus: bucket.antivirus,
686+ compression: bucket.compression,
687+ parseOutput: false
688+ });
689+
690+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
691+ } catch (e) {
692+ if (e.code == 404) {
693+ log(`Bucket ${bucket.name} does not exist in the project. Creating ... `);
694+
695+ response = await storageCreateBucket({
696+ bucketId: bucket['$id'],
697+ name: bucket.name,
698+ permissions: bucket['$permissions'],
699+ fileSecurity: bucket.fileSecurity,
700+ enabled: bucket.enabled,
701+ maximumFileSize: bucket.maximumFileSize,
702+ allowedFileExtensions: bucket.allowedFileExtensions,
703+ compression: bucket.compression,
704+ encryption: bucket.encryption,
705+ antivirus: bucket.antivirus,
706+ parseOutput: false
707+ })
708+
709+ success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
710+ } else {
711+ throw e;
712+ }
713+ }
714+ }
715+ }
716+
604717const deployTeam = async ({ all, yes } = {}) => {
605718 let response = {};
606719
@@ -686,6 +799,13 @@ deploy
686799 .option(`--yes`, `Flag to confirm all warnings`)
687800 .action(actionRunner(deployCollection));
688801
802+ deploy
803+ .command("bucket")
804+ .description("Deploy buckets in the current project.")
805+ .option(`--all`, `Flag to deploy all buckets`)
806+ .option(`--yes`, `Flag to confirm all warnings`)
807+ .action(actionRunner(deployBucket));
808+
689809deploy
690810 .command("team")
691811 .description("Deploy teams in the current project.")
0 commit comments