-
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.
Merge branch 'feature/dev-container'
- Loading branch information
Showing
16 changed files
with
407 additions
and
36 deletions.
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,63 @@ | ||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node-postgres | ||
{ | ||
"name": "Bun & Postgres", | ||
"dockerComposeFile": "docker-compose.yml", | ||
"service": "app", | ||
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", | ||
// Features to add to the dev container. More info: https://containers.dev/features. | ||
"features": { | ||
"ghcr.io/devcontainers/features/github-cli:1": {}, | ||
"ghcr.io/devcontainers-contrib/features/zsh-plugins:0": { | ||
"plugins": "zsh-completions zsh-autosuggestions zsh-syntax-highlighting", | ||
"omzPlugins": "https://github.com/zsh-users/zsh-completions.git https://github.com/zsh-users/zsh-autosuggestions.git https://github.com/zsh-users/zsh-syntax-highlighting.git" | ||
}, | ||
"ghcr.io/shyim/devcontainers-features/bun:0": {}, | ||
"ghcr.io/itsmechlark/features/postgresql:1.3.3": {} | ||
}, | ||
"waitFor": "onCreateCommand", | ||
"updateContentCommand": "bun i", | ||
"postAttachCommand": { | ||
"app": "bun dev", | ||
"studio": "bun db:studio" | ||
}, | ||
// "postCreateCommand": "bun db:migrate && bun db:seed", | ||
"postCreateCommand": "./scripts/create-db.sh", | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"biomejs.biome", | ||
"ms-azuretools.vscode-docker", | ||
"w0o0o.bun-scripts", | ||
"oven.bun-vscode", | ||
"antfu.browse-lite", | ||
"mtxr.sqltools", | ||
"mtxr.sqltools-driver-pg", | ||
"bierner.markdown-preview-github-styles" | ||
], | ||
"settings": { | ||
"editor.tabSize": 2, | ||
"editor.insertSpaces": true, | ||
"terminal.integrated.defaultProfile.linux": "zsh" | ||
} | ||
} | ||
}, | ||
"portsAttributes": { | ||
"3000": { | ||
"label": "App", | ||
"onAutoForward": "openPreview" | ||
}, | ||
"4983": { | ||
"label": "Studio" | ||
}, | ||
"5432": { | ||
"label": "Database" | ||
} | ||
}, | ||
"forwardPorts": [5432,3000,4983] | ||
|
||
// Configure tool-specific properties. | ||
// "customizations": {}, | ||
// Needed for bun install (for access to save .lockb). More info: https://aka.ms/dev-containers-non-root. | ||
// "remoteUser": "root" | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ POSTGRES_HOST=0.0.0.0 | |
POSTGRES_PORT=5432 | ||
JWT_SECRET=supersecretkey | ||
JWT_ALGORITHM=HS256 | ||
APP_PORT=3000 |
Empty file.
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
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
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,22 @@ | ||
import { db } from '@/database.providers'; | ||
import { users } from '@/users/users.schema'; | ||
import { getTableName } from 'drizzle-orm'; | ||
import { exit } from 'process'; | ||
// TODO: use react-ink | ||
|
||
const tables = [users]; | ||
|
||
console.log('Dropping the entire database'); | ||
|
||
for (const table of tables) { | ||
const name = getTableName(table); | ||
console.log(`Dropping ${name}`); | ||
await db.delete(users); | ||
console.log(`Dropped ${name}`); | ||
const tableResult = await db.select().from(table); | ||
console.log(`${name} result: `, tableResult); | ||
} | ||
|
||
console.log('Database dropped'); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,16 @@ import { db } from '@/database.providers'; | |
import { users } from '@/users/users.model'; | ||
|
||
const data = { | ||
id: users.id.default, | ||
id: 1, //do not use 'users.id.default', seed should be idempotent | ||
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'); | ||
console.log('Upserting user: ', data); | ||
await db.insert(users).values(data).onConflictDoNothing(); | ||
console.log('User upserted'); | ||
|
||
const userResult = await db.select().from(users); | ||
console.log('User result: ', userResult); | ||
|
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 |
---|---|---|
@@ -1,22 +1,49 @@ | ||
version: '3.8' | ||
|
||
services: | ||
db: | ||
image: postgres | ||
restart: always | ||
ports: | ||
- ${POSTGRES_PORT}:5432 | ||
image: postgres:16 | ||
restart: unless-stopped | ||
volumes: | ||
- postgres-data:/var/lib/postgresql/data | ||
environment: | ||
PGUSER: ${POSTGRES_USER} | ||
POSTGRES_DB: ${POSTGRES_DB} | ||
POSTGRES_USER: ${POSTGRES_USER} | ||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | ||
- POSTGRES_USER=${POSTGRES_USER} | ||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} | ||
- POSTGRES_DB=${POSTGRES_DB} | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready"] | ||
test: | ||
[ | ||
"CMD-SHELL", | ||
"PGPASSWORD=$${POSTGRES_PASSWORD} psql -U $${POSTGRES_USER} -d $${POSTGRES_DB} -c 'SELECT 1;' || exit 1" | ||
] | ||
interval: 1s | ||
timeout: 5s | ||
retries: 10 | ||
# This allows accessing externally from "localhost" in addition to "127.0.0.1" | ||
ports: | ||
- ${POSTGRES_PORT}:5432 | ||
|
||
app: | ||
image: node:20-bullseye | ||
restart: unless-stopped | ||
|
||
volumes: | ||
- pgdata:/var/lib/postgresql/data | ||
env_file: | ||
- .env | ||
- ..:/workspaces:cached | ||
- ../scripts:/scripts:cached | ||
|
||
# Overrides default command so things don't shut down after the process ends. | ||
command: sleep infinity | ||
|
||
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. | ||
network_mode: service:db | ||
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally. | ||
# (Adding the "ports" property to this file will not forward from a Codespace.) | ||
|
||
# depends_on: | ||
# - db | ||
# environment: | ||
# - POSTGRES_HOST=db # connect to the Postgres container with Docker Networking | ||
# - POSTGRES_PORT=5432 # use internal port of Postgres container | ||
|
||
volumes: | ||
pgdata: | ||
postgres-data: |
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,15 @@ | ||
#!/bin/bash | ||
|
||
# Source .env file to get environment variables | ||
source .env | ||
|
||
# TODO: find a fix for database ('medium') creation without having to rebuild | ||
|
||
# Run wait-for-db.sh with the sourced environment variables | ||
# ./scripts/wait-for-db.sh db:5432 --strict --timeout=60 -- psql -h db -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c 'SELECT 1;' | ||
|
||
# TODO: find a better way to wait for database ('medium') creation than waiting 10 seconds | ||
sleep 10 | ||
|
||
bun db:migrate | ||
bun db:seed |
Oops, something went wrong.