feat: create db connector for drizzle and generate schema

This commit is contained in:
Nicolas Meienberger 2023-04-15 15:51:29 +02:00 committed by Nicolas Meienberger
parent 19da1623a1
commit bf89c24702
2 changed files with 60 additions and 0 deletions

13
src/server/db/index.ts Normal file
View file

@ -0,0 +1,13 @@
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import { getConfig } from '../core/TipiConfig/TipiConfig';
const connectionString = `postgresql://${getConfig().postgresUsername}:${getConfig().postgresPassword}@${getConfig().postgresHost}:${getConfig().postgresPort}/${
getConfig().postgresDatabase
}?connect_timeout=300`;
const pool = new Pool({
connectionString,
});
export const db = drizzle(pool);

47
src/server/db/schema.ts Normal file
View file

@ -0,0 +1,47 @@
import { InferModel } from 'drizzle-orm';
import { pgTable, pgEnum, integer, varchar, timestamp, serial, boolean, text, jsonb } from 'drizzle-orm/pg-core';
export const updateStatusEnum = pgEnum('update_status_enum', ['SUCCESS', 'FAILED']);
export const appStatusEnum = pgEnum('app_status_enum', ['updating', 'missing', 'starting', 'stopping', 'uninstalling', 'installing', 'stopped', 'running']);
export const migrations = pgTable('migrations', {
id: integer('id').notNull(),
name: varchar('name', { length: 100 }).notNull(),
hash: varchar('hash', { length: 40 }).notNull(),
executedAt: timestamp('executed_at', { mode: 'string' }).defaultNow(),
});
export const userTable = pgTable('user', {
id: serial('id').notNull(),
username: varchar('username').notNull(),
password: varchar('password').notNull(),
createdAt: timestamp('createdAt', { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updatedAt', { mode: 'string' }).defaultNow().notNull(),
operator: boolean('operator').default(false).notNull(),
totpSecret: text('totp_secret'),
totpEnabled: boolean('totp_enabled').default(false).notNull(),
salt: text('salt'),
});
export type User = InferModel<typeof userTable>;
export const update = pgTable('update', {
id: serial('id').notNull(),
name: varchar('name').notNull(),
status: updateStatusEnum('status').notNull(),
createdAt: timestamp('createdAt', { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updatedAt', { mode: 'string' }).defaultNow().notNull(),
});
export const appTable = pgTable('app', {
id: varchar('id').notNull(),
status: appStatusEnum('status').default('stopped').notNull(),
lastOpened: timestamp('lastOpened', { withTimezone: true, mode: 'string' }).defaultNow(),
numOpened: integer('numOpened').notNull(),
config: jsonb('config').notNull(),
createdAt: timestamp('createdAt', { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updatedAt', { mode: 'string' }).defaultNow().notNull(),
version: integer('version').default(1).notNull(),
exposed: boolean('exposed').notNull(),
domain: varchar('domain'),
});
export type App = InferModel<typeof appTable>;