gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit705774a9705774a9added flush before findcaramboleyo705774a9/tests/indexed.test.js

1.7 KB

  1. import MPackDB, { PrimaryKeyType } from '../src/MPackDB.js';
  2. import { rm } from 'fs/promises';
  3. const TEST_DB_PATH = 'db/indexed';
  4. console.log('--- Indexed Database Test ---');
  5. // Clean up
  6. await rm(TEST_DB_PATH, { recursive: true, force: true });
  7. // Create database WITH indexes
  8. const db = new MPackDB(`${TEST_DB_PATH}/users`, {
  9. primaryKey: 'id',
  10. primaryKeyType: PrimaryKeyType.NUMBER,
  11. indexes: ['id', 'email', '*age'] // id and email are lexical, age is numeric (*)
  12. });
  13. console.log('\nTest 1: Insert with indexes');
  14. await db.insert({ name: 'Alice', email: '[email protected]', age: 30 });
  15. await db.insert({ name: 'Bob', email: '[email protected]', age: 25 });
  16. await db.insert({ name: 'Charlie', email: '[email protected]', age: 35 });
  17. console.log('✓ Inserted 3 records');
  18. console.log('\nTest 1.5: Persist indexes to disk');
  19. if (db._indexManager) {
  20. await db._indexManager.persist();
  21. console.log('✓ Indexes persisted');
  22. }
  23. console.log('\nTest 2: Read all (should use index)');
  24. const all = await db.find();
  25. console.log('✓ Found', all.length, 'records:', all);
  26. console.log('\nTest 3: Find by primary key (should use index)');
  27. const alice = await db.find(0);
  28. console.log('✓ Found by ID:', alice);
  29. console.log('\nTest 4: Delete (should update index)');
  30. await db.delete(r => r.age === 25);
  31. const remaining = await db.find();
  32. console.log('✓ Remaining:', remaining.length, 'records');
  33. console.log('\nTest 5: Verify index files created');
  34. const { readdirSync } = await import('fs');
  35. const files = readdirSync(TEST_DB_PATH);
  36. const indexFiles = files.filter(f => f.endsWith('.txt'));
  37. console.log('✓ Index files:', indexFiles);
  38. if (indexFiles.length !== 3) {
  39. throw new Error(`Expected 3 index files, got ${indexFiles.length}`);
  40. }
  41. console.log('\n✓ All indexed tests passed!');

Branches

Latest commits

  • 705774a9added flush before findcaramboleyo
  • b4db6391initial commitcaramboleyo