Improved SFW and tag media seed file to allow updates.

This commit is contained in:
DebaucheryLibrarian
2020-09-05 02:57:24 +02:00
parent bba73c4f31
commit c96e10b33d
3 changed files with 25 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
const nanoid = require('nanoid/non-secure');
// const nanoid = require('nanoid/non-secure');
const upsert = require('../src/utils/upsert');
const sfw = Object.entries({
@@ -577,6 +577,7 @@ const sfw = Object.entries({
thumbnail: `sfw/${category}/thumbs/${photo}.jpeg`,
lazy: `sfw/${category}/lazy/${photo}.jpeg`,
mime: 'image/jpeg',
is_sfw: true,
sfw_media_id: null,
group: category,
index,
@@ -649,12 +650,12 @@ const tagPosters = [
['trainbang', 'poster', 'Kali Roses in "Passing Me Around" for Blacked'],
['tap', 'poster', 'Kristy Black in SZ1986 for LegalPorno'],
]
.map(([slug, filename, comment], index) => ({
id: nanoid(),
.map(([slug, fileIndex, comment], index) => ({
id: `${slug}-${fileIndex}`,
tagSlug: slug,
path: `tags/${slug}/${filename}.jpeg`,
thumbnail: `tags/${slug}/thumbs/${filename}.jpeg`,
lazy: `tags/${slug}/lazy/${filename}.jpeg`,
path: `tags/${slug}/${fileIndex}.jpeg`,
thumbnail: `tags/${slug}/thumbs/${fileIndex}.jpeg`,
lazy: `tags/${slug}/lazy/${fileIndex}.jpeg`,
mime: 'image/jpeg',
index,
comment,
@@ -755,18 +756,9 @@ const tagPhotos = [
/* eslint-disable max-len */
exports.seed = knex => Promise.resolve()
.then(async () => {
const { inserted } = await upsert('media', sfw, 'id');
await upsert('media', sfw, 'id');
const sfwMediaIds = inserted.map(mediaEntry => ({
id: nanoid(),
media_id: mediaEntry.id,
}));
await upsert('media_sfw', sfwMediaIds, 'media_id');
})
.then(async () => {
const tagMedia = tagPosters.concat(tagPhotos);
const tags = await knex('tags').whereIn('slug', tagMedia.map(item => item.tagSlug));
const { inserted, updated } = await upsert('media', tagMedia.map(({
@@ -788,8 +780,18 @@ exports.seed = knex => Promise.resolve()
media_id: mediaIdsByPath[photo.path],
}));
return Promise.all([
await Promise.all([
upsert('tags_posters', tagPosterEntries, 'tag_id', knex),
upsert('tags_photos', tagPhotoEntries, ['tag_id', 'media_id'], knex),
]);
// clean up (re)moved tag media
await Promise.all([
knex('tags_posters')
.whereNotIn('media_id', tagPosters.map(photo => photo.id))
.delete(),
knex('tags_photos')
.whereNotIn('media_id', tagPhotos.map(photo => photo.id))
.delete(),
]);
});