Skip to content

Commit

Permalink
feat: postgres integration (#35)
Browse files Browse the repository at this point in the history
* Working postgres integration

* Remove optional fields from user create

* Move credentials to an env file
  • Loading branch information
Hajbo authored Sep 25, 2023
1 parent be322b2 commit 9014069
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=medium
POSTGRES_HOST=0.0.0.0
POSTGRES_PORT=5432
Binary file modified bun.lockb
Binary file not shown.
22 changes: 22 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
PGUSER: ${POSTGRES_USER}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 1s
timeout: 5s
retries: 10
volumes:
- pgdata:/var/lib/postgresql/data
env_file:
- .env
volumes:
pgdata:
10 changes: 10 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from "drizzle-kit";
import { dbCredentials } from "./src/config";

export default {
out: "./src/db/migrations",
schema: "./src/db/schemas/*.ts",
breakpoints: false,
driver: "pg",
dbCredentials: dbCredentials
} satisfies Config;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
},
"dependencies": {
"drizzle-orm": "^0.28.6",
"elysia": "latest"
"elysia": "latest",
"postgres": "^3.3.5"
},
"devDependencies": {
"bun-types": "latest",
"drizzle-kit": "^0.19.13",
"pg": "^8.11.3",
"vitepress": "^1.0.0-rc.15"
},
"peerDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const dbCredentials = {
host: process.env.POSTGRES_HOST || "0.0.0.0",
port: parseInt(process.env.POSTGRES_PORT || '5432'),
user: process.env.POSTGRES_USER || "postgres",
password: process.env.POSTGRES_PASSWORD || "postgres",
database: process.env.POSTGRES_DB || "medium"
}

export const dbCredentialsString = `postgres://${dbCredentials.user}:${dbCredentials.password}@${dbCredentials.host}:${dbCredentials.port}/${dbCredentials.database}`
13 changes: 13 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

import { dbCredentialsString } from '../config';

// for migrations
const migrationClient = postgres(dbCredentialsString, { max: 1 });
migrate(drizzle(migrationClient), {migrationsFolder: './migrations'})

// for query purposes
const queryClient = postgres(dbCredentialsString);
export const db: PostgresJsDatabase = drizzle(queryClient);
10 changes: 10 additions & 0 deletions src/db/migrations/0000_bored_warstar.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"email" text NOT NULL,
"bio" text NOT NULL,
"image" text NOT NULL,
"password" text NOT NULL,
"username" text NOT NULL,
"created_at" date DEFAULT CURRENT_DATE,
"updated_at" date DEFAULT CURRENT_DATE
);
75 changes: 75 additions & 0 deletions src/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"version": "5",
"dialect": "pg",
"id": "86aed854-dd08-4bcd-8138-412a71492c24",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"bio": {
"name": "bio",
"type": "text",
"primaryKey": false,
"notNull": true
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "date",
"primaryKey": false,
"notNull": false,
"default": "CURRENT_DATE"
},
"updated_at": {
"name": "updated_at",
"type": "date",
"primaryKey": false,
"notNull": false,
"default": "CURRENT_DATE"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
13 changes: 13 additions & 0 deletions src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1695584965813,
"tag": "0000_bored_warstar",
"breakpoints": false
}
]
}
27 changes: 27 additions & 0 deletions src/db/schemas/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import { sql } from "drizzle-orm";
import { pgTable, text, date, serial} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';
import { Type } from '@sinclair/typebox';



export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: text('email').notNull(),
bio: text('bio').notNull(),
image: text('image').notNull(),
password: text('password').notNull(),
username: text('username').notNull(),
created_at: date('created_at').default(sql`CURRENT_DATE`),
updated_at: date('updated_at').default(sql`CURRENT_DATE`),
});



// Schema for inserting a user - can be used to validate API requests
const insertUserSchemaRaw = createInsertSchema(users);
export const insertUserSchema = Type.Omit(insertUserSchemaRaw, ['id', 'created_at', 'updated_at']);

// Schema for selecting a user - can be used to validate API responses
export const selectUserSchema = createSelectSchema(users);
22 changes: 22 additions & 0 deletions src/db/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { exit } from 'process';
import { db } from './index';
import { users } from './schemas/users';


console.log("Migrations complete.")
const data = {
id: users.id.default,
email: '[email protected]',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
}
console.log("Inserting user: ", data)
await db.insert(users).values(data)
console.log("User inserted")

const userResult = await db.select().from(users);
console.log("User result: ", userResult);

exit(0);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Elysia } from "elysia";



const app = new Elysia().get("/", () => "Hello Elysia").listen(3000);

console.log(
Expand Down

0 comments on commit 9014069

Please sign in to comment.