Skip to content

Commit

Permalink
Merge b0044a1 into 3bbb659
Browse files Browse the repository at this point in the history
  • Loading branch information
martijndeh committed Dec 4, 2020
2 parents 3bbb659 + b0044a1 commit f4de31d
Show file tree
Hide file tree
Showing 18 changed files with 3,755 additions and 213 deletions.
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -16,7 +16,7 @@ Mammoth is a type-safe query builder. It only supports Postgres which we conside

```ts
const rows = await db
.select(db.foo.id, db.bar.name)
.select(star())
.from(db.foo)
.leftJoin(db.bar)
.on(db.foo.barId.eq(db.bar.id))
Expand All @@ -27,15 +27,14 @@ The above query produces the following SQL:

```sql
SELECT
foo.id,
bar.name
*
FROM foo
LEFT JOIN bar ON (foo.bar_id = bar.id)
WHERE
foo.id = $1
```

More importantly, the resulting type of rows is `{ id: string; name: string | undefined }[]`. Notice how the name is automatically nullable because of the left join.
More importantly, the resulting type of rows is `{ id: string; barId: string; name: string | undefined }[]`. Notice how the name is automatically nullable because of the left join.

### Query examples

Expand Down
4 changes: 3 additions & 1 deletion scripts/generate-select-fn.ts
Expand Up @@ -10,7 +10,9 @@ console.log(`export interface SelectFn {
.join(`, `)}>(${array
.map((_, index) => `c${index + 1}: C${index + 1}`)
.join(`, `)}): SelectQuery<
${array.map((_, index) => `GetSelectable<C${index + 1}>`).join(` & `)}
${array
.map((_, index) => `GetSelectable<C${index + 1}>`)
.join(` & `)}, ContainsStar<${array.map((_, index) => `C${index + 1}`).join(` | `)}>
>`;
})
.join(`;\n`)}
Expand Down
32 changes: 32 additions & 0 deletions scripts/generate-with-fn.ts
@@ -0,0 +1,32 @@
const iterable = new Array(10).fill(0);

console.log(`export interface WithFn {
${iterable
.map((_, index) => {
const array = new Array(index + 1).fill(0);
return ` <
${array
.map((_, index) => {
if (index === 0) {
return `N1 extends string, W1 extends QueryFn<never>`;
}
return `N${index + 1} extends string, W${index + 1} extends QueryFn<${new Array(index)
.fill(0)
.map((_, index) => `{ [K in N${index + 1}]: FromItem<W${index + 1}> }`)
.join(' & ')}>`;
})
.join(',')},
Q extends Query<any>
>(
${array
.map((_, index) => `name${index + 1}: N${index + 1}, with${index + 1}: W${index + 1}`)
.join(', ')},
callback: (args: ${array
.map((_, index) => `{ [K in N${index + 1}]: FromItem<W${index + 1}> }`)
.join(' & ')}) => Q,
): Q`;
})
.join(`;\n`)}
};`);

0 comments on commit f4de31d

Please sign in to comment.