mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
WIP Custom fields feature.
This commit is contained in:
@@ -6,6 +6,11 @@ export default class MetableCollection {
|
||||
this.VALUE_COLUMN = 'value';
|
||||
this.TYPE_COLUMN = 'type';
|
||||
this.model = null;
|
||||
this.extraColumns = [];
|
||||
|
||||
this.extraQuery = (query, meta) => {
|
||||
query.where('key', meta[this.KEY_COLUMN]);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,9 +64,14 @@ export default class MetableCollection {
|
||||
* @param {*} group
|
||||
*/
|
||||
removeAllMeta(group = 'default') {
|
||||
this.metadata.forEach(meta => {
|
||||
meta.markAsDeleted = true;
|
||||
});
|
||||
this.metadata = this.metadata.map((meta) => ({
|
||||
...meta,
|
||||
markAsDeleted: true,
|
||||
}));
|
||||
}
|
||||
|
||||
setExtraQuery(callback) {
|
||||
this.extraQuery = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,18 +110,35 @@ export default class MetableCollection {
|
||||
const opers = [];
|
||||
|
||||
if (deleted.length > 0) {
|
||||
const deleteOper = this.model.query()
|
||||
.whereIn('key', deleted.map((meta) => meta.key)).delete();
|
||||
|
||||
opers.push(deleteOper);
|
||||
deleted.forEach((meta) => {
|
||||
const deleteOper = this.model.query().beforeRun((query, result) => {
|
||||
this.extraQuery(query, meta);
|
||||
return result;
|
||||
}).delete();
|
||||
opers.push(deleteOper);
|
||||
});
|
||||
}
|
||||
inserted.forEach((meta) => {
|
||||
const insertOper = this.model.query().insert({
|
||||
[this.KEY_COLUMN]: meta.key,
|
||||
[this.VALUE_COLUMN]: meta.value,
|
||||
...this.extraColumns.reduce((obj, column) => {
|
||||
if (typeof meta[column] !== 'undefined') {
|
||||
obj[column] = meta[column];
|
||||
}
|
||||
return obj;
|
||||
}, {}),
|
||||
});
|
||||
opers.push(insertOper);
|
||||
});
|
||||
updated.forEach((meta) => {
|
||||
const updateOper = this.model.query().onBuild((query) => {
|
||||
this.extraQuery(query, meta);
|
||||
}).patch({
|
||||
[this.VALUE_COLUMN]: meta.value,
|
||||
});
|
||||
opers.push(updateOper);
|
||||
});
|
||||
await Promise.all(opers);
|
||||
}
|
||||
|
||||
@@ -198,6 +225,11 @@ export default class MetableCollection {
|
||||
this.metadata.push(meta);
|
||||
}
|
||||
|
||||
|
||||
toArray() {
|
||||
return this.metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static method to load metadata to the collection.
|
||||
* @param {Array} meta
|
||||
|
||||
Reference in New Issue
Block a user