UNPKG

@grapi/server

Version:

Grapi Schema Generator For GraphQL Server

86 lines (85 loc) 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); const dataModel_1 = require("../dataModel"); const lodash_1 = require("../lodash"); const index_1 = require("./index"); class ManyToMany { modelA; modelB; modelAField; modelBField; constructor({ modelA, modelB, modelAField, modelBField, }) { this.modelA = modelA; this.modelB = modelB; this.modelAField = modelAField; this.modelBField = modelBField; } getType() { return dataModel_1.RelationType.biManyToMany; } getModelA() { return this.modelA; } getModelAField() { return this.modelAField; } getModelB() { return this.modelB; } getModelBField() { return this.modelBField; } async addId({ modelAId, modelBId }, context) { await this.modelB.getDataSource().addIdToManyRelation(this.modelA.getNamings().singular, this.modelB.getNamings().singular, modelAId, modelBId, context); await this.modelA.getDataSource().addIdToManyRelation(this.modelB.getNamings().singular, this.modelA.getNamings().singular, modelBId, modelAId, context); } async createAndAddIdFromRefSide(model, modelData, refData, context) { let record; const { modelAId, modelBId } = refData; const execution = async (data) => { const mutation = model.getCreateMutationFactory().createMutation(data); return { object: await model.getDataSource().create(mutation) }; }; const { rootData, createdData, executed } = await (0, index_1.InputRecursiveRelation)(modelData, model, context, execution); if (executed) record = executed.object; else record = (await execution({ ...rootData, ...createdData })).object; return await this.addId({ modelAId: modelAId || record.id, modelBId: modelBId || record.id }, context); } async createAndAddIdForModelA({ modelAId, modelBData }, context) { return await this.createAndAddIdFromRefSide(this.modelB, modelBData, { modelAId }, context); } async createAndAddIdForModelB({ modelBId, modelAData }, context) { return await this.createAndAddIdFromRefSide(this.modelA, modelAData, { modelBId }, context); } async removeId({ modelAId, modelBId }, context) { await this.modelB.getDataSource().removeIdFromManyRelation(this.modelA.getNamings().singular, this.modelB.getNamings().singular, modelAId, modelBId, context); await this.modelA.getDataSource().removeIdFromManyRelation(this.modelB.getNamings().singular, this.modelA.getNamings().singular, modelBId, modelAId, context); } async deleteAndRemoveIdFromModelA({ modelAId, modelBId }, context) { await this.modelA.getDataSource().delete({ id: { [__1.Operator.eq]: modelAId } }); return this.removeId({ modelAId, modelBId }, context); } async deleteAndRemoveIdFromModelB({ modelAId, modelBId }, context) { await this.modelB.getDataSource().delete({ id: { [__1.Operator.eq]: modelBId } }); return this.removeId({ modelAId, modelBId }, context); } async joinModelA(modelBId, argument, context) { const listFindQuery = this.listFindQuery(argument); return await this.modelA.getDataSource().findManyFromManyRelation(this.modelB.getNamings().singular, this.modelA.getNamings().singular, modelBId, listFindQuery, context); } async joinModelB(modelAId, argument, context) { const listFindQuery = this.listFindQuery(argument); return await this.modelB.getDataSource().findManyFromManyRelation(this.modelA.getNamings().singular, this.modelB.getNamings().singular, modelAId, listFindQuery, context); } listFindQuery(argument) { let where = (0, lodash_1.get)(argument, `where`, {}); let orderBy = (0, lodash_1.get)(argument, `orderBy`, {}); where = __1.WhereInputPlugin.parseWhereIterate(where, this.modelB); orderBy = __1.OrderInputPlugin.parseOrder(orderBy); return { orderBy, where }; } } exports.default = ManyToMany;