(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1352 | }); |
| 1353 | |
| 1354 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1355 | definition: TDefinition, |
| 1356 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1357 | ) => |
| 1358 | Effect.gen(function* () { |
| 1359 | const validationError = pluginStorageQueryValidationError( |
| 1360 | definition, |
| 1361 | queryInput as |
| 1362 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1363 | | undefined, |
| 1364 | ); |
| 1365 | if (validationError) return yield* validationError; |
| 1366 | |
| 1367 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1368 | where: whereFor(definition.name), |
| 1369 | }); |
| 1370 | const filtered = sortByOwnerPrecedence(rows) |
| 1371 | .filter((row) => |
| 1372 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1373 | ) |
| 1374 | .filter((row) => |
| 1375 | rowMatchesPluginStorageWhere( |
| 1376 | row, |
| 1377 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1378 | ), |
| 1379 | ); |
| 1380 | |
| 1381 | const sorted = |
| 1382 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1383 | ? [...filtered].sort((left, right) => { |
| 1384 | for (const order of queryInput.orderBy ?? []) { |
| 1385 | const direction = order.direction === "desc" ? -1 : 1; |
| 1386 | const compared = |
| 1387 | comparePluginStorageValues( |
| 1388 | pluginStorageDataField(left.data, order.field), |
| 1389 | pluginStorageDataField(right.data, order.field), |
| 1390 | ) * direction; |
| 1391 | if (compared !== 0) return compared; |
| 1392 | } |
| 1393 | return ( |
| 1394 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1395 | left.key.localeCompare(right.key) |
| 1396 | ); |
| 1397 | }) |
| 1398 | : filtered; |
| 1399 | |
| 1400 | const offset = queryInput?.offset ?? 0; |
| 1401 | const limited = |
| 1402 | queryInput?.limit === undefined |
| 1403 | ? sorted.slice(offset) |
| 1404 | : sorted.slice(offset, offset + queryInput.limit); |
| 1405 | return limited.map((row) => |
| 1406 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1407 | ); |
| 1408 | }); |
| 1409 | |
| 1410 | return { |
| 1411 | collection: (definition) => ({ |
no test coverage detected