From fe515b3c1bc6f14bf427ce03939ec77d3087e7a9 Mon Sep 17 00:00:00 2001 From: Joppe Koers Date: Thu, 27 Jul 2023 14:44:15 +0200 Subject: [PATCH] Upcert instead of insert --- frontend/src/hooks.server.ts | 2 +- frontend/src/lib/server/db.ts | 11 ++++++++--- frontend/src/lib/server/schemas.ts | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/frontend/src/hooks.server.ts b/frontend/src/hooks.server.ts index c46ac1a..a3ea627 100644 --- a/frontend/src/hooks.server.ts +++ b/frontend/src/hooks.server.ts @@ -21,7 +21,7 @@ async function setupDBSingleton() { CREATE TABLE IF NOT EXISTS auth_user (id TEXT PRIMARY KEY, username TEXT, apikey TEXT); CREATE TABLE IF NOT EXISTS auth_key (id TEXT PRIMARY KEY, user_id TEXT REFERENCES auth_user(id) NOT NULL, primary_key BOOLEAN NOT NULL, hashed_password TEXT, expires BIGINT); CREATE TABLE IF NOT EXISTS auth_session (id TEXT PRIMARY KEY, user_id TEXT REFERENCES auth_user(id) NOT NULL, active_expires BIGINT NOT NULL, idle_expires BIGINT NOT NULL); -CREATE TABLE IF NOT EXISTS settings (settings json NOT NULL); +CREATE TABLE IF NOT EXISTS settings (id integer DEFAULT 1, settings json NOT NULL); ` await new Promise(resolve => { pool.query(schema, err => { diff --git a/frontend/src/lib/server/db.ts b/frontend/src/lib/server/db.ts index 546ff4c..591f41b 100644 --- a/frontend/src/lib/server/db.ts +++ b/frontend/src/lib/server/db.ts @@ -2,7 +2,7 @@ import postgres from 'pg' import { privateEnv } from '../../privateEnv' import { Settings, User, settings, user } from './schemas' import { drizzle } from 'drizzle-orm/node-postgres' -import { eq } from 'drizzle-orm' +import { eq, sql } from 'drizzle-orm' export const pool = new postgres.Pool({ connectionString: privateEnv.postgresUrl @@ -28,11 +28,16 @@ export const DB = { ...setting } const parse = await Settings.safeParseAsync(setting) - if (parse.success) { + if (!parse.success) { + return parse.error + } + // Make sure that we only ever have one row + const count = await db.select({ count: sql`count(*)` }).from(settings) + if (count.at(0)?.count === 0) { await db.insert(settings).values({ settings: newSettings }).execute() return } - return parse.error + await db.update(settings).set({ settings: newSettings }).where(eq(settings.id, 1)) } }, user: { diff --git a/frontend/src/lib/server/schemas.ts b/frontend/src/lib/server/schemas.ts index 6efbbde..0dead0c 100644 --- a/frontend/src/lib/server/schemas.ts +++ b/frontend/src/lib/server/schemas.ts @@ -1,5 +1,5 @@ import type { InferModel } from 'drizzle-orm' -import { bigint, boolean, pgTable, text, varchar, json } from 'drizzle-orm/pg-core' +import { bigint, boolean, pgTable, text, varchar, json, int, integer } from 'drizzle-orm/pg-core' import { createInsertSchema } from 'drizzle-zod' import { z } from 'zod' @@ -44,6 +44,7 @@ export type UserAttributes = Omit export type NewUser = InferModel export const settings = pgTable('settings', { + id: integer('id').primaryKey().default(1), settings: json('settings').$type().notNull() }) export const Settings = z.object({