|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* dependencies */ |
| 4 | +const { createReadStream } = require('fs'); |
| 5 | +const { parallel } = require('async'); |
| 6 | +const { getType: mimeTypeOf } = require('mime'); |
| 7 | +const { connect } = require('@lykmapipo/mongoose-common'); |
| 8 | +const { createModels } = require(`${__dirname}/..`); |
| 9 | + |
| 10 | +const readStreamFor = filename => { |
| 11 | + return createReadStream(`${__dirname}/../test/fixtures/${filename}`); |
| 12 | +}; |
| 13 | + |
| 14 | +// ensure connections |
| 15 | +connect(error => { |
| 16 | + // re-throw if error |
| 17 | + if (error) { |
| 18 | + throw error; |
| 19 | + } |
| 20 | + |
| 21 | + // models |
| 22 | + const { File, Image, Audio, Video, Document } = createModels(); |
| 23 | + |
| 24 | + // prepare generic file seed |
| 25 | + const seedFile = next => { |
| 26 | + const filename = 'file.txt'; |
| 27 | + const contentType = mimeTypeOf('.txt'); |
| 28 | + const options = { filename, contentType }; |
| 29 | + const readStream = readStreamFor(filename); |
| 30 | + File.write(options, readStream, error => next(error)); |
| 31 | + } |
| 32 | + |
| 33 | + // prepare image file seed |
| 34 | + const seedImage = next => { |
| 35 | + const filename = 'image.png'; |
| 36 | + const contentType = mimeTypeOf('.png'); |
| 37 | + const options = { filename, contentType }; |
| 38 | + const readStream = readStreamFor(filename); |
| 39 | + Image.write(options, readStream, error => next(error)); |
| 40 | + } |
| 41 | + |
| 42 | + // prepare audio file seed |
| 43 | + const seedAudio = next => { |
| 44 | + const filename = 'audio.mp3'; |
| 45 | + const contentType = mimeTypeOf('.mp3'); |
| 46 | + const options = { filename, contentType }; |
| 47 | + const readStream = readStreamFor(filename); |
| 48 | + Audio.write(options, readStream, error => next(error)); |
| 49 | + } |
| 50 | + |
| 51 | + // prepare video file seed |
| 52 | + const seedVideo = next => { |
| 53 | + const filename = 'video.mp4'; |
| 54 | + const contentType = mimeTypeOf('.mp4'); |
| 55 | + const options = { filename, contentType }; |
| 56 | + const readStream = readStreamFor(filename); |
| 57 | + Video.write(options, readStream, error => next(error)); |
| 58 | + } |
| 59 | + |
| 60 | + // prepare document file seed |
| 61 | + const seedDocument = next => { |
| 62 | + const filename = 'document.doc'; |
| 63 | + const contentType = mimeTypeOf('.doc'); |
| 64 | + const options = { filename, contentType }; |
| 65 | + const readStream = readStreamFor(filename); |
| 66 | + Document.write(options, readStream, error => next(error)); |
| 67 | + } |
| 68 | + |
| 69 | + // do seeding |
| 70 | + const seeds = [seedFile, seedImage, seedAudio, seedVideo]; |
| 71 | + parallel(seeds, error => { |
| 72 | + // re-throw if error |
| 73 | + if (error) { |
| 74 | + throw error; |
| 75 | + } |
| 76 | + process.exit(0); |
| 77 | + }); |
| 78 | + |
| 79 | +}); |
0 commit comments