Skip to content

Commit b273c19

Browse files
authored
Merge pull request #25 from jhomarolo/main
fix: update method now uses findOneAndUpdate instead updateOne
2 parents 6cca1a0 + 59eb930 commit b273c19

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/repository.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,24 @@ module.exports = class Repository {
8181
*
8282
* Update entity
8383
*
84-
* @param {type} entityInstance Entity instance
84+
* @param {type} entity Entity instance
8585
*
8686
* @return {type} True when success
8787
*/
88-
async update(entityInstance) {
88+
async update(entity) {
8989

90-
const payload = this.dataMapper.collectionFieldsWithValue(entityInstance)
90+
const payload = this.dataMapper.collectionFieldsWithValue(entity)
9191
delete payload._id
92+
const collectionIDs = this.dataMapper.collectionIDs()
9293

9394
const instance = await this.runner()
9495
const ret = await instance.collection(this.collectionQualifiedName)
95-
.updateOne({ _id: new ObjectId(entityInstance._id | entityInstance.id)},
96+
.findOneAndUpdate({ _id: new ObjectId(entity[collectionIDs[0]])},
9697
{ $set : payload },
97-
{ upsert: true })
98+
{ upsert: true, returnDocument: 'after'})
9899

99-
return !!(ret.modifiedCount || ret.upsertedCount)
100+
if(ret.ok === 0) return null
101+
return this.dataMapper.toEntity(ret.value)
100102
}
101103

102104

test/queries/update.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { entity, field } = require("@herbsjs/gotu")
1+
const { entity, field, id } = require("@herbsjs/gotu")
22
const Repository = require("../../src/repository")
33
const assert = require("assert")
44

@@ -7,7 +7,7 @@ describe("Update an Entity", () => {
77
const ParentEntity = entity('A Parent Entity', {})
88

99
return entity('A entity', {
10-
id: field(Number),
10+
id: id(String),
1111
stringTest: field(String),
1212
booleanTest: field(Boolean),
1313
entityTest: field(ParentEntity),
@@ -29,9 +29,9 @@ describe("Update an Entity", () => {
2929
collection: (f) => {
3030
spy.collectionName = f
3131
return {
32-
updateOne: (p) => {
32+
findOneAndUpdate: (p) => {
3333
spy.payload = p
34-
{ return { modifiedCount: 1, upsertedCount: 1 } }
34+
{ return { ok: '1', value: { _id: "70edc25fc39277307ca9a700", string_test: "test", boolean_test: true } }}
3535
}
3636
}
3737
}
@@ -49,15 +49,15 @@ describe("Update an Entity", () => {
4949
mongodb: mongodb(spy)
5050
})
5151

52-
anEntity.id = 1
52+
anEntity.id = "70edc25fc39277307ca9a700"
5353
anEntity.stringTest = "test"
5454
anEntity.booleanTest = true
5555

5656
//when
5757
const ret = await itemRepo.update(anEntity)
5858

5959
//then
60-
assert.deepStrictEqual(ret, true)
60+
assert.deepStrictEqual(ret.stringTest, "test")
6161
})
6262

6363
})

testdb/update.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ describe('Persist Entity', () => {
7575
//when
7676
aModifiedInstance.stringTest = "updated"
7777
const ret = await itemRepo.update(aModifiedInstance)
78-
79-
//then
80-
var findStatement = {}
81-
findStatement.string_test = "updated"
82-
const collectionConnection = await client.collection(collection)
83-
const retDB = await collectionConnection.findOne(findStatement)
84-
assert.deepStrictEqual(retDB.string_test, "updated")
85-
78+
assert.deepStrictEqual(ret.stringTest, "updated")
8679
})
8780
})
8881
})

0 commit comments

Comments
 (0)