Skip to content

Commit c6217f9

Browse files
author
Snowflake107
committed
memory db
1 parent eb46721 commit c6217f9

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ $ npm i --save @devsnowflake/jsql
1313

1414
```js
1515
const { Database } = require("@devsnowflake/jsql");
16+
17+
// file based
1618
const db = new Database("./database.json");
1719

20+
// in-memory
21+
const db = new Database(":memory:");
22+
1823
// creating a table
1924
db.prepare(`CREATE TABLE IF NOT EXISTS "DEMO" ("key" TEXT, "value" TEXT)`).run();
2025

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devsnowflake/jsql",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Use JSON as SQL.",
55
"main": "index.js",
66
"types": "typings/index.d.ts",

src/jsql/Database.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ const handlers = {
88
};
99

1010
class Database {
11-
constructor(name) {
12-
this.name = name;
13-
if (!fs.existsSync(this.name)) fs.writeFileSync(this.name, '{}');
11+
constructor(name, memory = false) {
12+
this.name = memory === true ? ':memory:' : name;
13+
this.memory = !memory && this.name === ':memory:' ? true : Boolean(memory);
14+
this._memdb = {};
15+
16+
Object.defineProperty(this, '_memdb', { enumerable: false });
17+
18+
if (!this.memory && !fs.existsSync(this.name)) fs.writeFileSync(this.name, '{}');
1419
}
1520

1621
get db() {
1722
try {
18-
const db = JSON.parse(fs.readFileSync(this.name, 'utf-8'));
23+
const db = this.memory ? this._memdb : JSON.parse(fs.readFileSync(this.name, 'utf-8'));
1924
return db;
2025
} catch {
2126
throw new Error('malformed database');
@@ -27,7 +32,8 @@ class Database {
2732
}
2833

2934
write(data) {
30-
fs.writeFileSync(this.name, JSON.stringify(data));
35+
if (!this.memory) fs.writeFileSync(this.name, JSON.stringify(data));
36+
else this._memdb = data;
3137
}
3238

3339
run(query) {
@@ -67,4 +73,4 @@ class Database {
6773
}
6874
}
6975

70-
module.exports = Database;
76+
module.exports = Database;

src/sql/parser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const parser = new Parser();
44
module.exports.parse = function (query) {
55
try {
66
return parser.astify(query);
7-
} catch(e) {
8-
const expected = [...new Set((e.expected ?? []).filter(m => m.type === "literal").map((m) => m.text))];
9-
throw new SyntaxError(`Expected ${expected.join(", ")} but received "${e.found}"!`);
7+
} catch (e) {
8+
const expected = [...new Set((e.expected ?? []).filter((m) => m.type === 'literal').map((m) => m.text))];
9+
throw new SyntaxError(`Expected ${expected.join(', ')} but received "${e.found}"!`);
1010
}
1111
};
1212

typings/index.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { AST } from "node-sql-parser"
2-
3-
declare module "jsql" {
1+
import { AST } from 'node-sql-parser';
42

3+
declare module 'jsql' {
54
export interface Handlers {
65
create: (q: AST) => void;
76
select: (q: AST) => any[];
@@ -24,13 +23,13 @@ declare module "jsql" {
2423
}
2524

2625
export class Database {
27-
constructor(name: string);
26+
memory: boolean;
27+
constructor(name: string, memory?: boolean);
2828
get db(): any;
2929
static get handlers(): Handlers;
3030
write(data: any): void;
3131
run(query: Statement): any;
3232
prepare(query: Statement): DatabasePrepare;
3333
parse(statement: Statement): string;
3434
}
35-
36-
}
35+
}

0 commit comments

Comments
 (0)