Skip to content

Commit

Permalink
refactor: migrate to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichthagel committed Oct 20, 2024
1 parent 3b245a4 commit a3f2811
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 57 deletions.
11 changes: 11 additions & 0 deletions packages/server-db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit";
import env from "server-env";

export default defineConfig({
out: "./drizzle",
schema: "./src/schema.ts",
dialect: "sqlite",
dbCredentials: {
url: env.DATABASE_URL,
},
});
33 changes: 33 additions & 0 deletions packages/server-db/drizzle/0000_hot_chamber.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE TABLE `Image` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`filename` text NOT NULL,
`title` text,
`source` text,
`createdAt` integer DEFAULT (unixepoch('now')) NOT NULL,
`updatedAt` integer DEFAULT (unixepoch('now')) NOT NULL,
`height` integer NOT NULL,
`width` integer NOT NULL
) STRICT;
--> statement-breakpoint
CREATE UNIQUE INDEX `Image_filename_unique` ON `Image` (`filename`);--> statement-breakpoint
CREATE INDEX `Image_createdAt_idx` ON `Image` (`createdAt`);--> statement-breakpoint
CREATE TABLE `Tag` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`slug` text NOT NULL,
`categorySlug` text,
FOREIGN KEY (`categorySlug`) REFERENCES `TagCategory`(`slug`) ON UPDATE cascade ON DELETE set null
) STRICT;
--> statement-breakpoint
CREATE UNIQUE INDEX `Tag_slug_unique` ON `Tag` (`slug`);--> statement-breakpoint
CREATE TABLE `TagCategory` (
`slug` text PRIMARY KEY NOT NULL,
`color` text
) STRICT;
--> statement-breakpoint
CREATE TABLE `_ImageToTag` (
`imageId` integer NOT NULL,
`tagId` integer NOT NULL,
PRIMARY KEY(`imageId`, `tagId`),
FOREIGN KEY (`imageId`) REFERENCES `Image`(`id`) ON UPDATE cascade ON DELETE cascade,
FOREIGN KEY (`tagId`) REFERENCES `Tag`(`id`) ON UPDATE cascade ON DELETE cascade
) STRICT;
237 changes: 237 additions & 0 deletions packages/server-db/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"version": "6",
"dialect": "sqlite",
"id": "ddde953e-3583-4e46-b321-08a2a0ce7119",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"Image": {
"name": "Image",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"filename": {
"name": "filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"source": {
"name": "source",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch('now'))"
},
"updatedAt": {
"name": "updatedAt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch('now'))"
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"Image_filename_unique": {
"name": "Image_filename_unique",
"columns": [
"filename"
],
"isUnique": true
},
"Image_createdAt_idx": {
"name": "Image_createdAt_idx",
"columns": [
"createdAt"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"Tag": {
"name": "Tag",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"slug": {
"name": "slug",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"categorySlug": {
"name": "categorySlug",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"Tag_slug_unique": {
"name": "Tag_slug_unique",
"columns": [
"slug"
],
"isUnique": true
}
},
"foreignKeys": {
"Tag_categorySlug_TagCategory_slug_fk": {
"name": "Tag_categorySlug_TagCategory_slug_fk",
"tableFrom": "Tag",
"tableTo": "TagCategory",
"columnsFrom": [
"categorySlug"
],
"columnsTo": [
"slug"
],
"onDelete": "set null",
"onUpdate": "cascade"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"TagCategory": {
"name": "TagCategory",
"columns": {
"slug": {
"name": "slug",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"color": {
"name": "color",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"_ImageToTag": {
"name": "_ImageToTag",
"columns": {
"imageId": {
"name": "imageId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"tagId": {
"name": "tagId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"_ImageToTag_imageId_Image_id_fk": {
"name": "_ImageToTag_imageId_Image_id_fk",
"tableFrom": "_ImageToTag",
"tableTo": "Image",
"columnsFrom": [
"imageId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "cascade"
},
"_ImageToTag_tagId_Tag_id_fk": {
"name": "_ImageToTag_tagId_Tag_id_fk",
"tableFrom": "_ImageToTag",
"tableTo": "Tag",
"columnsFrom": [
"tagId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "cascade"
}
},
"compositePrimaryKeys": {
"_ImageToTag_pkey": {
"columns": [
"imageId",
"tagId"
],
"name": "_ImageToTag_pkey"
}
},
"uniqueConstraints": {},
"checkConstraints": {}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
13 changes: 13 additions & 0 deletions packages/server-db/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "6",
"when": 1729443367755,
"tag": "0000_hot_chamber",
"breakpoints": true
}
]
}
3 changes: 2 additions & 1 deletion packages/server-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
}
},
"dependencies": {
"@libsql/client": "^0.14.0",
"drizzle-orm": "^0.35.0",
"postgres": "^3.4.4",
"server-env": "workspace:^"
},
"devDependencies": {
"drizzle-kit": "^0.26.2",
"tsconfig": "workspace:^",
"typescript": "^5.4.5"
}
Expand Down
12 changes: 3 additions & 9 deletions packages/server-db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { drizzle } from "drizzle-orm/libsql";
import env from "server-env";

import schema from "./schema.js";

export const pgClient = postgres(env.DATABASE_URL, {
connection: {
application_name: "teruko-svelte",
},
});

export const db = drizzle(pgClient, {
export const db = drizzle({
connection: env.DATABASE_URL,
schema,
logger: env.NODE_ENV === "development",
});
Expand Down
Loading

0 comments on commit a3f2811

Please sign in to comment.