Skip to content

Commit

Permalink
feat: add ability to introspect repository joins
Browse files Browse the repository at this point in the history
  • Loading branch information
retro committed Apr 14, 2024
1 parent d92b553 commit bc8edc8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,8 @@ await describe("semantic layer", async () => {
const docs: string[] = [];
const dimensions = repository.getDimensions();
const metrics = repository.getMetrics();
const joins = repository.getJoins();

for (const dimension of dimensions) {
docs.push(
`DIMENSION: ${dimension.getPath()}, TYPE: ${dimension.getType()}, DESCRIPTION: ${
Expand All @@ -1252,12 +1254,16 @@ await describe("semantic layer", async () => {
}, FORMAT: ${metric.getFormat() ?? "-"}`,
);
}
for (const join of joins) {
docs.push(`JOIN: ${join.left} -> ${join.right}, TYPE: ${join.type}`);
}

assert.deepEqual(docs, [
"DIMENSION: customers.customer_id, TYPE: number, DESCRIPTION: The unique identifier of the customer, FORMAT: -",
"DIMENSION: invoices.invoice_id, TYPE: number, DESCRIPTION: The unique identifier of the invoice, FORMAT: -",
"DIMENSION: invoices.customer_id, TYPE: number, DESCRIPTION: The unique identifier of the invoice customer, FORMAT: -",
"METRIC: invoices.total, TYPE: string, DESCRIPTION: -, FORMAT: percentage",
"JOIN: customers -> invoices, TYPE: oneToMany",
]);
});

Expand Down
6 changes: 6 additions & 0 deletions src/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ export class Repository<
return this.joins[modelName]?.[joinModelName];
}

getJoins() {
return Object.values(this.joins)
.flatMap((joins) => Object.values(joins))
.filter((join) => !join.reversed);
}

build(dialectName: AvailableDialects) {
const { client, Dialect } = getClientAndDialect(dialectName);
return new QueryBuilder<C, D, M, F>(this, new Dialect(), client);
Expand Down

0 comments on commit bc8edc8

Please sign in to comment.