Use upsert when importing libraries.

This commit is contained in:
mike.rode
2019-02-15 01:39:32 -06:00
parent 946da955d3
commit 3820029a98
8 changed files with 68 additions and 45 deletions
+1
View File
@@ -3,6 +3,7 @@ import plexService from '../../../services/plexApi';
const router = Router();
// users
router.get('/users', plexService.getUsers);
router.get('/most-watched', plexService.getMostWatched);
router.get('/sections', plexService.getSections);
+1 -1
View File
@@ -7,7 +7,7 @@ const sequelize = new Sequelize(
{
dialect: 'postgres',
host: 'localhost',
logging: false,
// logging: false,
},
);
+4 -1
View File
@@ -1,6 +1,9 @@
const plexLibrary = (sequelize, DataTypes) => {
const plexLibrary = sequelize.define('plex_library', {
title: DataTypes.STRING,
title: {
type: DataTypes.STRING,
unique: true,
},
type: DataTypes.STRING,
views: DataTypes.INTEGER,
rating_key: DataTypes.INTEGER,
+19 -12
View File
@@ -39,18 +39,25 @@ const importLibrary = async sectionId => {
const createLibrary = libraryData => {
console.log(libraryData[1]);
libraryData.forEach(async data => {
await models.PlexLibrary.create({
userId: 1,
title: data.title,
type: data.type,
views: data.views,
rating_key: data.ratingKey,
metadata_path: data.key,
summary: data.summary,
rating: data.rating,
year: data.year,
genre: JSON.stringify(data.Genre),
});
await models.PlexLibrary.upsert(
{
userId: 1,
title: data.title,
type: data.type,
views: data.views,
rating_key: data.ratingKey,
metadata_path: data.key,
summary: data.summary,
rating: data.rating,
year: data.year,
genre: JSON.stringify(data.Genre),
},
{
where: {
title: data.title,
},
},
);
});
};
+2 -1
View File
@@ -45,9 +45,10 @@ PlexApiClient.prototype.mostWatchedUrlParams = function(req) {
};
PlexApiClient.prototype.getLibraryDataBySectionUrlParams = function(req) {
const sectionId = req.sectionId || req.params.id;
return {
host: config.plex.plexServerUrl,
path: `/library/sections/${req.params.id}/all`,
path: `/library/sections/${sectionId}/all`,
queryParams: {
'X-Plex-Token': this.options.token || config.plex.token,
},