🐛 add env to useDb and apply prettier
tinyedge/deploy Deployed by TinyEdge

This commit is contained in:
2026-08-07 13:28:26 +02:00
parent 67727f96ab
commit cbe1b42f6e
54 changed files with 5492 additions and 2406 deletions
+12 -11
View File
@@ -1,17 +1,18 @@
import { createClient } from '@libsql/client'
import { drizzle } from 'drizzle-orm/libsql'
import * as schema from './schema'
import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import * as schema from './schema';
import type { H3Event } from 'h3';
let instance: ReturnType<typeof drizzle<typeof schema>> | undefined
let instance: ReturnType<typeof drizzle<typeof schema>> | undefined;
export function useDb() {
export function useDb(event: H3Event) {
if (!instance) {
const config = useRuntimeConfig()
const config = useRuntimeConfig(event);
const client = createClient({
url: config.databaseUrl,
authToken: config.databaseAuthToken || undefined,
})
instance = drizzle(client, { schema })
url: config.database.url,
authToken: config.database.authToken || undefined,
});
instance = drizzle(client, { schema });
}
return instance
return instance;
}
@@ -25,9 +25,7 @@
"indexes": {
"expense_participants_expense_id_idx": {
"name": "expense_participants_expense_id_idx",
"columns": [
"expense_id"
],
"columns": ["expense_id"],
"isUnique": false
}
},
@@ -36,12 +34,8 @@
"name": "expense_participants_expense_id_expenses_id_fk",
"tableFrom": "expense_participants",
"tableTo": "expenses",
"columnsFrom": [
"expense_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["expense_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -49,12 +43,8 @@
"name": "expense_participants_participant_id_participants_id_fk",
"tableFrom": "expense_participants",
"tableTo": "participants",
"columnsFrom": [
"participant_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["participant_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -113,9 +103,7 @@
"indexes": {
"expenses_pot_slug_idx": {
"name": "expenses_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -124,12 +112,8 @@
"name": "expenses_pot_slug_pots_slug_fk",
"tableFrom": "expenses",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -137,12 +121,8 @@
"name": "expenses_payer_id_participants_id_fk",
"tableFrom": "expenses",
"tableTo": "participants",
"columnsFrom": [
"payer_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["payer_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -187,9 +167,7 @@
"indexes": {
"participants_pot_slug_idx": {
"name": "participants_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -198,12 +176,8 @@
"name": "participants_pot_slug_pots_slug_fk",
"tableFrom": "participants",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -262,4 +236,4 @@
"internal": {
"indexes": {}
}
}
}
@@ -25,9 +25,7 @@
"indexes": {
"expense_participants_expense_id_idx": {
"name": "expense_participants_expense_id_idx",
"columns": [
"expense_id"
],
"columns": ["expense_id"],
"isUnique": false
}
},
@@ -36,12 +34,8 @@
"name": "expense_participants_expense_id_expenses_id_fk",
"tableFrom": "expense_participants",
"tableTo": "expenses",
"columnsFrom": [
"expense_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["expense_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -49,12 +43,8 @@
"name": "expense_participants_participant_id_participants_id_fk",
"tableFrom": "expense_participants",
"tableTo": "participants",
"columnsFrom": [
"participant_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["participant_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -121,9 +111,7 @@
"indexes": {
"expenses_pot_slug_idx": {
"name": "expenses_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -132,12 +120,8 @@
"name": "expenses_pot_slug_pots_slug_fk",
"tableFrom": "expenses",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -145,12 +129,8 @@
"name": "expenses_payer_id_participants_id_fk",
"tableFrom": "expenses",
"tableTo": "participants",
"columnsFrom": [
"payer_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["payer_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -202,9 +182,7 @@
"indexes": {
"participants_pot_slug_idx": {
"name": "participants_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -213,12 +191,8 @@
"name": "participants_pot_slug_pots_slug_fk",
"tableFrom": "participants",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -277,4 +251,4 @@
"internal": {
"indexes": {}
}
}
}
@@ -25,9 +25,7 @@
"indexes": {
"expense_participants_expense_id_idx": {
"name": "expense_participants_expense_id_idx",
"columns": [
"expense_id"
],
"columns": ["expense_id"],
"isUnique": false
}
},
@@ -36,12 +34,8 @@
"name": "expense_participants_expense_id_expenses_id_fk",
"tableFrom": "expense_participants",
"tableTo": "expenses",
"columnsFrom": [
"expense_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["expense_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -49,12 +43,8 @@
"name": "expense_participants_participant_id_participants_id_fk",
"tableFrom": "expense_participants",
"tableTo": "participants",
"columnsFrom": [
"participant_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["participant_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -121,9 +111,7 @@
"indexes": {
"expenses_pot_slug_idx": {
"name": "expenses_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -132,12 +120,8 @@
"name": "expenses_pot_slug_pots_slug_fk",
"tableFrom": "expenses",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
},
@@ -145,12 +129,8 @@
"name": "expenses_payer_id_participants_id_fk",
"tableFrom": "expenses",
"tableTo": "participants",
"columnsFrom": [
"payer_id"
],
"columnsTo": [
"id"
],
"columnsFrom": ["payer_id"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -202,9 +182,7 @@
"indexes": {
"participants_pot_slug_idx": {
"name": "participants_pot_slug_idx",
"columns": [
"pot_slug"
],
"columns": ["pot_slug"],
"isUnique": false
}
},
@@ -213,12 +191,8 @@
"name": "participants_pot_slug_pots_slug_fk",
"tableFrom": "participants",
"tableTo": "pots",
"columnsFrom": [
"pot_slug"
],
"columnsTo": [
"slug"
],
"columnsFrom": ["pot_slug"],
"columnsTo": ["slug"],
"onDelete": "no action",
"onUpdate": "no action"
}
@@ -24,4 +24,4 @@
"breakpoints": true
}
]
}
}
+55 -33
View File
@@ -1,41 +1,63 @@
import { sql } from 'drizzle-orm'
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { sql } from 'drizzle-orm';
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
export const pots = sqliteTable('pots', {
slug: text('slug').primaryKey(),
name: text('name').notNull(),
currency: text('currency').notNull(),
createdAt: integer('created_at').notNull().default(sql`(unixepoch())`),
})
createdAt: integer('created_at')
.notNull()
.default(sql`(unixepoch())`),
});
export const participants = sqliteTable('participants', {
id: integer('id').primaryKey({ autoIncrement: true }),
potSlug: text('pot_slug').notNull().references(() => pots.slug),
name: text('name').notNull(),
paymentLink: text('payment_link'),
createdAt: integer('created_at').notNull().default(sql`(unixepoch())`),
}, table => [
index('participants_pot_slug_idx').on(table.potSlug),
])
export const participants = sqliteTable(
'participants',
{
id: integer('id').primaryKey({ autoIncrement: true }),
potSlug: text('pot_slug')
.notNull()
.references(() => pots.slug),
name: text('name').notNull(),
paymentLink: text('payment_link'),
createdAt: integer('created_at')
.notNull()
.default(sql`(unixepoch())`),
},
(table) => [index('participants_pot_slug_idx').on(table.potSlug)],
);
export const expenses = sqliteTable('expenses', {
id: integer('id').primaryKey({ autoIncrement: true }),
potSlug: text('pot_slug').notNull().references(() => pots.slug),
// 'payment' rows are settlements: payerId is who paid, and the single row
// in expenseParticipants is who received it — same shape as a one-person
// split expense, which is mathematically what a settlement is.
type: text('type').notNull().default('expense'),
description: text('description').notNull(),
amountCents: integer('amount_cents').notNull(),
payerId: integer('payer_id').notNull().references(() => participants.id),
createdAt: integer('created_at').notNull().default(sql`(unixepoch())`),
}, table => [
index('expenses_pot_slug_idx').on(table.potSlug),
])
export const expenses = sqliteTable(
'expenses',
{
id: integer('id').primaryKey({ autoIncrement: true }),
potSlug: text('pot_slug')
.notNull()
.references(() => pots.slug),
// 'payment' rows are settlements: payerId is who paid, and the single row
// in expenseParticipants is who received it — same shape as a one-person
// split expense, which is mathematically what a settlement is.
type: text('type').notNull().default('expense'),
description: text('description').notNull(),
amountCents: integer('amount_cents').notNull(),
payerId: integer('payer_id')
.notNull()
.references(() => participants.id),
createdAt: integer('created_at')
.notNull()
.default(sql`(unixepoch())`),
},
(table) => [index('expenses_pot_slug_idx').on(table.potSlug)],
);
export const expenseParticipants = sqliteTable('expense_participants', {
expenseId: integer('expense_id').notNull().references(() => expenses.id),
participantId: integer('participant_id').notNull().references(() => participants.id),
}, table => [
index('expense_participants_expense_id_idx').on(table.expenseId),
])
export const expenseParticipants = sqliteTable(
'expense_participants',
{
expenseId: integer('expense_id')
.notNull()
.references(() => expenses.id),
participantId: integer('participant_id')
.notNull()
.references(() => participants.id),
},
(table) => [index('expense_participants_expense_id_idx').on(table.expenseId)],
);