(
definition: TDefinition,
queryInput?: PluginStorageCollectionQueryInput<TDefinition>,
)
| 1404 | }); |
| 1405 | |
| 1406 | const queryCollection = <TDefinition extends PluginStorageCollectionDefinition>( |
| 1407 | definition: TDefinition, |
| 1408 | queryInput?: PluginStorageCollectionQueryInput<TDefinition>, |
| 1409 | ) => |
| 1410 | Effect.gen(function* () { |
| 1411 | const validationError = pluginStorageQueryValidationError( |
| 1412 | definition, |
| 1413 | queryInput as |
| 1414 | | PluginStorageCollectionQueryInput<PluginStorageCollectionDefinition> |
| 1415 | | undefined, |
| 1416 | ); |
| 1417 | if (validationError) return yield* validationError; |
| 1418 | |
| 1419 | const rows = yield* input.core.findMany("plugin_storage", { |
| 1420 | where: whereFor(definition.name), |
| 1421 | }); |
| 1422 | const filtered = sortByOwnerPrecedence(rows) |
| 1423 | .filter((row) => |
| 1424 | queryInput?.keyPrefix === undefined ? true : row.key.startsWith(queryInput.keyPrefix), |
| 1425 | ) |
| 1426 | .filter((row) => |
| 1427 | rowMatchesPluginStorageWhere( |
| 1428 | row, |
| 1429 | queryInput?.where as Readonly<Record<string, unknown>> | undefined, |
| 1430 | ), |
| 1431 | ); |
| 1432 | |
| 1433 | const sorted = |
| 1434 | queryInput?.orderBy && queryInput.orderBy.length > 0 |
| 1435 | ? [...filtered].sort((left, right) => { |
| 1436 | for (const order of queryInput.orderBy ?? []) { |
| 1437 | const direction = order.direction === "desc" ? -1 : 1; |
| 1438 | const compared = |
| 1439 | comparePluginStorageValues( |
| 1440 | pluginStorageDataField(left.data, order.field), |
| 1441 | pluginStorageDataField(right.data, order.field), |
| 1442 | ) * direction; |
| 1443 | if (compared !== 0) return compared; |
| 1444 | } |
| 1445 | return ( |
| 1446 | ownerRank(left.owner as Owner) - ownerRank(right.owner as Owner) || |
| 1447 | left.key.localeCompare(right.key) |
| 1448 | ); |
| 1449 | }) |
| 1450 | : filtered; |
| 1451 | |
| 1452 | const offset = queryInput?.offset ?? 0; |
| 1453 | const limited = |
| 1454 | queryInput?.limit === undefined |
| 1455 | ? sorted.slice(offset) |
| 1456 | : sorted.slice(offset, offset + queryInput.limit); |
| 1457 | return limited.map((row) => |
| 1458 | pluginStorageEntryFromRow<PluginStorageCollectionData<TDefinition>>(row), |
| 1459 | ); |
| 1460 | }); |
| 1461 | |
| 1462 | return { |
| 1463 | collection: (definition) => ({ |
no test coverage detected