Skip to content

Commit

Permalink
Include Drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Bozzo authored and Lucas Bozzo committed Apr 28, 2024
1 parent 4cf4aa1 commit ae216ee
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Migrate DB

on:
push:
branches:
- main

jobs:
migrate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npm test
- run: npm run db:generate
- run: npm run db:migrate
13 changes: 13 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Config } from 'drizzle-kit';
import * as dotenv from 'dotenv';
dotenv.config({ path: '.dev.vars' });

export default {
schema: './src/data/schema.ts',
out: 'migrations',
driver: 'turso',
dbCredentials: {
url: process.env.TURSO_DATABASE_URL!,
authToken: process.env.TURSO_AUTH_TOKEN,
},
} satisfies Config;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
{
"scripts": {
"dev": "wrangler dev src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts"
"deploy": "wrangler deploy --minify src/index.ts",
"db:generate": "drizzle-kit generate:sqlite",
"db:push": "drizzle-kit push:sqlite",
"db:migrate": "tsx ./src/data/migrate.ts",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@libsql/client": "^0.6.0",
"drizzle-orm": "^0.30.9",
"hono": "^4.2.8"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240403.0",
"dotenv": "^16.4.5",
"drizzle-kit": "^0.20.17",
"wrangler": "^3.47.0"
}
}
9 changes: 9 additions & 0 deletions src/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client/web';

const turso = createClient({
url: process.env.TURSO_DATABASE_URL!,
authToken: process.env.TURSO_AUTH_TOKEN,
});

export const data = drizzle(turso);
22 changes: 22 additions & 0 deletions src/data/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// migrate.ts

import { config } from 'dotenv';
import { migrate } from 'drizzle-orm/libsql/migrator';
import { data } from './index';

config({ path: '.dev.vars' });

const main = async () => {
try {
console.log('Running migrations');
await migrate(data, { migrationsFolder: 'migrations' });
console.log('Migration complete!');
} catch (error) {
console.error('Migration failed');
console.error(error);
process.exit(1);
}
process.exit(0);
};

main();
7 changes: 7 additions & 0 deletions src/data/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sql } from 'drizzle-orm';
import { text, sqliteTable } from 'drizzle-orm/sqlite-core';

export const foo = sqliteTable('foo', {
bar: text('bar').notNull().default('Hey!'),
baz: text('baz').notNull().default('Hello World!'),
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hono } from 'hono/tiny';

const app = new Hono();

app.get('/', (c) => {
app.get('/random', (c) => {
return c.text('Hello Hono!');
});

Expand Down

0 comments on commit ae216ee

Please sign in to comment.