mirror of
https://github.com/KevinMidboe/infra-map.git
synced 2025-12-08 20:29:05 +00:00
separated db & function code (filament)
This commit is contained in:
@@ -1,91 +1,5 @@
|
|||||||
import { currentFilament } from './filament';
|
|
||||||
import pg from 'pg';
|
|
||||||
import { env } from '$env/dynamic/private';
|
|
||||||
import type { Filament } from '$lib/interfaces/printer';
|
import type { Filament } from '$lib/interfaces/printer';
|
||||||
|
import { getDb } from '../database';
|
||||||
const { Pool } = pg;
|
|
||||||
|
|
||||||
let pool: InstanceType<typeof Pool> | undefined;
|
|
||||||
|
|
||||||
async function initDb() {
|
|
||||||
if (pool) return pool;
|
|
||||||
|
|
||||||
pool = new Pool({
|
|
||||||
connectionString: env.DATABASE_URL // e.g. postgres://user:pass@localhost:5432/mydb
|
|
||||||
});
|
|
||||||
|
|
||||||
const client = await pool.connect();
|
|
||||||
try {
|
|
||||||
await client.query('BEGIN');
|
|
||||||
|
|
||||||
for (const stmt of schemas) {
|
|
||||||
await client.query(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
await client.query('COMMIT');
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error('Failed to create tables:', err.message);
|
|
||||||
await client.query('ROLLBACK');
|
|
||||||
throw err;
|
|
||||||
} finally {
|
|
||||||
client.release();
|
|
||||||
}
|
|
||||||
|
|
||||||
return pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
const schemas = [
|
|
||||||
`
|
|
||||||
CREATE TABLE IF NOT EXISTS filament (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
hex TEXT NOT NULL,
|
|
||||||
color TEXT NOT NULL,
|
|
||||||
material TEXT,
|
|
||||||
weight REAL,
|
|
||||||
link TEXT,
|
|
||||||
added INTEGER, -- epoch seconds
|
|
||||||
updated INTEGER, -- epoch seconds
|
|
||||||
UNIQUE (hex, updated)
|
|
||||||
)
|
|
||||||
`
|
|
||||||
];
|
|
||||||
|
|
||||||
async function seedData(pool: InstanceType<typeof Pool>) {
|
|
||||||
const baseTimestamp = Math.floor(new Date('2025-04-01T05:47:01+00:00').getTime() / 1000);
|
|
||||||
const filaments = currentFilament();
|
|
||||||
|
|
||||||
const client = await pool.connect();
|
|
||||||
try {
|
|
||||||
await client.query('BEGIN');
|
|
||||||
|
|
||||||
for (const f of filaments) {
|
|
||||||
await client.query(
|
|
||||||
`INSERT INTO filament (hex, color, material, weight, link, added, updated)
|
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
||||||
ON CONFLICT (hex, updated) DO NOTHING`,
|
|
||||||
[f.hex, f.color, f.material, f.weight, f.link, baseTimestamp, baseTimestamp]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await client.query('COMMIT');
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error('Failed to seed data:', err.message);
|
|
||||||
await client.query('ROLLBACK');
|
|
||||||
throw err;
|
|
||||||
} finally {
|
|
||||||
client.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export helper to use db elsewhere
|
|
||||||
async function getDb() {
|
|
||||||
if (pool) return pool;
|
|
||||||
|
|
||||||
const p = await initDb();
|
|
||||||
await seedData(p);
|
|
||||||
console.log('Database setup and seeding complete!');
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getAllFilament(): Promise<Array<Filament>> {
|
export async function getAllFilament(): Promise<Array<Filament>> {
|
||||||
const pool = await getDb();
|
const pool = await getDb();
|
||||||
100
src/lib/server/database/index.ts
Normal file
100
src/lib/server/database/index.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import pg from 'pg';
|
||||||
|
import { env } from '$env/dynamic/private';
|
||||||
|
import type { Filament } from '$lib/interfaces/printer';
|
||||||
|
|
||||||
|
const { Pool } = pg;
|
||||||
|
|
||||||
|
let pool: InstanceType<typeof Pool> | undefined;
|
||||||
|
|
||||||
|
async function initDb() {
|
||||||
|
if (pool) return pool;
|
||||||
|
|
||||||
|
pool = new Pool({
|
||||||
|
connectionString: env.DATABASE_URL // e.g. postgres://user:pass@localhost:5432/mydb
|
||||||
|
});
|
||||||
|
|
||||||
|
const client = await pool.connect();
|
||||||
|
try {
|
||||||
|
await client.query('BEGIN');
|
||||||
|
|
||||||
|
for (const stmt of schemas) {
|
||||||
|
await client.query(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.query('COMMIT');
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to create tables:', err.message);
|
||||||
|
await client.query('ROLLBACK');
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
const schemas = [
|
||||||
|
`
|
||||||
|
CREATE TABLE IF NOT EXISTS filament (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
hex TEXT NOT NULL,
|
||||||
|
color TEXT NOT NULL,
|
||||||
|
material TEXT,
|
||||||
|
weight REAL,
|
||||||
|
link TEXT,
|
||||||
|
added INTEGER, -- epoch seconds
|
||||||
|
updated INTEGER, -- epoch seconds
|
||||||
|
UNIQUE (hex, updated)
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
CREATE TABLE IF NOT EXISTS site (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
color TEXT NOT NULL,
|
||||||
|
image TEXT,
|
||||||
|
link TEXT,
|
||||||
|
background TEXT,
|
||||||
|
created INTEGER, -- epoch seconds
|
||||||
|
updated INTEGER, -- epoch seconds
|
||||||
|
UNIQUE (name, updated)
|
||||||
|
)
|
||||||
|
`
|
||||||
|
];
|
||||||
|
|
||||||
|
async function seedFilament(pool: InstanceType<typeof Pool>) {
|
||||||
|
const baseTimestamp = Math.floor(new Date('2025-04-01T05:47:01+00:00').getTime() / 1000);
|
||||||
|
const filaments: Filament[] = []; // disables seed
|
||||||
|
|
||||||
|
const client = await pool.connect();
|
||||||
|
try {
|
||||||
|
await client.query('BEGIN');
|
||||||
|
|
||||||
|
for (const f of filaments) {
|
||||||
|
await client.query(
|
||||||
|
`INSERT INTO filament (hex, color, material, weight, link, added, updated)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
|
ON CONFLICT (hex, updated) DO NOTHING`,
|
||||||
|
[f.hex, f.color, f.material, f.weight, f.link, baseTimestamp, baseTimestamp]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.query('COMMIT');
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to seed data:', err.message);
|
||||||
|
await client.query('ROLLBACK');
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export helper to use db elsewhere
|
||||||
|
export async function getDb() {
|
||||||
|
if (pool) return pool;
|
||||||
|
|
||||||
|
const p = await initDb();
|
||||||
|
await seedFilament(p);
|
||||||
|
console.log('Database setup and seeding complete!');
|
||||||
|
return p;
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
import { fetchP1P } from '$lib/server/homeassistant';
|
import { fetchP1P } from '$lib/server/homeassistant';
|
||||||
import { getAllFilament } from '$lib/server/database';
|
import { getAllFilament } from '$lib/server/database/filament';
|
||||||
import type { Filament } from '$lib/interfaces/printer';
|
import type { Filament } from '$lib/interfaces/printer';
|
||||||
|
|
||||||
interface PrinterState {
|
interface PrinterState {
|
||||||
|
|||||||
@@ -46,6 +46,7 @@
|
|||||||
let open = $state(false);
|
let open = $state(false);
|
||||||
let timeLeftInterval: ReturnType<typeof setInterval>;
|
let timeLeftInterval: ReturnType<typeof setInterval>;
|
||||||
|
|
||||||
|
console.log("got data:", data)
|
||||||
const rawFilament: Filament[] = data?.filament || [];
|
const rawFilament: Filament[] = data?.filament || [];
|
||||||
let filament = $derived(
|
let filament = $derived(
|
||||||
rawFilament
|
rawFilament
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { addFilament, updateFilament } from '$lib/server/database';
|
import { addFilament, updateFilament } from '$lib/server/database/filament';
|
||||||
import { json } from '@sveltejs/kit';
|
import { json } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const PUT: RequestHandler = async ({ params, request }) => {
|
export const PUT: RequestHandler = async ({ params, request }) => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
import { getFilamentByColor } from '$lib/server/database';
|
import { getFilamentByColor } from '$lib/server/database/filament';
|
||||||
|
|
||||||
export const load = async ({ params }: Parameters<PageServerLoad>[0]) => {
|
export const load = async ({ params }: Parameters<PageServerLoad>[0]) => {
|
||||||
let { id } = params;
|
let { id } = params;
|
||||||
|
|||||||
Reference in New Issue
Block a user