gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0afb8f4b0afb8f4bupdate now must be a callbackcaramboleyo0afb8f4b/tests/autopersist.test.js

1.6 KB

  1. import MPackDB, { PrimaryKeyType } from '../src/MPackDB.js';
  2. import { rm } from 'fs/promises';
  3. import { statSync } from 'fs';
  4. const TEST_DB_PATH = 'db/autopersist';
  5. console.log('--- Auto-Persist Test ---\n');
  6. // Clean up
  7. await rm(TEST_DB_PATH, { recursive: true, force: true });
  8. // Create DB with low threshold for testing
  9. const db = new MPackDB(`${TEST_DB_PATH}/users`, {
  10. primaryKey: 'id',
  11. primaryKeyType: PrimaryKeyType.NUMBER,
  12. indexes: ['id', 'name'],
  13. indexPersistThreshold: 5, // Persist after 5 inserts
  14. indexPersistInterval: 0 // Disable periodic (only threshold-based)
  15. });
  16. console.log('1. Insert 3 records (below threshold)');
  17. await db.insert({ name: 'Alice', age: 30 });
  18. await db.insert({ name: 'Bob', age: 25 });
  19. await db.insert({ name: 'Charlie', age: 35 });
  20. // Check index files
  21. let idIndexSize = statSync(`${TEST_DB_PATH}/users.id.txt`).size;
  22. console.log(` Index file size: ${idIndexSize} bytes (should be 0 - not persisted yet)`);
  23. console.log('\n2. Insert 3 more records (crosses threshold of 5)');
  24. await db.insert({ name: 'Dave', age: 40 });
  25. await db.insert({ name: 'Eve', age: 28 });
  26. await db.insert({ name: 'Frank', age: 33 });
  27. // Wait a bit for async persist
  28. await new Promise(resolve => setTimeout(resolve, 100));
  29. idIndexSize = statSync(`${TEST_DB_PATH}/users.id.txt`).size;
  30. console.log(` Index file size: ${idIndexSize} bytes (should be >0 - auto-persisted!)`);
  31. console.log('\n3. Test close() - should persist remaining deltas');
  32. await db.insert({ name: 'Grace', age: 29 });
  33. await db.close();
  34. idIndexSize = statSync(`${TEST_DB_PATH}/users.id.txt`).size;
  35. console.log(` Index file size: ${idIndexSize} bytes (should include all 7 records)`);
  36. console.log('\n✓ Auto-persist works!');

Branches

Latest commits

  • 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