Skip to content

Commit

Permalink
feat: move to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
SrIzan10 committed Aug 14, 2024
1 parent f9e1724 commit 3e000b0
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 185 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ README.md
node_modules
build
package
**/.env
**/.env

db/database.db
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
/.wrangler
/.wrangler

db/database.db
Empty file added db/.gitkeep
Empty file.
4 changes: 2 additions & 2 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
dialect: 'postgresql',
dialect: 'sqlite',
out: './src/drizzle',
schema: './src/lib/server/db/schema.ts',
dbCredentials: {
url: process.env.DK_DATABASE_URL!,
url: 'db/database.db'
},
// Print all statements
verbose: true,
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@sveltejs/adapter-node": "^5.2.1",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/better-sqlite3": "^7.6.11",
"@types/node": "^22.1.0",
"@types/ws": "^8.5.10",
"autoprefixer": "^10.4.17",
Expand All @@ -26,17 +27,15 @@
"tailwindcss": "^3.4.1",
"tslib": "^2.4.1",
"typescript": "^5.3.3",
"vite": "^5.0.3",
"ws": "^8.18.0"
"vite": "^5.0.3"
},
"type": "module",
"dependencies": {
"@lucia-auth/adapter-drizzle": "^1.0.7",
"@neondatabase/serverless": "^0.9.4",
"better-sqlite3": "^11.1.2",
"drizzle-orm": "^0.32.2",
"lucia": "^3.2.0",
"lucide-svelte": "^0.381.0",
"postgres": "^3.4.4",
"svelte-sonner": "^0.3.27",
"zod": "^3.23.8"
},
Expand Down
23 changes: 23 additions & 0 deletions src/drizzle/0000_curvy_skreet.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE TABLE `question` (
`id` text PRIMARY KEY NOT NULL,
`for_username` text NOT NULL,
`text` text NOT NULL,
`created_at` text DEFAULT (CURRENT_TIMESTAMP),
FOREIGN KEY (`for_username`) REFERENCES `user`(`username`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `session` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`expires_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` text PRIMARY KEY NOT NULL,
`username` text NOT NULL,
`hashed_password` text NOT NULL,
`image_url` text DEFAULT '/tbh.webp'
);
--> statement-breakpoint
CREATE UNIQUE INDEX `user_username_unique` ON `user` (`username`);
32 changes: 0 additions & 32 deletions src/drizzle/0000_wealthy_dagger.sql

This file was deleted.

77 changes: 43 additions & 34 deletions src/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
{
"id": "6202055c-f02c-43c8-b2da-1e6f52b0d9bb",
"version": "6",
"dialect": "sqlite",
"id": "0f63de69-2035-4dcd-afb4-8f29aaf78ef7",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.question": {
"question": {
"name": "question",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
"notNull": true,
"autoincrement": false
},
"for_username": {
"name": "for_username",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"text": {
"name": "text",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"created-at": {
"name": "created-at",
"type": "date",
"created_at": {
"name": "created_at",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": false,
"autoincrement": false,
"default": "(CURRENT_TIMESTAMP)"
}
},
"indexes": {},
Expand All @@ -52,27 +56,29 @@
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.session": {
"session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
"notNull": true,
"autoincrement": false
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"type": "integer",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
Expand All @@ -94,57 +100,60 @@
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.user": {
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"default": "'2a78a7e1-5468-474e-9858-2ab153c036c9'"
"autoincrement": false
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"hashed_password": {
"name": "hashed_password",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"image_url": {
"name": "image_url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'/tbh.webp'"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"indexes": {
"user_username_unique": {
"name": "user_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
],
"isUnique": true
}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
8 changes: 4 additions & 4 deletions src/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version": "7",
"dialect": "postgresql",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1723065498982,
"tag": "0000_wealthy_dagger",
"version": "6",
"when": 1723633889022,
"tag": "0000_curvy_skreet",
"breakpoints": true
}
]
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Lucia } from "lucia";
import { dev } from "$app/environment";
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
import db from "../db";
import * as schema from "../db/schema";

const adapter = new DrizzlePostgreSQLAdapter(db, schema.session, schema.user);
const adapter = new DrizzleSQLiteAdapter(db, schema.session, schema.user);

export const lucia = new Lucia(adapter, {
sessionCookie: {
Expand Down
9 changes: 4 additions & 5 deletions src/lib/server/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import * as schema from './schema';
import { DATABASE_URL } from '$env/static/private';
import postgres from 'postgres';
import Database from 'better-sqlite3';

const pg = postgres(DATABASE_URL);
const db = drizzle(pg, { schema });
const sqlite = new Database('db/database.db')
const db = drizzle(sqlite, { schema });

export default db;
19 changes: 8 additions & 11 deletions src/lib/server/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { sql } from "drizzle-orm";
import { pgTable, text, integer, date, timestamp } from "drizzle-orm/pg-core";
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";

export const user = pgTable("user", {
id: text("id").primaryKey().default(crypto.randomUUID()),
export const user = sqliteTable("user", {
id: text("id").primaryKey().notNull(),
username: text("username").notNull().unique(),
hashedPassword: text("hashed_password").notNull(),
imageUrl: text("image_url").default('/tbh.webp'),
});

export const session = pgTable("session", {
id: text("id").primaryKey(),
export const session = sqliteTable("session", {
id: text("id").notNull().primaryKey(),
userId: text("user_id")
.notNull()
.references(() => user.id),
expiresAt: timestamp("expires_at", {
withTimezone: true,
mode: "date"
}).notNull()
expiresAt: integer("expires_at").notNull()
});

export const question = pgTable("question", {
export const question = sqliteTable("question", {
id: text("id").primaryKey(),
forUsername: text("for_username")
.notNull()
.references(() => user.username),
body: text("text").notNull(),
createdAt: date('created-at')
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`),
});
Loading

0 comments on commit 3e000b0

Please sign in to comment.