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