spectre.db 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -0
- package/README.md +253 -0
- package/examples/advanced.js +85 -0
- package/examples/basic.js +76 -0
- package/examples/discord-bot.js +103 -0
- package/index.js +79 -0
- package/package.json +44 -0
- package/src/engine.js +605 -0
package/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { Engine, Table } = require('./src/engine');
|
|
5
|
+
|
|
6
|
+
function normalizePath(filePath) {
|
|
7
|
+
return path.resolve(filePath)
|
|
8
|
+
.replace(/\.json\.gz$/i, '')
|
|
9
|
+
.replace(/\.json$/i, '')
|
|
10
|
+
.replace(/\.gz$/i, '');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function mapLegacyOptions(opts) {
|
|
14
|
+
const mapped = {};
|
|
15
|
+
|
|
16
|
+
if (opts.maxCacheSize !== undefined) mapped.maxCacheSize = opts.maxCacheSize;
|
|
17
|
+
if (opts.cacheTTL !== undefined) mapped.cacheTTL = opts.cacheTTL;
|
|
18
|
+
if (opts.compress !== undefined) mapped.compress = opts.compress;
|
|
19
|
+
if (opts.encryptionKey !== undefined) mapped.encryptionKey = opts.encryptionKey;
|
|
20
|
+
if (opts.backupCount !== undefined) mapped.backupCount = opts.backupCount;
|
|
21
|
+
|
|
22
|
+
if (opts.autoSave !== undefined) {
|
|
23
|
+
mapped.compactInterval = opts.autoSave > 0 ? opts.autoSave : 0;
|
|
24
|
+
mapped.compactThreshold = opts.autoSave > 0 ? 50 : Infinity;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (opts.backup === false) mapped.backupCount = 0;
|
|
28
|
+
if (opts.cache === false) mapped.maxCacheSize = 0;
|
|
29
|
+
|
|
30
|
+
if (opts.compactThreshold !== undefined) mapped.compactThreshold = opts.compactThreshold;
|
|
31
|
+
if (opts.compactInterval !== undefined) mapped.compactInterval = opts.compactInterval;
|
|
32
|
+
|
|
33
|
+
return mapped;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class Database extends Engine {
|
|
37
|
+
constructor(filePath = './database.json', options = {}) {
|
|
38
|
+
const cleanPath = normalizePath(filePath);
|
|
39
|
+
const opts = mapLegacyOptions(options);
|
|
40
|
+
|
|
41
|
+
super(cleanPath, opts);
|
|
42
|
+
|
|
43
|
+
this._legacyWarmKeys = Array.isArray(options.warmKeys) ? options.warmKeys : [];
|
|
44
|
+
|
|
45
|
+
this.ready = this.ready.then(() => {
|
|
46
|
+
for (const key of this._legacyWarmKeys) {
|
|
47
|
+
try { this.get(key); } catch {}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
transaction(arg) {
|
|
53
|
+
if (typeof arg === 'function') {
|
|
54
|
+
return super.transaction(arg);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (Array.isArray(arg)) {
|
|
58
|
+
return super.transaction(tx => {
|
|
59
|
+
const results = [];
|
|
60
|
+
for (const op of arg) {
|
|
61
|
+
switch (op.type) {
|
|
62
|
+
case 'set': results.push(tx.set(op.key, op.value)); break;
|
|
63
|
+
case 'delete': results.push(tx.delete(op.key)); break;
|
|
64
|
+
case 'add': results.push(tx.add(op.key, op.value)); break;
|
|
65
|
+
case 'sub': results.push(tx.sub(op.key, op.value)); break;
|
|
66
|
+
case 'push': results.push(tx.push(op.key, op.value)); break;
|
|
67
|
+
case 'pull': results.push(tx.pull(op.key, op.value)); break;
|
|
68
|
+
default: throw new Error(`[spectre.db] Unknown operation type: "${op.type}"`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return results;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
throw new TypeError('[spectre.db] transaction() expects an array of operations or an async function');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { Database, Table };
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "spectre.db",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight, production-grade file-based JSON database for Node.js. Zero dependencies, WAL-based persistence, LRU cache, atomic writes, and optional AES-256 encryption.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"src/",
|
|
9
|
+
"examples/",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"start": "node examples/basic.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"database",
|
|
18
|
+
"json",
|
|
19
|
+
"storage",
|
|
20
|
+
"discord",
|
|
21
|
+
"discord-bot",
|
|
22
|
+
"key-value",
|
|
23
|
+
"wal",
|
|
24
|
+
"lru",
|
|
25
|
+
"cache",
|
|
26
|
+
"lightweight",
|
|
27
|
+
"file-based",
|
|
28
|
+
"no-dependencies",
|
|
29
|
+
"nodejs"
|
|
30
|
+
],
|
|
31
|
+
"author": "scarysmonsters",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/scarysmonsters/spectre.db.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/scarysmonsters/spectre.db/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/scarysmonsters/spectre.db#readme"
|
|
44
|
+
}
|