diff --git a/api/db.json b/api/db.json new file mode 100644 index 0000000..a192a06 --- /dev/null +++ b/api/db.json @@ -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"}]} \ No newline at end of file diff --git a/api/resolvers.js b/api/resolvers.js index e5daf7a..d335296 100644 --- a/api/resolvers.js +++ b/api/resolvers.js @@ -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) } } } diff --git a/api/rethinkdb.js b/api/rethinkdb.js deleted file mode 100644 index a1c2544..0000000 --- a/api/rethinkdb.js +++ /dev/null @@ -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 -}