-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(desktop): implement SQLite database integration with Drizzle ORM
- Loading branch information
Showing
14 changed files
with
890 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.db | ||
drizzle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'drizzle-kit' | ||
|
||
export default defineConfig({ | ||
dialect: 'sqlite', | ||
schema: './src/database/schemas/*', | ||
out: './drizzle', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import Database from 'better-sqlite3' | ||
import { drizzle } from 'drizzle-orm/better-sqlite3' | ||
import { migrate } from 'drizzle-orm/better-sqlite3/migrator' | ||
import { app } from 'electron' | ||
import { posts } from './schemas/posts' | ||
|
||
const dbPath = import.meta.env.DEV ? 'sqlite.db' : path.join(app.getPath('userData'), 'data.db') | ||
|
||
fs.mkdirSync(path.dirname(dbPath), { recursive: true }) | ||
|
||
const sqlite = new Database( | ||
dbPath, | ||
) | ||
|
||
export const db = drizzle(sqlite, { schema: { posts } }) | ||
|
||
function toDrizzleResult(row: Record<string, any>) | ||
function toDrizzleResult(rows: Record<string, any> | Array<Record<string, any>>) { | ||
if (!rows) { | ||
return [] | ||
} | ||
if (Array.isArray(rows)) { | ||
return rows.map((row) => { | ||
return Object.keys(row).map(key => row[key]) | ||
}) | ||
} else { | ||
return Object.keys(rows).map(key => rows[key]) | ||
} | ||
} | ||
|
||
export async function execute(_e, sqlstr, params, method) { | ||
const result = sqlite.prepare(sqlstr) | ||
const ret = result[method](...params) | ||
return toDrizzleResult(ret) | ||
} | ||
|
||
export async function runMigrate() { | ||
migrate(db, { | ||
migrationsFolder: path.join(__dirname, '../../drizzle'), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { drizzle } from 'drizzle-orm/sqlite-proxy' | ||
import { posts } from './schemas/posts' | ||
|
||
export const database = drizzle(async (...args) => { | ||
try { | ||
const result = await window.api.execute(...args) | ||
return { rows: result } | ||
} catch (e: any) { | ||
console.error('Error from sqlite proxy server: ', e.response.data) | ||
return { rows: [] } | ||
} | ||
}, { | ||
schema: { posts }, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core' | ||
|
||
export const posts = sqliteTable('posts', { | ||
id: int('id').primaryKey({ autoIncrement: true }), | ||
title: text('title').notNull().default(''), | ||
}) | ||
|
||
export type SelectPosts = typeof posts.$inferSelect | ||
export type InsertPosts = typeof posts.$inferInsert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
<script setup lang="ts"> | ||
import type { SelectPosts } from '../../../database/schemas/posts' | ||
import { database } from '../../../database/db.renderer' | ||
import { posts } from '../../../database/schemas/posts' | ||
const postList = ref<SelectPosts[]>([]) | ||
async function insertMockData() { | ||
await database.delete(posts).execute() | ||
await database.insert(posts).values([ | ||
{ | ||
title: 'Hello World', | ||
}, | ||
{ | ||
title: 'Hello World 2', | ||
}, | ||
]) | ||
} | ||
insertMockData().then(() => { | ||
console.log('Mock data inserted') | ||
database.query.posts.findMany().then((result) => { | ||
console.log(result) | ||
postList.value = result | ||
}) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<!-- <SettingsView /> --> | ||
TODO: Implement desktop database settings | ||
<pre> | ||
{{ postList }} | ||
</pre> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.