-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.vue
72 lines (53 loc) · 2.26 KB
/
app.vue
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
<script lang="ts" setup>
import { sql } from 'kysely';
import Database, { sqlite } from '~/database';
import Migrator from '~/database/BrowserMigrator';
// see: plugins/sqlite.ts for registering jeep-sqlite on the web
// see: database/BrowserMigrator to have a look on implementing migraitons on the web
onMounted(async () => {
await Migrator.migrate();
// query builder
console.log(await Database.insertInto('test').values({ name: 'wecler' }).execute());
console.log(await Database.selectFrom('test').selectAll().execute());
// raw queries
console.log(await sql`INSERT INTO test VALUES (null, 'Dawid') RETURNING *`.execute(Database));
// you can also use transactions
await Database.transaction().execute(async (trx) => {
await trx.insertInto('test').values({ name: 'Sami' }).returning('id')
.executeTakeFirstOrThrow();
return await trx.insertInto('test').values({ name: 'commit' }).returningAll().execute();
});
console.log(await Database.selectFrom('test').selectAll().execute());
try {
// and rollback
await Database.transaction().execute(async (trx) => {
await trx.insertInto('test').values({ name: 'Rollback' }).execute();
await trx.insertInto('test').values({ name: 'Rollback' }).execute();
throw 1;
})
} catch (e) {
//
}
console.log(await Database.selectFrom('test').selectAll().execute());
// this also works but...
// if the migrator is user you have to ensure that migrator runs before all the queries
// will be executed therefore it's probably better to use sql from kysely
const db = await sqlite.retrieveConnection('nuxt', false);
await db.beginTransaction();
await db.query("INSERT INTO `test` VALUES (null, ?)", ['Sami']);
await db.query("INSERT INTO `test` VALUES (null, ?)", ['commit']);
await db.commitTransaction();
console.log(await db.query('SELECT * FROM test'));
await db.beginTransaction();
await db.query("INSERT INTO `test` VALUES (null, ?)", ['Rollback']);
await db.query("INSERT INTO `test` VALUES (null, ?)", ['Rollback']);;
await db.rollbackTransaction();
await db.execute("INSERT INTO `test` VALUES (null, 'Jean')");
console.log(await db.query('SELECT `id`, `name` FROM test'));
});
</script>
<template>
<div>
<NuxtWelcome />
</div>
</template>