(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1518 | }); |
| 1519 | |
| 1520 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1521 | definition: TDefinition, |
| 1522 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1523 | ) => |
| 1524 | Effect.gen(function* () { |
| 1525 | const validationError = pluginStorageQueryValidationError( |
| 1526 | definition, |
| 1527 | queryInput as |
| 1528 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1529 | | undefined, |
| 1530 | ); |
| 1531 | if (validationError) return yield* validationError; |
| 1532 | |
| 1533 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1534 | where: whereFor(definition.name), |
| 1535 | }); |
| 1536 | const filtered = sortByOwnerPrecedence(rows) |
| 1537 | .filter((row) => |
| 1538 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1539 | ) |
| 1540 | .filter((row) => |
| 1541 | rowMatchesPluginStorageWhere( |
| 1542 | row, |
| 1543 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1544 | ), |
| 1545 | ); |
| 1546 | |
| 1547 | const sorted = |
| 1548 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1549 | ? [...filtered].sort((left, right) => { |
| 1550 | for (const order of queryInput.orderBy ?? []) { |
| 1551 | const direction = order.direction === "desc" ? -1 : 1; |
| 1552 | const compared = |
| 1553 | comparePluginStorageValues( |
| 1554 | pluginStorageDataField(left.data, order.field), |
| 1555 | pluginStorageDataField(right.data, order.field), |
| 1556 | ) * direction; |
| 1557 | if (compared !== 0) return compared; |
| 1558 | } |
| 1559 | return ( |
| 1560 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1561 | left.key.localeCompare(right.key) |
| 1562 | ); |
| 1563 | }) |
| 1564 | : filtered; |
| 1565 | |
| 1566 | const offset = queryInput?.offset ?? 0; |
| 1567 | const limited = |
| 1568 | queryInput?.limit === undefined |
| 1569 | ? sorted.slice(offset) |
| 1570 | : sorted.slice(offset, offset + queryInput.limit); |
| 1571 | return limited.map((row) => |
| 1572 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1573 | ); |
| 1574 | }); |
| 1575 | |
| 1576 | return { |
| 1577 | collection: (definition) => ({ |
no test coverage detected