-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
39 lines (29 loc) · 995 Bytes
/
example.ts
File metadata and controls
39 lines (29 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import assert from "node:assert/strict";
import { prismaPostgres } from "./src/index.ts";
const { sql, query } = prismaPostgres({
connectionString: process.env.PRISMA_DIRECT_TCP_URL!
});
await sql.exec`create table if not exists users (
id uuid primary key default gen_random_uuid(),
email text not null unique,
name text not null
)`;
await sql.exec`insert into users (email, name) values ('test@example.com', 'Test User') on conflict do nothing`;
type User = {
id: string;
email: string;
name: string;
};
// async iterator mode
const usersIterator = sql<User>`select * from users`;
for await (const user of usersIterator) {
console.log(user.id, user.name, user.email);
}
// direct collection mode
const [user1] =
await sql<User>`select * from users where email = ${"test@example.com"}`.collect();
// explicit parameters
const [user2] = await query<User>("select * from users where email = $1", [
"test@example.com",
]).collect();
assert.equal(user1.id, user2.id);