mpackdb
All repositories: gitoria
3.2 KB
import MPackDB, { PrimaryKeyType } from '../src/MPackDB.js';import { rm } from 'fs/promises';const TEST_DB_PATH = 'db/find-after-restart';console.log('--- Find After Insert + Restart Test ---');console.log('Tests if data survives server restart (init/compaction)\n');await rm(TEST_DB_PATH, { recursive: true, force: true });class User {id = null;name = null;email = null;}class Users extends MPackDB {_classToUse = User;_primaryKey = 'id';_primaryKeyType = PrimaryKeyType.UUID;}// Session 1: insert a userconsole.log('SESSION 1: Insert user');const db1 = new Users(`${TEST_DB_PATH}/users`);const id1 = await db1.insert({ email: '[email protected]' });console.log(` Inserted user: ${id1}`);const [check1] = await db1.find(u => u.email === '[email protected]');console.log(` Verify find in same session: ${check1 ? 'FOUND' : 'NOT FOUND'}`);await db1.close();console.log(' Closed DB (simulating server stop)\n');// Session 2: restart — init() runs compaction, then findconsole.log('SESSION 2: Restart and find');const db2 = new Users(`${TEST_DB_PATH}/users`);const [check2] = await db2.find(u => u.email === '[email protected]');console.log(` Find after restart: ${check2 ? 'FOUND ' + check2.id : 'NOT FOUND'}`);console.log(` ${check2 ? '✓ PASS' : '✗ FAIL — DATA LOST BY COMPACTION'}\n`);// Session 2: insert second user (different email)const id2 = await db2.insert({ email: '[email protected]' });console.log(` Inserted second user: ${id2}`);await db2.close();console.log(' Closed DB\n');// Session 3: restart again, check both usersconsole.log('SESSION 3: Restart and find both');const db3 = new Users(`${TEST_DB_PATH}/users`);const all = await db3.find();console.log(` Total users: ${all.length}`);for (const u of all) {console.log(` ${u.id}: ${u.email}`);}const [check3] = await db3.find(u => u.email === '[email protected]');console.log(` Find first user: ${check3 ? 'FOUND' : 'NOT FOUND'}`);console.log(` ${check3 ? '✓ PASS' : '✗ FAIL'}`);// Now simulate what login.js does: find, if not found insertconsole.log('\n Simulating login for [email protected] (should find existing):');const [existing] = await db3.find(u => u.email === '[email protected]');if (existing) {console.log(` Found existing user ${existing.id} — no duplicate insert`);console.log(' ✓ PASS');} else {console.log(' NOT FOUND — would create duplicate!');console.log(' ✗ FAIL');}await db3.close();// Session 4: test with delete + compactionconsole.log('\nSESSION 4: Delete a user, restart (compaction), check');const db4 = new Users(`${TEST_DB_PATH}/users`);await db4.delete(u => u.email === '[email protected]');console.log(' Deleted [email protected]');await db4.close();const db5 = new Users(`${TEST_DB_PATH}/users`);const afterDelete = await db5.find();console.log(` After restart: ${afterDelete.length} users`);for (const u of afterDelete) {console.log(` ${u.id}: ${u.email}`);}const [shouldExist] = await db5.find(u => u.email === '[email protected]');const [shouldNotExist] = await db5.find(u => u.email === '[email protected]');console.log(` [email protected]: ${shouldExist ? 'FOUND ✓' : 'NOT FOUND ✗'}`);console.log(` [email protected]: ${shouldNotExist ? 'FOUND ✗ (should be deleted)' : 'NOT FOUND ✓'}`);await db5.close();
Branches
- mastermain branch
Latest commits
- 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