mirror of
https://github.com/BreizhHardware/Jellystat.git
synced 2026-01-18 16:27:20 +01:00
Added Sync feature to only sync Recently Added Items that don't exist in the database (this will not update existing data), default interval now set to 15 Minutes Renamed existing sync to Full sync (should function exactly the same), default interval now set to 1 day Reworked handling of items no longer on jellyfin. Items are no longer deleted but are now marked as archived so that we can still view their items when looking at Playback Activity. Added options to purge Archived data. This will either purge just the item (including seasons and episodes if its a show) or the item plus all related watch activity
169 lines
4.4 KiB
JavaScript
169 lines
4.4 KiB
JavaScript
const { Pool } = require('pg');
|
|
const pgp = require('pg-promise')();
|
|
const {
|
|
update_query: update_query_map,
|
|
} = require('./models/bulk_insert_update_handler');
|
|
|
|
const _POSTGRES_USER = process.env.POSTGRES_USER;
|
|
const _POSTGRES_PASSWORD = process.env.POSTGRES_PASSWORD;
|
|
const _POSTGRES_IP = process.env.POSTGRES_IP;
|
|
const _POSTGRES_PORT = process.env.POSTGRES_PORT;
|
|
|
|
if (
|
|
[_POSTGRES_USER, _POSTGRES_PASSWORD, _POSTGRES_IP, _POSTGRES_PORT].includes(
|
|
undefined
|
|
)
|
|
) {
|
|
console.log('Error: Postgres details not defined');
|
|
return;
|
|
}
|
|
|
|
const pool = new Pool({
|
|
user: _POSTGRES_USER,
|
|
host: _POSTGRES_IP,
|
|
database: 'jfstat',
|
|
password: _POSTGRES_PASSWORD,
|
|
port: _POSTGRES_PORT,
|
|
});
|
|
|
|
pool.on('error', (err, client) => {
|
|
console.error('Unexpected error on idle client', err);
|
|
return;
|
|
//process.exit(-1);
|
|
});
|
|
|
|
async function deleteBulk(table_name, data) {
|
|
const client = await pool.connect();
|
|
let result = 'SUCCESS';
|
|
let message = '';
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
if (data && data.length !== 0) {
|
|
const deleteQuery = {
|
|
text: `DELETE FROM ${table_name} WHERE "Id" IN (${pgp.as.csv(data)})`,
|
|
};
|
|
// console.log(deleteQuery);
|
|
await client.query(deleteQuery);
|
|
}
|
|
|
|
await client.query('COMMIT');
|
|
message = data.length + ' Rows removed.';
|
|
} catch (error) {
|
|
await client.query('ROLLBACK');
|
|
message = 'Bulk delete error: ' + error;
|
|
result = 'ERROR';
|
|
} finally {
|
|
client.release();
|
|
}
|
|
return { Result: result, message: '' + message };
|
|
}
|
|
|
|
async function updateSingleFieldBulk(table_name, data,field_name, new_value) {
|
|
const client = await pool.connect();
|
|
let result = 'SUCCESS';
|
|
let message = '';
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
if (data && data.length !== 0) {
|
|
const updateQuery = {
|
|
text: `UPDATE ${table_name} SET "${field_name}"='${new_value}' WHERE "Id" IN (${pgp.as.csv(data)})`,
|
|
};
|
|
// console.log(deleteQuery);
|
|
await client.query(updateQuery);
|
|
}
|
|
|
|
await client.query('COMMIT');
|
|
message = data.length + ' Rows updated.';
|
|
} catch (error) {
|
|
await client.query('ROLLBACK');
|
|
message = 'Bulk update error: ' + error;
|
|
result = 'ERROR';
|
|
} finally {
|
|
client.release();
|
|
}
|
|
return { Result: result, message: '' + message };
|
|
}
|
|
|
|
async function insertBulk(table_name, data, columns) {
|
|
//dedupe data
|
|
|
|
if (Array.isArray(data)) {
|
|
data = data.reduce((accumulator, currentItem) => {
|
|
const isDuplicate = accumulator.some((item) =>
|
|
currentItem.Id
|
|
? item.Id === currentItem.Id
|
|
: item.rowid === currentItem.rowid
|
|
);
|
|
|
|
if (!isDuplicate) {
|
|
accumulator.push(currentItem);
|
|
}
|
|
|
|
return accumulator;
|
|
}, []);
|
|
}
|
|
|
|
//
|
|
const client = await pool.connect();
|
|
let result = 'SUCCESS';
|
|
let message = '';
|
|
try {
|
|
await client.query('BEGIN');
|
|
const update_query = update_query_map.find(
|
|
(query) => query.table === table_name
|
|
).query;
|
|
await client.query('COMMIT');
|
|
const cs = new pgp.helpers.ColumnSet(columns, { table: table_name });
|
|
const query = pgp.helpers.insert(data, cs) + update_query; // Update the column names accordingly
|
|
await client.query(query);
|
|
} catch (error) {
|
|
await client.query('ROLLBACK');
|
|
message = '' + error;
|
|
result = 'ERROR';
|
|
} finally {
|
|
client.release();
|
|
}
|
|
return {
|
|
Result: result,
|
|
message: message ? 'Bulk insert error: ' + message : '',
|
|
};
|
|
}
|
|
|
|
async function query(text, params) {
|
|
try {
|
|
const result = await pool.query(text, params);
|
|
return result;
|
|
} catch (error) {
|
|
if (error?.routine === 'auth_failed') {
|
|
console.log('Error 401: Unable to Authenticate with Postgres DB');
|
|
} else if (error?.code === 'ENOTFOUND') {
|
|
console.log('Error: Unable to Connect to Postgres DB');
|
|
} else if (error?.code === 'ERR_SOCKET_BAD_PORT') {
|
|
console.log(
|
|
'Error: Invalid Postgres DB Port Range. Port should be >= 0 and < 65536.'
|
|
);
|
|
} else if (error?.code === 'ECONNREFUSED') {
|
|
console.log(
|
|
'Error: Postgres DB Connection refused at ' +
|
|
error.address +
|
|
':' +
|
|
error.port
|
|
);
|
|
} else {
|
|
console.error('Error occurred while executing query:', error);
|
|
}
|
|
return [];
|
|
// throw error;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
query: query,
|
|
deleteBulk: deleteBulk,
|
|
insertBulk: insertBulk,
|
|
updateSingleFieldBulk:updateSingleFieldBulk,
|
|
// initDB: initDB,
|
|
};
|