(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1737 | }); |
| 1738 | |
| 1739 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1740 | definition: TDefinition, |
| 1741 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1742 | ) => |
| 1743 | Effect.gen(function* () { |
| 1744 | const validationError = pluginStorageQueryValidationError( |
| 1745 | definition, |
| 1746 | queryInput as |
| 1747 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1748 | | undefined, |
| 1749 | ); |
| 1750 | if (validationError) return yield* validationError; |
| 1751 | |
| 1752 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1753 | where: whereFor(definition.name), |
| 1754 | }); |
| 1755 | const filtered = sortByOwnerPrecedence(rows) |
| 1756 | .filter((row) => |
| 1757 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1758 | ) |
| 1759 | .filter((row) => |
| 1760 | rowMatchesPluginStorageWhere( |
| 1761 | row, |
| 1762 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1763 | ), |
| 1764 | ); |
| 1765 | |
| 1766 | const sorted = |
| 1767 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1768 | ? [...filtered].sort((left, right) => { |
| 1769 | for (const order of queryInput.orderBy ?? []) { |
| 1770 | const direction = order.direction === "desc" ? -1 : 1; |
| 1771 | const compared = |
| 1772 | comparePluginStorageValues( |
| 1773 | pluginStorageDataField(left.data, order.field), |
| 1774 | pluginStorageDataField(right.data, order.field), |
| 1775 | ) * direction; |
| 1776 | if (compared !== 0) return compared; |
| 1777 | } |
| 1778 | return ( |
| 1779 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1780 | left.key.localeCompare(right.key) |
| 1781 | ); |
| 1782 | }) |
| 1783 | : filtered; |
| 1784 | |
| 1785 | const offset = queryInput?.offset ?? 0; |
| 1786 | const limited = |
| 1787 | queryInput?.limit === undefined |
| 1788 | ? sorted.slice(offset) |
| 1789 | : sorted.slice(offset, offset + queryInput.limit); |
| 1790 | return limited.map((row) => |
| 1791 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1792 | ); |
| 1793 | }); |
| 1794 | |
| 1795 | return { |
| 1796 | collection: (definition) => ({ |
no test coverage detected