UNPKG

ryuudb

Version:

A lightweight, customizable JSON/YAML database for Node.js by Jr Busaco

77 lines (64 loc) 2.74 kB
import RyuuDB, { FileAdapter, MemoryAdapter } from './src/index.js'; import { strict as assert } from 'assert'; async function runTests() { console.log('Starting RyuuDB tests...\n'); // Test 1: Basic JSON file operations console.log('Test 1: JSON file operations'); const jsonDb = new RyuuDB({ filePath: 'test.json' }); await jsonDb.load(); jsonDb.set('users', [{ id: 1, name: 'JrDev06' }, { id: 2, name: 'Alice' }]); assert.deepEqual(jsonDb.get('users'), [{ id: 1, name: 'JrDev06' }, { id: 2, name: 'Alice' }]); console.log('✓ Set and get users'); jsonDb.update('users', { id: 1 }, { name: 'Jr Busaco' }); assert.equal(jsonDb.find('users', { id: 1 }).name, 'Jr Busaco'); console.log('✓ Updated user name'); jsonDb.remove('users', { id: 2 }); assert.equal(jsonDb.get('users').length, 1); console.log('✓ Removed user\n'); // Test 2: Chaining console.log('Test 2: Chaining methods'); jsonDb .set('posts', []) .set('posts', [{ id: 1, title: 'Hello' }]) .update('posts', { id: 1 }, { title: 'Hello World' }); assert.equal(jsonDb.find('posts', { id: 1 }).title, 'Hello World'); console.log('✓ Chained set and update\n'); // Test 3: Custom query function console.log('Test 3: Custom query functions'); const found = jsonDb.find('users', user => user.name.includes('Busaco')); assert.equal(found.id, 1); console.log('✓ Found user with custom query'); const filtered = jsonDb.filter('users', user => user.id > 0); assert.equal(filtered.length, 1); console.log('✓ Filtered users with custom query\n'); // Test 4: YAML support console.log('Test 4: YAML file operations'); const yamlDb = new RyuuDB({ filePath: 'test.yaml', format: 'yaml' }); await yamlDb.load(); yamlDb.set('tasks', [{ id: 1, task: 'Code RyuuDB' }]); assert.equal(yamlDb.get('tasks')[0].task, 'Code RyuuDB'); console.log('✓ YAML set and get\n'); // Test 5: Memory adapter console.log('Test 5: Memory adapter'); const memDb = new RyuuDB({ adapter: 'memory', defaultData: { notes: [] } }); memDb.set('notes', [{ id: 1, text: 'Test note' }]); assert.equal(memDb.get('notes')[0].text, 'Test note'); console.log('✓ Memory adapter works\n'); // Test 6: Events console.log('Test 6: Event handling'); let eventFired = false; jsonDb.on('save', () => (eventFired = true)); await jsonDb.save(); assert.equal(eventFired, true); console.log('✓ Save event triggered\n'); // Test 7: Clear database console.log('Test 7: Clear database'); jsonDb.clear(); assert.deepEqual(jsonDb.get(), {}); console.log('✓ Cleared database\n'); console.log('All tests passed!'); } runTests().catch(err => { console.error('Test failed:', err); process.exit(1); });