1
0
Fork 0
mirror of synced 2024-06-14 08:24:48 +12:00

Generic handle objectid

This commit is contained in:
Mel O'Hagan 2022-08-23 15:33:04 +01:00
parent 7d16917f8d
commit da4107b26c
2 changed files with 7 additions and 9 deletions

View file

@ -92,10 +92,8 @@ module MongoDBModule {
if (json[field] instanceof Object) {
json[field] = self.createObjectIds(json[field])
}
if (
(field === "_id" || field?.startsWith("$")) &&
typeof json[field] === "string"
) {
if (typeof json[field] === "string" && json[field].toLowerCase().startsWith("objectid"))
{
const id = json[field].match(
/(?<=objectid\(['"]).*(?=['"]\))/gi
)?.[0]

View file

@ -103,16 +103,16 @@ describe("MongoDB Integration", () => {
restore()
})
it("creates ObjectIds if the _id fields contains a match on ObjectId", async () => {
it("creates ObjectIds if the field contains a match on ObjectId", async () => {
const query = {
json: {
filter: {
_id: "ObjectId('ACBD12345678ABCD12345678')",
name: "ObjectId('name')"
name: "ObjectId('BBBB12345678ABCD12345678')"
},
update: {
_id: "ObjectId('FFFF12345678ABCD12345678')",
name: "ObjectId('updatedName')",
name: "ObjectId('CCCC12345678ABCD12345678')",
},
options: {
upsert: false,
@ -126,11 +126,11 @@ describe("MongoDB Integration", () => {
const args = config.integration.client.updateOne.mock.calls[0]
expect(args[0]).toEqual({
_id: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"),
name: "ObjectId('name')",
name: mongo.ObjectID.createFromHexString("BBBB12345678ABCD12345678"),
})
expect(args[1]).toEqual({
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"),
name: "ObjectId('updatedName')",
name: mongo.ObjectID.createFromHexString("CCCC12345678ABCD12345678"),
})
expect(args[2]).toEqual({
upsert: false