Skip to content

Commit

Permalink
fix(core): ignore undefined value in model.format (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest authored Dec 17, 2023
1 parent 94c401e commit 618a38e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export class Database<S = any> {
if (primary.some(key => key in update)) {
throw new TypeError(`cannot modify primary key`)
}
return await sel._action('set', sel.model.format(update)).execute()
update = sel.model.format(update)
if (Object.keys(update).length === 0) return {}
return await sel._action('set', update).execute()
}

async remove<T extends Keys<S>>(table: T, query: Query<S[T]>): Promise<Driver.WriteResult> {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class Model<S = any> {
const fields = Object.keys(this.fields)
Object.entries(source).map(([key, value]) => {
key = prefix + key
if (value === undefined) return
if (fields.includes(key)) {
result[key] = this.resolveValue(key, value)
return
Expand Down
8 changes: 8 additions & 0 deletions packages/tests/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ namespace OrmOperations {
await expect(database.get('temp2', {})).to.eventually.have.shape(table)
})

it('noop', async () => {
const table = await setup(database, 'temp2', barTable)
await database.set('temp2', {}, {})
await expect(database.get('temp2', {})).to.eventually.have.shape(table)
await database.set('temp2', {}, { text: undefined })
await expect(database.get('temp2', {})).to.eventually.have.shape(table)
})

it('using expressions', async () => {
const table = await setup(database, 'temp2', barTable)
table[1].num = table[1].id * 2
Expand Down

0 comments on commit 618a38e

Please sign in to comment.