(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1374 | }); |
| 1375 | |
| 1376 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1377 | definition: TDefinition, |
| 1378 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1379 | ) => |
| 1380 | Effect.gen(function* () { |
| 1381 | const validationError = pluginStorageQueryValidationError( |
| 1382 | definition, |
| 1383 | queryInput as |
| 1384 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1385 | | undefined, |
| 1386 | ); |
| 1387 | if (validationError) return yield* validationError; |
| 1388 | |
| 1389 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1390 | where: whereFor(definition.name), |
| 1391 | }); |
| 1392 | const filtered = sortByOwnerPrecedence(rows) |
| 1393 | .filter((row) => |
| 1394 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1395 | ) |
| 1396 | .filter((row) => |
| 1397 | rowMatchesPluginStorageWhere( |
| 1398 | row, |
| 1399 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1400 | ), |
| 1401 | ); |
| 1402 | |
| 1403 | const sorted = |
| 1404 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1405 | ? [...filtered].sort((left, right) => { |
| 1406 | for (const order of queryInput.orderBy ?? []) { |
| 1407 | const direction = order.direction === "desc" ? -1 : 1; |
| 1408 | const compared = |
| 1409 | comparePluginStorageValues( |
| 1410 | pluginStorageDataField(left.data, order.field), |
| 1411 | pluginStorageDataField(right.data, order.field), |
| 1412 | ) * direction; |
| 1413 | if (compared !== 0) return compared; |
| 1414 | } |
| 1415 | return ( |
| 1416 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1417 | left.key.localeCompare(right.key) |
| 1418 | ); |
| 1419 | }) |
| 1420 | : filtered; |
| 1421 | |
| 1422 | const offset = queryInput?.offset ?? 0; |
| 1423 | const limited = |
| 1424 | queryInput?.limit === undefined |
| 1425 | ? sorted.slice(offset) |
| 1426 | : sorted.slice(offset, offset + queryInput.limit); |
| 1427 | return limited.map((row) => |
| 1428 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1429 | ); |
| 1430 | }); |
| 1431 | |
| 1432 | return { |
| 1433 | collection: (definition) => ({ |
no test coverage detected