Skip to content

Commit

Permalink
update model.update
Browse files Browse the repository at this point in the history
  • Loading branch information
toviszsolt committed Jun 9, 2024
1 parent 60b4f90 commit 4e3f21a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 60 deletions.
73 changes: 14 additions & 59 deletions src/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const { data, saveDataToFile } = require('./storage');
const model = (collectionName, schema = {}) => {
const schemaHasProps = Object.keys(schema).length > 0;

if (/^[a-z0-9_-]+$/i.test(collectionName) == false) {
const msg = 'Collection name must contain only small caps, numbers, hypens or underscores.';
if (/^[a-z0-9_-]+$/.test(collectionName) == false) {
const msg = `Collection name (${collectionName}) must contain only small caps, numbers, hypens or underscores.`;
throw new Error(msg);
}

if (collectionName.endsWith('s') === false) {
const msg = 'Collection name must be plural. For example: users, categories, products.';
const msg = `Collection name (${collectionName}) must be plural. For example: users, categories, products.`;
throw new Error(msg);
}

Expand Down Expand Up @@ -70,21 +70,17 @@ const model = (collectionName, schema = {}) => {

let changedItems = 0;
const timestamp = timeNow();
const results = await find(query);
const resultsClone = objClone(results);
const items = await find(query);

await executeMiddleware('pre', collectionName, 'update', resultsClone);
await executeMiddleware('pre', collectionName, 'update', items);

resultsClone.forEach((el, i) => {
items.forEach((el) => {
let changedFields = 0;
const index = data[collectionName].indexOf(el);
const applyUpdates = schemaHasProps ? applySchema(updates, schema) : updates;

objTraverse(updates, ({ value, path, isNode }) => {
if (isNode) return;

if (['_id', '_version', '_created', '_updated'].includes(path)) {
const msg = 'Protected fields cannot be updated: _id, _version, _created, _updated';
throw new Error(msg);
}
objTraverse(applyUpdates, ({ value, path, isNode }) => {
if (isNode || path.startsWith('$')) return;

if (objPathResolve(el, path) !== value) {
changedFields++;
Expand All @@ -101,67 +97,26 @@ const model = (collectionName, schema = {}) => {
}
}

// for (const key in updates) {
// if (updates.hasOwnProperty(key)) {
// const fieldToUpdate = updates[key];

// if (updates.$unset) {
// for (const unsetKeys in updates.$unset) {
// if (updates.$unset.hasOwnProperty(unsetKeys)) {
// const unsetKeyChain = unsetKeys.split('.');
// const unsetLastKey = unsetKeyChain.pop();

// let currentObj = el;
// for (const unsetKey of unsetKeyChain) {
// currentObj = currentObj[unsetKey];
// }

// delete currentObj[unsetLastKey];
// trulyUpdatedFields++;
// }
// }
// } else if (schemaHasProps && schema[key] && schema[key].$ref) {
// const refCollection = data[schema[key].$ref];
// if (refCollection) {
// const refItem = refCollection.find((elRef) => elRef._id === fieldToUpdate);
// if (refItem) {
// trulyUpdatedFields++;
// el[key] = { _ref: { collection: schema[key].$ref, id: refItem._id } };
// }
// }
// } else {
// const newValue = schemaHasProps ? applySchema(fieldToUpdate, schema[key]) : fieldToUpdate;
// if (el[key] !== newValue) {
// trulyUpdatedFields++;
// el[key] = newValue;
// }
// }
// }
// }

if (changedFields) {
changedItems++;
el._version = (el._version || 1) + 1;

if (config.defaultFields) {
el._created ??= timestamp;
el._updated = timestamp;
}

const { _id, _version, _created, _updated } = el;
const applyUpdates = schemaHasProps ? applySchema(el, schema) : el;

results[i] = { ...applyUpdates, _id, _version, _created, _updated };
changedItems++;
data[collectionName][index] = objClone(el);
}
});

if (changedItems > 0) {
saveDataToFile(collectionName);
}

await executeMiddleware('post', collectionName, 'update', objClone(results));
await executeMiddleware('post', collectionName, 'update', items);

resolve(resolveRefs(results));
resolve(resolveRefs(items));
} catch (err) {
reject(err);
}
Expand Down
6 changes: 5 additions & 1 deletion test/lib/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ describe('model.update', () => {

it('update a document with skip _id, _version, _created, _updated', async () => {
const data = { age: 32, address: { city: 'Miami' }, _id: 0, _version: 0, _created: 0, _updated: 0 };
await expect(testModel.update({ name: 'John' }, data)).rejects.toThrow(/protected/i);
const updated = await testModel.update({ name: 'John' }, data);
expect(updated[0]._id).not.toBe(0);
expect(updated[0]._version).not.toBe(0);
expect(updated[0]._created).not.toBe(0);
expect(updated[0]._updated).not.toBe(0);
});

it('update a document with same value', async () => {
Expand Down

0 comments on commit 4e3f21a

Please sign in to comment.