diff --git a/packages/database/.gitignore b/packages/database/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/packages/database/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/packages/database/README.md b/packages/database/README.md new file mode 100644 index 0000000..192014d --- /dev/null +++ b/packages/database/README.md @@ -0,0 +1 @@ +# @boring.tools/database diff --git a/packages/database/drizzle.config.ts b/packages/database/drizzle.config.ts new file mode 100644 index 0000000..cca18a7 --- /dev/null +++ b/packages/database/drizzle.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from 'drizzle-kit' +export default defineConfig({ + schema: './src/schema/index.ts', + out: './src/migrations', + dialect: 'postgresql', + dbCredentials: { + url: process.env.POSTGRES_URL as string, + }, + verbose: true, + strict: true, +}) diff --git a/packages/database/package.json b/packages/database/package.json new file mode 100644 index 0000000..f7690c9 --- /dev/null +++ b/packages/database/package.json @@ -0,0 +1,23 @@ +{ + "name": "@boring.tools/database", + "module": "src/index.ts", + "type": "module", + "scripts": { + "db:push": "drizzle-kit push", + "db:studio": "drizzle-kit studio", + "db:generate": "drizzle-kit generate", + "db:migrate": "drizzle-kit migrate" + }, + "devDependencies": { + "@types/bun": "latest", + "@types/pg": "^8.11.10", + "drizzle-kit": "^0.24.2" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "drizzle-orm": "^0.33.0", + "pg": "^8.13.0" + } +} diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts new file mode 100644 index 0000000..5d3f655 --- /dev/null +++ b/packages/database/src/index.ts @@ -0,0 +1,12 @@ +import { drizzle } from 'drizzle-orm/postgres-js' +import { Client } from 'pg' + +import * as schema from './schema' +export * from './schema' + +const POSTGRES_URL = import.meta.env.POSTGRES_URL ?? process.env.POSTGRES_URL + +const client = new Client({ connectionString: POSTGRES_URL }) +await client.connect() + +export const db = drizzle(client, { schema }) diff --git a/packages/database/src/schema/accessToken.ts b/packages/database/src/schema/accessToken.ts new file mode 100644 index 0000000..f3aa7ca --- /dev/null +++ b/packages/database/src/schema/accessToken.ts @@ -0,0 +1,24 @@ +import { relations } from 'drizzle-orm' +import { pgTable, text, timestamp, uuid, varchar } from 'drizzle-orm/pg-core' +import { user } from '.' + +export const accessToken = pgTable('accessToken', { + id: uuid('id').primaryKey().defaultRandom(), + userId: varchar('userId', { length: 32 }).references(() => user.id, { + onDelete: 'cascade', + }), + token: text('token').notNull(), + name: text('name').notNull(), + createdAt: timestamp('createdAt').defaultNow().notNull(), + lastUsedOn: timestamp('lastUsedOn'), +}) + +export const accessTokenRelation = relations(accessToken, ({ one }) => ({ + user: one(user, { + fields: [accessToken.userId], + references: [user.id], + }), +})) + +export type AccessTokenSelect = typeof accessToken.$inferSelect +export type AccessTokenInsert = typeof accessToken.$inferInsert diff --git a/packages/database/src/schema/index.ts b/packages/database/src/schema/index.ts new file mode 100644 index 0000000..6782b75 --- /dev/null +++ b/packages/database/src/schema/index.ts @@ -0,0 +1,2 @@ +export * from './user' +export * from './accessToken' diff --git a/packages/database/src/schema/user.ts b/packages/database/src/schema/user.ts new file mode 100644 index 0000000..50d446e --- /dev/null +++ b/packages/database/src/schema/user.ts @@ -0,0 +1,16 @@ +import { relations } from 'drizzle-orm' +import { pgTable, text, timestamp, varchar } from 'drizzle-orm/pg-core' +import { accessToken } from '.' + +export const user = pgTable('user', { + id: varchar('id', { length: 32 }).primaryKey(), // Clerk User Id + name: text('name'), + email: text('email').notNull().unique(), +}) + +export const userRelation = relations(user, ({ many }) => ({ + accessTokens: many(accessToken), +})) + +export type UserSelect = typeof user.$inferSelect +export type UserInsert = typeof user.$inferInsert diff --git a/packages/database/tsconfig.json b/packages/database/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/packages/database/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}