-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Working postgres integration * Remove optional fields from user create * Move credentials to an env file
- Loading branch information
Showing
13 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters