Generic handle objectid

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

View File

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

View File

@ -103,16 +103,16 @@ describe("MongoDB Integration", () => {
restore() 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 = { const query = {
json: { json: {
filter: { filter: {
_id: "ObjectId('ACBD12345678ABCD12345678')", _id: "ObjectId('ACBD12345678ABCD12345678')",
name: "ObjectId('name')" name: "ObjectId('BBBB12345678ABCD12345678')"
}, },
update: { update: {
_id: "ObjectId('FFFF12345678ABCD12345678')", _id: "ObjectId('FFFF12345678ABCD12345678')",
name: "ObjectId('updatedName')", name: "ObjectId('CCCC12345678ABCD12345678')",
}, },
options: { options: {
upsert: false, upsert: false,
@ -126,11 +126,11 @@ describe("MongoDB Integration", () => {
const args = config.integration.client.updateOne.mock.calls[0] const args = config.integration.client.updateOne.mock.calls[0]
expect(args[0]).toEqual({ expect(args[0]).toEqual({
_id: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), _id: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"),
name: "ObjectId('name')", name: mongo.ObjectID.createFromHexString("BBBB12345678ABCD12345678"),
}) })
expect(args[1]).toEqual({ expect(args[1]).toEqual({
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"),
name: "ObjectId('updatedName')", name: mongo.ObjectID.createFromHexString("CCCC12345678ABCD12345678"),
}) })
expect(args[2]).toEqual({ expect(args[2]).toEqual({
upsert: false upsert: false