Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaked TS generation of DO SQlite types #2734

Merged
merged 1 commit into from
Sep 19, 2024
Merged

Tweaked TS generation of DO SQlite types #2734

merged 1 commit into from
Sep 19, 2024

Conversation

geelen
Copy link
Collaborator

@geelen geelen commented Sep 18, 2024

After #2722, we now have a final, very minimal SQLite API for durable objects. So I've just tweaked the TS generation to give a few generics to make things easier to work with. These are the changes:

diff --git a/.local/types-main/2023-07-01/index.ts b/bazel-bin/types/definitions/2023-07-01/index.ts
index 45460a4d..556a55d3 100755
--- a/.local/types-main/2023-07-01/index.ts
+++ b/bazel-bin/types/definitions/2023-07-01/index.ts
@@ -2778,20 +2778,24 @@ export declare const WebSocketPair: {
   };
 };
 export interface SqlStorage {
-  exec(query: string, ...bindings: any[]): SqlStorageCursor;
+  exec<T extends Record<string, SqlStorageValue>>(
+    query: string,
+    ...bindings: any[]
+  ): SqlStorageCursor<T>;
   get databaseSize(): number;
   Cursor: typeof SqlStorageCursor;
   Statement: typeof SqlStorageStatement;
 }
 export declare abstract class SqlStorageStatement {}
-export declare abstract class SqlStorageCursor {
-  raw(): IterableIterator<((ArrayBuffer | string | number) | null)[]>;
+export type SqlStorageValue = ArrayBuffer | string | number | null;
+export declare abstract class SqlStorageCursor<
+  T extends Record<string, SqlStorageValue>,
+> {
+  raw<U extends SqlStorageValue[]>(): IterableIterator<U>;
   get columnNames(): string[];
   get rowsRead(): number;
   get rowsWritten(): number;
-  [Symbol.iterator](): IterableIterator<
-    Record<string, (ArrayBuffer | string | number) | null>
-  >;
+  [Symbol.iterator](): IterableIterator<T>;
 }
 export interface Socket {
   get readable(): ReadableStream;

And the relevant diff between 2023-07-01 and experimental:

 export declare abstract class ScheduledEvent extends ExtendableEvent {
   readonly scheduledTime: number;
   readonly cron: string;
@@ -2782,6 +2836,8 @@ export interface SqlStorage {
     query: string,
     ...bindings: any[]
   ): SqlStorageCursor<T>;
+  prepare(query: string): SqlStorageStatement;
+  ingest(query: string): SqlStorageIngestResult;
   get databaseSize(): number;
   Cursor: typeof SqlStorageCursor;
   Statement: typeof SqlStorageStatement;
@@ -2797,6 +2853,12 @@ export declare abstract class SqlStorageCursor<
   get rowsWritten(): number;
   [Symbol.iterator](): IterableIterator<T>;
 }
+export interface SqlStorageIngestResult {
+  remainder: string;
+  rowsRead: number;
+  rowsWritten: number;
+  statementCount: number;
+}
 export interface Socket {
   get readable(): ReadableStream;
   get writable(): WritableStream;

@Frederik-Baetens
Copy link
Contributor

Frederik-Baetens commented Sep 18, 2024

We don't have a way of conditionally adding them in, do we?

We do, wrangler types --x-include-runtime generates types based on your compat flags and bindings. I'd expect that to work out of the box if you're just using an established experimental flag, and if it doesn't, we can definitely extend that mechanism to conditionally generate types.

We should also build on that mechanism to disable the sql specific types for non-sqlite DOs.

src/workerd/api/sql.h Outdated Show resolved Hide resolved
@kentonv
Copy link
Member

kentonv commented Sep 18, 2024

LGTM but CI seems unhappy...

@geelen geelen merged commit 63650fe into main Sep 19, 2024
12 of 13 checks passed
@geelen geelen deleted the glen/do-sqlite-types branch September 19, 2024 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants