mpackdb
All repositories: gitoria
2.2 KB
import MPackDB, { PrimaryKeyType } from '../src/MPackDB.js';import { rm } from 'fs/promises';const TEST_DB_PATH = 'db/refresh';console.log('--- Refresh Test ---');console.log('Two DB instances on the same file: one writes/deletes, the other reads.\n');await rm(TEST_DB_PATH, { recursive: true, force: true });// Instance A: the "app"const dbA = new MPackDB(`${TEST_DB_PATH}/users`, { primaryKey: '*id', indexes: ['email'] });await dbA.insert({ email: '[email protected]' });await dbA.insert({ email: '[email protected]' });await dbA.insert({ email: '[email protected]' });// Instance B: the "admin" (read-only, same files, no compaction to avoid breaking A's write stream)const dbB = new MPackDB(`${TEST_DB_PATH}/users`, { primaryKey: '*id', indexes: ['email'], compact: false });// B should see all 3const beforeDelete = await dbB.find();console.log(`B sees ${beforeDelete.length} records before delete (expected: 3)`);console.log(` ${beforeDelete.length === 3 ? '✓ PASS' : '✗ FAIL'}`);// A deletes bobawait dbA.delete(r => r.email === '[email protected]');console.log('\nA deleted [email protected]');// B should now see 2 (thanks to refresh)const afterDelete = await dbB.find();console.log(`B sees ${afterDelete.length} records after delete (expected: 2)`);const emails = afterDelete.map(u => u.email);console.log(` emails: ${emails.join(', ')}`);const bobGone = !emails.includes('[email protected]');console.log(` bob gone: ${bobGone ? '✓ PASS' : '✗ FAIL — bob still visible (stale meta)'}`);// A inserts a new recordawait dbA.insert({ email: '[email protected]' });console.log('\nA inserted [email protected]');// B should see 3 againconst afterInsert = await dbB.find();console.log(`B sees ${afterInsert.length} records after insert (expected: 3)`);const emails2 = afterInsert.map(u => u.email);console.log(` emails: ${emails2.join(', ')}`);const daveVisible = emails2.includes('[email protected]');console.log(` dave visible: ${daveVisible ? '✓ PASS' : '✗ FAIL'}`);await dbA.close();await dbB.close();const allPass = beforeDelete.length === 3 && afterDelete.length === 2 && bobGone && afterInsert.length === 3 && daveVisible;console.log(`\n${allPass ? '✓ All refresh tests passed!' : '✗ Some tests FAILED'}`);
Branches
- mastermain branch
Latest commits
- c4cdb9b6node: import prefixes (Deno compat) + pre-existing index-state WIPcaramboleyo
- 0afb8f4bupdate now must be a callbackcaramboleyo
- cde73eb4release 1.0.6caramboleyo
- d01dda02add index hints, intersection, boundingBox; remove findByIndexcaramboleyo
- b8ffc1a0release 1.0.5caramboleyo
- d47876a1reimplemented lost features like indexed find and more testscaramboleyo
- 7f08da9afixed insert ignoring model definitioncaramboleyo
- 705774a9added flush before findcaramboleyo
- b4db6391initial commitcaramboleyo