Skip to content

Commit

Permalink
refactor: table name, add db utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Mar 8, 2024
1 parent cc2d4df commit aceba75
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 8 deletions.
4 changes: 3 additions & 1 deletion drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { env } from "@/env.js"
import { type Config } from "drizzle-kit"

import { databasePrefix } from "@/lib/constants"

export default {
schema: "./src/db/schema.ts",
driver: "pg",
out: "./drizzle",
dbCredentials: {
connectionString: env.DATABASE_URL,
},
tablesFilter: ["shadcn-table_*"],
tablesFilter: [`${databasePrefix}_*`],
} satisfies Config
27 changes: 27 additions & 0 deletions drizzle/0000_married_blob.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
DO $$ BEGIN
CREATE TYPE "shadcn_label" AS ENUM('bug', 'feature', 'enhancement', 'documentation');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "shadcn_priority" AS ENUM('low', 'medium', 'high');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "shadcn_status" AS ENUM('todo', 'in-progress', 'done', 'canceled');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "shadcn_tasks" (
"id" varchar(128) PRIMARY KEY NOT NULL,
"code" varchar(255),
"title" varchar(255),
"status" "shadcn_status" DEFAULT 'todo' NOT NULL,
"label" "shadcn_label" DEFAULT 'bug' NOT NULL,
"priority" "shadcn_priority" DEFAULT 'low' NOT NULL,
CONSTRAINT "shadcn_tasks_code_unique" UNIQUE("code")
);
99 changes: 99 additions & 0 deletions drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"id": "604b112c-e9fc-476a-b1c3-72eb27682da3",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"shadcn_tasks": {
"name": "shadcn_tasks",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "varchar(128)",
"primaryKey": true,
"notNull": true
},
"code": {
"name": "code",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"title": {
"name": "title",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "shadcn_status",
"primaryKey": false,
"notNull": true,
"default": "'todo'"
},
"label": {
"name": "label",
"type": "shadcn_label",
"primaryKey": false,
"notNull": true,
"default": "'bug'"
},
"priority": {
"name": "priority",
"type": "shadcn_priority",
"primaryKey": false,
"notNull": true,
"default": "'low'"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"shadcn_tasks_code_unique": {
"name": "shadcn_tasks_code_unique",
"nullsNotDistinct": false,
"columns": [
"code"
]
}
}
}
},
"enums": {
"shadcn_label": {
"name": "shadcn_label",
"values": {
"bug": "bug",
"feature": "feature",
"enhancement": "enhancement",
"documentation": "documentation"
}
},
"shadcn_priority": {
"name": "shadcn_priority",
"values": {
"low": "low",
"medium": "medium",
"high": "high"
}
},
"shadcn_status": {
"name": "shadcn_status",
"values": {
"todo": "todo",
"in-progress": "in-progress",
"done": "done",
"canceled": "canceled"
}
}
},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
14 changes: 13 additions & 1 deletion drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
{"version":"5","dialect":"mysql","entries":[]}
{
"version": "5",
"dialect": "mysql",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1709909205617,
"tag": "0000_married_blob",
"breakpoints": true
}
]
}
11 changes: 5 additions & 6 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pgEnum, pgTableCreator, varchar } from "drizzle-orm/pg-core"
import { pgTable } from "@/db/utils"
import { pgEnum, varchar } from "drizzle-orm/pg-core"

import { createId } from "@/lib/utils"

Expand All @@ -9,23 +10,21 @@ import { createId } from "@/lib/utils"
* @see https://orm.drizzle.team/docs/goodies#multi-project-schema
*/

export const pgTable = pgTableCreator((name) => `shadcn-table_${name}`)

export const statusEnum = pgEnum("status", [
export const statusEnum = pgEnum("shadcn_status", [
"todo",
"in-progress",
"done",
"canceled",
])

export const labelEnum = pgEnum("label", [
export const labelEnum = pgEnum("shadcn_label", [
"bug",
"feature",
"enhancement",
"documentation",
])

export const priorityEnum = pgEnum("priority", ["low", "medium", "high"])
export const priorityEnum = pgEnum("shadcn_priority", ["low", "medium", "high"])

export const tasks = pgTable("tasks", {
id: varchar("id", { length: 128 })
Expand Down
5 changes: 5 additions & 0 deletions src/db/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { pgTableCreator } from "drizzle-orm/pg-core"

import { databasePrefix } from "@/lib/constants"

export const pgTable = pgTableCreator((name) => `${databasePrefix}_${name}`)
3 changes: 3 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const unknownError = "An unknown error occurred. Please try again later."

export const databasePrefix = "shadcn"

0 comments on commit aceba75

Please sign in to comment.