-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
47 lines (40 loc) · 1.17 KB
/
main.ts
File metadata and controls
47 lines (40 loc) · 1.17 KB
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
40
41
42
43
44
45
46
47
import { ZenStackClient } from '@zenstackhq/orm';
import { SqlJsDialect } from '@zenstackhq/orm/dialects/sql.js';
import initSqlJs from 'sql.js';
import { schema } from './zenstack/schema';
async function main() {
// initialize sql.js engine
const SQL = await initSqlJs();
// create database client with sql.js dialect
const db = new ZenStackClient(schema, {
dialect: new SqlJsDialect({ sqlJs: new SQL.Database() }),
});
// push schema to the database (`$pushSchema` is for testing only)
await db.$pushSchema();
// create a user with some posts
await db.user.create({
data: {
id: 1,
email: 'u1@test.com',
posts: {
create: [{ title: 'Post1' }, { title: 'Post2' }]
}
}
});
// high-level query API
const userWithPosts = await db.user.findFirst({
where: { id: 1 },
include: { posts: true }
});
console.log(userWithPosts);
// low-level SQL query builder API
const userPostJoin = await db
.$qb
.selectFrom('User')
.innerJoin('Post', 'Post.authorId', 'User.id')
.select(['User.id', 'User.email', 'Post.title'])
.where('User.id', '=', 1)
.execute();
console.log(userPostJoin);
}
main();