Skip to content

Commit ce23be3

Browse files
committed
✨ feat(db): add turso database support
- added turso database support to the drizzle setup - updated .env file to include turso database url and auth token - created new templates for turso database - updated the database config files to include turso - added turso specific drizzle config file - updated the package.json to include the necessary packages for turso - added support for turso in the main index.ts file - updated the env-update function to handle multiple environment variables - updated the cspell words to include turso 🐛 fix(env): fix env file update - updated the `updateEnvFile` function to correctly handle multiple environment variables - added support for adding new environment variables if they do not exist - fixed a bug where existing environment variables were being overwritten 📝 docs(cspell): add turso to cspell words - added "Turso" to the cSpell words list in the `.vscode/settings.json` file ♻️ refactor(db): improve database config structure - updated the database configuration files to use an object for environment variables instead of a single string - this change improves the readability and maintainability of the code - added better error handling for missing environment variables - improved the structure of the database configuration files for better organization and clarity 📦 build(deps): update dependencies - updated drizzle-orm and related packages to the latest versions - added @neondatabase/serverless and @libsql/client packages for Neon and Turso support respectively - updated types for bun sqlite
1 parent f942fdd commit ce23be3

File tree

9 files changed

+78
-16
lines changed

9 files changed

+78
-16
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["libsql", "neondatabase"]
2+
"cSpell.words": ["libsql", "neondatabase", "Turso"]
33
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ You'll be guided through:
4949
## 🧪 Supported Databases
5050

5151
- PostgreSQL - Default, Neon
52-
- SQLite - Default, Bun SQLite
52+
- SQLite - Default, Turso, Bun SQLite
5353

5454
Each database type comes with its own pre-configured templates and `.env` variables.
5555

src/configs/databases/postgres_sql.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ const PostgresqlDatabasesConfigs = [
33
name: 'PostgreSQL',
44
path: 'postgres_sql',
55
template_path: 'templates/postgres/postgres',
6-
env_var: 'DATABASE_URL',
6+
env_var: {
7+
DATABASE_URL: 'postgres://postgres:postgres@localhost:5432/postgres',
8+
},
79
packages: ['drizzle-orm', 'mysql2', 'dotenv', 'drizzle-kit'],
810
},
911
{
1012
name: 'Neon',
1113
path: 'neon',
1214
template_path: 'templates/postgres/neon',
13-
env_var: 'DATABASE_URL',
15+
env_var: {
16+
DATABASE_URL: '',
17+
},
1418
packages: [
1519
'drizzle-orm',
1620
'@neondatabase/serverless',

src/configs/databases/sqlite.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@ const SQLiteDatabasesConfigs = [
33
name: 'SQLite',
44
path: 'sqlite',
55
template_path: 'templates/sqlite/sqlite',
6-
env_var: 'DB_FILE_NAME',
6+
env_var: {
7+
DB_FILE_NAME: 'file:local.db',
8+
},
9+
packages: ['drizzle-orm', '@libsql/client', 'dotenv', 'drizzle-kit'],
10+
},
11+
{
12+
name: 'Turso',
13+
path: 'turso',
14+
template_path: 'templates/sqlite/turso',
15+
env_var: {
16+
TURSO_DATABASE_URL: '',
17+
TURSO_AUTH_TOKEN: '',
18+
},
719
packages: ['drizzle-orm', '@libsql/client', 'dotenv', 'drizzle-kit'],
820
},
921
{
1022
name: 'Bun SQLite',
1123
path: 'bun_sqlite',
1224
template_path: 'templates/sqlite/bun-sqlite',
13-
env_var: 'DB_FILE_NAME',
25+
env_var: {
26+
DB_FILE_NAME: 'mydb.sqlite',
27+
},
1428
packages: ['drizzle-orm', '@types/bun', 'dotenv', 'drizzle-kit'],
1529
},
1630
]

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ try {
102102
}
103103

104104
try {
105-
await updateEnvFile(envVar)
105+
await updateEnvFile(envVar as Record<string, string>)
106106
// console.log(`⚙ .env file updated with ${envVar} at top`)
107107
} catch (error) {
108108
console.error('🚨 Error updating .env file:', error)
@@ -147,7 +147,8 @@ try {
147147
await pkgMangerRun(s, pkg_manger, dbConfig)
148148
s.stop(
149149
`\n📁 Template copied to ${dbPath.toString()}
150-
\n🛠 drizzle.config.ts added!
150+
\n⚙ .env file vars at on top updated!
151+
\n🛠 drizzle.config.ts added!
151152
\n📑 package.json scripts updated!
152153
\n✅ Drizzle Setup completed!`
153154
)

src/utils/env-update.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
import path from 'path'
22
import fs from 'fs-extra'
33

4-
async function updateEnvFile(envVar: string, value: string = '') {
4+
async function updateEnvFile(envVars: Record<string, string>) {
55
const envFilePath = path.join(process.cwd(), '.env')
66

77
let lines: string[] = []
88

9-
// Check if .env file exists
9+
// Load existing content if the file exists
1010
const exists = await fs.pathExists(envFilePath)
11-
1211
if (exists) {
1312
const content = await fs.readFile(envFilePath, 'utf-8')
14-
lines = content.split('\n').filter((line) => !line.startsWith(`${envVar}=`))
13+
lines = content.split('\n')
14+
}
15+
16+
// Create a map of existing env variables
17+
const envMap = new Map<string, string>()
18+
for (const line of lines) {
19+
const match = line.match(/^([^=]+)=(.*)$/)
20+
if (match) {
21+
envMap.set(match[1], match[2])
22+
}
23+
}
24+
25+
// Update or add new values
26+
for (const [key, value] of Object.entries(envVars)) {
27+
envMap.set(key, `"${value}"`)
1528
}
1629

17-
// Add the env_var at the top
18-
lines.unshift(`${envVar}="${value}"`)
30+
// Reconstruct the .env file content
31+
const updatedLines = Array.from(envMap.entries()).map(([k, v]) => `${k}=${v}`)
1932

20-
// Write updated content
21-
await fs.writeFile(envFilePath, lines.join('\n'))
33+
await fs.writeFile(envFilePath, updatedLines.join('\n'))
2234
}
2335

2436
export default updateEnvFile

templates/sqlite/turso/db/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'dotenv/config'
2+
import { drizzle } from 'drizzle-orm/libsql'
3+
4+
const db = drizzle({
5+
connection: {
6+
url: process.env.DATABASE_URL!,
7+
authToken: process.env.DATABASE_AUTH_TOKEN!,
8+
},
9+
})
10+
11+
export default db

templates/sqlite/turso/db/schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2+
3+
export const usersTable = sqliteTable('users_table', {
4+
id: int().primaryKey({ autoIncrement: true }),
5+
name: text().notNull(),
6+
age: int().notNull(),
7+
email: text().notNull().unique(),
8+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'dotenv/config'
2+
import { defineConfig } from 'drizzle-kit'
3+
4+
export default defineConfig({
5+
out: './drizzle',
6+
schema: '{{path}}/schema.ts',
7+
dialect: 'turso',
8+
dbCredentials: {
9+
url: process.env.TURSO_DATABASE_URL!,
10+
authToken: process.env.TURSO_AUTH_TOKEN!,
11+
},
12+
})

0 commit comments

Comments
 (0)