* Get items by query.
(query: Query, opts?: QueryOptions)
| 519 | * Get items by query. |
| 520 | */ |
| 521 | async readByQuery(query: Query, opts?: QueryOptions): Promise<Item[]> { |
| 522 | if (query.version && !isPublishedVersionKey(query.version)) { |
| 523 | return (await handleVersion(this, opts?.key ?? null, query, opts)) as Item[]; |
| 524 | } |
| 525 | |
| 526 | const updatedQuery = |
| 527 | opts?.emitEvents !== false |
| 528 | ? await emitter.emitFilter( |
| 529 | this.eventScope === 'items' |
| 530 | ? ['items.query', `${this.collection}.items.query`] |
| 531 | : `${this.eventScope}.query`, |
| 532 | query, |
| 533 | { |
| 534 | collection: this.collection, |
| 535 | }, |
| 536 | { |
| 537 | database: this.knex, |
| 538 | schema: this.schema, |
| 539 | accountability: this.accountability, |
| 540 | }, |
| 541 | ) |
| 542 | : query; |
| 543 | |
| 544 | let ast = await getAstFromQuery( |
| 545 | { |
| 546 | collection: this.collection, |
| 547 | query: updatedQuery, |
| 548 | accountability: this.accountability, |
| 549 | }, |
| 550 | { |
| 551 | schema: this.schema, |
| 552 | knex: this.knex, |
| 553 | }, |
| 554 | ); |
| 555 | |
| 556 | ast = await processAst( |
| 557 | { ast, action: 'read', accountability: this.accountability }, |
| 558 | { knex: this.knex, schema: this.schema }, |
| 559 | ); |
| 560 | |
| 561 | const records = await runAst(ast, this.schema, this.accountability, { |
| 562 | knex: this.knex, |
| 563 | // GraphQL requires relational keys to be returned regardless |
| 564 | stripNonRequested: opts?.stripNonRequested !== undefined ? opts.stripNonRequested : true, |
| 565 | }); |
| 566 | |
| 567 | // TODO when would this happen? |
| 568 | if (records === null) { |
| 569 | throw new ForbiddenError(); |
| 570 | } |
| 571 | |
| 572 | const filteredRecords = |
| 573 | opts?.emitEvents !== false |
| 574 | ? await emitter.emitFilter( |
| 575 | this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`, |
| 576 | records, |
| 577 | { |
| 578 | query: updatedQuery, |
no test coverage detected