MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import JSONdb from './jsondb.js';
2import { unlinkSync } from 'fs';
3
4const db = new JSONdb('./mydata.json');
5
6db.set('username', 'alice');
7db.set('score', 42);
8db.set('settings', {
9 theme: 'dark',
10 notifications: true,
11 language: 'en'
12});
13db.set('tags', ['javascript', 'nodejs', 'database']);
14
15console.log('Username:', db.get('username'));
16console.log('Score:', db.get('score'));
17console.log('Settings:', db.get('settings'));
18
19console.log('Has username?', db.has('username'));
20console.log('Has email?', db.has('email'));
21
22console.log('Email:', db.get('email'));
23
24db.set('score', 100);
25console.log('New score:', db.get('score'));
26
27db.delete('tags');
28console.log('Has tags?', db.has('tags'));
29
30const snapshot = db.JSON();
31console.log('Full DB:', snapshot);
32
33db.JSON({ fresh: 'start', count: 1 });
34console.log('After replace:', db.JSON());
35
36db.deleteAll();
37console.log('After deleteAll:', db.JSON()); // {}
38
39const customDb = new JSONdb('./custom.json', {
40 asyncWrite: false,
41 syncOnWrite: false,
42 jsonSpaces: 2
43});
44
45customDb.set('key1', 'value1');
46customDb.set('key2', 'value2');
47customDb.sync();
48
49try {
50 unlinkSync('./mydata.json');
51 unlinkSync('./custom.json');
52 console.log('\nTest files cleaned up successfully.');
53} catch (err) {
54 console.error('Cleanup error:', err.message);
55}