Skip to content

Commit

Permalink
Setup database (#20) (#22)
Browse files Browse the repository at this point in the history
* fix: init drizzle

* feat: add schema and drizzle boilerplate

* feat: add message template helpers

* fix: pluralize table name

* feat: add user helpers, move to helpers folder

* feat: add template deletion helper

* feat: add user helper

* fix: return insert values and parity

---------

Co-authored-by: Jasper Mayone <[email protected]>
  • Loading branch information
rayhanadev and jaspermayone authored May 9, 2024
1 parent d32c521 commit c348d1e
Show file tree
Hide file tree
Showing 16 changed files with 1,591 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .replit
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ language = "nodejs"
run = ["node", "index.js"]
deploymentTarget = "cloudrun"
ignorePorts = false

[[ports]]
localPort = 3000
externalPort = 80
11 changes: 11 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "dotenv/config";
import type { Config } from "drizzle-kit";

export default {
schema: "./src/db/schema.ts",
out: "./migrations",
driver: "pg",
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
} satisfies Config;
15 changes: 15 additions & 0 deletions migrations/0000_yellow_stardust.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE IF NOT EXISTS "bloom_templates" (
"id" serial PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"content" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "bloom_user" (
"id" serial PRIMARY KEY NOT NULL,
"external_id" text NOT NULL,
"initial_message_content" text NOT NULL,
"initial_message_timestamp" timestamp (3) DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "templates_name_idx" ON "bloom_templates" ("name");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "user_external_id_idx" ON "bloom_user" ("external_id");
1 change: 1 addition & 0 deletions migrations/0001_closed_pixie.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "bloom_user" RENAME TO "bloom_users";
94 changes: 94 additions & 0 deletions migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"id": "3dc8027f-a4c1-42ce-bf89-fcc36761156e",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"bloom_templates": {
"name": "bloom_templates",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {
"templates_name_idx": {
"name": "templates_name_idx",
"columns": [
"name"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"bloom_user": {
"name": "bloom_user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"external_id": {
"name": "external_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"initial_message_content": {
"name": "initial_message_content",
"type": "text",
"primaryKey": false,
"notNull": true
},
"initial_message_timestamp": {
"name": "initial_message_timestamp",
"type": "timestamp (3)",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"user_external_id_idx": {
"name": "user_external_id_idx",
"columns": [
"external_id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
94 changes: 94 additions & 0 deletions migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"id": "40225452-e629-4653-bb6b-16ac90b546a6",
"prevId": "3dc8027f-a4c1-42ce-bf89-fcc36761156e",
"version": "5",
"dialect": "pg",
"tables": {
"bloom_templates": {
"name": "bloom_templates",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {
"templates_name_idx": {
"name": "templates_name_idx",
"columns": [
"name"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"bloom_users": {
"name": "bloom_users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"external_id": {
"name": "external_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"initial_message_content": {
"name": "initial_message_content",
"type": "text",
"primaryKey": false,
"notNull": true
},
"initial_message_timestamp": {
"name": "initial_message_timestamp",
"type": "timestamp (3)",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"user_external_id_idx": {
"name": "user_external_id_idx",
"columns": [
"external_id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
20 changes: 20 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1715221164313,
"tag": "0000_yellow_stardust",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1715222353526,
"tag": "0001_closed_pixie",
"breakpoints": true
}
]
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"scripts": {
"start": "node dist/index.js",
"build": "tsc",
"dev": "nodemon src/index.ts"
"dev": "nodemon src/index.ts",
"db:migrate": "tsx scripts/migrate.ts",
"db:gen-migration": "drizzle-kit generate:pg"
},
"keywords": [],
"author": "Jasper Mayone <[email protected]>",
Expand All @@ -28,11 +30,15 @@
"@types/express": "^4.17.21",
"colors": "^1.4.0",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.30.10",
"express": "^4.19.2",
"js-yaml": "^4.1.0",
"nodemon": "^3.0.3"
"nodemon": "^3.0.3",
"postgres": "^3.4.4",
"tsx": "^4.9.3"
},
"devDependencies": {
"drizzle-kit": "^0.20.18",
"prisma": "^5.13.0",
"ts-node": "^10.9.2",
"typescript": "~5.4.3"
Expand Down
Loading

0 comments on commit c348d1e

Please sign in to comment.