mirror of
https://github.com/KevinMidboe/infra-map.git
synced 2025-10-29 17:40:28 +00:00
filament uses postgres
This commit is contained in:
@@ -2,7 +2,7 @@ export interface Filament {
|
||||
hex: string;
|
||||
color: string;
|
||||
material: string;
|
||||
weight: string;
|
||||
weight: number;
|
||||
count: number;
|
||||
link: string;
|
||||
created: number;
|
||||
|
||||
@@ -1,108 +1,104 @@
|
||||
import { currentFilament } from './filament';
|
||||
|
||||
import { open } from 'sqlite';
|
||||
import sqlite3 from 'sqlite3';
|
||||
import { resolve } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname } from 'path';
|
||||
import pg from 'pg';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import type { Filament } from '$lib/interfaces/printer';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const dbPath = resolve(__dirname, '../../../db.sqlite');
|
||||
const { Pool } = pg;
|
||||
|
||||
let db;
|
||||
let pool: InstanceType<typeof Pool> | undefined;
|
||||
|
||||
async function initDb() {
|
||||
const db = await open({
|
||||
filename: dbPath,
|
||||
driver: sqlite3.Database
|
||||
if (pool) return pool;
|
||||
|
||||
pool = new Pool({
|
||||
connectionString: env.DATABASE_URL // e.g. postgres://user:pass@localhost:5432/mydb
|
||||
});
|
||||
|
||||
// Transaction to run schemas
|
||||
await db.exec('BEGIN TRANSACTION');
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
for (const stmt of schemas) {
|
||||
await db.run(stmt);
|
||||
await client.query(stmt);
|
||||
}
|
||||
await db.exec('COMMIT');
|
||||
} catch (err) {
|
||||
|
||||
await client.query('COMMIT');
|
||||
} catch (err: any) {
|
||||
console.error('Failed to create tables:', err.message);
|
||||
await db.exec('ROLLBACK');
|
||||
await client.query('ROLLBACK');
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
|
||||
return db;
|
||||
return pool;
|
||||
}
|
||||
|
||||
const schemas = [
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS filament (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
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
|
||||
updated INTEGER, -- epoch seconds
|
||||
UNIQUE (hex, updated)
|
||||
)
|
||||
`
|
||||
];
|
||||
|
||||
async function seedData(db) {
|
||||
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 stmt = await db.prepare(`
|
||||
INSERT OR IGNORE INTO filament (hex, color, material, weight, link, added, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
await db.exec('BEGIN TRANSACTION');
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
for (const f of filaments) {
|
||||
const existing = await db.get('SELECT 1 FROM filament WHERE hex = ? AND updated = ?', [
|
||||
f.hex,
|
||||
baseTimestamp
|
||||
]);
|
||||
await client.query('BEGIN');
|
||||
|
||||
if (!existing) {
|
||||
await db.run(
|
||||
`INSERT INTO filament (hex, color, material, weight, link, added, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[f.hex, f.color, f.material, f.weight, f.link, baseTimestamp, baseTimestamp]
|
||||
);
|
||||
}
|
||||
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 db.exec('COMMIT');
|
||||
} catch (err) {
|
||||
await client.query('COMMIT');
|
||||
} catch (err: any) {
|
||||
console.error('Failed to seed data:', err.message);
|
||||
await db.exec('ROLLBACK');
|
||||
await client.query('ROLLBACK');
|
||||
throw err;
|
||||
} finally {
|
||||
await stmt.finalize();
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
// Export helper to use db elsewhere
|
||||
async function getDb() {
|
||||
if (db !== undefined) return db;
|
||||
if (pool) return pool;
|
||||
|
||||
db = await initDb();
|
||||
await seedData(db);
|
||||
const p = await initDb();
|
||||
await seedData(p);
|
||||
console.log('Database setup and seeding complete!');
|
||||
return p;
|
||||
}
|
||||
|
||||
export async function getAllFilament(): Promise<Array<Filament>> {
|
||||
const db = await getDb();
|
||||
const result = await db?.all('SELECT * FROM filament');
|
||||
return result || [];
|
||||
const pool = await getDb();
|
||||
const result = await pool.query('SELECT * FROM filament');
|
||||
return result.rows || [];
|
||||
}
|
||||
|
||||
export async function getFilamentByColor(name: string) {
|
||||
const db = await getDb();
|
||||
const result = await db?.get('SELECT * FROM filament WHERE LOWER(color) = ?', [name]);
|
||||
return result || undefined;
|
||||
const pool = await getDb();
|
||||
const result = await pool.query('SELECT * FROM filament WHERE LOWER(color) = LOWER($1) LIMIT 1', [
|
||||
name
|
||||
]);
|
||||
return result.rows[0] || undefined;
|
||||
}
|
||||
|
||||
export async function addFilament(
|
||||
@@ -114,22 +110,62 @@ export async function addFilament(
|
||||
) {
|
||||
const timestamp = Math.floor(new Date().getTime() / 1000);
|
||||
|
||||
const db = await getDb();
|
||||
const result = await db.run(
|
||||
const pool = await getDb();
|
||||
const result = await pool.query(
|
||||
`INSERT INTO filament (hex, color, material, weight, link, added, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id`,
|
||||
[hex, color, material, weight, link, timestamp, timestamp]
|
||||
);
|
||||
return { id: result.lastID };
|
||||
return { id: result.rows[0].id };
|
||||
}
|
||||
|
||||
export async function updatefilament({ id, make, model, year }) {
|
||||
const db = await getDb();
|
||||
await db.run(
|
||||
'UPDATE filaments SET make = ?, model = ?, year = ? WHERE id = ?',
|
||||
make,
|
||||
model,
|
||||
year,
|
||||
id
|
||||
);
|
||||
export async function updateFilament({
|
||||
id,
|
||||
hex,
|
||||
color,
|
||||
material,
|
||||
weight,
|
||||
link
|
||||
}: {
|
||||
id: number;
|
||||
hex?: string;
|
||||
color?: string;
|
||||
material?: string;
|
||||
weight?: number;
|
||||
link?: string;
|
||||
}) {
|
||||
const pool = await getDb();
|
||||
|
||||
// Dynamically build query based on provided fields
|
||||
const fields = [];
|
||||
const values = [];
|
||||
let i = 1;
|
||||
|
||||
if (hex !== undefined) {
|
||||
fields.push(`hex = $${i++}`);
|
||||
values.push(hex);
|
||||
}
|
||||
if (color !== undefined) {
|
||||
fields.push(`color = $${i++}`);
|
||||
values.push(color);
|
||||
}
|
||||
if (material !== undefined) {
|
||||
fields.push(`material = $${i++}`);
|
||||
values.push(material);
|
||||
}
|
||||
if (weight !== undefined) {
|
||||
fields.push(`weight = $${i++}`);
|
||||
values.push(weight);
|
||||
}
|
||||
if (link !== undefined) {
|
||||
fields.push(`link = $${i++}`);
|
||||
values.push(link);
|
||||
}
|
||||
|
||||
if (fields.length === 0) return; // nothing to update
|
||||
|
||||
values.push(id);
|
||||
const query = `UPDATE filament SET ${fields.join(', ')}, updated = EXTRACT(EPOCH FROM NOW())::INT WHERE id = $${i}`;
|
||||
await pool.query(query, values);
|
||||
}
|
||||
|
||||
@@ -2,84 +2,84 @@ import type { Filament } from '$lib/interfaces/printer';
|
||||
|
||||
const filament: Filament[] = [
|
||||
{
|
||||
Hex: '#DD4344',
|
||||
Color: 'Scarlet Red',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 2,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742848731'
|
||||
hex: '#DD4344',
|
||||
color: 'Scarlet Red',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 2,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742848731'
|
||||
},
|
||||
{
|
||||
Hex: '#61C57F',
|
||||
Color: 'Grass Green',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 2,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742783195'
|
||||
hex: '#61C57F',
|
||||
color: 'Grass Green',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 2,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742783195'
|
||||
},
|
||||
{
|
||||
Hex: '#F7DA5A',
|
||||
Color: 'Lemon Yellow',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 2,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742717659'
|
||||
hex: '#F7DA5A',
|
||||
color: 'Lemon Yellow',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 2,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742717659'
|
||||
},
|
||||
{
|
||||
Hex: '#E8DBB7',
|
||||
Color: 'Desert Tan',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 1,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=48612736401756'
|
||||
hex: '#E8DBB7',
|
||||
color: 'Desert Tan',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 1,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=48612736401756'
|
||||
},
|
||||
{
|
||||
Hex: "url('https://www.transparenttextures.com/patterns/asfalt-dark.png'",
|
||||
Color: 'White Marble',
|
||||
Material: 'PLA Marble',
|
||||
Weight: '1kg',
|
||||
Count: 1,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/products/pla-marble?variant=43964050964699'
|
||||
hex: "url('https://www.transparenttextures.com/patterns/asfalt-dark.png'",
|
||||
color: 'White Marble',
|
||||
material: 'PLA Marble',
|
||||
weight: 1,
|
||||
count: 1,
|
||||
link: 'https://eu.store.bambulab.com/en-no/products/pla-marble?variant=43964050964699'
|
||||
},
|
||||
{
|
||||
Hex: '#0078C0',
|
||||
Color: 'Marine Blue',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 1,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996751073499'
|
||||
hex: '#0078C0',
|
||||
color: 'Marine Blue',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 1,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996751073499'
|
||||
},
|
||||
{
|
||||
Hex: '#000000',
|
||||
Color: 'Charcoal',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 2,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742750427'
|
||||
hex: '#000000',
|
||||
color: 'Charcoal',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 2,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742750427'
|
||||
},
|
||||
{
|
||||
Hex: '#ffffff',
|
||||
Color: 'Ivory White',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 2,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742586587'
|
||||
hex: '#ffffff',
|
||||
color: 'Ivory White',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 2,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742586587'
|
||||
},
|
||||
{
|
||||
Hex: '#E8AFCE',
|
||||
Color: 'Sakura Pink',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 1,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742684891'
|
||||
hex: '#E8AFCE',
|
||||
color: 'Sakura Pink',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 1,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742684891'
|
||||
},
|
||||
{
|
||||
Hex: '#AE96D5',
|
||||
Color: 'Lilac Purple',
|
||||
Material: 'PLA Matte',
|
||||
Weight: '1kg',
|
||||
Count: 1,
|
||||
Link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742914267'
|
||||
hex: '#AE96D5',
|
||||
color: 'Lilac Purple',
|
||||
material: 'PLA Matte',
|
||||
weight: 1,
|
||||
count: 1,
|
||||
link: 'https://eu.store.bambulab.com/en-no/collections/pla/products/pla-matte?variant=42996742914267'
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
59
src/routes/printer/filament/+server.ts
Normal file
59
src/routes/printer/filament/+server.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { addFilament, updateFilament } from '$lib/server/database';
|
||||
import { json } from '@sveltejs/kit';
|
||||
|
||||
export const PUT: RequestHandler = async ({ params, request }) => {
|
||||
try {
|
||||
const id = Number(params.id);
|
||||
if (isNaN(id)) {
|
||||
return new Response(JSON.stringify({ error: 'Invalid id' }), { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
|
||||
await updateFilament({ id, ...body });
|
||||
|
||||
return json({ success: true });
|
||||
} catch (err: any) {
|
||||
console.log(err);
|
||||
console.error('Failed to update filament:', err.message);
|
||||
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
|
||||
status: 500
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
|
||||
// Extract values by input `name` attributes
|
||||
const hex = formData.get('Hex')?.toString().trim();
|
||||
const color = formData.get('Color name')?.toString().trim();
|
||||
const material = formData.get('Material')?.toString().trim();
|
||||
const weightStr = formData.get('Weight')?.toString().trim();
|
||||
const link = formData.get('Link')?.toString().trim();
|
||||
|
||||
if (!hex || !color || !material || !weightStr || !link) {
|
||||
return new Response(JSON.stringify({ error: 'All fields are required' }), { status: 400 });
|
||||
}
|
||||
|
||||
// convert "0.5 kg" → 0.5, "1 kg" → 1, etc
|
||||
const weight = parseFloat(weightStr);
|
||||
|
||||
if (!hex || !color) {
|
||||
return new Response(JSON.stringify({ error: 'hex and color are required' }), {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
await addFilament(hex, color, material, weight, link);
|
||||
|
||||
return Response.redirect(`${request.url}/${color}`, 303);
|
||||
} catch (err: any) {
|
||||
console.log(err);
|
||||
console.error('Failed to add filament:', err.message);
|
||||
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
|
||||
status: 500
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -8,6 +8,5 @@ export const load = async ({ params }: Parameters<PageServerLoad>[0]) => {
|
||||
}
|
||||
|
||||
const filament = await getFilamentByColor(id);
|
||||
console.log('fil:', filament);
|
||||
return { id, filament: filament };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user