Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit a98e0f5

Browse files
committed
De-static-ify D1 Statement class, and update README.md
Due to a limitation of `esbuild`, private static members are lowered: https://github.com/evanw/esbuild/blob/35c263fc4c3d35d7304713ee7a06dda9700da919/internal/js_parser/js_parser_lower.go#L1729-L1755
1 parent 6bace6b commit a98e0f5

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

packages/d1/README.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,14 @@ fun, full-featured, fully-local simulator for Cloudflare Workers. See
88

99
```js
1010
import { BetaDatabase } from "@miniflare/d1";
11-
import { MemoryStorage } from "@miniflare/storage-memory";
12-
const db = new BetaDatabase(new MemoryStorage());
11+
import { createSQLiteDB } from "@miniflare/shared";
12+
const db = new BetaDatabase(createSQLiteDB(":memory:"));
1313

14-
// BetaDatabase only supports .fetch(), once D1 is out of beta the full API will be available here:
15-
await db.fetch("/execute", {
16-
method: "POST",
17-
body: JSON.stringify({
18-
sql: `CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL);`,
19-
}),
20-
});
21-
const response = await db.fetch("/query", {
22-
method: "POST",
23-
body: JSON.stringify({
24-
sql: `SELECT * FROM sqlite_schema`,
25-
}),
26-
});
27-
console.log(await response.json());
14+
await db.exec(
15+
`CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL);`
16+
);
17+
const response = await db.prepare(`SELECT * FROM sqlite_schema`).all();
18+
console.log(await response);
2819
/*
2920
{
3021
"success": true,

packages/d1/src/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { SqliteDB } from "@miniflare/shared";
33
import { Statement } from "./statement";
44

55
export class BetaDatabase {
6-
#db: SqliteDB;
6+
readonly #db: SqliteDB;
77

88
constructor(db: SqliteDB) {
99
this.#db = db;

packages/d1/src/statement.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Statement {
3535
return new Statement(this.#db, this.#query, params);
3636
}
3737

38-
private prepareAndBind() {
38+
#prepareAndBind() {
3939
const prepared = this.#db.prepare(this.#query);
4040
if (this.#bindings === undefined) return prepared;
4141
try {
@@ -55,9 +55,9 @@ export class Statement {
5555

5656
async all() {
5757
const start = performance.now();
58-
const statementWithBindings = this.prepareAndBind();
58+
const statementWithBindings = this.#prepareAndBind();
5959
try {
60-
const results = Statement.#all(statementWithBindings);
60+
const results = this.#all(statementWithBindings);
6161
return {
6262
results,
6363
duration: performance.now() - start,
@@ -71,7 +71,7 @@ export class Statement {
7171
}
7272
}
7373

74-
static #all(statementWithBindings: SqliteStatement) {
74+
#all(statementWithBindings: SqliteStatement) {
7575
try {
7676
return statementWithBindings.all();
7777
} catch (e: unknown) {
@@ -82,33 +82,31 @@ export class Statement {
8282
(e as Error).message
8383
)
8484
) {
85-
return Statement.#run(statementWithBindings);
85+
return this.#run(statementWithBindings);
8686
}
8787
throw e;
8888
}
8989
}
9090

9191
async first(col?: string) {
92-
const statementWithBindings = this.prepareAndBind();
92+
const statementWithBindings = this.#prepareAndBind();
9393
try {
94-
const data = Statement.#first(statementWithBindings);
94+
const data = this.#first(statementWithBindings);
9595
return typeof col === "string" ? data[col] : data;
9696
} catch (e) {
9797
throw errorWithCause("D1_FIRST_ERROR", e);
9898
}
9999
}
100100

101-
static #first(statementWithBindings: SqliteStatement) {
101+
#first(statementWithBindings: SqliteStatement) {
102102
return statementWithBindings.get();
103103
}
104104

105105
async run() {
106106
const start = performance.now();
107-
const statementWithBindings = this.prepareAndBind();
107+
const statementWithBindings = this.#prepareAndBind();
108108
try {
109-
const { changes, lastInsertRowid } = Statement.#run(
110-
statementWithBindings
111-
);
109+
const { changes, lastInsertRowid } = this.#run(statementWithBindings);
112110
return {
113111
results: null,
114112
duration: performance.now() - start,
@@ -122,16 +120,16 @@ export class Statement {
122120
}
123121
}
124122

125-
static #run(statementWithBindings: SqliteStatement) {
123+
#run(statementWithBindings: SqliteStatement) {
126124
return statementWithBindings.run();
127125
}
128126

129127
async raw() {
130-
const statementWithBindings = this.prepareAndBind();
131-
return Statement.#raw(statementWithBindings);
128+
const statementWithBindings = this.#prepareAndBind();
129+
return this.#raw(statementWithBindings);
132130
}
133131

134-
static #raw(statementWithBindings: SqliteStatement) {
132+
#raw(statementWithBindings: SqliteStatement) {
135133
return statementWithBindings.raw() as any;
136134
}
137135
}

0 commit comments

Comments
 (0)