(
adapter: ORMAdapter<S>,
options: ToORMOptions = {},
)
| 318 | } |
| 319 | |
| 320 | export function toORM<S extends AnySchema>( |
| 321 | adapter: ORMAdapter<S>, |
| 322 | options: ToORMOptions = {}, |
| 323 | ): AbstractQuery<S> { |
| 324 | const context = options.context ?? adapter.context; |
| 325 | const internal: ORMAdapter<S> = |
| 326 | context === adapter.context ? adapter : { ...adapter, context }; |
| 327 | |
| 328 | function toTable<TableName extends keyof S["tables"]>( |
| 329 | name: TableName, |
| 330 | ): S["tables"][TableName] { |
| 331 | const table = internal.tables[name]; |
| 332 | if (!table) throw new Error(`[FumaDB] Invalid table name ${String(name)}.`); |
| 333 | |
| 334 | return table; |
| 335 | } |
| 336 | |
| 337 | const query = { |
| 338 | internal, |
| 339 | async count(name, { where } = {}) { |
| 340 | const table = toTable(name); |
| 341 | let conditions = where ? buildCondition(table.columns, where) : undefined; |
| 342 | if (conditions === true) conditions = undefined; |
| 343 | if (conditions === false) return 0; |
| 344 | |
| 345 | const constrainedWhere = await applyReadPolicies(table, conditions, context); |
| 346 | if (constrainedWhere === false) return 0; |
| 347 | return await internal.count(table, { where: constrainedWhere }); |
| 348 | }, |
| 349 | async upsert(name, { where, ...options }) { |
| 350 | const table = toTable(name); |
| 351 | const conditions = where ? buildCondition(table.columns, where) : undefined; |
| 352 | if (conditions === false) return; |
| 353 | let compiledWhere: Condition | undefined | false = conditions === true ? undefined : conditions; |
| 354 | |
| 355 | compiledWhere = await applyUpdatePolicies( |
| 356 | table, |
| 357 | compiledWhere, |
| 358 | options.update, |
| 359 | context, |
| 360 | "upsert", |
| 361 | options.create, |
| 362 | ); |
| 363 | if (compiledWhere === false) return; |
| 364 | await runCreatePolicies(table, options.create, context); |
| 365 | await internal.upsert(table, { |
| 366 | where: compiledWhere, |
| 367 | ...options, |
| 368 | }); |
| 369 | }, |
| 370 | async create(name, values) { |
| 371 | const table = toTable(name); |
| 372 | await runCreatePolicies(table, values, context); |
| 373 | return await internal.create(table, values); |
| 374 | }, |
| 375 | async createMany(name, values) { |
| 376 | const table = toTable(name); |
| 377 | for (const value of values) { |
no outgoing calls
no test coverage detected