@@ -692,14 +692,23 @@ const deleteAttribute = async (collection, attribute) => {
692
692
});
693
693
}
694
694
695
- const deepSimilar = (remote, local, collection) => {
695
+ /**
696
+ * Check if attribute non-changeable fields has been changed
697
+ * If so return the differences as an object.
698
+ * @param remote
699
+ * @param local
700
+ * @param collection
701
+ * @returns {undefined|{reason: string, action: *, attribute, key: string}}
702
+ */
703
+ const checkAttributeChanges = (remote, local, collection) => {
696
704
if (local === undefined) {
697
705
return undefined;
698
706
}
699
707
700
708
const keyName = `${chalk.yellow(local.key)} in ${collection.name} (${collection['$id']})`;
701
709
const action = chalk.cyan('recreating');
702
710
let reason = '';
711
+
703
712
for (let key of Object.keys(remote)) {
704
713
if (changeableKeys.includes(key)) {
705
714
continue;
@@ -714,23 +723,38 @@ const deepSimilar = (remote, local, collection) => {
714
723
return reason === '' ? undefined : { key: keyName, attribute: remote, reason, action };
715
724
}
716
725
717
- const findMatch = (attribute, attributes) => attributes.find((attr) => attr.key === attribute.key);
726
+ /**
727
+ * Check if attributes contain the given attribute
728
+ * @param attribute
729
+ * @param attributes
730
+ * @returns {*}
731
+ */
732
+ const attributesContains = (attribute, attributes) => attributes.find((attr) => attr.key === attribute.key);
733
+
718
734
719
- const mapChangeAttribute = (attribute, collection, isAdding) => {
735
+ const generateChangesObject = (attribute, collection, isAdding) => {
720
736
return {
721
737
key: `${chalk.yellow(attribute.key)} in ${collection.name} (${collection['$id']})`,
722
- attribute,
738
+ attribute: attribute ,
723
739
reason: isAdding ? 'Field doesn\'t exist on the remote server' : 'Field doesn\'t exist in appwrite.json file',
724
740
action: isAdding ? chalk.green('adding') : chalk.red('deleting')
725
741
};
726
742
727
743
};
728
744
729
- const updatedList = async (remoteAttributes, localAttributes, collection) => {
745
+ /**
746
+ * Filter deleted and recreated attributes,
747
+ * return list of attributes to create
748
+ * @param remoteAttributes
749
+ * @param localAttributes
750
+ * @param collection
751
+ * @returns {Promise< *|*[]>}
752
+ */
753
+ const attributesToCreate = async (remoteAttributes, localAttributes, collection) => {
730
754
731
- const deleting = remoteAttributes.filter((attribute) => !findMatch (attribute, localAttributes)).map((attr) => mapChangeAttribute (attr, collection, false));
732
- const adding = localAttributes.filter((attribute) => !findMatch (attribute, remoteAttributes)).map((attr) => mapChangeAttribute (attr, collection, true));
733
- const conflicts = remoteAttributes.map((attribute) => deepSimilar (attribute, findMatch (attribute, localAttributes), collection)).filter(attribute => attribute !== undefined);
755
+ const deleting = remoteAttributes.filter((attribute) => !attributesContains (attribute, localAttributes)).map((attr) => generateChangesObject (attr, collection, false));
756
+ const adding = localAttributes.filter((attribute) => !attributesContains (attribute, remoteAttributes)).map((attr) => generateChangesObject (attr, collection, true));
757
+ const conflicts = remoteAttributes.map((attribute) => checkAttributeChanges (attribute, attributesContains (attribute, localAttributes), collection)).filter(attribute => attribute !== undefined);
734
758
735
759
let changedAttributes = [];
736
760
const changing = [...deleting, ...adding, ...conflicts]
@@ -751,7 +775,7 @@ const updatedList = async (remoteAttributes, localAttributes, collection) => {
751
775
if (conflicts.length > 0) {
752
776
changedAttributes = conflicts.map((change) => change.attribute);
753
777
await Promise.all(changedAttributes.map((changed) => deleteAttribute(collection, changed)));
754
- remoteAttributes = remoteAttributes.filter((attribute) => !findMatch (attribute, changedAttributes))
778
+ remoteAttributes = remoteAttributes.filter((attribute) => !attributesContains (attribute, changedAttributes))
755
779
}
756
780
757
781
const deletingAttributes = deleting.map((change) => change.attribute);
@@ -766,7 +790,7 @@ const updatedList = async (remoteAttributes, localAttributes, collection) => {
766
790
}
767
791
}
768
792
769
- return localAttributes.filter((attribute) => !findMatch (attribute, remoteAttributes));
793
+ return localAttributes.filter((attribute) => !attributesContains (attribute, remoteAttributes));
770
794
}
771
795
772
796
const pushCollection = async ({ all, yes } = {}) => {
@@ -864,7 +888,7 @@ const pushCollection = async ({ all, yes } = {}) => {
864
888
let attributes = collection.attributes;
865
889
866
890
if (collection.isExisted) {
867
- attributes = await updatedList (collection.remoteVersion.attributes, collection.attributes, collection);
891
+ attributes = await attributesToCreate (collection.remoteVersion.attributes, collection.attributes, collection);
868
892
869
893
if (Array.isArray(attributes) && attributes.length < = 0) {
870
894
log(`No changes has been detected. Skipping ${collection.name} ( ${collection['$id']} )`);
0 commit comments