-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
75 lines (57 loc) · 1.64 KB
/
index.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import fastify from 'fastify'
import Database from 'better-sqlite3'
import { fastifyKysely } from 'fastify-kysely'
import { Generated, Kysely, SqliteDialect } from 'kysely'
interface Database {
person: PersonTable
}
interface PersonTable {
id: Generated<number>
first_name: string
last_name: string
}
declare module 'fastify' {
interface FastifyKyselyNamespaces {
sqliteDB: Kysely<Database>
}
}
const listen = async (): Promise<void> => {
const sqliteDialect = new SqliteDialect({
database: new Database(':memory:')
})
const kyselyInstance = new Kysely<Database>({dialect: sqliteDialect});
const server = fastify()
await server.register(fastifyKysely, {
namespace: 'sqliteDB',
kysely: kyselyInstance
})
await server.kysely.sqliteDB.schema.createTable('person')
.addColumn('id', 'integer', (col) => col.primaryKey())
.addColumn('first_name', 'varchar')
.addColumn('last_name', 'varchar')
.execute();
await server.kysely.sqliteDB.insertInto('person')
.values([
{
first_name: 'Max',
last_name: 'Jack',
},
{
first_name: 'Greg',
last_name: 'Johnson',
},
])
.execute();
server.get('/', async (request, reply) => {
const result = await request.server.kysely.sqliteDB.selectFrom('person').selectAll().execute()
return result
})
server.listen({ port: 5000 }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening at ${address}`)
})
}
listen().then()