252 lines
9.6 KiB
JavaScript
252 lines
9.6 KiB
JavaScript
const db = require('../../db.js');
|
|
|
|
// Fonctions de gestion de la base de données interne
|
|
|
|
async function create_database() {
|
|
const queries = [
|
|
`CREATE TABLE IF NOT EXISTS projects (
|
|
id SERIAL PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
description TEXT,
|
|
start_date DATE,
|
|
status INTEGER NOT NULL CHECK (status = ANY (ARRAY [0, 1, 2, 3]))
|
|
);`,
|
|
`ALTER TABLE projects OWNER TO timelapse;`,
|
|
`CREATE TABLE IF NOT EXISTS measurements (
|
|
id SERIAL PRIMARY KEY,
|
|
project_id INTEGER REFERENCES projects ON DELETE CASCADE,
|
|
timestamp TIMESTAMP NOT NULL,
|
|
path VARCHAR(255),
|
|
temperature DOUBLE PRECISION,
|
|
humidity DOUBLE PRECISION,
|
|
order_id INTEGER NOT NULL,
|
|
CONSTRAINT unique_project_photo_order UNIQUE (project_id, order_id)
|
|
);`,
|
|
`ALTER TABLE measurements OWNER TO timelapse;`,
|
|
`CREATE TABLE IF NOT EXISTS videos (
|
|
id SERIAL PRIMARY KEY,
|
|
project_id INTEGER REFERENCES projects ON DELETE CASCADE,
|
|
measurement_ids TEXT NOT NULL,
|
|
video_file VARCHAR(255),
|
|
resolution VARCHAR(255),
|
|
duration INTEGER,
|
|
status INTEGER NOT NULL CHECK (status = ANY (ARRAY [0, 1, 2, 3])),
|
|
name VARCHAR(255),
|
|
progress DOUBLE PRECISION,
|
|
started_at TIMESTAMP,
|
|
updated_at TIMESTAMP,
|
|
eta DOUBLE PRECISION
|
|
);`,
|
|
`ALTER TABLE videos OWNER TO timelapse;`,
|
|
`CREATE TABLE IF NOT EXISTS camera (
|
|
id SERIAL PRIMARY KEY,
|
|
interval INTEGER NOT NULL,
|
|
maintenance INTEGER NOT NULL,
|
|
active INTEGER DEFAULT 0 NOT NULL
|
|
);`,
|
|
`ALTER TABLE camera OWNER TO timelapse;`
|
|
];
|
|
|
|
try {
|
|
for (const query of queries) {
|
|
await db.query(query);
|
|
}
|
|
console.log('Database tables created or verified successfully.');
|
|
} catch (err) {
|
|
console.error('Error creating database tables:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function check_database_existence() {
|
|
const query = `
|
|
SELECT table_name
|
|
FROM information_schema.tables
|
|
WHERE table_schema = 'public'
|
|
AND table_name IN ('projects', 'measurements', 'videos', 'camera');
|
|
`;
|
|
|
|
try {
|
|
const result = await db.query(query);
|
|
const existingTables = result.rows.map(row => row.table_name);
|
|
|
|
const requiredTables = ['projects', 'measurements', 'videos', 'camera'];
|
|
const missingTables = requiredTables.filter(table => !existingTables.includes(table));
|
|
|
|
if (missingTables.length > 0) {
|
|
console.error('Missing or improperly constructed tables:', missingTables);
|
|
throw new Error(`The following tables are missing or not properly constructed: ${missingTables.join(', ')}`);
|
|
} else {
|
|
console.log('All required tables exist and are properly constructed.');
|
|
}
|
|
} catch (err) {
|
|
console.error('Error checking database tables:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function delete_database() {
|
|
const queries = [
|
|
`DROP TABLE IF EXISTS videos;`,
|
|
`DROP TABLE IF EXISTS measurements;`,
|
|
`DROP TABLE IF EXISTS projects;`,
|
|
`DROP TABLE IF EXISTS camera;`
|
|
];
|
|
|
|
try {
|
|
for (const query of queries) {
|
|
await db.query(query);
|
|
}
|
|
console.log('Database tables deleted successfully.');
|
|
} catch (err) {
|
|
console.error('Error deleting database tables:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function init_function() {
|
|
try {
|
|
await check_database_existence();
|
|
} catch (err) {
|
|
console.error('Database check failed:', err);
|
|
try {
|
|
await delete_database();
|
|
await create_database();
|
|
console.log('Database initialized successfully.');
|
|
} catch (err) {
|
|
console.error('Error initializing database:', err);
|
|
throw err;
|
|
}
|
|
} finally {
|
|
console.log('Database initialization process completed.');
|
|
}
|
|
}
|
|
|
|
init_function()
|
|
.then(() => console.log('Database initialization completed.'))
|
|
.catch(err => console.error('Error during database initialization:', err));
|
|
|
|
// Fonctions pour les projets
|
|
function handleDatabaseOperation(operation) {
|
|
return async (...args) => {
|
|
try {
|
|
return await operation(...args);
|
|
} catch (err) {
|
|
console.error(`Error during database operation: ${operation.name}`, err);
|
|
throw err;
|
|
}
|
|
};
|
|
}
|
|
|
|
const project = {
|
|
get_all_projects: handleDatabaseOperation(async () => {
|
|
const query = `SELECT * FROM projects;`;
|
|
return (await db.query(query)).rows;
|
|
}),
|
|
get_project_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `SELECT * FROM projects WHERE id = $1;`;
|
|
return (await db.query(query, [id])).rows[0];
|
|
}),
|
|
create_project: handleDatabaseOperation(async (name, description, start_date, status) => {
|
|
const query = `INSERT INTO projects (name, description, start_date, status) VALUES ($1, $2, $3, $4) RETURNING *;`;
|
|
return (await db.query(query, [name, description, start_date, status])).rows[0];
|
|
}),
|
|
edit_project_by_id: handleDatabaseOperation(async (id, updates) => {
|
|
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
|
const values = [id, ...Object.values(updates)];
|
|
const query = `UPDATE projects SET ${fields} WHERE id = $1 RETURNING *;`;
|
|
return (await db.query(query, values)).rows[0];
|
|
}),
|
|
delete_project_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `DELETE FROM projects WHERE id = $1;`;
|
|
await db.query(query, [id]);
|
|
})
|
|
};
|
|
|
|
const measurement = {
|
|
get_all_measurements: handleDatabaseOperation(async () => {
|
|
const query = `SELECT * FROM measurements;`;
|
|
return (await db.query(query)).rows;
|
|
}),
|
|
get_measurement_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `SELECT * FROM measurements WHERE id = $1;`;
|
|
return (await db.query(query, [id])).rows[0];
|
|
}),
|
|
get_measurement_by_project_and_order_id: handleDatabaseOperation(async (project_id, order_id) => {
|
|
const query = `SELECT * FROM measurements WHERE project_id = $1 AND order_id = $2;`;
|
|
return (await db.query(query, [project_id, order_id])).rows[0];
|
|
}),
|
|
get_measurements_by_project_id: handleDatabaseOperation(async (project_id) => {
|
|
const query = `SELECT * FROM measurements WHERE project_id = $1;`;
|
|
return (await db.query(query, [project_id])).rows;
|
|
}),
|
|
create_measurement: handleDatabaseOperation(async (project_id, timestamp, path, temperature, humidity, order_id) => {
|
|
const query = `INSERT INTO measurements (project_id, timestamp, path, temperature, humidity, order_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;`;
|
|
return (await db.query(query, [project_id, timestamp, path, temperature, humidity, order_id])).rows[0];
|
|
}),
|
|
edit_measurement_by_id: handleDatabaseOperation(async (id, updates) => {
|
|
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
|
const values = [id, ...Object.values(updates)];
|
|
const query = `UPDATE measurements SET ${fields} WHERE id = $1 RETURNING *;`;
|
|
return (await db.query(query, values)).rows[0];
|
|
}),
|
|
delete_measurement_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `DELETE FROM measurements WHERE id = $1;`;
|
|
await db.query(query, [id]);
|
|
})
|
|
};
|
|
|
|
const video = {
|
|
get_all_videos: handleDatabaseOperation(async () => {
|
|
const query = `SELECT * FROM videos;`;
|
|
return (await db.query(query)).rows;
|
|
}),
|
|
get_video_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `SELECT * FROM videos WHERE id = $1;`;
|
|
return (await db.query(query, [id])).rows[0];
|
|
}),
|
|
get_videos_by_project_id: handleDatabaseOperation(async (project_id) => {
|
|
const query = `SELECT * FROM videos WHERE project_id = $1;`;
|
|
return (await db.query(query, [project_id])).rows;
|
|
}),
|
|
create_video: handleDatabaseOperation(async (project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta) => {
|
|
const query = `INSERT INTO videos (project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;`;
|
|
return (await db.query(query, [project_id, measurement_ids, video_file, resolution, duration, status, name, progress, started_at, updated_at, eta])).rows[0];
|
|
}),
|
|
edit_video_by_id: handleDatabaseOperation(async (id, updates) => {
|
|
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
|
const values = [id, ...Object.values(updates)];
|
|
const query = `UPDATE videos SET ${fields} WHERE id = $1 RETURNING *;`;
|
|
return (await db.query(query, values)).rows[0];
|
|
}),
|
|
delete_video_by_id: handleDatabaseOperation(async (id) => {
|
|
const query = `DELETE FROM videos WHERE id = $1;`;
|
|
await db.query(query, [id]);
|
|
})
|
|
};
|
|
|
|
const camera = {
|
|
get_camera: handleDatabaseOperation(async () => {
|
|
const query = `SELECT * FROM camera;`;
|
|
return (await db.query(query)).rows;
|
|
}),
|
|
edit_camera: handleDatabaseOperation(async (id, updates) => {
|
|
const fields = Object.keys(updates).map((key, index) => `${key} = $${index + 2}`).join(', ');
|
|
const values = [id, ...Object.values(updates)];
|
|
const query = `UPDATE camera SET ${fields} WHERE id = $1 RETURNING *;`;
|
|
return (await db.query(query, values)).rows[0];
|
|
}),
|
|
delete_camera: handleDatabaseOperation(async (id) => {
|
|
const query = `DELETE FROM camera WHERE id = $1;`;
|
|
await db.query(query, [id]);
|
|
})
|
|
};
|
|
|
|
// Export des modules
|
|
module.exports = {
|
|
project,
|
|
measurement,
|
|
video,
|
|
camera
|
|
};
|