'use strict'; // const config = require('config'); // const util = require('util'); // const path = require('path'); const fs = require('fs'); const moment = require('moment'); const knex = require('../knex'); async function save() { const stashes = await knex('stashes') .select('stashes.*', 'users.username') .leftJoin('users', 'users.id', 'stashes.user_id'); const filename = `stashes-${moment().format('YYYY-MM-DD_hh_mm')}.json`; let savedStashes = 0; await stashes.reduce(async (chain, stash) => { await chain; const scenes = await knex('stashes_scenes') .select('releases.title', 'releases.created_at', 'releases.entry_id', 'entities.slug as entity_slug', 'entities.type as entity_type') .leftJoin('releases', 'releases.id', 'stashes_scenes.scene_id') .leftJoin('entities', 'entities.id', 'releases.entity_id') .where('stashes_scenes.stash_id', stash.id); const movies = await knex('stashes_movies') .select('movies.title', 'movies.created_at', 'movies.entry_id', 'entities.slug as entity_slug', 'entities.type as entity_type') .leftJoin('movies', 'movies.id', 'stashes_movies.movie_id') .leftJoin('entities', 'entities.id', 'movies.entity_id') .where('stashes_movies.stash_id', stash.id); const actors = await knex('stashes_actors') .select('actors.slug', 'actors.created_at', 'entities.slug as entity_slug', 'entities.type as entity_type') .leftJoin('actors', 'actors.id', 'stashes_actors.actor_id') .leftJoin('entities', 'entities.id', 'actors.entity_id') .where('stashes_actors.stash_id', stash.id); console.log('scenes', scenes); console.log('movies', movies); console.log('actors', actors); const curatedStash = JSON.stringify({ username: stash.username, name: stash.name, slug: stash.slug, public: stash.public, primary: stash.primary, createdAt: stash.created_at, scenes: scenes.map((scene) => ({ title: scene.title, entryId: scene.entry_id, entitySlug: scene.entity_slug, entityType: scene.entity_type, createdAt: scene.created_at, })), movies: movies.map((movie) => ({ title: movie.title, entryId: movie.entry_id, entitySlug: movie.entity_slug, entityType: movie.entity_type, createdAt: movie.created_at, })), actors: actors.map((actor) => ({ slug: actor.slug, entitySlug: actor.entity_slug, entityType: actor.entity_type, createdAt: actor.created_at, })), }); await fs.promises.appendFile(filename, `${curatedStash}\n`); savedStashes += 1; }, Promise.resolve([])); console.log(`Saved ${savedStashes} stashes`); process.exit(); } save();