Add lowdb

This commit is contained in:
Michael Merrill 2017-04-04 23:26:51 -06:00
parent de4fdc0632
commit c092666403
3 changed files with 22 additions and 31 deletions

1
api/db.json Normal file
View File

@ -0,0 +1 @@
{"uploads":[{"name":"IMG_0599.JPG","type":"image/jpeg","size":343845,"path":"/tmp/uploads/upload_7b8d12d21774c726904d6272f6176d57"},{"name":"IMG_0599.JPG","type":"image/jpeg","size":343845,"path":"/tmp/uploads/upload_e39a69250284e0113b276e3dcf87dbb4"},{"name":"kalen-emsley-99660.jpg","type":"image/jpeg","size":14036175,"path":"/tmp/uploads/upload_fe22084971ed01e6849cd7fa253ccf4b"}]}

View File

@ -1,32 +1,35 @@
import getRethinkDB from './rethinkdb'
import low from 'lowdb'
import fileAsync from 'lowdb/lib/storages/file-async'
const getNewVal = (result) => {
if (result.changes.length === 1) {
return result.changes[0].new_val
}
return result.changes.map((file) => file.new_val)
// Start database using file-async storage
const db = low('db.json', {
storage: fileAsync
})
db.defaults({uploads: []})
.write()
const saveFile = (file) => {
return db.get('uploads')
.push(file)
.last()
.write()
.then((result) => result)
}
export default {
Query: {
allUploads () {
const db = getRethinkDB()
return db.table('uploads')
.then((result) => result)
return db.get('uploads').value()
}
},
Mutation: {
singleUpload (_, {file}) {
const db = getRethinkDB()
return db.table('uploads')
.insert(file, {returnChanges: true})
.then((result) => getNewVal(result))
return saveFile(file)
},
multiUpload (_, {files}) {
const db = getRethinkDB()
return db.table('uploads')
.insert(files, {returnChanges: true})
.then((result) => getNewVal(result))
return Promise.all(files.map((file) => {
return saveFile(file)
})).then((results) => results)
}
}
}

View File

@ -1,13 +0,0 @@
import rethinkdbdash from 'rethinkdbdash'
let driver
export default () => {
if (!driver) {
driver = rethinkdbdash({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
db: process.env.DB_NAME
})
}
return driver
}