@@ -2,7 +2,7 @@ const inquirer = require("inquirer");
2
2
const JSONbig = require("json-bigint")({ storeAsString: false });
3
3
const { Command } = require("commander");
4
4
const { localConfig } = require("../config");
5
- const { questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
5
+ const { questionsDeployBuckets, questionsDeployTeams, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
6
6
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
7
7
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
8
8
const {
@@ -23,8 +23,12 @@ const {
23
23
databasesDeleteAttribute,
24
24
databasesListAttributes,
25
25
databasesListIndexes,
26
- databasesDeleteIndex
26
+ databasesDeleteIndex,
27
+ databasesUpdateCollection
27
28
} = require("./databases");
29
+ const {
30
+ storageGetBucket, storageUpdateBucket, storageCreateBucket
31
+ } = require("./storage");
28
32
const {
29
33
teamsGet,
30
34
teamsUpdate,
@@ -437,17 +441,27 @@ const deployCollection = async ({ all, yes } = {}) => {
437
441
438
442
let databaseId;
439
443
444
+ const localDatabase = localConfig.getDatabase(collection.databaseId);
445
+
440
446
try {
441
447
const database = await databasesGet({
442
448
databaseId: collection.databaseId,
443
449
parseOutput: false,
444
450
});
445
451
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} )`);
446
460
} catch(err) {
447
461
log(`Database ${collection.databaseId} not found. Creating it now...`);
448
462
const database = await databasesCreate({
449
463
databaseId: collection.databaseId,
450
- name: collection.databaseId,
464
+ name: localDatabase.name ?? collection.databaseId,
451
465
parseOutput: false,
452
466
});
453
467
databaseId = database.$id;
@@ -547,6 +561,18 @@ const deployCollection = async ({ all, yes } = {}) => {
547
561
}
548
562
549
563
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']} )`);
550
576
} catch (e) {
551
577
if (e.code == 404) {
552
578
log(`Collection ${collection.name} does not exist in the project. Creating ... `);
@@ -601,6 +627,93 @@ const deployCollection = async ({ all, yes } = {}) => {
601
627
}
602
628
}
603
629
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
+
604
717
const deployTeam = async ({ all, yes } = {}) => {
605
718
let response = {};
606
719
@@ -686,6 +799,13 @@ deploy
686
799
.option(`--yes`, `Flag to confirm all warnings`)
687
800
.action(actionRunner(deployCollection));
688
801
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
+
689
809
deploy
690
810
.command("team")
691
811
.description("Deploy teams in the current project.")
0 commit comments