@algochad/prisma-core
Version:
A comprehensive NestJS library that provides EF-Core-like operations using Prisma and GraphQL. Features LINQ-style query builders, advanced data manipulation, GraphQL integration with genql, and a unified API for both Prisma and GraphQL operations. Includ
167 lines • 6.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLMutationBuilder = void 0;
class GraphQLMutationBuilder {
genqlClient;
operationNames;
constructor(genqlClient, operationNames) {
this.genqlClient = genqlClient;
this.operationNames = operationNames;
}
static create(genqlClient, operationNames) {
return new GraphQLMutationBuilder(genqlClient, operationNames);
}
static createFrom(builder, genqlClient, operationNames) {
return new GraphQLMutationBuilder(genqlClient, operationNames);
}
async Create(data, select) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
const mutation = {
[this.operationNames.create]: {
__args: { data },
...(select || { __scalar: true }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.create];
}
async CreateMany(data, returnCount = true) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
if (!this.operationNames.createMany) {
throw new Error('CreateMany operation not configured');
}
const mutation = {
[this.operationNames.createMany]: {
__args: { data },
...(returnCount ? { count: true } : { __scalar: true }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.createMany];
}
async Update(where, data, select) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
const mutation = {
[this.operationNames.update]: {
__args: { where, data },
...(select || { __scalar: true }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.update];
}
async UpdateMany(where, data) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
if (!this.operationNames.updateMany) {
throw new Error('UpdateMany operation not configured');
}
const mutation = {
[this.operationNames.updateMany]: {
__args: { where, data },
count: true,
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.updateMany];
}
async Upsert(where, create, update, select) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
if (!this.operationNames.upsert) {
throw new Error('Upsert operation not configured');
}
const mutation = {
[this.operationNames.upsert]: {
__args: { where, create, update },
...(select || { __scalar: true }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.upsert];
}
async Delete(where, select) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
const mutation = {
[this.operationNames.delete]: {
__args: { where },
...(select || { __scalar: true }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.delete];
}
async DeleteMany(where) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
if (!this.operationNames.deleteMany) {
throw new Error('DeleteMany operation not configured');
}
const mutation = {
[this.operationNames.deleteMany]: {
__args: { where },
count: true,
},
};
const result = await this.genqlClient.mutation(mutation);
return result[this.operationNames.deleteMany];
}
async BulkUpsert(records) {
if (!this.operationNames.upsert) {
throw new Error('Upsert operation not configured');
}
const results = [];
for (const record of records) {
const result = await this.Upsert(record.where, record.create, record.update);
results.push(result);
}
return results;
}
async ExecuteCustomMutation(operationName, args, selection = true) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
const mutation = {
[operationName]: {
__args: args,
...(typeof selection === 'object'
? selection
: { result: selection }),
},
};
const result = await this.genqlClient.mutation(mutation);
return result[operationName];
}
async ExecuteBatch(mutations) {
if (!this.genqlClient.mutation) {
throw new Error('Mutation not supported by this GraphQL client');
}
const batchMutation = {};
const operationKeys = [];
mutations.forEach((mut, index) => {
const key = `${mut.operationName}_${index}`;
operationKeys.push(key);
batchMutation[key] = {
__args: mut.args,
...(typeof mut.selection === 'object'
? mut.selection
: { result: mut.selection || true }),
};
});
const result = await this.genqlClient.mutation(batchMutation);
return operationKeys.map((key) => result[key]);
}
}
exports.GraphQLMutationBuilder = GraphQLMutationBuilder;
//# sourceMappingURL=graphql-mutation-builder.js.map