gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmaster87888725release 1.0.7caramboleyomaster/tests/prefix-syntax.test.js

4.7 KB

  1. import MPackDB from '../src/MPackDB.js';
  2. import { rm } from 'fs/promises';
  3. const TEST_DB_PATH = 'db/prefix';
  4. console.log('='.repeat(60));
  5. console.log('Primary Key Prefix Syntax Test');
  6. console.log('='.repeat(60));
  7. await rm(TEST_DB_PATH, { recursive: true, force: true });
  8. // ============================================================================
  9. // Test 1: *id = numeric primary key
  10. // ============================================================================
  11. console.log('\n📋 Test 1: *id = numeric primary key');
  12. console.log('-'.repeat(60));
  13. const db1 = new MPackDB(`${TEST_DB_PATH}/numeric`, {
  14. primaryKey: '*id' // No need for primaryKeyType!
  15. });
  16. await db1.insert({ name: 'Alice', age: 30 });
  17. await db1.insert({ name: 'Bob', age: 25 });
  18. const all1 = await db1.find();
  19. console.log(`✓ Inserted 2 records with auto-increment IDs: ${all1.map(r => r.id).join(', ')}`);
  20. const byId1 = await db1.find(0);
  21. console.log(`✓ Find by ID (0): ${byId1[0]?.name}`);
  22. await db1.close();
  23. // ============================================================================
  24. // Test 2: @uuid = UUID primary key
  25. // ============================================================================
  26. console.log('\n📋 Test 2: @uuid = UUID primary key');
  27. console.log('-'.repeat(60));
  28. const db2 = new MPackDB(`${TEST_DB_PATH}/uuid`, {
  29. primaryKey: '@uuid' // No need for primaryKeyType!
  30. });
  31. await db2.insert({ uuid: 'aaa-111', name: 'Alice', age: 30 });
  32. await db2.insert({ uuid: 'bbb-222', name: 'Bob', age: 25 });
  33. const all2 = await db2.find();
  34. console.log(`✓ Inserted 2 records with UUIDs: ${all2.map(r => r.uuid).join(', ')}`);
  35. const byUuid = await db2.find('aaa-111');
  36. console.log(`✓ Find by UUID (aaa-111): ${byUuid[0]?.name}`);
  37. await db2.close();
  38. // ============================================================================
  39. // Test 3: username = string primary key (default)
  40. // ============================================================================
  41. console.log('\n📋 Test 3: username = string primary key (default)');
  42. console.log('-'.repeat(60));
  43. const db3 = new MPackDB(`${TEST_DB_PATH}/string`, {
  44. primaryKey: 'username' // No prefix = string
  45. });
  46. await db3.insert({ username: 'alice123', name: 'Alice', age: 30 });
  47. await db3.insert({ username: 'bob456', name: 'Bob', age: 25 });
  48. const all3 = await db3.find();
  49. console.log(`✓ Inserted 2 records with usernames: ${all3.map(r => r.username).join(', ')}`);
  50. const byUsername = await db3.find('bob456');
  51. console.log(`✓ Find by username (bob456): ${byUsername[0]?.name}`);
  52. await db3.close();
  53. // ============================================================================
  54. // Test 4: Mixed indexes with prefixes
  55. // ============================================================================
  56. console.log('\n📋 Test 4: Mixed indexes with prefixes');
  57. console.log('-'.repeat(60));
  58. const db4 = new MPackDB(`${TEST_DB_PATH}/mixed`, {
  59. primaryKey: '*id', // Numeric primary key
  60. indexes: ['email', '*score', '@uuid'], // Lexical, numeric, UUID indexes
  61. indexPersistThreshold: 2
  62. });
  63. await db4.insert({ email: '[email protected]', score: 95, uuid: 'aaa-111' });
  64. await db4.insert({ email: '[email protected]', score: 87, uuid: 'bbb-222' });
  65. await db4.insert({ email: '[email protected]', score: 92, uuid: 'ccc-333' });
  66. await new Promise(resolve => setTimeout(resolve, 100));
  67. const all4 = await db4.find();
  68. console.log(`✓ Inserted 3 records with mixed indexes`);
  69. // Check index files
  70. const { readdirSync, statSync } = await import('fs');
  71. const indexFiles = readdirSync(TEST_DB_PATH).filter(f => f.startsWith('mixed.') && f.endsWith('.txt'));
  72. const nonEmpty = indexFiles.filter(f => statSync(`${TEST_DB_PATH}/${f}`).size > 0);
  73. console.log(`✓ Created ${nonEmpty.length} index files: ${nonEmpty.join(', ')}`);
  74. await db4.close();
  75. // ============================================================================
  76. // Test 5: Verify meta.json only has nextId for numeric PK
  77. // ============================================================================
  78. console.log('\n📋 Test 5: Verify meta.json content');
  79. console.log('-'.repeat(60));
  80. const { readFileSync } = await import('fs');
  81. const meta1 = JSON.parse(readFileSync(`${TEST_DB_PATH}/numeric.meta.json`, 'utf-8'));
  82. console.log(`✓ Numeric PK meta: ${JSON.stringify(meta1)} (has nextId: ${meta1.hasOwnProperty('nextId')})`);
  83. const meta2 = JSON.parse(readFileSync(`${TEST_DB_PATH}/uuid.meta.json`, 'utf-8'));
  84. console.log(`✓ UUID PK meta: ${JSON.stringify(meta2)} (has nextId: ${meta2.hasOwnProperty('nextId')})`);
  85. const meta3 = JSON.parse(readFileSync(`${TEST_DB_PATH}/string.meta.json`, 'utf-8'));
  86. console.log(`✓ String PK meta: ${JSON.stringify(meta3)} (has nextId: ${meta3.hasOwnProperty('nextId')})`);
  87. console.log('\n' + '='.repeat(60));
  88. console.log('✓ All prefix syntax tests passed!');
  89. console.log('='.repeat(60));

Branches

  • mastermain branch

Latest commits

  • 87888725release 1.0.7caramboleyo
  • 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