Skip to content

Commit 772580d

Browse files
committed
🐛 Remove undefined props
At the moment when we pass payload like this: ```ts {someProp: undefined} ``` It stores it mongoDb Db, but it auto convert it to null value, however it might be error prone as we may enqueue job that has `callbackUrl: undefined`, then the mongo queue saves in db, which converts it to `callbackUrl: null`, then when getting the message from queue worker validate the job payload and has type `t.partial({callbackUrl: t.string}})`. The validation will fail.
1 parent 9a5c893 commit 772580d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

mongodb-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default class Queue<T = any> {
118118
});
119119
}
120120

121-
const results = await this.col.insertMany(msgs);
121+
const results = await this.col.insertMany(msgs, {ignoreUndefined: true});
122122
if (payload instanceof Array) return '' + results.insertedIds;
123123
return '' + results.insertedIds[0];
124124
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reedsy/mongodb-queue",
3-
"version": "5.1.0",
3+
"version": "6.0.0",
44
"description": "Message queues which uses MongoDB.",
55
"main": "mongodb-queue.js",
66
"scripts": {

test/default.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ setup().then(({client, db}) => {
5656
t.end();
5757
});
5858

59+
test('remove undefined properties', async function(t) {
60+
const queue = new MongoDbQueue(db, 'default');
61+
const id = await queue.add({text: 'Hello, World!', undefinedProp: undefined});
62+
t.ok(id, 'Received an id for this message');
63+
64+
const msg = await queue.get();
65+
t.ok(msg.id, 'Got a msg.id');
66+
t.equal('undefinedProp' in msg.payload, false, 'Payload has undefinedProp and it should be removed');
67+
t.end();
68+
});
69+
5970
test('client.close()', function(t) {
6071
t.pass('client.close()');
6172
client.close();

0 commit comments

Comments
 (0)