(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1704 | }); |
| 1705 | |
| 1706 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1707 | definition: TDefinition, |
| 1708 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1709 | ) => |
| 1710 | Effect.gen(function* () { |
| 1711 | const validationError = pluginStorageQueryValidationError( |
| 1712 | definition, |
| 1713 | queryInput as |
| 1714 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1715 | | undefined, |
| 1716 | ); |
| 1717 | if (validationError) return yield* validationError; |
| 1718 | |
| 1719 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1720 | where: whereFor(definition.name), |
| 1721 | }); |
| 1722 | const filtered = sortByOwnerPrecedence(rows) |
| 1723 | .filter((row) => |
| 1724 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1725 | ) |
| 1726 | .filter((row) => |
| 1727 | rowMatchesPluginStorageWhere( |
| 1728 | row, |
| 1729 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1730 | ), |
| 1731 | ); |
| 1732 | |
| 1733 | const sorted = |
| 1734 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1735 | ? [...filtered].sort((left, right) => { |
| 1736 | for (const order of queryInput.orderBy ?? []) { |
| 1737 | const direction = order.direction === "desc" ? -1 : 1; |
| 1738 | const compared = |
| 1739 | comparePluginStorageValues( |
| 1740 | pluginStorageDataField(left.data, order.field), |
| 1741 | pluginStorageDataField(right.data, order.field), |
| 1742 | ) * direction; |
| 1743 | if (compared !== 0) return compared; |
| 1744 | } |
| 1745 | return ( |
| 1746 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1747 | left.key.localeCompare(right.key) |
| 1748 | ); |
| 1749 | }) |
| 1750 | : filtered; |
| 1751 | |
| 1752 | const offset = queryInput?.offset ?? 0; |
| 1753 | const limited = |
| 1754 | queryInput?.limit === undefined |
| 1755 | ? sorted.slice(offset) |
| 1756 | : sorted.slice(offset, offset + queryInput.limit); |
| 1757 | return limited.map((row) => |
| 1758 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1759 | ); |
| 1760 | }); |
| 1761 | |
| 1762 | return { |
| 1763 | collection: (definition) => ({ |
no test coverage detected