Skip to content

Commit

Permalink
Moved db init and migration to seperate files and experimented with h…
Browse files Browse the repository at this point in the history
…ooks
  • Loading branch information
Rerbun committed Apr 29, 2024
1 parent e7e1a65 commit eda5d1f
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ vite.config.js.timestamp-*
vite.config.ts.timestamp-*

docker-compose.y*
*.db
*.db
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/build build/
COPY --from=builder /app/node_modules node_modules/
COPY --from=builder /app/migrations migrations
COPY package.json .
EXPOSE 3000
ENV NODE_ENV=production
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"format": "prettier --write .",
"prepare": "husky",
"remove-databases": "rm -f ./data/sqlite.db ./data/sqlite.dev.db",
"migrate": "npm exec drizzle-kit generate:sqlite",
"migrate:clean": "npm run remove-databases && rm -rf ./migrations && npm run migrate",
"generate:migration": "npm exec drizzle-kit generate:sqlite",
"generate:migration:clean": "npm run remove-databases && rm -rf ./migrations && npm run migrate",
"docker:dev": "docker compose -f docker-compose.dev.yml up --build"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/components/TodoList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let editMode: boolean = false;
computedTodo.subscribe((value) => {
todo = Todo.fromObject(value);
if (value) todo = Todo.fromObject(value);
});
const handleCheck = (event: Event, touchedTodo: Record<string, any>) => {
Expand Down
8 changes: 8 additions & 0 deletions src/db/init.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from 'path';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';

const sqlite = new Database(
path.resolve(process.env.NODE_ENV === 'production' ? '/data/sqlite.db' : 'data/sqlite.dev.db')
);
export const db = drizzle(sqlite);
4 changes: 4 additions & 0 deletions src/db/migrate.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import { db } from './init.server';

migrate(db, { migrationsFolder: 'migrations' });
1 change: 1 addition & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './db/migrate.server';
14 changes: 2 additions & 12 deletions src/utils/database-utils.server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import path from 'path';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import Database from 'better-sqlite3';
import { eq, desc, inArray } from 'drizzle-orm';
import { pick, once } from 'lodash-es';
import { pick } from 'lodash-es';
import { Todo } from '../interfaces/Todo';
import { todoTable } from '../db/schema.server';

const sqlite = new Database(
path.resolve(process.env.NODE_ENV === 'production' ? '/data/sqlite.db' : 'data/sqlite.dev.db')
);
const db = drizzle(sqlite);

once(() => migrate(db, { migrationsFolder: 'migrations' }));
import { db } from '../db/init.server';

export const getTodoById = async (id: string) => {
const todoEntry: Record<string, any> = (
Expand Down

0 comments on commit eda5d1f

Please sign in to comment.