Skip to content

Commit

Permalink
feat: Non-entity tables can be queies with global ambits
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-crowell committed Aug 2, 2024
1 parent 4bbd77a commit 8e7a296
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 0 additions & 4 deletions packages/data/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Core
import * as action from './schema/action.ts';
import * as amit from './schema/ambit.ts';
import * as dispatch from './schema/dispatch.ts';
import * as mutate from './schema/mutate.ts';
import * as session from './schema/session.ts';
Expand Down Expand Up @@ -59,7 +58,6 @@ export function schemaFlatten<G extends Record<string, { table: unknown; relates

export const schema = schemaFlatten({
action,
amit,
dispatch,
mutate,
session,
Expand All @@ -84,7 +82,6 @@ export const schema = schemaFlatten({

export {
action,
amit,
dispatch,
mutate,
session,
Expand Down Expand Up @@ -137,7 +134,6 @@ export type SchemaSelect = {
*/
export const core = {
action,
amit,
dispatch,
mutate,
session,
Expand Down
12 changes: 10 additions & 2 deletions packages/data/src/transaction/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Transaction } from './transaction.types';
import { sql, SQL, type TableConfig } from 'drizzle-orm';
import { getTableName, sql, SQL, type TableConfig } from 'drizzle-orm';
import type { PgTableWithColumns } from 'drizzle-orm/pg-core';
import { scope } from '@do-ob/data/scope';
import { type Input } from '@do-ob/core';
Expand Down Expand Up @@ -36,7 +36,15 @@ export function query<
throw new Error('Unauthorized. No subject provided for the query operation.');
}

const result = await tx.select().from(table).fullJoin(schema.entity, dataFilter.eq(table.$id, schema.entity.$id)).limit(limit).offset(offset)
const tableName = getTableName(table);

const builder = tx.select().from(table);

if(tableName.startsWith('entity_')) {
builder.leftJoin(schema.entity, dataFilter.eq(table.$id, schema.entity.$id));
}

const result = await builder.limit(limit).offset(offset)
.orderBy(...order({ table, entity: schema.entity }, dataOrder))
.where(dataFilter.and(
scope($subject, ambit),
Expand Down

0 comments on commit 8e7a296

Please sign in to comment.