0
0
switched/tools/migration/use-gridfs.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-11-06 17:57:03 +09:00
// for Node.js interpret
const { default: db } = require('../../built/db/mongodb')
const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file')
const { Duplex } = require('stream')
2017-11-07 22:08:21 +09:00
const { default: zip } = require('@prezzemolo/zip')
2017-11-06 17:57:03 +09:00
const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => {
const writeStream = bucket.openUploadStreamWithId(...rest)
2017-11-07 19:17:42 +09:00
2017-11-06 17:57:03 +09:00
const dataStream = new Duplex()
dataStream.push(buffer)
dataStream.push(null)
writeStream.once('finish', resolve)
writeStream.on('error', reject)
dataStream.pipe(writeStream)
})
const migrateToGridFS = async (doc) => {
const id = doc._id
const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので
2017-11-06 17:57:03 +09:00
const created_at = doc.created_at
const name = doc.name
2017-11-07 21:04:32 +09:00
const type = doc.type
2017-11-06 17:57:03 +09:00
delete doc._id
delete doc.created_at
delete doc.datasize
delete doc.hash
delete doc.data
delete doc.name
2017-11-07 21:04:32 +09:00
delete doc.type
2017-11-06 17:57:03 +09:00
const bucket = await getGridFSBucket()
2017-11-07 21:04:32 +09:00
const added = await writeToGridFS(bucket, buffer, id, name, { contentType: type, metadata: doc })
2017-11-06 17:57:03 +09:00
const result = await DriveFile.update(id, {
$set: {
uploadDate: created_at
}
})
return added && result.ok === 1
}
2017-11-07 19:17:42 +09:00
async function main() {
2017-11-07 22:08:21 +09:00
const count = await DriveFile.count({});
const dop = Number.parseInt(process.argv[2]) || 5
return zip(
1,
async (time) => {
const doc = await DriveFile.find({}, { limit: dop, skip: time * dop })
return Promise.all(doc.map(migrateToGridFS))
},
((count - (count % dop)) / dop) + 1
)
2017-11-06 17:57:03 +09:00
}
main().then(console.dir).catch(console.error)