mongodb-ops 0.11.0 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/mongodb-ops.js +406 -381
- package/package.json +32 -28
- package/readme.md +130 -65
- package/test/fixtures/README.md +7 -0
- package/test/fixtures/vendor-orders-validator.json +224 -0
- package/test/mongodb-ops.test.js +160 -0
- package/test/mongodb-write-errors.test.js +89 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
const assert = require('node:assert/strict');
|
|
5
|
+
const { MongoMemoryServer } = require('mongodb-memory-server');
|
|
6
|
+
const { MongoDBToolSet } = require('..');
|
|
7
|
+
const validator = require('./fixtures/vendor-orders-validator.json');
|
|
8
|
+
|
|
9
|
+
const order = (id) => ({
|
|
10
|
+
_id: id,
|
|
11
|
+
ts: new Date('2026-01-01T00:00:00.000Z'),
|
|
12
|
+
order_id: id.replace(/^EDI-/, ''),
|
|
13
|
+
vendor: 'acm',
|
|
14
|
+
submitted_by: 'test',
|
|
15
|
+
status: 'new',
|
|
16
|
+
address: {
|
|
17
|
+
company_name: '',
|
|
18
|
+
name: 'Test Recipient',
|
|
19
|
+
address_line1: '1 Main St',
|
|
20
|
+
address_line2: '',
|
|
21
|
+
city: 'Los Angeles',
|
|
22
|
+
state_province: 'CA',
|
|
23
|
+
postal_code: '90001',
|
|
24
|
+
country_code: 'US'
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
async function rejection(promise) {
|
|
29
|
+
try {
|
|
30
|
+
await promise;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return error;
|
|
33
|
+
}
|
|
34
|
+
assert.fail('Expected the real MongoDB write to reject');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('real MongoDB write errors retain their diagnostic detail', async (t) => {
|
|
38
|
+
const mongod = await MongoMemoryServer.create();
|
|
39
|
+
try {
|
|
40
|
+
const connString = mongod.getUri('write_error_test');
|
|
41
|
+
const client = await MongoDBToolSet.getDbClient(connString);
|
|
42
|
+
await client.db().createCollection('orders', {
|
|
43
|
+
validator,
|
|
44
|
+
validationLevel: 'strict',
|
|
45
|
+
validationAction: 'error'
|
|
46
|
+
});
|
|
47
|
+
await MongoDBToolSet.insertOne('orders', order('EDI-1001'), connString);
|
|
48
|
+
|
|
49
|
+
const single = await rejection(MongoDBToolSet.updateOne(
|
|
50
|
+
'orders', { $set: { invoice_date: null } }, { _id: 'EDI-1001' }, connString
|
|
51
|
+
));
|
|
52
|
+
const bulk = await rejection(MongoDBToolSet.updateBulkUnOrdered('orders', [{
|
|
53
|
+
filter: { _id: 'EDI-1001' },
|
|
54
|
+
update: { $set: { invoice_date: null } }
|
|
55
|
+
}], connString));
|
|
56
|
+
const duplicate = await rejection(MongoDBToolSet.insertOne(
|
|
57
|
+
'orders', order('EDI-1001'), connString
|
|
58
|
+
));
|
|
59
|
+
|
|
60
|
+
await t.test('G0: single-write rejection is an Error, not a string or copied object', () => {
|
|
61
|
+
assert.ok(single instanceof Error, 'single-write rejection must be an Error');
|
|
62
|
+
});
|
|
63
|
+
await t.test('G0: bulk-write rejection is an Error, not a BulkWriteResult or copied object', () => {
|
|
64
|
+
assert.ok(bulk instanceof Error, 'bulk-write rejection must be an Error');
|
|
65
|
+
});
|
|
66
|
+
await t.test('G1: updateOne retains the real validator field diagnostic', () => {
|
|
67
|
+
const rules = single?.errInfo?.details?.schemaRulesNotSatisfied;
|
|
68
|
+
assert.ok(Array.isArray(rules), 'updateOne must retain errInfo.details.schemaRulesNotSatisfied');
|
|
69
|
+
assert.equal(single.code, 121);
|
|
70
|
+
assert.match(JSON.stringify(rules), /"propertyName":"invoice_date"/);
|
|
71
|
+
});
|
|
72
|
+
await t.test('G2: bulk rejection exposes writeErrors directly with the validator detail', () => {
|
|
73
|
+
assert.ok(Array.isArray(bulk?.writeErrors), 'bulk rejection must expose writeErrors directly');
|
|
74
|
+
assert.equal(bulk.writeErrors.length, 1);
|
|
75
|
+
assert.equal(bulk.writeErrors[0].code, 121);
|
|
76
|
+
const rules = bulk.writeErrors[0].err.errInfo.details.schemaRulesNotSatisfied;
|
|
77
|
+
assert.match(JSON.stringify(rules), /"propertyName":"invoice_date"/);
|
|
78
|
+
});
|
|
79
|
+
await t.test('G3: duplicate-key rejection retains MongoDB code 11000', () => {
|
|
80
|
+
assert.equal(duplicate?.code, 11000, 'duplicate-key rejection must retain code 11000');
|
|
81
|
+
});
|
|
82
|
+
} finally {
|
|
83
|
+
try {
|
|
84
|
+
await MongoDBToolSet.closeDBConn();
|
|
85
|
+
} finally {
|
|
86
|
+
await mongod.stop();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|