Skip to content

Commit

Permalink
More Entquery filters (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Sep 5, 2023
1 parent 4e61e60 commit 748ef42
Show file tree
Hide file tree
Showing 13 changed files with 1,145 additions and 132 deletions.
9 changes: 9 additions & 0 deletions ts/src/core/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,22 @@ export interface QueryableDataOptions
extends SelectBaseDataOptions,
QueryDataOptions {}

// for now, no complicated joins or no need to support multiple joins
// just one simple join
interface JoinOptions<T2 extends Data = Data, K2 = keyof T2> {
tableName: string;
alias?: string;
clause: clause.Clause<T2,K2>;
}

export interface QueryDataOptions<T extends Data = Data, K = keyof T> {
distinct?: boolean;
clause: clause.Clause<T, K>;
orderby?: OrderBy; // this technically doesn't make sense when querying just one row but whatevs
groupby?: K;
limit?: number;
disableTransformations?: boolean;
join?:JoinOptions;
}

// For loading data from database
Expand Down
53 changes: 32 additions & 21 deletions ts/src/core/clause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class simpleClause<T extends Data, K = keyof T> implements Clause<T, K> {
}
}

// NB: we're not using alias in this class in clause method
// if we end up with a subclass that does, we need to handle it
class queryClause<T extends Data, K = keyof T> implements Clause<T, K> {
constructor(
protected dependentQueryOptions: QueryableDataOptions, // private value: any, // private op: string, // private handleNull?: Clause<T, K>,
Expand Down Expand Up @@ -143,20 +145,6 @@ class existsQueryClause<T extends Data, K = keyof T> extends queryClause<T, K> {
}
}

class columnInQueryClause<T extends Data, K = keyof T> extends queryClause<
T,
K
> {
constructor(
protected dependentQueryOptions: QueryableDataOptions,
protected col: K,
) {
// TODO renderCol needed here...
//TODO cal just kill this
super(dependentQueryOptions, `${col} IN`);
}
}

class isNullClause<T extends Data, K = keyof T> implements Clause<T, K> {
constructor(protected col: K) {}

Expand Down Expand Up @@ -205,6 +193,30 @@ class isNotNullClause<T extends Data, K = keyof T> implements Clause<T, K> {
}
}

class simpleExpression<T extends Data, K = keyof T> implements Clause<T, K> {
constructor(protected expression: string) {}

clause(idx: number, alias?: string): string {
return this.expression;
}

columns(): K[] {
return [];
}

values(): any[] {
return [];
}

logValues(): any[] {
return [];
}

instanceKey(): string {
return `${this.expression}`;
}
}

class arraySimpleClause<T extends Data, K = keyof T> implements Clause<T, K> {
constructor(protected col: K, private value: any, private op: string) {}

Expand Down Expand Up @@ -835,13 +847,6 @@ export function DBTypeNotIn<T extends Data, K = keyof T>(
return new notInClause(col, values, typ);
}

export function ColInQuery<T extends Data, K = keyof T>(
col: K,
queryOptions: QueryableDataOptions,
): Clause<T, K> {
return new columnInQueryClause(queryOptions, col);
}

interface TsQuery {
// todo lang ::reconfig
language: "english" | "french" | "german" | "simple";
Expand Down Expand Up @@ -1212,3 +1217,9 @@ export function getCombinedClause<V extends Data = Data, K = keyof V>(
}
return cls;
}

export function Expression<T extends Data, K = keyof T>(
expression: string,
): Clause<T, K> {
return new simpleExpression(expression);
}
36 changes: 30 additions & 6 deletions ts/src/core/ent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ function defaultEdgeQueryOptions(
id1: ID,
edgeType: string,
id2?: ID,
): EdgeQueryableDataOptions {
): Required<Omit<EdgeQueryableDataOptions, "disableTransformations">> {
let cls = clause.And(clause.Eq("id1", id1), clause.Eq("edge_type", edgeType));
if (id2) {
cls = clause.And(cls, clause.Eq("id2", id2));
Expand Down Expand Up @@ -1424,7 +1424,7 @@ export async function loadCustomEdges<T extends AssocEdge>(
fields,
defaultOptions,
tableName,
} = await loadEgesInfo(options);
} = await loadEdgesInfo(options);

const rows = await loadRows({
tableName,
Expand All @@ -1439,7 +1439,7 @@ export async function loadCustomEdges<T extends AssocEdge>(
});
}

async function loadEgesInfo<T extends AssocEdge>(
async function loadEdgesInfo<T extends AssocEdge>(
options: loadCustomEdgesOptions<T>,
id2?: ID,
) {
Expand All @@ -1450,7 +1450,7 @@ async function loadEgesInfo<T extends AssocEdge>(
}

const defaultOptions = defaultEdgeQueryOptions(id1, edgeType, id2);
let cls = defaultOptions.clause!;
let cls = defaultOptions.clause;
if (options.queryOptions?.clause) {
cls = clause.And(cls, options.queryOptions.clause);
}
Expand Down Expand Up @@ -1543,11 +1543,11 @@ export async function loadEdgeForID2<T extends AssocEdge>(
cls: actualClause,
fields,
tableName,
} = await loadEgesInfo(options, options.id2);
} = await loadEdgesInfo(options, options.id2);

const row = await loadRow({
tableName,
fields: fields,
fields,
clause: actualClause,
context: options.context,
});
Expand All @@ -1556,6 +1556,30 @@ export async function loadEdgeForID2<T extends AssocEdge>(
}
}

export async function loadTwoWayEdges<T extends AssocEdge>(
opts: loadCustomEdgesOptions<T>,
): Promise<T[]> {
const { cls: actualClause, fields, tableName } = await loadEdgesInfo(opts);

const rows = await loadRows({
tableName,
alias: "t1",
fields,
clause: actualClause,
context: opts.context,
join: {
tableName,
alias: "t2",
clause: clause.And(
// these are not values so need this to not think they're values...
clause.Expression("t1.id1 = t2.id2"),
clause.Expression("t1.id2 = t2.id1"),
),
},
});
return rows as T[];
}

export async function loadNodesByEdge<T extends Ent>(
viewer: Viewer,
id1: ID,
Expand Down
Loading

0 comments on commit 748ef42

Please sign in to comment.