Skip to content

Commit

Permalink
feat(core): use const for type var of join
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 25, 2024
1 parent 1a01bb3 commit 73c1cb1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export class Database<S = any, C extends Context = Context> extends Service<unde
return new Selection(this.getDriver(table), table, query)
}

join<U extends Join1.Input<S>>(tables: U, callback?: Join1.Predicate<S, U>, optional?: boolean[]): Selection<Join1.Output<S, U>>
join<U extends Join2.Input<S>>(tables: U, callback?: Join2.Predicate<S, U>, optional?: Dict<boolean, Keys<U>>): Selection<Join2.Output<S, U>>
join<const U extends Join1.Input<S>>(tables: U, callback?: Join1.Predicate<S, U>, optional?: boolean[]): Selection<Join1.Output<S, U>>
join<const U extends Join2.Input<S>>(tables: U, callback?: Join2.Predicate<S, U>, optional?: Dict<boolean, Keys<U>>): Selection<Join2.Output<S, U>>
join(tables: any, query?: any, optional?: any) {
if (Array.isArray(tables)) {
const sel = new Selection(this.getDriver(tables[0]), Object.fromEntries(tables.map((name) => [name, this.select(name)])))
Expand Down
18 changes: 9 additions & 9 deletions packages/tests/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace JsonTests {
})

it('$.object on cell', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy('bar', {
x: row => $.array($.object(row.foo)),
})
Expand All @@ -179,7 +179,7 @@ namespace JsonTests {
})

it('$.array groupBy', async () => {
await expect(database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
await expect(database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy(['foo'], {
x: row => $.array(row.bar.obj.x),
y: row => $.array(row.bar.obj.y),
Expand All @@ -191,7 +191,7 @@ namespace JsonTests {
{ foo: { id: 2, value: 2 }, x: [3], y: ['c'] },
])

await expect(database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
await expect(database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy(['foo'], {
x: row => $.array(row.bar.obj.x),
y: row => $.array(row.bar.obj.y),
Expand All @@ -203,7 +203,7 @@ namespace JsonTests {
['c'],
])

await expect(database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
await expect(database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy(['foo'], {
x: row => $.array(row.bar.obj.x),
y: row => $.array(row.bar.obj.y),
Expand Down Expand Up @@ -234,7 +234,7 @@ namespace JsonTests {
})

it('$.array in json', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy('foo', {
bars: row => $.array($.object({
value: row.bar.value,
Expand Down Expand Up @@ -278,7 +278,7 @@ namespace JsonTests {
})

it('$.array with expressions', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy('foo', {
bars: row => $.array($.object({
value: row.bar.value,
Expand Down Expand Up @@ -307,7 +307,7 @@ namespace JsonTests {
})

it('$.array nested', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.orderBy(row => row.foo.id)
.groupBy('foo', {
y: row => $.array(row.bar.obj.x),
Expand All @@ -325,7 +325,7 @@ namespace JsonTests {
})

it('non-aggr func', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy('foo', {
y: row => $.array(row.bar.obj.x),
})
Expand All @@ -346,7 +346,7 @@ namespace JsonTests {
})

it('non-aggr func inside aggr', async () => {
const res = await database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
const res = await database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.orderBy(row => row.foo.id)
.groupBy('foo', {
y: row => $.array(row.bar.obj.x),
Expand Down
12 changes: 6 additions & 6 deletions packages/tests/src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,18 @@ namespace SelectionTests {
export function join(database: Database<Tables>) {
it('inner join', async () => {
await expect(database
.join(['foo', 'bar'] as const)
.join(['foo', 'bar'])
.execute()
).to.eventually.have.length(18)

await expect(database
.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.value, bar.value))
.join(['foo', 'bar'], (foo, bar) => $.eq(foo.value, bar.value))
.execute()
).to.eventually.have.length(2)
})

it('group', async () => {
await expect(database.join(['foo', 'bar'] as const, (foo, bar) => $.eq(foo.id, bar.pid))
await expect(database.join(['foo', 'bar'], (foo, bar) => $.eq(foo.id, bar.pid))
.groupBy('foo', { count: row => $.sum(row.bar.uid) })
.orderBy(row => row.foo.id)
.execute()).to.eventually.deep.equal([
Expand Down Expand Up @@ -330,7 +330,7 @@ namespace SelectionTests {

it('aggregate', async () => {
await expect(database
.join(['foo', 'bar'] as const)
.join(['foo', 'bar'])
.execute(row => $.count(row.bar.id))
).to.eventually.equal(6)
})
Expand Down Expand Up @@ -432,12 +432,12 @@ namespace SelectionTests {
const one = database.select('bar').evaluate(row => $.subtract($.count(row.id), 5))
const sel = x => database.select('bar').where(row => $.eq(x, row.uid)).evaluate(row => $.count(row.id))
await expect(database
.join(['foo', 'bar'] as const, (foo, bar) => $.gt(foo.value, one))
.join(['foo', 'bar'], (foo, bar) => $.gt(foo.value, one))
.execute()
).to.eventually.have.length(12)

await expect(database
.join(['foo', 'bar'] as const, (foo, bar) => $.lt(foo.value, sel(foo.id)))
.join(['foo', 'bar'], (foo, bar) => $.lt(foo.value, sel(foo.id)))
.execute()
).to.eventually.have.length(6)
})
Expand Down

0 comments on commit 73c1cb1

Please sign in to comment.