@possumtech/sqlrite 0.1.1 → 0.1.4
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/CODE_OF_CONDUCT.md +13 -0
- package/CONTRIBUTING.md +1 -0
- package/README.md +118 -37
- package/SqlRite.d.ts +22 -0
- package/SqlRite.js +45 -0
- package/biome.json +30 -0
- package/package.json +11 -4
- package/sql/test.sql +28 -0
- package/test/test.js +19 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# sqlrite Contributor Code of Conduct
|
|
2
|
+
|
|
3
|
+
sqlrite is an open source project and not a community. It exists to perform the
|
|
4
|
+
technical goals outlined in the readme and implemented in the codebase. All
|
|
5
|
+
contributions are equally welcome, except from those who reject the premise that
|
|
6
|
+
Sqlrite is an open source project and not a community.
|
|
7
|
+
|
|
8
|
+
Those seeking a "community" are encouraged to reach out to a local church,
|
|
9
|
+
mosque, temple, synagogue, or local civic organization, at your discretion, for
|
|
10
|
+
community support. sqlrite does not support or endorse any cause or campaign.
|
|
11
|
+
|
|
12
|
+
Conversations on the platform are to pertain to the project. All other topics
|
|
13
|
+
are unwelcome.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Contributions are welcome, and I will do my best to work with you on any workflow, coding style, or other issues that may arise.
|
package/README.md
CHANGED
|
@@ -5,41 +5,93 @@ SQL Done Right
|
|
|
5
5
|
## About sqlrite
|
|
6
6
|
|
|
7
7
|
The sqlrite package is a modern node module that delivers an opinionated
|
|
8
|
-
alternative to ORMs.
|
|
8
|
+
alternative to ORMs. It is a thin wrapper around the
|
|
9
|
+
[native sqlite module](https://nodejs.org/api/sqlite.html), which
|
|
10
|
+
enables one to separate SQL code from Javascript code.
|
|
9
11
|
|
|
10
12
|
## Opinions
|
|
11
13
|
|
|
12
|
-
1. **SQL
|
|
14
|
+
1. **SQL Supremacy**: Your application is data, and SQL is the best interface.
|
|
13
15
|
|
|
14
16
|
2. **Standards**: Node is the standard for server-side web apps, and it now
|
|
15
17
|
contains a native sqlite module. Sqlite is the standard for SQL.
|
|
16
18
|
|
|
17
19
|
3. **Simplicity**: It takes as much time to master an ORM as it would take to
|
|
18
20
|
just master SQL, and with worse performance. For all but the most distributed,
|
|
19
|
-
concurrent, and custom use cases, sqlite is the
|
|
21
|
+
concurrent, and custom use cases, sqlite is the simple, correct choice.
|
|
20
22
|
|
|
21
|
-
4. **Security**: Inline SQL is insecure,
|
|
23
|
+
4. **Security**: Inline SQL is insecure, unmaintainable, and error-prone.
|
|
22
24
|
|
|
23
|
-
5. **
|
|
25
|
+
5. **Speed**: By enforcing the use of prepared statements, sqlrite ensures that
|
|
26
|
+
your queries are compiled and cached by the sqlite engine.
|
|
27
|
+
|
|
28
|
+
6. **Separation**: SQL code should be in separate SQL files rather than
|
|
24
29
|
scattered throughout your JS codebase.
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
7. **Size**: By relying on the native sqlite module, and nothing else, sqlrite
|
|
32
|
+
won't bloat your project with unnecessary dependencies. This thing is *tiny*.
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
27
35
|
|
|
28
36
|
**SQL**
|
|
29
37
|
|
|
30
|
-
Add a `sql`
|
|
38
|
+
Add a `sql` folder to your project and include as many `.sql` files as you
|
|
31
39
|
wish, with whatever folder structure you like. Sqlrite will automatically load
|
|
32
40
|
them all.
|
|
33
41
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
| Syntax | Name | Description |
|
|
43
|
+
|---------------------|------------------------|------------------------|
|
|
44
|
+
| `-- INIT: txName` | Executed Transaction | Executed transaction |
|
|
45
|
+
| `-- EXEC: txName` | Executable Transaction | Executable transaction |
|
|
46
|
+
| `-- PREP: stmtName` | Prepared Statements | Prepared statement |
|
|
47
|
+
|
|
48
|
+
There are three types of "chunk" one can add to a `.sql` file:
|
|
49
|
+
|
|
50
|
+
1. **INIT**: A transaction that is executed when the module is instantiated.
|
|
51
|
+
This is where you should create your tables, for example.
|
|
52
|
+
|
|
53
|
+
2. **EXEC**: A transaction that can be executed at any time. For example,
|
|
54
|
+
dropping a table. This is where you should put your transactions that are
|
|
55
|
+
not prepared statements, like maintaining your database.
|
|
56
|
+
|
|
57
|
+
3. **PREP**: A prepared statement that can be executed at any time. This is
|
|
58
|
+
where you should put your queries. After declaring a prepared statement, you can
|
|
59
|
+
then run it with either the `.all({})`, `.get({})` or `.run({})` methods, as per
|
|
60
|
+
the native sqlite API.
|
|
61
|
+
|
|
62
|
+
| Method | Description |
|
|
63
|
+
|------------|-------------------------------------------------------|
|
|
64
|
+
| `.all({})` | Returns all rows that match the query. |
|
|
65
|
+
| `.get({})` | Returns the first row that matches the query. |
|
|
66
|
+
| `.run({})` | Executes the query and returns the (optional) result. |
|
|
67
|
+
|
|
68
|
+
### Synchronous/Asynchronous
|
|
69
|
+
|
|
70
|
+
The native sqlite module currently only supports synchronous operations. This
|
|
71
|
+
can pose a performance issue in some common cases. Sqlrite addresses this by
|
|
72
|
+
allowing one to run one's queries asynchronously by appending `.async`:
|
|
73
|
+
|
|
74
|
+
For example, instead of:
|
|
75
|
+
|
|
76
|
+
**Synchronous**
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
console.log(sql.getPositions.all());
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Asynchronous**
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
sql.async.getPositions.all().then((positions) => console.log(positions));
|
|
86
|
+
```
|
|
87
|
+
|
|
38
88
|
|
|
39
89
|
**Example SQL File**
|
|
40
90
|
|
|
41
91
|
```sql
|
|
42
|
-
--
|
|
92
|
+
-- INIT: createEmployeeTable
|
|
93
|
+
BEGIN TRANSACTION;
|
|
94
|
+
|
|
43
95
|
CREATE TABLE IF NOT EXISTS employees (
|
|
44
96
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
45
97
|
name TEXT NOT NULL,
|
|
@@ -47,58 +99,87 @@ CREATE TABLE IF NOT EXISTS employees (
|
|
|
47
99
|
salary REAL NOT NULL
|
|
48
100
|
);
|
|
49
101
|
|
|
50
|
-
|
|
102
|
+
END TRANSACTION;
|
|
103
|
+
|
|
104
|
+
-- EXEC: deleteTable
|
|
105
|
+
BEGIN TRANSACTION;
|
|
106
|
+
|
|
107
|
+
DROP TABLE IF EXISTS employees;
|
|
108
|
+
|
|
109
|
+
END TRANSACTION;
|
|
110
|
+
|
|
111
|
+
-- PREP: addEmployee
|
|
51
112
|
INSERT INTO employees (name, position, salary)
|
|
52
113
|
VALUES ($name, $position, $salary);
|
|
53
114
|
|
|
54
|
-
--
|
|
115
|
+
-- PREP: getPositions
|
|
116
|
+
SELECT name, position FROM employees;
|
|
117
|
+
|
|
118
|
+
-- PREP: getHighestPaidEmployee
|
|
55
119
|
SELECT name FROM employees ORDER BY salary DESC LIMIT 1;
|
|
56
120
|
```
|
|
57
121
|
|
|
58
122
|
**Example Node File**
|
|
59
123
|
|
|
60
124
|
```js
|
|
61
|
-
import
|
|
125
|
+
import SqlRite from "@possumtech/sqlrite";
|
|
62
126
|
|
|
63
|
-
const sql = new
|
|
127
|
+
const sql = new SqlRite();
|
|
64
128
|
|
|
65
|
-
(
|
|
66
|
-
|
|
129
|
+
sql.addEmployee.run({ name: "John", position: "CEO", salary: 99999 });
|
|
130
|
+
sql.addEmployee.run({ name: "Jane", position: "COO", salary: 49998 });
|
|
131
|
+
sql.addEmployee.run({ name: "Jack", position: "CFO", salary: 49997 });
|
|
132
|
+
sql.addEmployee.run({ name: "Jill", position: "CIO", salary: 49996 });
|
|
67
133
|
|
|
68
|
-
|
|
69
|
-
await sql.addEmployee.run({ name: "Jane", position: "COO", salary: 49998 });
|
|
70
|
-
await sql.addEmployee.run({ name: "Jack", position: "CFO", salary: 49998 });
|
|
71
|
-
await sql.addEmployee.run({ name: "Jill", position: "CIO", salary: 49998 });
|
|
134
|
+
const employee = sql.getHighestPaidEmployee.get();
|
|
72
135
|
|
|
73
|
-
|
|
136
|
+
assert(employee?.name === "John", "The highest paid employee should be John");
|
|
74
137
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
138
|
+
sql.async.getPositions.all().then((positions) => console.log(positions));
|
|
139
|
+
|
|
140
|
+
console.log(`The highest paid employee is ${employee.name}.`);
|
|
78
141
|
|
|
142
|
+
sql.deleteTable();
|
|
143
|
+
```
|
|
79
144
|
|
|
80
145
|
## Installation
|
|
81
146
|
|
|
82
|
-
Navigate to your project directory and run the following command:
|
|
147
|
+
1. Navigate to your project directory and run the following command:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
npm install @possumtech/sqlrite
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
2. Then create a `sql` directory in your project directory. This is where you
|
|
154
|
+
will put your SQL files.
|
|
83
155
|
|
|
84
156
|
```bash
|
|
85
|
-
|
|
157
|
+
mkdir sql
|
|
158
|
+
cd sql
|
|
159
|
+
touch exampleFile.sql
|
|
86
160
|
```
|
|
87
161
|
|
|
88
162
|
## Configuration
|
|
89
163
|
|
|
90
164
|
```js
|
|
91
|
-
import
|
|
165
|
+
import SqlRite from "@possumtech/sqlrite";
|
|
92
166
|
|
|
93
|
-
const sql = new
|
|
94
|
-
|
|
95
|
-
|
|
167
|
+
const sql = new SqlRite({
|
|
168
|
+
// SQLite database file path.
|
|
169
|
+
path: ":memory:",
|
|
96
170
|
|
|
97
|
-
|
|
98
|
-
|
|
171
|
+
// Path to your SQL directory.
|
|
172
|
+
dir: "sql/",
|
|
99
173
|
});
|
|
100
174
|
```
|
|
101
175
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
176
|
+
You will almost certainly wish to replace the `path` with a path to your
|
|
177
|
+
database file. Otherwise, the database will be created in memory and lost when
|
|
178
|
+
the process ends.
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
const sql = new SqlRite({ path: "path/to/your/database.sqlite3" });
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Additional arguments will be passed to the options object of the native sqlite
|
|
185
|
+
module.
|
package/SqlRite.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Database, DatabaseSync, Statement } from 'node:sqlite';
|
|
2
|
+
|
|
3
|
+
interface SqlRiteOptions {
|
|
4
|
+
path?: string;
|
|
5
|
+
dir?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface SqlRiteAsyncPreparedStatements {
|
|
9
|
+
all: (params?: Record<string, any>) => Promise<any[]>;
|
|
10
|
+
get: (params?: Record<string, any>) => Promise<any>;
|
|
11
|
+
run: (params?: Record<string, any>) => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface SqlRiteAsyncMethods {
|
|
15
|
+
[key: string]: (() => Promise<void>) | SqlRiteAsyncPreparedStatements;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default class SqlRite {
|
|
19
|
+
constructor(options?: SqlRiteOptions);
|
|
20
|
+
async: SqlRiteAsyncMethods;
|
|
21
|
+
[key: string]: (() => void) | Statement | SqlRiteAsyncMethods | any;
|
|
22
|
+
}
|
package/SqlRite.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { DatabaseSync } from "node:sqlite";
|
|
3
|
+
|
|
4
|
+
export default class SqlRite {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
const defaults = {
|
|
7
|
+
path: ":memory:",
|
|
8
|
+
dir: "sql/",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const merged = { ...defaults, ...options };
|
|
12
|
+
|
|
13
|
+
const db = new DatabaseSync(merged.path, merged);
|
|
14
|
+
|
|
15
|
+
const { dir } = merged;
|
|
16
|
+
const files = fs.readdirSync(dir);
|
|
17
|
+
const code = files.map((f) => fs.readFileSync(dir + f, "utf8")).join("");
|
|
18
|
+
|
|
19
|
+
this.async = {};
|
|
20
|
+
|
|
21
|
+
const chunks =
|
|
22
|
+
/-- (?<chunk>(?<type>INIT|EXEC|PREP): (?<name>\w+)\n(?<sql>.*?))($|(?=-- (INIT|EXEC|PREP):))/gs;
|
|
23
|
+
|
|
24
|
+
for (const chunk of code.matchAll(chunks)) {
|
|
25
|
+
const { type, name, sql } = chunk.groups;
|
|
26
|
+
switch (type) {
|
|
27
|
+
case "INIT":
|
|
28
|
+
db.exec(sql);
|
|
29
|
+
break;
|
|
30
|
+
case "EXEC":
|
|
31
|
+
this[name] = () => db.exec(sql);
|
|
32
|
+
this.async[name] = async () => db.exec(sql);
|
|
33
|
+
break;
|
|
34
|
+
case "PREP":
|
|
35
|
+
this[name] = db.prepare(sql);
|
|
36
|
+
|
|
37
|
+
this.async[name] = {};
|
|
38
|
+
this.async[name].all = async (params = {}) => this[name].all(params);
|
|
39
|
+
this.async[name].get = async (params = {}) => this[name].get(params);
|
|
40
|
+
this.async[name].run = async (params = {}) => this[name].run(params);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
package/biome.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
|
|
3
|
+
"vcs": {
|
|
4
|
+
"enabled": false,
|
|
5
|
+
"clientKind": "git",
|
|
6
|
+
"useIgnoreFile": false
|
|
7
|
+
},
|
|
8
|
+
"files": {
|
|
9
|
+
"ignoreUnknown": false,
|
|
10
|
+
"ignore": ["SqlRite.d.ts", "package.json", "package-lock.json"]
|
|
11
|
+
},
|
|
12
|
+
"formatter": {
|
|
13
|
+
"enabled": true,
|
|
14
|
+
"indentStyle": "tab"
|
|
15
|
+
},
|
|
16
|
+
"organizeImports": {
|
|
17
|
+
"enabled": true
|
|
18
|
+
},
|
|
19
|
+
"linter": {
|
|
20
|
+
"enabled": true,
|
|
21
|
+
"rules": {
|
|
22
|
+
"recommended": true
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"javascript": {
|
|
26
|
+
"formatter": {
|
|
27
|
+
"quoteStyle": "double"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@possumtech/sqlrite",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "SQL Done Right",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"node",
|
|
7
|
+
"sqlite",
|
|
8
|
+
"sql",
|
|
9
|
+
"orm",
|
|
10
|
+
"database"
|
|
11
|
+
],
|
|
6
12
|
"homepage": "https://github.com/possumtech/sqlrite#readme",
|
|
7
13
|
"bugs": {
|
|
8
14
|
"url": "https://github.com/possumtech/sqlrite/issues"
|
|
@@ -14,8 +20,9 @@
|
|
|
14
20
|
"license": "MIT",
|
|
15
21
|
"author": "@wikitopian",
|
|
16
22
|
"type": "module",
|
|
17
|
-
"main": "
|
|
23
|
+
"main": "SqlRite.js",
|
|
18
24
|
"scripts": {
|
|
19
|
-
"
|
|
25
|
+
"syntax": "npx @biomejs/biome check",
|
|
26
|
+
"test": "node --test"
|
|
20
27
|
}
|
|
21
28
|
}
|
package/sql/test.sql
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
-- INIT: createEmployeeTable
|
|
2
|
+
BEGIN TRANSACTION;
|
|
3
|
+
|
|
4
|
+
CREATE TABLE IF NOT EXISTS employees (
|
|
5
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
6
|
+
name TEXT NOT NULL,
|
|
7
|
+
position TEXT NOT NULL,
|
|
8
|
+
salary REAL NOT NULL
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
END TRANSACTION;
|
|
12
|
+
|
|
13
|
+
-- EXEC: deleteTable
|
|
14
|
+
BEGIN TRANSACTION;
|
|
15
|
+
|
|
16
|
+
DROP TABLE IF EXISTS employees;
|
|
17
|
+
|
|
18
|
+
END TRANSACTION;
|
|
19
|
+
|
|
20
|
+
-- PREP: addEmployee
|
|
21
|
+
INSERT INTO employees (name, position, salary)
|
|
22
|
+
VALUES ($name, $position, $salary);
|
|
23
|
+
|
|
24
|
+
-- PREP: getPositions
|
|
25
|
+
SELECT name, position FROM employees;
|
|
26
|
+
|
|
27
|
+
-- PREP: getHighestPaidEmployee
|
|
28
|
+
SELECT name FROM employees ORDER BY salary DESC LIMIT 1;
|
package/test/test.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import SqlRite from "../SqlRite.js";
|
|
3
|
+
|
|
4
|
+
const sql = new SqlRite();
|
|
5
|
+
|
|
6
|
+
sql.addEmployee.run({ name: "John", position: "CEO", salary: 99999 });
|
|
7
|
+
sql.addEmployee.run({ name: "Jane", position: "COO", salary: 49998 });
|
|
8
|
+
sql.addEmployee.run({ name: "Jack", position: "CFO", salary: 49997 });
|
|
9
|
+
sql.addEmployee.run({ name: "Jill", position: "CIO", salary: 49996 });
|
|
10
|
+
|
|
11
|
+
const employee = sql.getHighestPaidEmployee.get();
|
|
12
|
+
|
|
13
|
+
assert(employee?.name === "John", "The highest paid employee should be John");
|
|
14
|
+
|
|
15
|
+
sql.async.getPositions.all().then((positions) => console.log(positions));
|
|
16
|
+
|
|
17
|
+
console.log(`The highest paid employee is ${employee.name}.`);
|
|
18
|
+
|
|
19
|
+
sql.deleteTable();
|