Skip to content

Commit

Permalink
Merge pull request #98 from ExWeiv/dev
Browse files Browse the repository at this point in the history
BUG Fixes for save
  • Loading branch information
loeiks authored Jul 30, 2024
2 parents 48fdd40 + c892e46 commit 1f0a0c5
Show file tree
Hide file tree
Showing 120 changed files with 391 additions and 380 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ In this file you can find what's changed in each version. (Versions with -dev, -

---

### 4.9.3^

- BUG Fixes for some functions.
- Security updates/fixes.

### 4.9.1

- BUG fix for query and aggregation filters (weivData.filters).
Expand Down
22 changes: 13 additions & 9 deletions app/lib/Functions/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ async function save(collectionId, item, options) {
const { safeOptions, safeItem } = await (0, validator_1.validateParams)({ collectionId, item, options }, ["collectionId", "item"], "save");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, enableVisitorId, convertIds } = { convertIds: (0, weiv_data_config_1.getConvertIdsValue)(), ...safeOptions };
let actionType = "insert";
let editedItem;
if (safeItem._id) {
safeItem._id = (0, id_converters_1.convertIdToObjectId)(safeItem._id);
actionType = "update";
if (suppressHooks != true) {
editedItem = await (0, hook_manager_1.runDataHook)(collectionId, "beforeUpdate", [safeItem, context]).catch((err) => {
(0, error_manager_1.kaptanLogar)("00002", `beforeUpdate (save) Hook Failure ${err}`);
Expand All @@ -27,6 +29,7 @@ async function save(collectionId, item, options) {
}
else {
safeItem._owner = await (0, member_id_helpers_1.getOwnerId)(enableVisitorId);
actionType = "insert";
if (suppressHooks != true) {
editedItem = await (0, hook_manager_1.runDataHook)(collectionId, "beforeInsert", [safeItem, context]).catch((err) => {
(0, error_manager_1.kaptanLogar)("00002", `beforeInsert (save) Hook Failure ${err}`);
Expand All @@ -37,19 +40,17 @@ async function save(collectionId, item, options) {
...safeItem,
...editedItem
};
let filter;
if (safeItem._id && typeof safeItem._id === "string" && onlyOwner) {
filter = { _id: editedItem._id };
const filter = safeItem._id ? { _id: safeItem._id } : { _id: new mongodb_1.ObjectId() };
if (onlyOwner) {
const currentMemberId = await (0, member_id_helpers_1.getOwnerId)(enableVisitorId);
if (currentMemberId) {
filter._owner = currentMemberId;
}
}
const { collection } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth);
const { upsertedId, acknowledged } = await collection.updateOne(filter ? filter : { _id: new mongodb_1.ObjectId() }, { $set: { ...editedItem, _updatedDate: new Date() }, $setOnInsert: !editedItem._createdDate ? { _createdDate: new Date() } : {} }, { readConcern, upsert: true });
const returnedItem = { ...editedItem, _id: editedItem._id };
if (acknowledged) {
if (upsertedId) {
const returnedItem = await collection.findOneAndUpdate(filter, { $set: { ...editedItem, _updatedDate: new Date() }, $setOnInsert: !editedItem._createdDate ? { _createdDate: new Date() } : {} }, { readConcern, upsert: true, returnDocument: "after" });
if (returnedItem) {
if (actionType === "insert") {
const editedResult = await (0, hook_manager_1.runDataHook)(collectionId, "afterInsert", [convertIds ? (0, internal_id_converter_1.convertDocumentIDs)(returnedItem) : returnedItem, context]).catch((err) => {
(0, error_manager_1.kaptanLogar)("00003", `afterInsert Hook Failure ${err}`);
});
Expand All @@ -60,7 +61,7 @@ async function save(collectionId, item, options) {
return convertIds ? { item: (0, internal_id_converter_1.convertDocumentIDs)(returnedItem) } : { item: returnedItem };
}
}
else {
else if (actionType === "update") {
const editedResult = await (0, hook_manager_1.runDataHook)(collectionId, "afterUpdate", [convertIds ? (0, internal_id_converter_1.convertDocumentIDs)(returnedItem) : returnedItem, context]).catch((err) => {
(0, error_manager_1.kaptanLogar)("00003", `afterUpdate Hook Failure ${err}`);
});
Expand All @@ -71,9 +72,12 @@ async function save(collectionId, item, options) {
return convertIds ? { item: (0, internal_id_converter_1.convertDocumentIDs)(returnedItem) } : { item: returnedItem };
}
}
else {
(0, error_manager_1.kaptanLogar)("00016", `this error is not expected, try again or create a issue in WeivData GitHub repo`);
}
}
else {
(0, error_manager_1.kaptanLogar)("00016", `acknowledged is not true for (save function)`);
(0, error_manager_1.kaptanLogar)("00016", `couldn't save item, this error is unexpected`);
}
}
catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/weiv-data",
"version": "4.9.1",
"version": "4.9.4",
"description": "Custom API Library for Wix sites to connect MongoDB. Designed to easily switch from wix-data APIs.",
"main": "./lib/index.js",
"files": [
Expand Down
28 changes: 15 additions & 13 deletions app/src/Functions/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
const context = prepareHookContext(collectionId);
const { suppressAuth, suppressHooks, readConcern, onlyOwner, enableVisitorId, convertIds } = { convertIds: getConvertIdsValue(), ...safeOptions };

// Convert ID to ObjectId if exist
let actionType: "insert" | "update" = "insert";
let editedItem;

if (safeItem._id) {
// Update
safeItem._id = convertIdToObjectId(safeItem._id);
actionType = "update";

if (suppressHooks != true) {
editedItem = await runDataHook<'beforeUpdate'>(collectionId, "beforeUpdate", [safeItem, context]).catch((err) => {
Expand All @@ -32,6 +34,7 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
// Insert
safeItem._owner = await getOwnerId(enableVisitorId);
actionType = "insert";

if (suppressHooks != true) {
editedItem = await runDataHook<'beforeInsert'>(collectionId, "beforeInsert", [safeItem, context]).catch((err) => {
Expand All @@ -46,27 +49,24 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
}

// For updates
let filter: { _id: ObjectId, _owner?: string } | undefined;
if (safeItem._id && typeof safeItem._id === "string" && onlyOwner) {
filter = { _id: editedItem._id };
const filter: { _id: ObjectId, _owner?: string } = safeItem._id ? { _id: safeItem._id } : { _id: new ObjectId() };
if (onlyOwner) {
const currentMemberId = await getOwnerId(enableVisitorId);
if (currentMemberId) {
filter._owner = currentMemberId;
}
}

const { collection } = await connectionHandler(collectionId, suppressAuth);
const { upsertedId, acknowledged } = await collection.updateOne(
filter ? filter : { _id: new ObjectId() },
const returnedItem = await collection.findOneAndUpdate(
filter,
{ $set: { ...editedItem, _updatedDate: new Date() }, $setOnInsert: !editedItem._createdDate ? { _createdDate: new Date() } : {} },
{ readConcern, upsert: true }
{ readConcern, upsert: true, returnDocument: "after" }
);

const returnedItem = { ...editedItem, _id: editedItem._id };

if (acknowledged) {
if (returnedItem) {
// Hooks handling
if (upsertedId) {
if (actionType === "insert") {
// Item Inserted
const editedResult = await runDataHook<'afterInsert'>(collectionId, "afterInsert", [convertIds ? convertDocumentIDs(returnedItem) : returnedItem, context]).catch((err) => {
kaptanLogar("00003", `afterInsert Hook Failure ${err}`);
Expand All @@ -77,7 +77,7 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
return convertIds ? { item: convertDocumentIDs(returnedItem) } : { item: returnedItem };
}
} else {
} else if (actionType === "update") {
// Item Updated
const editedResult = await runDataHook<'afterUpdate'>(collectionId, "afterUpdate", [convertIds ? convertDocumentIDs(returnedItem) : returnedItem, context]).catch((err) => {
kaptanLogar("00003", `afterUpdate Hook Failure ${err}`);
Expand All @@ -88,9 +88,11 @@ export async function save(collectionId: CollectionID, item: Item, options?: Wei
} else {
return convertIds ? { item: convertDocumentIDs(returnedItem) } : { item: returnedItem };
}
} else {
kaptanLogar("00016", `this error is not expected, try again or create a issue in WeivData GitHub repo`);
}
} else {
kaptanLogar("00016", `acknowledged is not true for (save function)`);
kaptanLogar("00016", `couldn't save item, this error is unexpected`);
}
} catch (err) {
kaptanLogar("00016", `when saving an item to collection: ${err}`);
Expand Down
4 changes: 2 additions & 2 deletions docs/functions/Hooks.afterCount.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
</div><div class="tsd-comment tsd-typography"></div></li><li><span><span class="tsd-kind-parameter">context</span>: <a href="../types/Hooks.HookContext.html" class="tsd-signature-type tsd-kind-type-alias">HookContext</a></span><div class="tsd-comment tsd-typography"><p>Contextual information about the hook.</p>
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Promise</span><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">number</span></h4><p>The count to return to <code>count()</code> instead of the original count. Returning a rejected promise will not block the operation, but will return a rejected promise to the caller as well as trigger the <code>onFailure()</code> hook.</p>
<div class="tsd-comment tsd-typography"><h4 class="tsd-anchor-link"><a id="Description" class="tsd-anchor"></a>Description<a href="#Description" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h4><p>A hook that is triggered after a <code>count()</code> operation.</p>
</div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/ExWeiv/weiv-data/blob/a7d4d70ee6669512afcef857bae1e1a5b416f497/app/weivdata.d.ts#L1627">weivdata.d.ts:1627</a></li></ul></aside></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1"></use></svg><span>WeivData API Reference | ExWeiv Apps</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base=".."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a><br>
</div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/ExWeiv/weiv-data/blob/d9f5141140ec688c745862268ed9d219c325bae2/app/weivdata.d.ts#L1627">weivdata.d.ts:1627</a></li></ul></aside></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1"></use></svg><span>WeivData API Reference | ExWeiv Apps</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base=".."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a><br>
<span id="generation-date"></span>
<script>
window.GENERATION_DATE = 1722305529424;
window.GENERATION_DATE = 1722340618857;

(() => {

Expand Down
Loading

0 comments on commit 1f0a0c5

Please sign in to comment.