Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to delete items in a collection #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@ const onDeleteObject = async (Model, gqltype, controller, args, session, linkToP
return Model.findByIdAndDelete(args, deletedObject.modelArgs).session(session);
};

const onDeleteSubject = async (Model, controller, id, session) => {
const currentObject = await Model.findById({ _id: id }).lean();

if (controller && controller.onDelete) {
await controller.onDelete(currentObject, session);
}

return Model.findByIdAndDelete(id, { session });
};

const onUpdateSubject = async (Model, gqltype, controller, args, session, linkToParent) => {
const materializedModel = await materializeModel(args, gqltype, linkToParent, 'UPDATE', session);
const objectId = args.id;
Expand Down Expand Up @@ -510,9 +520,7 @@ const onUpdateSubject = async (Model, gqltype, controller, args, session, linkTo
await controller.onUpdating(objectId, modifiedObject, args, session);
}

const result = Model.findByIdAndUpdate(
objectId, modifiedObject, { new: true },
);
const result = Model.findByIdAndUpdate(objectId, modifiedObject, { session, new: true });

if (controller && controller.onUpdated) {
await controller.onUpdated(result, args, session);
Expand Down Expand Up @@ -633,7 +641,10 @@ const executeItemFunction = async (gqltype, collectionField, objectId, session,
};
break;
case operations.DELETE:
// TODO: implement
operationFunction = async (collectionItem) => {
await onDeleteSubject(typesDict.types[collectionGQLType.name].model,
typesDict.types[collectionGQLType.name].controller, collectionItem, session);
};
}

for (const element of collectionFieldsList) {
Expand Down