Skip to content

Commit

Permalink
feat: API improvements (#5)
Browse files Browse the repository at this point in the history
- type inference for query results
- rename database to repository
- reorganize codebase
- repository.build now accepts dialect name
- expose query builder schema
  • Loading branch information
retro committed Mar 20, 2024
1 parent d9821e3 commit 5bdf207
Show file tree
Hide file tree
Showing 25 changed files with 1,082 additions and 283 deletions.
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ This library allows you to define models and their respective fields, including
**Defining a Model:**

```typescript
const customersModel = C.model("customers")
import * as semanticLayer from "@verybigthings/semantic-layer";

const customersModel = semanticLayer
.model("customers")
.fromTable("Customer")
.withDimension("customer_id", {
type: "number",
Expand All @@ -49,19 +52,23 @@ const customersModel = C.model("customers")
sql: ({ model }) => model.column("LastName"),
});

const invoicesModel = C.model("invoices")
const invoicesModel = semanticLayer
.model("invoices")
.fromTable("Invoice")
.withDimension("invoice_id", {
type: "number",
primaryKey: true,
sql: ({ model, sql }) => sql`${model.column("InvoiceId")}`,
})
.withMetric("total", {
type: "sum",
// node-postgres returns string types for big integers
type: "string",
aggregateWith: "sum",
sql: ({ model }) => model.column("Total"),
});

const invoiceLinesModel = C.model("invoice_lines")
const invoiceLinesModel = semanticLayer
.model("invoice_lines")
.fromTable("InvoiceLine")
.withDimension("invoice_line_id", {
type: "number",
Expand All @@ -77,19 +84,25 @@ const invoiceLinesModel = C.model("invoice_lines")
sql: ({ model }) => model.column("TrackId"),
})
.withMetric("quantity", {
type: "sum",
// node-postgres returns string types for big integers
type: "string",
aggregateWith: "sum",
sql: ({ model }) => model.column("Quantity"),
})
.withMetric("total_unit_price", {
type: "sum",
// node-postgres returns string types for big integers

type: "string",
aggregateWith: "sum"
sql: ({ model }) => model.column("UnitPrice"),
});
```

**Defining a Database and joining models:**
**Defining a Repository and joining models:**

```typescript
const db = C.database()
const repository = semanticLayer
.repository()
.withModel(customersModel)
.withModel(invoicesModel)
.withModel(invoiceLinesModel)
Expand All @@ -105,6 +118,8 @@ const db = C.database()
({ sql, dimensions }) =>
sql`${dimensions.invoices.invoice_id} = ${dimensions.invoice_lines.invoice_id}`
);

const queryBuilder = repository.build("postgresql");
```

### Data Querying
Expand All @@ -113,31 +128,31 @@ Leverage the library's querying capabilities to fetch dimensions and metrics, ap

```typescript
// Dimension and metric query
const query = db.query({
const query = queryBuilder.buildQuery({
dimensions: ["customers.customer_id"],
metrics: ["invoices.total"],
order: { "customers.customer_id": "asc" },
limit: 10,
});

// Metric query with filters
const query = db.query({
const query = queryBuilder.buildQuery({
metrics: ["invoices.total", "invoice_lines.quantity"],
filters: [
{ operator: "equals", member: "customers.customer_id", value: [1] },
],
});

// Dimension query with filters
const query = db.query({
const query = queryBuilder.buildQuery({
dimensions: ["customers.first_name", "customers.last_name"],
filters: [
{ operator: "equals", member: "customers.customer_id", value: [1] },
],
});

// Filtering and sorting
const query = db.query({
const query = queryBuilder.buildQuery({
dimensions: ["customers.first_name"],
metrics: ["invoices.total"],
filters: [{ operator: "gt", member: "invoices.total", value: [100] }],
Expand Down
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"pg": "^8.11.3",
"rimraf": "^5.0.5",
"semantic-release": "^23.0.4",
"typescript": "^5.4.2"
"typescript": "^5.4.2",
"zod-to-json-schema": "^3.22.4"
},
"config": {
"commitizen": {
Expand Down
Loading

0 comments on commit 5bdf207

Please sign in to comment.