Skip to content

Commit

Permalink
fix(mongo): existence check -> equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Apr 6, 2024
1 parent 74fd647 commit e81267f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
7 changes: 5 additions & 2 deletions packages/mongo/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function transformFieldQuery(query: Query.FieldQuery, key: string, filters: Filt
} else if (query instanceof RegExp) {
return { $regex: query }
} else if (isNullable(query)) {
return { $exists: false }
return null
}

// query operators
Expand Down Expand Up @@ -52,7 +52,7 @@ function transformFieldQuery(query: Query.FieldQuery, key: string, filters: Filt
} else if (prop === '$el') {
const child = transformFieldQuery(query[prop], key, filters)
if (child === false) return false
if (child !== true) result.$elemMatch = child
if (child !== true) result.$elemMatch = child!
} else if (prop === '$regexFor') {
filters.push({
$expr: {
Expand All @@ -62,6 +62,9 @@ function transformFieldQuery(query: Query.FieldQuery, key: string, filters: Filt
},
},
})
} else if (prop === '$exists') {
if (query[prop]) return { $ne: null }
else return null
} else {
result[prop] = query[prop]
}
Expand Down
3 changes: 0 additions & 3 deletions packages/mysql/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export class MySQLBuilder extends Builder {
}

this.transformers['date'] = {
encode: value => value,
decode: value => `cast(${value} as date)`,
load: value => {
if (isNullable(value) || typeof value === 'object') return value
Expand All @@ -67,14 +66,12 @@ export class MySQLBuilder extends Builder {
}

this.transformers['time'] = {
encode: value => value,
decode: value => `cast(${value} as time)`,
load: value => this.driver.types['time'].load(value),
dump: value => isNullable(value) ? value : Time.template('yyyy-MM-dd hh:mm:ss.SSS', value),
}

this.transformers['timestamp'] = {
encode: value => value,
decode: value => `cast(${value} as datetime)`,
load: value => {
if (isNullable(value) || typeof value === 'object') return value
Expand Down
10 changes: 1 addition & 9 deletions packages/postgres/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,12 @@ export class PostgresBuilder extends Builder {
}

this.transformers['boolean'] = {
encode: value => value,
decode: value => `(${value})::boolean`,
load: value => value,
dump: value => value,
}

this.transformers['decimal'] = {
encode: value => value,
decode: value => `(${value})::double precision`,
load: value => isNullable(value) ? value : +value,
dump: value => value,
}

this.transformers['binary'] = {
Expand All @@ -112,7 +107,6 @@ export class PostgresBuilder extends Builder {
}

this.transformers['date'] = {
encode: value => value,
decode: value => `cast(${value} as date)`,
load: value => {
if (isNullable(value) || typeof value === 'object') return value
Expand All @@ -125,15 +119,13 @@ export class PostgresBuilder extends Builder {
}

this.transformers['time'] = {
encode: value => value,
decode: value => `cast(${value} as time)`,
load: value => this.driver.types['time'].load(value),
dump: value => this.driver.types['time'].dump(value),
}

this.transformers['timestamp'] = {
encode: value => value,
decode: value => `cast(${value} as datetime)`,
decode: value => `cast(${value} as timestamp)`,
load: value => {
if (isNullable(value) || typeof value === 'object') return value
return new Date(value)
Expand Down
10 changes: 5 additions & 5 deletions packages/sql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export type EvalOperators = {
} & { $: (expr: any) => string }

interface Transformer<S=any, T=any> {
encode(value: string): string
decode(value: string): string
load(value: S): T
dump(value: T): S
encode?(value: string): string
decode?(value: string): string
load?(value: S | null): T | null
dump?(value: T | null): S | null
}

interface State {
Expand Down Expand Up @@ -312,7 +312,7 @@ export class Builder {
protected transform(value: string, type: Type | Eval.Expr | undefined, method: 'encode' | 'decode' | 'load' | 'dump', miss?: any) {
type = Type.isType(type) ? type : Type.fromTerm(type)
const transformer = this.transformers[type.type] ?? this.transformers[this.driver.database.types[type.type]?.type!]
return transformer ? transformer[method](value) : (miss ?? value)
return transformer?.[method] ? transformer[method](value) : (miss ?? value)
}

protected groupObject(_fields: any) {
Expand Down
10 changes: 7 additions & 3 deletions packages/tests/src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { valueMap, isNullable } from 'cosmokit'
import { valueMap, isNullable, deduplicate } from 'cosmokit'
import { $, Database, Field, Type, unravel } from 'minato'
import { expect } from 'chai'

Expand Down Expand Up @@ -264,7 +264,7 @@ function ModelOperations(database: Database<Tables, Types>) {
function getValue(obj: any, path: string) {
if (path.includes('.')) {
const index = path.indexOf('.')
return getValue(obj[path.slice(0, index)] ??= {}, path.slice(index + 1))
return getValue(obj[path.slice(0, index)] ?? {}, path.slice(index + 1))
} else {
return obj[path]
}
Expand Down Expand Up @@ -566,7 +566,11 @@ namespace ModelOperations {

it('project with dot notation', async () => {
const table = await setup(database, 'dobjects', dobjectTable)
const keys = Object.keys(database.tables['dobjects'].fields).flatMap(k => k.split('.').reduce((arr, c) => arr.length ? [`${arr[0]}.${c}`, ...arr] : [c], []))
const keys = deduplicate([
'foo.nested.object',
'foo.nested.object.embed',
...Object.keys(database.tables['dobjects'].fields).flatMap(k => k.split('.').reduce((arr, c) => arr.length ? [`${arr[0]}.${c}`, ...arr] : [c], [])),
])
await Promise.all(keys.map(key =>
expect(database.select('dobjects').project([key as any]).execute()).to.eventually.have.deep.members(table.map(row => unravel({ [key]: getValue(row, key) })))
))
Expand Down
1 change: 1 addition & 0 deletions packages/tests/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace OrmOperations {
data.text = null as never
await database.set('temp2', { timestamp: { $exists: true } }, { text: null })
await expect(database.get('temp2', {})).to.eventually.have.shape(table)
await expect(database.get('temp2', { text: { $exists: false } })).to.eventually.have.length(1)
})

it('noop', async () => {
Expand Down

0 comments on commit e81267f

Please sign in to comment.