Skip to content

Commit

Permalink
Upcert instead of insert
Browse files Browse the repository at this point in the history
  • Loading branch information
SirMorfield committed Jul 27, 2023
1 parent ce7bc15 commit fe515b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontend/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(resolve => {
pool.query(schema, err => {
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/lib/server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<number>`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: {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/server/schemas.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -44,6 +44,7 @@ export type UserAttributes = Omit<User, 'id'>
export type NewUser = InferModel<typeof user, 'insert'>

export const settings = pgTable('settings', {
id: integer('id').primaryKey().default(1),
settings: json('settings').$type<Settings>().notNull()
})
export const Settings = z.object({
Expand Down

0 comments on commit fe515b3

Please sign in to comment.