@cloud-cli/d0 1.0.2 → 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 CHANGED
@@ -7,18 +7,24 @@ SQLite server over HTTP
7
7
  **POST /query**
8
8
 
9
9
  Run a prepared SQLite statement.
10
- Send a JSON with two properties: `s` for statement and `d` for data (optional)
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', {
19
+ method: 'POST',
15
20
  body: JSON.stringify({
16
21
  s: 'SELECT * FROM user WHERE id = ?',
17
- d: [123]
22
+ d: [123],
23
+ m: 'all',
18
24
  });
19
25
  });
20
26
 
21
- // using the server-provided library
27
+ // select using the server-provided library
22
28
  import db from 'https://db.example.com/index.mjs';
23
29
 
24
30
  const user = await db.query('SELECT * FROM user WHERE id = ?', [123]);
package/client.mjs CHANGED
@@ -1,11 +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
+ method: 'POST',
6
7
  body: JSON.stringify({
7
8
  s: statement,
8
9
  d: data || null,
10
+ m: method,
9
11
  }),
10
12
  });
11
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 result = d ? runner.run(d) : runner.run();
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/d0",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",