MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / toORM

Function toORM

packages/core/fumadb/src/query/orm/index.ts:320–446  ·  view source on GitHub ↗
(
  adapter: ORMAdapter<S>,
  options: ToORMOptions = {},
)

Source from the content-addressed store, hash-verified

318}
319
320export 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) {

Callers 8

fromConvexFunction · 0.90
fromMongoDBFunction · 0.90
fromDrizzleFunction · 0.90
createORMFunction · 0.90
fromKyselyFunction · 0.90
fromPrismaFunction · 0.90
transactionFunction · 0.90
valueFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected