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