From e2875aee8cebe00ee0033f65ba5d6c1dab9f2a03 Mon Sep 17 00:00:00 2001 From: Alex Stark Date: Fri, 12 Aug 2016 18:47:42 -0700 Subject: [PATCH] add update or create and fix find or create user --- src/db/stork/Model.js | 14 ++++++++++---- src/server/models/User.js | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/db/stork/Model.js b/src/db/stork/Model.js index c2d19dc..1c7c62e 100644 --- a/src/db/stork/Model.js +++ b/src/db/stork/Model.js @@ -31,13 +31,19 @@ class Model { }); } - // TODO updateOrCreate(obj) { - + return this.db.find(obj) + .then((foundObj) => { + if (!foundObj) { + return this.create(obj); + } else { + return update(foundObj, obj); + } + }); } create(obj) { - return this.db.insert(obj).into(this.table).returning(...Object.keys(obj)); + return this.db.insert(obj).into(this.table).returning(...Object.keys(obj), 'id'); } save(obj) { @@ -48,7 +54,7 @@ class Model { return this.db(this.table) .update(updateObj, [...updateObj]) .where(criteriaObj) - .returning(...Object.keys(updateObj)); + .returning(...Object.keys(updateObj), 'id'); } remove(obj) { diff --git a/src/server/models/User.js b/src/server/models/User.js index fda3789..008acd2 100644 --- a/src/server/models/User.js +++ b/src/server/models/User.js @@ -22,6 +22,6 @@ export const getAllUserData = (req, res) => { export const findOrCreateUser = (req, res) => { - User.findOrCreateUser(req.body) + User.findOrCreate(req.body) .then((user) => res.json(user)); };