An open API service indexing awesome lists of open source software.

https://github.com/renqiankun/electron-vite-template

electron vite vue3 bettter-sqlite3 drizzle-orm
https://github.com/renqiankun/electron-vite-template

better-sqlite3 drizzle-orm electron vite vue3

Last synced: over 1 year ago
JSON representation

electron vite vue3 bettter-sqlite3 drizzle-orm

Awesome Lists containing this project

README

          

[ English](README.md) | [ δΈ­ζ–‡](README.zh.md)
# πŸš€ Electron + Vite + Drizzle ORM + Better-SQLite3

A modern desktop application template based on **Electron + Vite + Drizzle ORM + Better-SQLite3**.

## ✨ Features
- **Drizzle ORM**: A modern, lightweight ORM solution
- **Vue 3**: Used as the UI layer (can be replaced as needed)
- **Electron v34.0.0** + **Node.js v20.18.0**
- **Better-SQLite3**: High-performance, synchronous SQLite database library

---

## πŸ“‚ Directory Structure

```bash
πŸ“¦ Project Root
β”œβ”€β”€ assets # Static resources (including app icons for packaging)
β”œβ”€β”€ electron # Electron-related code
β”‚ β”œβ”€β”€ main # Main process code
β”‚ β”‚ β”œβ”€β”€ auto-update # electron-updater auto update
β”‚ β”‚ β”œβ”€β”€ db # Database-related code
β”‚ β”‚ β”œβ”€β”€ router # Routes (provides access to the database)
β”‚ β”‚ β”œβ”€β”€ utils.ts # Utility functions
β”‚ β”‚ β”œβ”€β”€ dbServicesInit.ts # Database initialization logic
β”‚ β”‚ β”œβ”€β”€ index.ts # Main process entry file
β”‚ β”œβ”€β”€ preload # Preload scripts
β”œβ”€β”€ migrations # Database migration files
β”œβ”€β”€ public # Vue public resources
β”œβ”€β”€ src # Vue source code
β”œβ”€β”€ drizzle.config.ts # Drizzle ORM migration configuration
β”œβ”€β”€ electron-builder.json # Electron build configuration
β”œβ”€β”€ vite.config.ts # Vite build configuration
```

---

## βš™οΈ Environment Setup

1. **Node.js** version `v20.18.0`
2. **Visual Studio 2022** (requires **Desktop development with C++** component)
3. **Python 3.7**
4. **Configure environment variables**
```sh
npm config edit
```
Add the following content:
```ini
msvs_version=2022
python=python3.7
```
5. **Globally install `node-gyp`**
```sh
npm install -g node-gyp
```

---

## πŸš€ Start the Project

```sh
npm install # Install dependencies
npm rebuild # Rebuild local dependencies
npx electron-rebuild -f -w better-sqlite3 # Adapt Electron native modules (optional: specify module name, e.g., better-sqlite3)
npm run syncSchema # Sync the database schema
npm run dev # Start the project
npm run build # Build the project
```

---

## πŸ”¨ Development Guide

### **πŸ“Œ Local Database Synchronization**
- **After modifying the database schema, execute:**
```sh
npm run syncSchema
```
This command includes three steps:
1. `npm rebuild` - Rebuild `better-sqlite3` to match the local Node.js version
2. `npx drizzle-kit push` - Sync the schema directly to the local database
3. `npx electron-rebuild -f -w better-sqlite3` - Rebuild `better-sqlite3` for Electron compatibility

### **πŸ“Œ Database Upgrade During Packaging**

1. Run the build command, which will first execute `npm run generateSchema` to generate database migration files:
```sh
npm run build
```

```
The database file is separated into development (`.env.db`) and production (`.db`) versions.
This prevents issues when using Drizzle ORM for both local development and packaged installations.
If using the same file, remove the existing database file or rename it before installing the production package.
```

---

### **πŸ“Œ Preload Script Usage**
1. **`webPreferences` disables Node.js in the renderer, enables context isolation, but does not enable sandbox mode**, requiring native modules or IPC communication to be exposed in `preload`.
2. **Preload files are compiled as `.mjs` (ES module format)**, and `import` is used to include other modules internally.

### **πŸ“Œ Database Migration Strategy**
Database migrations are handled differently for **development** and **production** environments:

#### **1️⃣ Production Environment**
- **Database migration files are automatically generated during the build process**, no manual intervention required.
- Ensure that `drizzle.config.ts` points to the correct database file.

#### **2️⃣ Development Environment**
- **After modifying the schema, run:**
```sh
npm run syncSchema
```
**This executes the following steps:**
- Rebuild `better-sqlite3`
- Use `drizzle-kit push` to directly sync the database
- Rebuild `better-sqlite3` for Electron compatibility

#### **3️⃣ Migration File Management**
- The `migrations` directory stores database migration files.
- **Do not delete this directory**, as it may cause data loss.

---

## πŸ“Œ To-Do List
βœ… **Database Communication Example**
βœ… **App Update with electron-updater Example**
βœ… **Add Logger**
⬜ **Multi-Window Example**

---

πŸŽ‰ **Enjoy coding with Electron + Vite + Drizzle ORM!** πŸš€