feat: add drizzle schema for user_settings
This commit is contained in:
parent
bd26ba77e6
commit
cf790c60df
8 changed files with 108 additions and 1 deletions
9
drizzle.config.ts
Normal file
9
drizzle.config.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import process from 'node:process'
|
||||||
|
import { defineConfig } from 'drizzle-kit'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
schema: './src/db/schema.ts',
|
||||||
|
out: './src/db/migrations',
|
||||||
|
dialect: 'postgresql',
|
||||||
|
dbCredentials: { url: process.env.DATABASE_URL ?? '' },
|
||||||
|
})
|
||||||
|
|
@ -6,7 +6,7 @@ export default antfu({
|
||||||
quotes: 'single',
|
quotes: 'single',
|
||||||
},
|
},
|
||||||
typescript: true,
|
typescript: true,
|
||||||
ignores: ['.omc'],
|
ignores: ['.omc', 'src/db/migrations'],
|
||||||
rules: {
|
rules: {
|
||||||
'no-console': 'off',
|
'no-console': 'off',
|
||||||
'antfu/no-top-level-await': 'off',
|
'antfu/no-top-level-await': 'off',
|
||||||
|
|
|
||||||
6
src/db/migrate.ts
Normal file
6
src/db/migrate.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { migrate } from 'drizzle-orm/node-postgres/migrator'
|
||||||
|
import { db, pool } from '@/lib/db.js'
|
||||||
|
|
||||||
|
await migrate(db, { migrationsFolder: './src/db/migrations' })
|
||||||
|
await pool.end()
|
||||||
|
console.log('migrations applied')
|
||||||
6
src/db/migrations/0000_equal_steel_serpent.sql
Normal file
6
src/db/migrations/0000_equal_steel_serpent.sql
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TABLE "user_settings" (
|
||||||
|
"user_id" bigint PRIMARY KEY NOT NULL,
|
||||||
|
"version" integer DEFAULT 1 NOT NULL,
|
||||||
|
"data" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
59
src/db/migrations/meta/0000_snapshot.json
Normal file
59
src/db/migrations/meta/0000_snapshot.json
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
{
|
||||||
|
"id": "fe121b40-b985-49f0-a36c-b2955b766990",
|
||||||
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
|
"version": "7",
|
||||||
|
"dialect": "postgresql",
|
||||||
|
"tables": {
|
||||||
|
"public.user_settings": {
|
||||||
|
"name": "user_settings",
|
||||||
|
"schema": "",
|
||||||
|
"columns": {
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "bigint",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true
|
||||||
|
},
|
||||||
|
"version": {
|
||||||
|
"name": "version",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"name": "data",
|
||||||
|
"type": "jsonb",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"default": "'{}'::jsonb"
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "timestamp with time zone",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"default": "now()"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"policies": {},
|
||||||
|
"checkConstraints": {},
|
||||||
|
"isRLSEnabled": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"enums": {},
|
||||||
|
"schemas": {},
|
||||||
|
"sequences": {},
|
||||||
|
"roles": {},
|
||||||
|
"policies": {},
|
||||||
|
"views": {},
|
||||||
|
"_meta": {
|
||||||
|
"columns": {},
|
||||||
|
"schemas": {},
|
||||||
|
"tables": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/db/migrations/meta/_journal.json
Normal file
13
src/db/migrations/meta/_journal.json
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"version": "7",
|
||||||
|
"dialect": "postgresql",
|
||||||
|
"entries": [
|
||||||
|
{
|
||||||
|
"idx": 0,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1777423106134,
|
||||||
|
"tag": "0000_equal_steel_serpent",
|
||||||
|
"breakpoints": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
8
src/db/schema.ts
Normal file
8
src/db/schema.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { bigint, integer, jsonb, pgTable, timestamp } from 'drizzle-orm/pg-core'
|
||||||
|
|
||||||
|
export const userSettings = pgTable('user_settings', {
|
||||||
|
userId: bigint('user_id', { mode: 'bigint' }).primaryKey(),
|
||||||
|
version: integer('version').notNull().default(1),
|
||||||
|
data: jsonb('data').$type<Record<string, unknown>>().notNull().default({}),
|
||||||
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
})
|
||||||
6
src/lib/db.ts
Normal file
6
src/lib/db.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { drizzle } from 'drizzle-orm/node-postgres'
|
||||||
|
import { Pool } from 'pg'
|
||||||
|
import { config } from '@/shared/config.js'
|
||||||
|
|
||||||
|
export const pool = new Pool({ connectionString: config.databaseUrl })
|
||||||
|
export const db = drizzle(pool)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue