Skip to content

Commit

Permalink
updateRelationInCrud (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrui committed Aug 3, 2023
1 parent 32a936f commit 87a4e25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/integrations/gei-crud/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gei-crud",
"version": "0.7.2",
"version": "0.7.3",
"description": "GraphQL Editor integration for stucco. Allows basic crud operations and relations.",
"main": "lib/index.js",
"private": false,
Expand Down
11 changes: 7 additions & 4 deletions packages/integrations/gei-crud/src/Object/oneToMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { prepareRelatedField, prepareRelatedModel } from '../data.js';
import { DB } from '../db/mongo.js';

export const handler = async (input: FieldResolveInput) =>
DB().then((db) => {
DB().then(async (db) => {
const source = input.source;
if (!source) {
throw new Error(
'Invalid input. This resolver work only if it is piped from other resolver. Either make it correct way or remove sourceParameters from resolver',
);
}

const s = source as Record<string, any>;
const field = prepareRelatedField(input);
const prepareField = prepareRelatedField(input).replace(/[{ }]/g, '').split(":");
const fieldForFounding = prepareField[0];
const fieldWithArray = prepareField[1] ? prepareField[1] : undefined

return db
.collection(prepareRelatedModel(input))
.find({ [field]: s._id })
.toArray();
.find( { [fieldForFounding]: (fieldWithArray? { $in: s[fieldWithArray]} : s._id )}).toArray()
});
6 changes: 4 additions & 2 deletions packages/integrations/gei-crud/src/Object/oneToOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const handler = async (input: FieldResolveInput) =>
);
}
const s = source as Record<string, any>;
const field = prepareRelatedField(input);
return db.collection(prepareRelatedModel(input)).findOne({ [field]: s._id });
const prepareField = prepareRelatedField(input).replace(/[{ }]/g, '').split(":");
const field = prepareField[0] ;
const arIds = prepareField[1] ? prepareField[1] : undefined
return db.collection(prepareRelatedModel(input)).findOne({ [field]: arIds || s._id });
});

0 comments on commit 87a4e25

Please sign in to comment.