(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1127 | }); |
| 1128 | |
| 1129 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1130 | definition: TDefinition, |
| 1131 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1132 | ) => |
| 1133 | Effect.gen(function* () { |
| 1134 | const validationError = pluginStorageQueryValidationError( |
| 1135 | definition, |
| 1136 | queryInput as |
| 1137 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1138 | | undefined, |
| 1139 | ); |
| 1140 | if (validationError) return yield* validationError; |
| 1141 | |
| 1142 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1143 | where: whereFor(definition.name), |
| 1144 | }); |
| 1145 | const filtered = sortByOwnerPrecedence(rows) |
| 1146 | .filter((row) => |
| 1147 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1148 | ) |
| 1149 | .filter((row) => |
| 1150 | rowMatchesPluginStorageWhere( |
| 1151 | row, |
| 1152 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1153 | ), |
| 1154 | ); |
| 1155 | |
| 1156 | const sorted = |
| 1157 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1158 | ? [...filtered].sort((left, right) => { |
| 1159 | for (const order of queryInput.orderBy ?? []) { |
| 1160 | const direction = order.direction === "desc" ? -1 : 1; |
| 1161 | const compared = |
| 1162 | comparePluginStorageValues( |
| 1163 | pluginStorageDataField(left.data, order.field), |
| 1164 | pluginStorageDataField(right.data, order.field), |
| 1165 | ) * direction; |
| 1166 | if (compared !== 0) return compared; |
| 1167 | } |
| 1168 | return ( |
| 1169 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1170 | left.key.localeCompare(right.key) |
| 1171 | ); |
| 1172 | }) |
| 1173 | : filtered; |
| 1174 | |
| 1175 | const offset = queryInput?.offset ?? 0; |
| 1176 | const limited = |
| 1177 | queryInput?.limit === undefined |
| 1178 | ? sorted.slice(offset) |
| 1179 | : sorted.slice(offset, offset + queryInput.limit); |
| 1180 | return limited.map((row) => |
| 1181 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1182 | ); |
| 1183 | }); |
| 1184 | |
| 1185 | return { |
| 1186 | collection: (definition) => ({ |
no test coverage detected