Skip to content

Commit

Permalink
feat(packages/drizzledb-pg): initial drizzle postgres database library (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpellegrini authored Mar 24, 2024
1 parent a15c09b commit f142514
Show file tree
Hide file tree
Showing 13 changed files with 1,243 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ vite.config.ts.timestamp-*

# vercel
.vercel/

# local docker volumes
/.docker/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ vite.config.ts.timestamp-*
# vercel
.vercel/

# local docker volumes
/.docker/

#-------------------------------------------------------------------------------------------------------------------
# Prettier-specific overrides
#-------------------------------------------------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
services:
postgres:
image: postgres:16.2-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
logging:
options:
max-size: 10m
max-file: '3'
ports:
- '5432:5432'
volumes:
- ./.docker/pg-data:/var/lib/postgresql/data

mailpit:
image: axllent/mailpit
restart: always
environment:
MP_MAX_MESSAGES: 5000
MP_DATA_FILE: /data/mailpit.db
MP_SMTP_AUTH_ACCEPT_ANY: 1
MP_SMTP_AUTH_ALLOW_INSECURE: 1
ports:
- 8025:8025
- 1025:1025
volumes:
- ./.docker/mailpit-data:/data
45 changes: 45 additions & 0 deletions packages/drizzledb-pg/drizzle.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
/**
* https://orm.drizzle.team/kit-docs/config-reference#out
*/
out: './drizzle',

/**
* https://orm.drizzle.team/kit-docs/config-reference#schema
*/
schema: './src/schema/*',

/**
* https://orm.drizzle.team/kit-docs/config-reference#breakpoints
*/
breakpoints: false,

/**
* https://orm.drizzle.team/kit-docs/config-reference#verbose
*/
verbose: true,

/**
* https://orm.drizzle.team/kit-docs/config-reference#strict
*/
strict: true,

/**
* https://orm.drizzle.team/kit-docs/config-reference#schemafilter
*/
schemaFilter: ['public'],

/**
* https://orm.drizzle.team/kit-docs/config-reference#driver
*/
driver: 'pg',

/**
* https://orm.drizzle.team/kit-docs/config-reference#driver
*/
dbCredentials: {
connectionString: process.env.DB_CONNECTION_STRING,
},
})
3 changes: 3 additions & 0 deletions packages/drizzledb-pg/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '@toolchain/eslint-config/profile/node'

export default config
39 changes: 39 additions & 0 deletions packages/drizzledb-pg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@packages/drizzledb-pg",
"version": "0.0.0",
"private": true,
"description": "Drizzle ORM Database Client and Schema for PostgreSQL",
"license": "UNLICENSED",
"type": "module",
"exports": {
"./client": "./src/client/index.ts",
"./schema": "./src/schema/index.ts"
},
"scripts": {
"clean": "del .turbo coverage",
"db:generate": "drizzle-kit generate:pg",
"db:migrate": "node --env-file=.env --import=tsx ./src/migrate.ts",
"db:push": "drizzle-kit push:pg",
"db:studio": "drizzle-kit studio",
"lint": "eslint .",
"test": "vitest run",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"dotenv": "16.4.5",
"drizzle-orm": "0.30.4",
"postgres": "3.4.4",
"znv": "0.4.0",
"zod": "3.22.4"
},
"devDependencies": {
"@toolchain/eslint-config": "workspace:*",
"@toolchain/typescript-config": "workspace:*",
"@toolchain/vitest-config": "workspace:*",
"@types/node": "20.11.30",
"@types/pg": "8.11.4",
"drizzle-kit": "0.20.14",
"pg": "8.11.3",
"tsx": "4.7.1"
}
}
1 change: 1 addition & 0 deletions packages/drizzledb-pg/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { db } from './postgresjs-client.js'
19 changes: 19 additions & 0 deletions packages/drizzledb-pg/src/client/postgresjs-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { parseEnv, z } from 'znv'

import 'dotenv/config'

const { DB_CONNECTION_STRING, DB_MAX_CONNECTIONS } = parseEnv(process.env, {
DB_CONNECTION_STRING: z.string().url().default('postgresql://undefined'),
DB_MAX_CONNECTIONS: z.number().min(1).default(1),
})

export const connection = postgres(DB_CONNECTION_STRING, {
max: DB_MAX_CONNECTIONS,
connection: {
application_name: 'postgres.js',
},
})

export const db = drizzle(connection)
17 changes: 17 additions & 0 deletions packages/drizzledb-pg/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { migrate } from 'drizzle-orm/postgres-js/migrator'

import { connection, db } from './client/postgresjs-client.js'

try {
console.log('Drizzle database migration starting...')

await migrate(db, {
migrationsFolder: './drizzle',
})

console.log('Drizzle database migration completed successfully!')
} catch (err) {
console.log('Drizzle database migrations failed!', err)
} finally {
await connection.end()
}
1 change: 1 addition & 0 deletions packages/drizzledb-pg/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { count } from 'drizzle-orm'
6 changes: 6 additions & 0 deletions packages/drizzledb-pg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./node_modules/@toolchain/typescript-config/tsconfig-base.json",
"compilerOptions": {
"types": ["vitest/globals"]
}
}
15 changes: 15 additions & 0 deletions packages/drizzledb-pg/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// eslint-disable-next-line import/no-extraneous-dependencies -- (as designed, vite is hoisted and available globally)
import { defineConfig, mergeConfig } from 'vitest/config'

import sharedConfig from '@toolchain/vitest-config'

import packageJson from './package.json'

export default mergeConfig(
sharedConfig,
defineConfig({
test: {
name: packageJson.name,
},
}),
)
Loading

0 comments on commit f142514

Please sign in to comment.