MCPcopy
hub / github.com/directus/directus / readByQuery

Method readByQuery

api/src/services/items.ts:521–606  ·  view source on GitHub ↗

* Get items by query.

(query: Query, opts?: QueryOptions)

Source from the content-addressed store, hash-verified

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,

Callers 15

handleVersionFunction · 0.95
getCoreGraceExpiresAtFunction · 0.95
handlerFunction · 0.95
handleRequestMethod · 0.95
readConfigMethod · 0.95
syncWebhookMethod · 0.95
readByQueryMethod · 0.95
readConfigMethod · 0.95
getKeysByQueryMethod · 0.95
readOneMethod · 0.95
readManyMethod · 0.95

Calls 8

isPublishedVersionKeyFunction · 0.90
handleVersionFunction · 0.85
getAstFromQueryFunction · 0.85
processAstFunction · 0.85
runAstFunction · 0.85
getDatabaseFunction · 0.85
emitFilterMethod · 0.80
emitActionMethod · 0.80

Tested by

no test coverage detected