Skip to content

Commit

Permalink
feat(minato): support logical exprs in querying to-many relation (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest authored Aug 22, 2024
1 parent c239d6c commit 4b649b8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi
))
}
} else if (relation.type === 'oneToMany') {
if (query.$or) results.push(Eval.or(...query.$or.map((q: any) => this.transformRelationQuery(table, row, key, q).$expr)))
if (query.$and) results.push(...query.$and.map((q: any) => this.transformRelationQuery(table, row, key, q).$expr))
if (query.$not) results.push(Eval.not(this.transformRelationQuery(table, row, key, query.$not).$expr))
if (query.$some) {
results.push(Eval.in(
relation.fields.map(x => row[x]),
Expand All @@ -658,6 +661,9 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi
const assocTable: any = Relation.buildAssociationTable(table, relation.table)
const fields: any[] = relation.fields.map(x => Relation.buildAssociationKey(x, table))
const references = relation.references.map(x => Relation.buildAssociationKey(x, relation.table))
if (query.$or) results.push(Eval.or(...query.$or.map((q: any) => this.transformRelationQuery(table, row, key, q).$expr)))
if (query.$and) results.push(...query.$and.map((q: any) => this.transformRelationQuery(table, row, key, q).$expr))
if (query.$not) results.push(Eval.not(this.transformRelationQuery(table, row, key, query.$not).$expr))
if (query.$some) {
const innerTable = this.select(relation.table, query.$some).evaluate(relation.references)
const relTable = this.select(assocTable, r => Eval.in(references.map(x => r[x]), innerTable)).evaluate(fields)
Expand Down
13 changes: 6 additions & 7 deletions packages/mongo/tests/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { $, Database, Primary } from 'minato'
import { $, Database, Driver, Primary } from 'minato'
import { Context, ForkScope, Logger } from 'cordis'
import { expect } from 'chai'
import MongoDriver from '@minatojs/driver-mongo'
import '@minatojs/tests'
import { expect } from '@minatojs/tests'

const logger = new Logger('mongo')

Expand Down Expand Up @@ -105,11 +104,11 @@ describe('@minatojs/driver-mongo/migrate-virtualKey', () => {
await resetConfig(false)
await expect(database.get('temp1', {})).to.eventually.deep.eq(table)

await (Object.values(database.drivers)[0] as MongoDriver).drop('_fields')
await (Object.values(database.drivers)[0] as Driver).drop('_fields')
await resetConfig(true)
await expect(database.get('temp1', {})).to.eventually.deep.eq(table)

await (Object.values(database.drivers)[0] as MongoDriver).drop('_fields')
await (Object.values(database.drivers)[0] as Driver).drop('_fields')
await resetConfig(false)
await expect(database.get('temp1', {})).to.eventually.deep.eq(table)
})
Expand Down Expand Up @@ -139,11 +138,11 @@ describe('@minatojs/driver-mongo/migrate-virtualKey', () => {
table.push(await database.create('temp2', { text: 'awesome baz' }))
await expect(database.get('temp2', {})).to.eventually.deep.eq(table)

await (Object.values(database.drivers)[0] as MongoDriver).drop('_fields')
await (Object.values(database.drivers)[0] as Driver).drop('_fields')
await resetConfig(true)
await expect(database.get('temp2', {})).to.eventually.deep.eq(table)

await (Object.values(database.drivers)[0] as MongoDriver).drop('_fields')
await (Object.values(database.drivers)[0] as Driver).drop('_fields')
await resetConfig(false)
await expect(database.get('temp2', {})).to.eventually.deep.eq(table)

Expand Down
2 changes: 2 additions & 0 deletions packages/tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Transaction from './transaction'
import Relation from './relation'
import './setup'

export { expect } from 'chai'

const Keywords = ['name']
type Keywords = 'name'

Expand Down
37 changes: 37 additions & 0 deletions packages/tests/src/relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,43 @@ namespace RelationTests {
...user,
posts: posts.filter(post => post.author?.id === user.id),
})))

await expect(database.get('user', {
posts: {
$or: [
{
$some: {
author: {
id: 1,
},
},
},
{
$none: {
author: {},
},
},
],
},
})).to.eventually.have.shape([users[0], users[2]].map(user => ({
...user,
posts: posts.filter(post => post.author?.id === user.id),
})))

await expect(database.get('user', {
posts: {
$not: {
$some: {
author: {
id: 1,
},
},
},
},
})).to.eventually.have.shape([users[1], users[2]].map(user => ({
...user,
posts: posts.filter(post => post.author?.id === user.id),
})))
})

it('manyToMany', async () => {
Expand Down
1 change: 1 addition & 0 deletions yakumo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- esbuild
clean:
- tsc --clean
- name: yakumo/run
- name: yakumo-esbuild
- name: yakumo-mocha
- name: yakumo-tsc

0 comments on commit 4b649b8

Please sign in to comment.