gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb8ffc1a0b8ffc1a0release 1.0.5caramboleyob8ffc1a0/tests/find-after-insert-restart.test.js

3.2 KB

  1. import MPackDB, { PrimaryKeyType } from '../src/MPackDB.js';
  2. import { rm } from 'fs/promises';
  3. const TEST_DB_PATH = 'db/find-after-restart';
  4. console.log('--- Find After Insert + Restart Test ---');
  5. console.log('Tests if data survives server restart (init/compaction)\n');
  6. await rm(TEST_DB_PATH, { recursive: true, force: true });
  7. class User {
  8. id = null;
  9. name = null;
  10. email = null;
  11. }
  12. class Users extends MPackDB {
  13. _classToUse = User;
  14. _primaryKey = 'id';
  15. _primaryKeyType = PrimaryKeyType.UUID;
  16. }
  17. // Session 1: insert a user
  18. console.log('SESSION 1: Insert user');
  19. const db1 = new Users(`${TEST_DB_PATH}/users`);
  20. const id1 = await db1.insert({ email: '[email protected]' });
  21. console.log(` Inserted user: ${id1}`);
  22. const [check1] = await db1.find(u => u.email === '[email protected]');
  23. console.log(` Verify find in same session: ${check1 ? 'FOUND' : 'NOT FOUND'}`);
  24. await db1.close();
  25. console.log(' Closed DB (simulating server stop)\n');
  26. // Session 2: restart — init() runs compaction, then find
  27. console.log('SESSION 2: Restart and find');
  28. const db2 = new Users(`${TEST_DB_PATH}/users`);
  29. const [check2] = await db2.find(u => u.email === '[email protected]');
  30. console.log(` Find after restart: ${check2 ? 'FOUND ' + check2.id : 'NOT FOUND'}`);
  31. console.log(` ${check2 ? '✓ PASS' : '✗ FAIL — DATA LOST BY COMPACTION'}\n`);
  32. // Session 2: insert second user (different email)
  33. const id2 = await db2.insert({ email: '[email protected]' });
  34. console.log(` Inserted second user: ${id2}`);
  35. await db2.close();
  36. console.log(' Closed DB\n');
  37. // Session 3: restart again, check both users
  38. console.log('SESSION 3: Restart and find both');
  39. const db3 = new Users(`${TEST_DB_PATH}/users`);
  40. const all = await db3.find();
  41. console.log(` Total users: ${all.length}`);
  42. for (const u of all) {
  43. console.log(` ${u.id}: ${u.email}`);
  44. }
  45. const [check3] = await db3.find(u => u.email === '[email protected]');
  46. console.log(` Find first user: ${check3 ? 'FOUND' : 'NOT FOUND'}`);
  47. console.log(` ${check3 ? '✓ PASS' : '✗ FAIL'}`);
  48. // Now simulate what login.js does: find, if not found insert
  49. console.log('\n Simulating login for [email protected] (should find existing):');
  50. const [existing] = await db3.find(u => u.email === '[email protected]');
  51. if (existing) {
  52. console.log(` Found existing user ${existing.id} — no duplicate insert`);
  53. console.log(' ✓ PASS');
  54. } else {
  55. console.log(' NOT FOUND — would create duplicate!');
  56. console.log(' ✗ FAIL');
  57. }
  58. await db3.close();
  59. // Session 4: test with delete + compaction
  60. console.log('\nSESSION 4: Delete a user, restart (compaction), check');
  61. const db4 = new Users(`${TEST_DB_PATH}/users`);
  62. await db4.delete(u => u.email === '[email protected]');
  63. console.log(' Deleted [email protected]');
  64. await db4.close();
  65. const db5 = new Users(`${TEST_DB_PATH}/users`);
  66. const afterDelete = await db5.find();
  67. console.log(` After restart: ${afterDelete.length} users`);
  68. for (const u of afterDelete) {
  69. console.log(` ${u.id}: ${u.email}`);
  70. }
  71. const [shouldExist] = await db5.find(u => u.email === '[email protected]');
  72. const [shouldNotExist] = await db5.find(u => u.email === '[email protected]');
  73. console.log(` [email protected]: ${shouldExist ? 'FOUND ✓' : 'NOT FOUND ✗'}`);
  74. console.log(` [email protected]: ${shouldNotExist ? 'FOUND ✗ (should be deleted)' : 'NOT FOUND ✓'}`);
  75. await db5.close();

Branches

Latest commits

  • b8ffc1a0release 1.0.5caramboleyo
  • d47876a1reimplemented lost features like indexed find and more testscaramboleyo
  • 7f08da9afixed insert ignoring model definitioncaramboleyo
  • 705774a9added flush before findcaramboleyo
  • b4db6391initial commitcaramboleyo