@cloud-cli/d0 1.0.3 → 1.1.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/README.md +9 -4
- package/client.mjs +2 -1
- package/dist/index.js +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,19 +7,24 @@ SQLite server over HTTP
|
|
|
7
7
|
**POST /query**
|
|
8
8
|
|
|
9
9
|
Run a prepared SQLite statement.
|
|
10
|
-
|
|
10
|
+
Accepts a JSON with these properties:
|
|
11
|
+
|
|
12
|
+
- `s`: string with the statement
|
|
13
|
+
- `d`: data to bind on a statement (optional)
|
|
14
|
+
- `m`: method to execute all, run or get. Run is the default (optional)
|
|
11
15
|
|
|
12
16
|
```js
|
|
13
|
-
// using fetch
|
|
17
|
+
// select all items using fetch
|
|
14
18
|
fetch('https://db.example.com/query', {
|
|
15
19
|
method: 'POST',
|
|
16
20
|
body: JSON.stringify({
|
|
17
21
|
s: 'SELECT * FROM user WHERE id = ?',
|
|
18
|
-
d: [123]
|
|
22
|
+
d: [123],
|
|
23
|
+
m: 'all',
|
|
19
24
|
});
|
|
20
25
|
});
|
|
21
26
|
|
|
22
|
-
// using the server-provided library
|
|
27
|
+
// select using the server-provided library
|
|
23
28
|
import db from 'https://db.example.com/index.mjs';
|
|
24
29
|
|
|
25
30
|
const user = await db.query('SELECT * FROM user WHERE id = ?', [123]);
|
package/client.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
const baseURL = 'https://__API_URL__';
|
|
2
2
|
|
|
3
3
|
export default {
|
|
4
|
-
async query(statement, data) {
|
|
4
|
+
async query(statement, data, method = 'run') {
|
|
5
5
|
const req = await fetch(new URL('/query', baseURL), {
|
|
6
6
|
method: 'POST',
|
|
7
7
|
body: JSON.stringify({
|
|
8
8
|
s: statement,
|
|
9
9
|
d: data || null,
|
|
10
|
+
m: method,
|
|
10
11
|
}),
|
|
11
12
|
});
|
|
12
13
|
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createServer } from 'node:http';
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import SQLite from 'better-sqlite3';
|
|
4
|
+
const methods = ['all', 'run', 'get'];
|
|
4
5
|
export function getDatabase() {
|
|
5
6
|
const db = new SQLite(process.env.SQLITE_DB_PATH || './db.sqlite3');
|
|
6
7
|
db.pragma('journal_mode = WAL');
|
|
@@ -32,9 +33,10 @@ async function onQuery({ db, request, response }) {
|
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
try {
|
|
35
|
-
const { s, d } = JSON.parse(query);
|
|
36
|
+
const { s, d, m = 'run' } = JSON.parse(query);
|
|
36
37
|
const runner = db.prepare(s);
|
|
37
|
-
const
|
|
38
|
+
const method = methods.includes(m) ? m : 'run';
|
|
39
|
+
const result = d ? runner[method](d) : runner[method]();
|
|
38
40
|
response.end(JSON.stringify(result));
|
|
39
41
|
}
|
|
40
42
|
catch (error) {
|