Skip to content

Commit

Permalink
feat: use findFirst in get if id and query.id is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ps73 committed Feb 16, 2022
1 parent 331b066 commit 563162d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion dist/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ class PrismaService extends adapter_commons_1.AdapterService {
try {
const { query, filters } = this.filterQuery(params);
const { whitelist } = this.options;
(0, utils_1.checkIdInQuery)({ id, query, idField: this.options.id });
const { where, select, include } = (0, utils_1.buildPrismaQueryParams)({
id, query, filters, whitelist,
}, this.options.id);
if (typeof query.id === 'object') {
const result = yield this.Model.findFirst(Object.assign({ where: Object.assign(Object.assign({}, where), { id: Object.assign(Object.assign({}, where.id), { equals: id }) }) }, (0, utils_1.buildSelectOrInclude)({ select, include })));
if (!result)
throw new errors.NotFound(`No record found for id '${id}' and query`);
return result;
}
(0, utils_1.checkIdInQuery)({ id, query, idField: this.options.id });
const result = yield this.Model.findUnique(Object.assign({ where }, (0, utils_1.buildSelectOrInclude)({ select, include })));
if (!result)
throw new errors.NotFound(`No record found for id '${id}'`);
Expand Down
Binary file modified prisma/tests.db
Binary file not shown.
16 changes: 15 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,24 @@ export class PrismaService<ModelData = Record<string, any>> extends AdapterServi
try {
const { query, filters } = this.filterQuery(params);
const { whitelist } = this.options;
checkIdInQuery({ id, query, idField: this.options.id });
const { where, select, include } = buildPrismaQueryParams({
id, query, filters, whitelist,
}, this.options.id);
if (typeof query.id === 'object') {
const result: Partial<ModelData> = await this.Model.findFirst({
where: {
...where,
id: {
...where.id,
equals: id,
},
},
...buildSelectOrInclude({ select, include }),
});
if (!result) throw new errors.NotFound(`No record found for id '${id}' and query`);
return result;
}
checkIdInQuery({ id, query, idField: this.options.id });
const result: Partial<ModelData> = await this.Model.findUnique({
where,
...buildSelectOrInclude({ select, include }),
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,27 @@ describe('Feathers Prisma Service', () => {

expect(result).to.have.lengthOf(1);
});

it('.get + multiple id queries + NotFound', async () => {
try {
await todosService.create([
{ title: 'Todo2', prio: 2, userId: data.id },
{ title: 'Todo3', prio: 4, done: true, userId: data.id },
]);
const results = await todosService.find();
const inIds = [results[1].id, results[2].id];

await todosService.get(results[0].id, {
query: {
id: {
$in: inIds,
}
},
});
} catch (e) {
expect(e.code).to.be.equal(404);
}
});
});
});

Expand Down

0 comments on commit 563162d

Please sign in to comment.