Skip to content

Commit

Permalink
feat: use drizzle orm to connect the database
Browse files Browse the repository at this point in the history
  • Loading branch information
ikxin committed May 7, 2024
1 parent 28796f2 commit 1660d81
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
dist
dev.db*
migrations
sqlite.db
Binary file modified bun.lockb
Binary file not shown.
10 changes: 10 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from "drizzle-kit";

export default {
out: "./service/db/migrations",
schema: "./service/db/schema.ts",
driver: "better-sqlite",
dbCredentials: {
url: "sqlite.db",
},
} satisfies Config;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"service": "bun --watch service/index.ts",
"generate": "prisma generate",
"tsc": "vue-tsc",
"init-db": "prisma init --datasource-provider sqlite",
"init-prisma": "prisma init --datasource-provider sqlite",
"migrate": "prisma migrate dev --name init",
"kill-vlmcsd": "pkill -f vlmcsd"
"kill-vlmcsd": "pkill -f vlmcsd",
"db-studio": "drizzle-kit studio",
"init-db": "drizzle-kit generate:sqlite && bun service/scripts/migrate.ts"
},
"dependencies": {
"@arco-design/web-vue": "^2.55.1",
Expand Down
7 changes: 7 additions & 0 deletions service/db/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";

export const record = sqliteTable("record", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
createdAt: integer("created_at").notNull(),
});
14 changes: 12 additions & 2 deletions service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Elysia } from 'elysia'
import { staticPlugin } from '@elysiajs/static'
import { arch, platform } from 'os'
import { $ } from 'bun'
import { drizzle } from 'drizzle-orm/bun-sqlite'
import { Database } from 'bun:sqlite'
import * as schema from './db/schema'

const sqlite = new Database('sqlite.db')
const db = drizzle(sqlite, { schema })

const app = new Elysia()

Expand All @@ -14,8 +20,14 @@ app.use(

app.get('/*', () => Bun.file('dist/index.html'))

app.get('/api/record', async () => {
return await db.query.record.findMany()
})

app.listen(3000)

console.log(`Elysia is running at on port ${app.server?.url} ...`)

try {
platform() === 'win32'
? await $`taskkill /IM vlmcsd* /F`.nothrow()
Expand All @@ -24,8 +36,6 @@ try {
console.error(err)
}

console.log(`Elysia is running at on port ${app.server?.url} ...`)

const vlmcsd = Bun.spawnSync([
`./service/binaries/vlmcsd-${platform()}-${arch()}`,
])
Expand Down
8 changes: 8 additions & 0 deletions service/scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Database } from 'bun:sqlite'
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";

const sqlite = new Database("sqlite.db");
const db = drizzle(sqlite);

migrate(db, { migrationsFolder: "./service/db/migrations" });

0 comments on commit 1660d81

Please sign in to comment.