Skip to content
File

Blob: types/defines/d1.d.ts

typescript109 lines
1interface D1Meta {
2 duration: number;
3 size_after: number;
4 rows_read: number;
5 rows_written: number;
6 last_row_id: number;
7 changed_db: boolean;
8 changes: number;
9 
10 /**
11 * The region of the database instance that executed the query.
12 */
13 served_by_region?: string;
14 
15 /**
16 * The three letters airport code of the colo that executed the query.
17 */
18 served_by_colo?: string;
19 
20 /**
21 * True if-and-only-if the database instance that executed the query was the primary.
22 */
23 served_by_primary?: boolean;
24 
25 timings?: {
26 /**
27 * The duration of the SQL query execution by the database instance. It doesn't include any network time.
28 */
29 sql_duration_ms: number;
30 };
31 
32 /**
33 * Number of total attempts to execute the query, due to automatic retries.
34 * Note: All other fields in the response like `timings` only apply to the last attempt.
35 */
36 total_attempts?: number;
37}
38 
39interface D1Response {
40 success: true;
41 meta: D1Meta & Record<string, unknown>;
42 error?: never;
43}
44 
45type D1Result<T = unknown> = D1Response & {
46 results: T[];
47};
48 
49interface D1ExecResult {
50 count: number;
51 duration: number;
52}
53 
54type D1SessionConstraint =
55 // Indicates that the first query should go to the primary, and the rest queries
56 // using the same D1DatabaseSession will go to any replica that is consistent with
57 // the bookmark maintained by the session (returned by the first query).
58 | 'first-primary'
59 // Indicates that the first query can go anywhere (primary or replica), and the rest queries
60 // using the same D1DatabaseSession will go to any replica that is consistent with
61 // the bookmark maintained by the session (returned by the first query).
62 | 'first-unconstrained';
63type D1SessionBookmark = string;
64 
65declare abstract class D1Database {
66 prepare(query: string): D1PreparedStatement;
67 batch<T = unknown>(statements: D1PreparedStatement[]): Promise<D1Result<T>[]>;
68 exec(query: string): Promise<D1ExecResult>;
69 
70 /**
71 * Creates a new D1 Session anchored at the given constraint or the bookmark.
72 * All queries executed using the created session will have sequential consistency,
73 * meaning that all writes done through the session will be visible in subsequent reads.
74 *
75 * @param constraintOrBookmark Either the session constraint or the explicit bookmark to anchor the created session.
76 */
77 withSession(
78 constraintOrBookmark?: D1SessionBookmark | D1SessionConstraint
79 ): D1DatabaseSession;
80 
81 /**
82 * @deprecated dump() will be removed soon, only applies to deprecated alpha v1 databases.
83 */
84 dump(): Promise<ArrayBuffer>;
85}
86 
87declare abstract class D1DatabaseSession {
88 prepare(query: string): D1PreparedStatement;
89 batch<T = unknown>(statements: D1PreparedStatement[]): Promise<D1Result<T>[]>;
90 
91 /**
92 * @returns The latest session bookmark across all executed queries on the session.
93 * If no query has been executed yet, `null` is returned.
94 */
95 getBookmark(): D1SessionBookmark | null;
96}
97 
98declare abstract class D1PreparedStatement {
99 bind(...values: unknown[]): D1PreparedStatement;
100 first<T = unknown>(colName: string): Promise<T | null>;
101 first<T = Record<string, unknown>>(): Promise<T | null>;
102 run<T = Record<string, unknown>>(): Promise<D1Result<T>>;
103 all<T = Record<string, unknown>>(): Promise<D1Result<T>>;
104 raw<T = unknown[]>(options: {
105 columnNames: true;
106 }): Promise<[string[], ...T[]]>;
107 raw<T = unknown[]>(options?: { columnNames?: false }): Promise<T[]>;
108}