-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2734 from cloudflare/glen/do-sqlite-types
Tweaked TS generation of DO SQlite types
- Loading branch information
Showing
2 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DurableObject } from "cloudflare:workers"; | ||
import { expectTypeOf } from "expect-type"; | ||
|
||
// Aliased as SqlStorageValue, but let's assert it includes the raw types we expect | ||
type Value = ArrayBuffer | string | number | null; | ||
|
||
class TestDOSql extends DurableObject { | ||
test() { | ||
const db = this.ctx.storage.sql; | ||
|
||
expectTypeOf<SqlStorage>(db); | ||
|
||
expectTypeOf<number>(db.databaseSize); | ||
|
||
// Verify default row type of exec | ||
for (const row of db.exec("...")) { | ||
expectTypeOf<Record<string, Value>>(row); | ||
} | ||
|
||
// Verify scoped row type of exec | ||
for (const row of db.exec<{ name: string; phone: number }>("...")) { | ||
expectTypeOf<{ name: string; phone: number }>(row); | ||
|
||
// @ts-expect-error double-checking our assertions are strict | ||
expectTypeOf<{ name: string; phone: string }>(row); | ||
// @ts-expect-error double-checking our assertions are strict | ||
expectTypeOf<Record<string, number>>(row); | ||
} | ||
|
||
const cursor = db.exec("...", 1, "two") | ||
expectTypeOf<SqlStorageCursor<Record<string, Value>>>(cursor); | ||
|
||
expectTypeOf<number>(cursor.rowsRead); | ||
expectTypeOf<number>(cursor.rowsWritten); | ||
expectTypeOf<string[]>(cursor.columnNames); | ||
} | ||
} |