(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1643 | }); |
| 1644 | |
| 1645 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1646 | definition: TDefinition, |
| 1647 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1648 | ) => |
| 1649 | Effect.gen(function* () { |
| 1650 | const validationError = pluginStorageQueryValidationError( |
| 1651 | definition, |
| 1652 | queryInput as |
| 1653 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1654 | | undefined, |
| 1655 | ); |
| 1656 | if (validationError) return yield* validationError; |
| 1657 | |
| 1658 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1659 | where: whereFor(definition.name), |
| 1660 | }); |
| 1661 | const filtered = sortByOwnerPrecedence(rows) |
| 1662 | .filter((row) => |
| 1663 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1664 | ) |
| 1665 | .filter((row) => |
| 1666 | rowMatchesPluginStorageWhere( |
| 1667 | row, |
| 1668 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1669 | ), |
| 1670 | ); |
| 1671 | |
| 1672 | const sorted = |
| 1673 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1674 | ? [...filtered].sort((left, right) => { |
| 1675 | for (const order of queryInput.orderBy ?? []) { |
| 1676 | const direction = order.direction === "desc" ? -1 : 1; |
| 1677 | const compared = |
| 1678 | comparePluginStorageValues( |
| 1679 | pluginStorageDataField(left.data, order.field), |
| 1680 | pluginStorageDataField(right.data, order.field), |
| 1681 | ) * direction; |
| 1682 | if (compared !== 0) return compared; |
| 1683 | } |
| 1684 | return ( |
| 1685 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1686 | left.key.localeCompare(right.key) |
| 1687 | ); |
| 1688 | }) |
| 1689 | : filtered; |
| 1690 | |
| 1691 | const offset = queryInput?.offset ?? 0; |
| 1692 | const limited = |
| 1693 | queryInput?.limit === undefined |
| 1694 | ? sorted.slice(offset) |
| 1695 | : sorted.slice(offset, offset + queryInput.limit); |
| 1696 | return limited.map((row) => |
| 1697 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1698 | ); |
| 1699 | }); |
| 1700 | |
| 1701 | return { |
| 1702 | collection: (definition) => ({ |
no test coverage detected