stitchdb 1.0.0 → 1.2.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/dist/index.d.mts +18 -2
- package/dist/index.d.ts +18 -2
- package/dist/index.js +23 -7
- package/dist/index.mjs +23 -7
- package/package.json +9 -3
package/dist/index.d.mts
CHANGED
|
@@ -30,15 +30,26 @@ interface ExecResult {
|
|
|
30
30
|
}
|
|
31
31
|
declare class StitchDBError extends Error {
|
|
32
32
|
status: number;
|
|
33
|
-
constructor(message: string, status
|
|
33
|
+
constructor(message: string, status?: number);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* StitchDB client.
|
|
37
|
+
*
|
|
38
|
+
* Uses HTTP/2 with persistent connections (via fetch keep-alive).
|
|
39
|
+
* All requests share one TCP connection — no repeated TLS handshakes.
|
|
40
|
+
* Batch API sends multiple queries in one request.
|
|
41
|
+
*/
|
|
35
42
|
declare class StitchDB {
|
|
36
43
|
private url;
|
|
37
44
|
private apiKey;
|
|
45
|
+
private headers;
|
|
38
46
|
constructor(config: StitchDBConfig);
|
|
39
47
|
/** Run a SQL query with parameterized bindings. */
|
|
40
48
|
query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<QueryResult<T>>;
|
|
41
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Run multiple queries in a single HTTP request.
|
|
51
|
+
* All queries execute atomically. One Worker invocation for N queries.
|
|
52
|
+
*/
|
|
42
53
|
batch(queries: {
|
|
43
54
|
sql: string;
|
|
44
55
|
params?: unknown[];
|
|
@@ -47,6 +58,11 @@ declare class StitchDB {
|
|
|
47
58
|
run(sql: string): Promise<ExecResult>;
|
|
48
59
|
/** Insert a row. */
|
|
49
60
|
insert(table: string, data: Record<string, unknown>): Promise<QueryResult>;
|
|
61
|
+
/**
|
|
62
|
+
* Insert multiple rows in a single request using batch API.
|
|
63
|
+
* One Worker invocation regardless of how many rows.
|
|
64
|
+
*/
|
|
65
|
+
insertMany(table: string, rows: Record<string, unknown>[]): Promise<BatchResult>;
|
|
50
66
|
/** Update rows matching a WHERE clause. */
|
|
51
67
|
update(table: string, data: Record<string, unknown>, where: string, whereParams?: unknown[]): Promise<QueryResult>;
|
|
52
68
|
/** Delete rows matching a WHERE clause. */
|
package/dist/index.d.ts
CHANGED
|
@@ -30,15 +30,26 @@ interface ExecResult {
|
|
|
30
30
|
}
|
|
31
31
|
declare class StitchDBError extends Error {
|
|
32
32
|
status: number;
|
|
33
|
-
constructor(message: string, status
|
|
33
|
+
constructor(message: string, status?: number);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* StitchDB client.
|
|
37
|
+
*
|
|
38
|
+
* Uses HTTP/2 with persistent connections (via fetch keep-alive).
|
|
39
|
+
* All requests share one TCP connection — no repeated TLS handshakes.
|
|
40
|
+
* Batch API sends multiple queries in one request.
|
|
41
|
+
*/
|
|
35
42
|
declare class StitchDB {
|
|
36
43
|
private url;
|
|
37
44
|
private apiKey;
|
|
45
|
+
private headers;
|
|
38
46
|
constructor(config: StitchDBConfig);
|
|
39
47
|
/** Run a SQL query with parameterized bindings. */
|
|
40
48
|
query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<QueryResult<T>>;
|
|
41
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Run multiple queries in a single HTTP request.
|
|
51
|
+
* All queries execute atomically. One Worker invocation for N queries.
|
|
52
|
+
*/
|
|
42
53
|
batch(queries: {
|
|
43
54
|
sql: string;
|
|
44
55
|
params?: unknown[];
|
|
@@ -47,6 +58,11 @@ declare class StitchDB {
|
|
|
47
58
|
run(sql: string): Promise<ExecResult>;
|
|
48
59
|
/** Insert a row. */
|
|
49
60
|
insert(table: string, data: Record<string, unknown>): Promise<QueryResult>;
|
|
61
|
+
/**
|
|
62
|
+
* Insert multiple rows in a single request using batch API.
|
|
63
|
+
* One Worker invocation regardless of how many rows.
|
|
64
|
+
*/
|
|
65
|
+
insertMany(table: string, rows: Record<string, unknown>[]): Promise<BatchResult>;
|
|
50
66
|
/** Update rows matching a WHERE clause. */
|
|
51
67
|
update(table: string, data: Record<string, unknown>, where: string, whereParams?: unknown[]): Promise<QueryResult>;
|
|
52
68
|
/** Delete rows matching a WHERE clause. */
|
package/dist/index.js
CHANGED
|
@@ -27,7 +27,7 @@ __export(index_exports, {
|
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(index_exports);
|
|
29
29
|
var StitchDBError = class extends Error {
|
|
30
|
-
constructor(message, status) {
|
|
30
|
+
constructor(message, status = 0) {
|
|
31
31
|
super(message);
|
|
32
32
|
this.name = "StitchDBError";
|
|
33
33
|
this.status = status;
|
|
@@ -37,12 +37,19 @@ var StitchDB = class {
|
|
|
37
37
|
constructor(config) {
|
|
38
38
|
this.url = (config.url || "https://db.stitchdb.com").replace(/\/$/, "");
|
|
39
39
|
this.apiKey = config.apiKey;
|
|
40
|
+
this.headers = {
|
|
41
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
42
|
+
"Content-Type": "application/json"
|
|
43
|
+
};
|
|
40
44
|
}
|
|
41
45
|
/** Run a SQL query with parameterized bindings. */
|
|
42
46
|
async query(sql, params) {
|
|
43
47
|
return this.post("/v1/query", { sql, params });
|
|
44
48
|
}
|
|
45
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Run multiple queries in a single HTTP request.
|
|
51
|
+
* All queries execute atomically. One Worker invocation for N queries.
|
|
52
|
+
*/
|
|
46
53
|
async batch(queries) {
|
|
47
54
|
return this.post("/v1/batch", { queries });
|
|
48
55
|
}
|
|
@@ -56,6 +63,16 @@ var StitchDB = class {
|
|
|
56
63
|
const sql = `INSERT INTO "${table}" (${cols.map((c) => `"${c}"`).join(", ")}) VALUES (${cols.map(() => "?").join(", ")})`;
|
|
57
64
|
return this.query(sql, Object.values(data));
|
|
58
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Insert multiple rows in a single request using batch API.
|
|
68
|
+
* One Worker invocation regardless of how many rows.
|
|
69
|
+
*/
|
|
70
|
+
async insertMany(table, rows) {
|
|
71
|
+
if (rows.length === 0) return { results: [], meta: { rows_read: 0, rows_written: 0, duration_ms: 0, queries_count: 0 } };
|
|
72
|
+
const cols = Object.keys(rows[0]);
|
|
73
|
+
const sql = `INSERT INTO "${table}" (${cols.map((c) => `"${c}"`).join(", ")}) VALUES (${cols.map(() => "?").join(", ")})`;
|
|
74
|
+
return this.batch(rows.map((row) => ({ sql, params: cols.map((c) => row[c]) })));
|
|
75
|
+
}
|
|
59
76
|
/** Update rows matching a WHERE clause. */
|
|
60
77
|
async update(table, data, where, whereParams) {
|
|
61
78
|
const set = Object.keys(data).map((c) => `"${c}" = ?`).join(", ");
|
|
@@ -83,11 +100,10 @@ var StitchDB = class {
|
|
|
83
100
|
async post(path, body) {
|
|
84
101
|
const res = await fetch(`${this.url}${path}`, {
|
|
85
102
|
method: "POST",
|
|
86
|
-
headers:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
body: JSON.stringify(body)
|
|
103
|
+
headers: this.headers,
|
|
104
|
+
body: JSON.stringify(body),
|
|
105
|
+
// @ts-ignore — keepalive hint for HTTP/2 connection reuse
|
|
106
|
+
keepalive: true
|
|
91
107
|
});
|
|
92
108
|
const data = await res.json();
|
|
93
109
|
if (!res.ok || data.error) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
var StitchDBError = class extends Error {
|
|
3
|
-
constructor(message, status) {
|
|
3
|
+
constructor(message, status = 0) {
|
|
4
4
|
super(message);
|
|
5
5
|
this.name = "StitchDBError";
|
|
6
6
|
this.status = status;
|
|
@@ -10,12 +10,19 @@ var StitchDB = class {
|
|
|
10
10
|
constructor(config) {
|
|
11
11
|
this.url = (config.url || "https://db.stitchdb.com").replace(/\/$/, "");
|
|
12
12
|
this.apiKey = config.apiKey;
|
|
13
|
+
this.headers = {
|
|
14
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
15
|
+
"Content-Type": "application/json"
|
|
16
|
+
};
|
|
13
17
|
}
|
|
14
18
|
/** Run a SQL query with parameterized bindings. */
|
|
15
19
|
async query(sql, params) {
|
|
16
20
|
return this.post("/v1/query", { sql, params });
|
|
17
21
|
}
|
|
18
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Run multiple queries in a single HTTP request.
|
|
24
|
+
* All queries execute atomically. One Worker invocation for N queries.
|
|
25
|
+
*/
|
|
19
26
|
async batch(queries) {
|
|
20
27
|
return this.post("/v1/batch", { queries });
|
|
21
28
|
}
|
|
@@ -29,6 +36,16 @@ var StitchDB = class {
|
|
|
29
36
|
const sql = `INSERT INTO "${table}" (${cols.map((c) => `"${c}"`).join(", ")}) VALUES (${cols.map(() => "?").join(", ")})`;
|
|
30
37
|
return this.query(sql, Object.values(data));
|
|
31
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Insert multiple rows in a single request using batch API.
|
|
41
|
+
* One Worker invocation regardless of how many rows.
|
|
42
|
+
*/
|
|
43
|
+
async insertMany(table, rows) {
|
|
44
|
+
if (rows.length === 0) return { results: [], meta: { rows_read: 0, rows_written: 0, duration_ms: 0, queries_count: 0 } };
|
|
45
|
+
const cols = Object.keys(rows[0]);
|
|
46
|
+
const sql = `INSERT INTO "${table}" (${cols.map((c) => `"${c}"`).join(", ")}) VALUES (${cols.map(() => "?").join(", ")})`;
|
|
47
|
+
return this.batch(rows.map((row) => ({ sql, params: cols.map((c) => row[c]) })));
|
|
48
|
+
}
|
|
32
49
|
/** Update rows matching a WHERE clause. */
|
|
33
50
|
async update(table, data, where, whereParams) {
|
|
34
51
|
const set = Object.keys(data).map((c) => `"${c}" = ?`).join(", ");
|
|
@@ -56,11 +73,10 @@ var StitchDB = class {
|
|
|
56
73
|
async post(path, body) {
|
|
57
74
|
const res = await fetch(`${this.url}${path}`, {
|
|
58
75
|
method: "POST",
|
|
59
|
-
headers:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
body: JSON.stringify(body)
|
|
76
|
+
headers: this.headers,
|
|
77
|
+
body: JSON.stringify(body),
|
|
78
|
+
// @ts-ignore — keepalive hint for HTTP/2 connection reuse
|
|
79
|
+
keepalive: true
|
|
64
80
|
});
|
|
65
81
|
const data = await res.json();
|
|
66
82
|
if (!res.ok || data.error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stitchdb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "StitchDB client for JavaScript and TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,7 +19,13 @@
|
|
|
19
19
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
20
20
|
"prepublishOnly": "npm run build"
|
|
21
21
|
},
|
|
22
|
-
"keywords": [
|
|
22
|
+
"keywords": [
|
|
23
|
+
"stitchdb",
|
|
24
|
+
"database",
|
|
25
|
+
"sql",
|
|
26
|
+
"edge",
|
|
27
|
+
"serverless"
|
|
28
|
+
],
|
|
23
29
|
"license": "MIT",
|
|
24
30
|
"author": {
|
|
25
31
|
"name": "StitchDB",
|
|
@@ -35,4 +41,4 @@
|
|
|
35
41
|
"tsup": "^8.0.0",
|
|
36
42
|
"typescript": "^5.0.0"
|
|
37
43
|
}
|
|
38
|
-
}
|
|
44
|
+
}
|