|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29538] driver-mongodb through the engine: a write answers the record the |
| 5 | + * next read answers — a declared column the write never touched is `null` in |
| 6 | + * the insert and update answer, as on the following `findOne` (#29408). |
| 7 | + * |
| 8 | + * This driver stores no key for a field given no value (`withoutUndefinedOwnKeys` |
| 9 | + * at the insert doors), its `create` / `bulkCreate` echo the document they |
| 10 | + * inserted, and its `update` re-reads the document. So the row each write |
| 11 | + * hands the engine is sparse. Measured on `origin/main` before the fix with |
| 12 | + * this same harness: every write answer below carried no `note` / `qty` key |
| 13 | + * and answered the formula `doubled` as `null` (a fault on the missing |
| 14 | + * `qty`), while the next `findOne` answered `note: null`, `qty: null` and |
| 15 | + * `doubled: -1`. |
| 16 | + * |
| 17 | + * Runs without a server, on the fake `Db` of |
| 18 | + * `mongodb-declared-column-null-on-read.test.ts`, with the write calls the |
| 19 | + * engine's insert and by-id update doors reach. The fake keeps each document |
| 20 | + * as inserted and answers a projection as a real mongod does — the named keys |
| 21 | + * the document HAS. The real-server suites are gated on `mongodb-memory-server` |
| 22 | + * (#5517). |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 26 | +import { ObjectQL } from '@objectstack/objectql'; |
| 27 | +import { MongoDBDriver } from './mongodb-driver.js'; |
| 28 | + |
| 29 | +type Doc = Record<string, unknown>; |
| 30 | + |
| 31 | +/** |
| 32 | + * A fake `Db`: one store of documents kept as inserted, served through a |
| 33 | + * collection that declares ONLY the native `Collection` members the path under |
| 34 | + * test calls. Two shapes, one per path: the single-row doors (`create`, |
| 35 | + * `update`, `findOne`) reach `insertOne` / `updateOne` / `findOne`; the batch |
| 36 | + * door (`bulkCreate`) reaches `insertMany`, and its next read is a `find`. |
| 37 | + */ |
| 38 | +function fakeStore() { |
| 39 | + const docs: Doc[] = []; |
| 40 | + const project = (doc: Doc, projection: Record<string, number> | undefined): Doc => { |
| 41 | + const keep = Object.keys(projection ?? {}).filter((k) => projection![k] === 1); |
| 42 | + const out: Doc = {}; |
| 43 | + for (const [k, v] of Object.entries(doc)) { |
| 44 | + if (k === '_id' && projection?._id === 0) continue; |
| 45 | + if (keep.length > 0 && !keep.includes(k)) continue; |
| 46 | + out[k] = v; |
| 47 | + } |
| 48 | + return out; |
| 49 | + }; |
| 50 | + // Equality on plain keys only — the writes and reads below address a row |
| 51 | + // by `id` alone. A combinator or any other operator is REFUSED, never read |
| 52 | + // as a field name. |
| 53 | + const matches = (doc: Doc, filter: Doc | undefined): boolean => |
| 54 | + Object.entries(filter ?? {}).every(([k, v]) => { |
| 55 | + if (k.startsWith('$')) throw new Error(`fake Db: filter operator ${k} is not implemented`); |
| 56 | + if (v !== null && typeof v === 'object') { |
| 57 | + const ops = Object.keys(v as Doc); |
| 58 | + if (ops.length !== 1 || ops[0] !== '$eq') throw new Error(`fake Db: operator ${ops.join(',')} is not implemented`); |
| 59 | + return doc[k] === (v as Doc).$eq; |
| 60 | + } |
| 61 | + return doc[k] === v; |
| 62 | + }); |
| 63 | + const push = (doc: Doc) => { docs.push({ _id: `oid_${docs.length}`, ...doc }); }; |
| 64 | + const pick = (filter: Doc) => docs.find((x) => matches(x, filter)); |
| 65 | + const pickAll = (filter: Doc, options?: { projection?: Record<string, number> }) => |
| 66 | + docs.filter((x) => matches(x, filter)).map((d) => project(d, options?.projection)); |
| 67 | + return { docs, push, pick, pickAll, project }; |
| 68 | +} |
| 69 | + |
| 70 | +/** The single-row doors: `create`, `update` and `findOne`. */ |
| 71 | +function singleRowDb() { |
| 72 | + const store = fakeStore(); |
| 73 | + const collection = { |
| 74 | + insertOne: async (doc: Doc) => { store.push(doc); return {}; }, |
| 75 | + updateOne: async (filter: Doc, update: { $set?: Doc }) => { |
| 76 | + const updateOps = Object.keys(update); |
| 77 | + if (updateOps.length !== 1 || updateOps[0] !== '$set') throw new Error(`fake Db: update ${updateOps.join(',')} is not implemented`); |
| 78 | + const d = store.pick(filter); |
| 79 | + if (d) Object.assign(d, update.$set); |
| 80 | + return { matchedCount: d ? 1 : 0 }; |
| 81 | + }, |
| 82 | + findOne: async (filter: Doc, options: { projection?: Record<string, number> }) => { |
| 83 | + const d = store.pick(filter); |
| 84 | + return d ? store.project(d, options?.projection) : null; |
| 85 | + }, |
| 86 | + }; |
| 87 | + return { db: { collection: () => collection }, docs: store.docs }; |
| 88 | +} |
| 89 | + |
| 90 | +/** The batch door: `bulkCreate`, and `find` for the next read. */ |
| 91 | +function batchDb() { |
| 92 | + const store = fakeStore(); |
| 93 | + const collection = { |
| 94 | + insertMany: async (many: Doc[]) => { for (const doc of many) store.push(doc); return {}; }, |
| 95 | + find: (filter: Doc, options: { projection?: Record<string, number> }) => ({ |
| 96 | + toArray: async () => store.pickAll(filter, options), |
| 97 | + }), |
| 98 | + }; |
| 99 | + return { db: { collection: () => collection }, docs: store.docs }; |
| 100 | +} |
| 101 | + |
| 102 | +const OBJECT = 'nk_mongo_write_item'; |
| 103 | + |
| 104 | +const own = (row: unknown, key: string): boolean => |
| 105 | + row != null && typeof row === 'object' && Object.prototype.hasOwnProperty.call(row, key); |
| 106 | + |
| 107 | +describe('[#29538] driver-mongodb: a write answers the record the next read answers', () => { |
| 108 | + let engine: ObjectQL; |
| 109 | + let docs: Doc[]; |
| 110 | + |
| 111 | + const boot = async (fake: { db: unknown; docs: Doc[] }) => { |
| 112 | + const driver = new MongoDBDriver({ url: 'mongodb://127.0.0.1:1/unused' }); |
| 113 | + docs = fake.docs; |
| 114 | + (driver as any).db = fake.db; |
| 115 | + (driver as any).connect = async () => {}; |
| 116 | + (driver as any).disconnect = async () => {}; |
| 117 | + engine = new ObjectQL(); |
| 118 | + engine.registerDriver(driver as never, true); |
| 119 | + await engine.init(); |
| 120 | + engine.registry.registerObject({ |
| 121 | + name: OBJECT, |
| 122 | + label: 'Item', |
| 123 | + fields: { |
| 124 | + name: { type: 'text' }, |
| 125 | + note: { type: 'text' }, |
| 126 | + qty: { type: 'number' }, |
| 127 | + doubled: { type: 'formula', expression: { dialect: 'cel', source: 'record.qty != null ? record.qty * 2 : -1' } }, |
| 128 | + }, |
| 129 | + } as never); |
| 130 | + }; |
| 131 | + |
| 132 | + beforeEach(async () => { await boot(singleRowDb()); }); |
| 133 | + |
| 134 | + const expectAnswerIsNextRead = async (answer: Doc, next?: Doc) => { |
| 135 | + expect(answer).toStrictEqual(next ?? await engine.findOne(OBJECT, { where: { id: answer.id } })); |
| 136 | + expect(own(answer, 'note')).toBe(true); |
| 137 | + expect(answer.note).toBeNull(); |
| 138 | + expect(own(answer, 'qty')).toBe(true); |
| 139 | + expect(answer.qty).toBeNull(); |
| 140 | + // FORMULA CONTROL: evaluated against the `null`, as the read evaluates it. |
| 141 | + expect(answer.doubled).toBe(-1); |
| 142 | + }; |
| 143 | + |
| 144 | + it('insert: the answer deep-equals the next findOne', async () => { |
| 145 | + const answer = await engine.insert(OBJECT, { name: 'Ada' }) as Doc; |
| 146 | + expect(own(docs[0], 'note')).toBe(false); |
| 147 | + await expectAnswerIsNextRead(answer); |
| 148 | + }); |
| 149 | + |
| 150 | + it('batch insert (bulkCreate): every answered row deep-equals its next read', async () => { |
| 151 | + await boot(batchDb()); |
| 152 | + const answers = await engine.insert(OBJECT, [{ name: 'Ada' }, { name: 'Bo' }]) as Doc[]; |
| 153 | + expect(answers).toHaveLength(2); |
| 154 | + expect(own(docs[0], 'note')).toBe(false); |
| 155 | + for (const answer of answers) { |
| 156 | + const [next] = await engine.find(OBJECT, { where: { id: answer.id } }) as Doc[]; |
| 157 | + await expectAnswerIsNextRead(answer, next); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + it('update by id: the answer deep-equals the next findOne', async () => { |
| 162 | + const created = await engine.insert(OBJECT, { name: 'Ada' }) as Doc; |
| 163 | + const answer = await engine.update(OBJECT, { id: created.id, name: 'Ada L.' }) as Doc; |
| 164 | + expect(answer.name).toBe('Ada L.'); |
| 165 | + await expectAnswerIsNextRead(answer); |
| 166 | + // The stored document still carries no key for the column: only the |
| 167 | + // answer is shaped, never what the write stores. |
| 168 | + expect(own(docs[0], 'note')).toBe(false); |
| 169 | + }); |
| 170 | +}); |
0 commit comments