Skip to content

Commit

Permalink
refactoring model
Browse files Browse the repository at this point in the history
  • Loading branch information
toviszsolt committed Jun 9, 2024
1 parent 9d91401 commit b4681ea
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const model = (collectionName, schema = {}) => {

if (!data[collectionName]) data[collectionName] = [];

const create = (items) => {
const createItems = (items) => {
return new Promise(async (resolve, reject) => {
try {
items = getType(items) === 'array' ? items : [items];
Expand Down Expand Up @@ -60,7 +60,7 @@ const model = (collectionName, schema = {}) => {
});
};

const update = (items, updates) => {
const updateItems = (items, updates) => {
return new Promise(async (resolve, reject) => {
try {
if (getType(updates) !== 'object') {
Expand Down Expand Up @@ -141,12 +141,27 @@ const model = (collectionName, schema = {}) => {
});
};

const find = (query, withRefs = false) => {
return new Promise((resolve, reject) => {
try {
if (getType(query) !== 'object') {
const msg = 'Invalid object type of query.';
throw new Error(msg);
}
const results = applyQuery(data[collectionName], query);
resolve(withRefs ? resolveRefs(results) : results);
} catch (err) {
reject(err);
}
});
};

const insertOne = async (item) => {
if (getType(item) !== 'object') {
const msg = 'Invalid object type of item.';
throw new Error(msg);
}
const results = await create(item);
const results = await createItems(item);
return resolveRefs(results[0]);
};

Expand All @@ -155,43 +170,28 @@ const model = (collectionName, schema = {}) => {
const msg = 'Invalid array type of items.';
throw new Error(msg);
}
const results = await create(items);
const results = await createItems(items);
return resolveRefs(results);
};

const findByIdAndUpdate = async (id, updates) => {
const itemToUpdate = await findOne({ _id: id });
const results = await update([itemToUpdate], updates);
const items = await findOne({ _id: id });
const results = await updateItems([items], updates);
return resolveRefs(results[0]);
};

const updateOne = async (query, updates) => {
const itemToUpdate = await findOne(query);
const results = await update([itemToUpdate], updates);
const items = await findOne(query);
const results = await updateItems([items], updates);
return resolveRefs(results[0]);
};

const updateMany = async (query, updates) => {
const itemToUpdate = await find(query);
const results = await update(itemToUpdate, updates);
const items = await find(query);
const results = await updateItems(items, updates);
return resolveRefs(results);
};

const find = (query, withRefs = false) => {
return new Promise((resolve, reject) => {
try {
if (getType(query) !== 'object') {
const msg = 'Invalid object type of query.';
throw new Error(msg);
}
const results = applyQuery(data[collectionName], query);
resolve(withRefs ? resolveRefs(results) : results);
} catch (err) {
reject(err);
}
});
};

const findOne = async (query, withRefs = false) => {
const results = await find(query);
return withRefs ? resolveRefs(results[0]) : results[0];
Expand All @@ -217,20 +217,20 @@ const model = (collectionName, schema = {}) => {
};

const findByIdAndDelete = async (id) => {
const itemToDelete = await findById(id);
const results = await deleteItems([itemToDelete]);
const items = await findById(id);
const results = await deleteItems([items]);
return resolveRefs(results[0]);
};

const deleteOne = async (query) => {
const itemToDelete = await findOne(query);
const results = await deleteItems([itemToDelete]);
const items = await findOne(query);
const results = await deleteItems([items]);
return resolveRefs(results[0]);
};

const deleteMany = async (query) => {
const itemsToDelete = await find(query);
const results = await deleteItems(itemsToDelete);
const items = await find(query);
const results = await deleteItems(items);
return resolveRefs(results);
};

Expand Down

0 comments on commit b4681ea

Please sign in to comment.