Skip to content

Commit

Permalink
fix(mongo): fix upsert using expressions with initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Aug 14, 2023
1 parent ac75782 commit 8d94066
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,9 @@ export class MongoDriver extends Driver {
const $unset = Object.entries($set)
.filter(([_, value]) => typeof value === 'object')
.map(([key, _]) => key)
const preset = Object.fromEntries(transformer.walkedKeys.map(key => [tempKey + '.' + key, '$' + key]))
const preset = Object.fromEntries(transformer.walkedKeys.map(key => [tempKey + '.' + key, {
$ifNull: ['$' + key, initial[key]]

Check failure on line 503 in packages/mongo/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
}]))

bulk.find(query).upsert().updateOne([
...transformer.walkedKeys.length ? [{ $set: preset }] : [],
Expand Down
12 changes: 12 additions & 0 deletions packages/tests/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ namespace OrmOperations {
])
await expect(database.get('temp2', {})).to.eventually.have.shape(table)
})

it('using expressions with initial values', async () => {
const table = await setup(database, 'temp3', bazTable)
const data = [
{ ida: 114, idb: '514', value: 'baz' },
]
table.push(...data.map(bar => merge(database.tables.temp3.create(), bar)))
await database.upsert('temp3', row => [
{ ida: 114, idb: '514', value: $.concat(row.value, 'baz') },
])
await expect(database.get('temp3', {})).to.eventually.have.deep.members(table)
})
}

export const remove = function Remove(database: Database<Tables>) {
Expand Down

0 comments on commit 8d94066

Please sign in to comment.