mpackdb
All repositories: gitoria
4.7 KB
import MPackDB from '../src/MPackDB.js';import { rm } from 'fs/promises';const TEST_DB_PATH = 'db/prefix';console.log('='.repeat(60));console.log('Primary Key Prefix Syntax Test');console.log('='.repeat(60));await rm(TEST_DB_PATH, { recursive: true, force: true });// ============================================================================// Test 1: *id = numeric primary key// ============================================================================console.log('\n📋 Test 1: *id = numeric primary key');console.log('-'.repeat(60));const db1 = new MPackDB(`${TEST_DB_PATH}/numeric`, {primaryKey: '*id' // No need for primaryKeyType!});await db1.insert({ name: 'Alice', age: 30 });await db1.insert({ name: 'Bob', age: 25 });const all1 = await db1.find();console.log(`✓ Inserted 2 records with auto-increment IDs: ${all1.map(r => r.id).join(', ')}`);const byId1 = await db1.find(0);console.log(`✓ Find by ID (0): ${byId1[0]?.name}`);await db1.close();// ============================================================================// Test 2: @uuid = UUID primary key// ============================================================================console.log('\n📋 Test 2: @uuid = UUID primary key');console.log('-'.repeat(60));const db2 = new MPackDB(`${TEST_DB_PATH}/uuid`, {primaryKey: '@uuid' // No need for primaryKeyType!});await db2.insert({ uuid: 'aaa-111', name: 'Alice', age: 30 });await db2.insert({ uuid: 'bbb-222', name: 'Bob', age: 25 });const all2 = await db2.find();console.log(`✓ Inserted 2 records with UUIDs: ${all2.map(r => r.uuid).join(', ')}`);const byUuid = await db2.find('aaa-111');console.log(`✓ Find by UUID (aaa-111): ${byUuid[0]?.name}`);await db2.close();// ============================================================================// Test 3: username = string primary key (default)// ============================================================================console.log('\n📋 Test 3: username = string primary key (default)');console.log('-'.repeat(60));const db3 = new MPackDB(`${TEST_DB_PATH}/string`, {primaryKey: 'username' // No prefix = string});await db3.insert({ username: 'alice123', name: 'Alice', age: 30 });await db3.insert({ username: 'bob456', name: 'Bob', age: 25 });const all3 = await db3.find();console.log(`✓ Inserted 2 records with usernames: ${all3.map(r => r.username).join(', ')}`);const byUsername = await db3.find('bob456');console.log(`✓ Find by username (bob456): ${byUsername[0]?.name}`);await db3.close();// ============================================================================// Test 4: Mixed indexes with prefixes// ============================================================================console.log('\n📋 Test 4: Mixed indexes with prefixes');console.log('-'.repeat(60));const db4 = new MPackDB(`${TEST_DB_PATH}/mixed`, {primaryKey: '*id', // Numeric primary keyindexes: ['email', '*score', '@uuid'], // Lexical, numeric, UUID indexesindexPersistThreshold: 2});await db4.insert({ email: '[email protected]', score: 95, uuid: 'aaa-111' });await db4.insert({ email: '[email protected]', score: 87, uuid: 'bbb-222' });await db4.insert({ email: '[email protected]', score: 92, uuid: 'ccc-333' });await new Promise(resolve => setTimeout(resolve, 100));const all4 = await db4.find();console.log(`✓ Inserted 3 records with mixed indexes`);// Check index filesconst { readdirSync, statSync } = await import('fs');const indexFiles = readdirSync(TEST_DB_PATH).filter(f => f.startsWith('mixed.') && f.endsWith('.txt'));const nonEmpty = indexFiles.filter(f => statSync(`${TEST_DB_PATH}/${f}`).size > 0);console.log(`✓ Created ${nonEmpty.length} index files: ${nonEmpty.join(', ')}`);await db4.close();// ============================================================================// Test 5: Verify meta.json only has nextId for numeric PK// ============================================================================console.log('\n📋 Test 5: Verify meta.json content');console.log('-'.repeat(60));const { readFileSync } = await import('fs');const meta1 = JSON.parse(readFileSync(`${TEST_DB_PATH}/numeric.meta.json`, 'utf-8'));console.log(`✓ Numeric PK meta: ${JSON.stringify(meta1)} (has nextId: ${meta1.hasOwnProperty('nextId')})`);const meta2 = JSON.parse(readFileSync(`${TEST_DB_PATH}/uuid.meta.json`, 'utf-8'));console.log(`✓ UUID PK meta: ${JSON.stringify(meta2)} (has nextId: ${meta2.hasOwnProperty('nextId')})`);const meta3 = JSON.parse(readFileSync(`${TEST_DB_PATH}/string.meta.json`, 'utf-8'));console.log(`✓ String PK meta: ${JSON.stringify(meta3)} (has nextId: ${meta3.hasOwnProperty('nextId')})`);console.log('\n' + '='.repeat(60));console.log('✓ All prefix syntax tests passed!');console.log('='.repeat(60));
Branches
- mastermain branch
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