Skip to content
File

Blob: types/test/types/d1.ts

typescript133 lines
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5function expectType<T>(_value: T) {}
6 
7type Row = { col1: string; col2: string };
8 
9export const handler: ExportedHandler<{ DB: D1Database }> = {
10 async fetch(request, env) {
11 const stmt = env.DB.prepare(`SELECT * FROM tbl WHERE id = ?`);
12 
13 // RUN
14 {
15 const response = await stmt.bind(1).run();
16 expectType<{
17 meta: Record<string, unknown>;
18 success: boolean;
19 results?: any;
20 }>(response);
21 }
22 
23 // ALL
24 {
25 const { results } = await stmt.bind(1).all();
26 expectType<Record<string, unknown>[]>(results);
27 }
28 {
29 const { results } = await stmt.bind(1).all<Row>();
30 expectType<Row[]>(results);
31 }
32 
33 // RAW
34 {
35 const results = await stmt.bind(1).raw();
36 expectType<unknown[][]>(results);
37 }
38 {
39 const results = await stmt.bind(1).raw<[string, number, boolean]>();
40 expectType<[string, number, boolean][]>(results);
41 }
42 {
43 const results = await stmt.bind(1).raw<[string, number, boolean]>({});
44 expectType<[string, number, boolean][]>(results);
45 }
46 {
47 const results = await stmt
48 .bind(1)
49 .raw<[string, number, boolean]>({ columnNames: false });
50 expectType<[string, number, boolean][]>(results);
51 }
52 {
53 const results = await stmt.bind(1).raw({ columnNames: true });
54 expectType<[string[], ...unknown[]]>(results);
55 }
56 {
57 const [columns, ...rows] = await stmt
58 .bind(1)
59 .raw<[string, number, boolean]>({ columnNames: true });
60 expectType<string[]>(columns);
61 expectType<[string, number, boolean][]>(rows);
62 }
63 
64 // FIRST
65 {
66 const result = await stmt.bind(1).first();
67 expectType<Record<string, unknown> | null>(result);
68 }
69 {
70 const result = await stmt.bind(1).first<Row>();
71 expectType<Row | null>(result);
72 }
73 
74 // FIRST (col)
75 {
76 const result = await stmt.bind(1).first("col1");
77 expectType<unknown | null>(result);
78 }
79 {
80 const result = await stmt.bind(1).first<string>("col1");
81 expectType<string | null>(result);
82 }
83 
84 // BATCH
85 {
86 const results = await env.DB.batch([stmt.bind(1), stmt.bind(2)]);
87 expectType<D1Result[]>(results);
88 expectType<unknown[]>(results[0].results);
89 }
90 {
91 const results = await env.DB.batch<Row>([stmt.bind(1), stmt.bind(2)]);
92 expectType<D1Result<Row>[]>(results);
93 expectType<Row[]>(results[0].results);
94 }
95 
96 // EXEC
97 {
98 const response = await env.DB.exec(`
99 select 1;
100 select * from tbl;
101 `);
102 expectType<{ count: number; duration: number }>(response);
103 }
104 
105 // WITHSESSION
106 {
107 expectType<D1DatabaseSession>(env.DB.withSession());
108 const session = env.DB.withSession("first-primary");
109 expectType<D1DatabaseSession>(session);
110 
111 const bookmark = session.getBookmark();
112 expectType<D1SessionBookmark | null>(bookmark);
113 
114 // SESSION PREPARE
115 const stmt = session.prepare(`SELECT * FROM tbl WHERE id = ?`);
116 
117 // SESSION BATCH
118 {
119 const results = await env.DB.batch([stmt.bind(1), stmt.bind(2)]);
120 expectType<D1Result[]>(results);
121 expectType<unknown[]>(results[0].results);
122 }
123 {
124 const results = await env.DB.batch<Row>([stmt.bind(1), stmt.bind(2)]);
125 expectType<D1Result<Row>[]>(results);
126 expectType<Row[]>(results[0].results);
127 }
128 }
129 
130 return new Response();
131 },
132};