Improve merge on update

This commit is contained in:
John Lyon-Smith
2018-05-09 15:32:53 -07:00
parent 57355088f0
commit d087da2ce7
8 changed files with 40 additions and 58 deletions

View File

@@ -83,10 +83,9 @@ export class OptionStrip extends Component {
{ {
flex: 1, flex: 1,
justifyContent: "center", justifyContent: "center",
// TODO: Setting specific border widths broken in RN 0.49. Enable in RN 0.55 and above? borderTopWidth: 1,
// borderTopWidth: 1, borderBottomWidth: 1,
// borderBottomWidth: 1, borderLeftWidth: 1,
// borderLeftWidth: 1,
borderColor: "black", borderColor: "black",
}, },
index === 0 && { index === 0 && {

View File

@@ -4892,16 +4892,16 @@
} }
} }
}, },
"mongoose-doc-merge": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/mongoose-doc-merge/-/mongoose-doc-merge-1.0.0.tgz",
"integrity": "sha512-rci5BJyqc/0uuQtvXVMh4jJqHi0DdpM8u1s5oAysc28mznPqZ+KfqcCVjTR1RA/4Maii8qC9QRkZfI0UCrAh6g=="
},
"mongoose-legacy-pluralize": { "mongoose-legacy-pluralize": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
"integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
}, },
"mongoose-merge-plugin": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/mongoose-merge-plugin/-/mongoose-merge-plugin-0.0.5.tgz",
"integrity": "sha1-AQDPzO2vaUzmNt8o7r6K1RuLKVs="
},
"monzilla": { "monzilla": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/monzilla/-/monzilla-1.1.1.tgz", "resolved": "https://registry.npmjs.org/monzilla/-/monzilla-1.1.1.tgz",

View File

@@ -39,7 +39,7 @@
"jsonwebtoken": "^7.4.0", "jsonwebtoken": "^7.4.0",
"mongodb": "^2.2.35", "mongodb": "^2.2.35",
"mongoose": "^5.0.13", "mongoose": "^5.0.13",
"mongoose-merge-plugin": "0.0.5", "mongoose-doc-merge": "^1.0.0",
"node-fetch": "^2.1.2", "node-fetch": "^2.1.2",
"nodemailer": "^4.0.1", "nodemailer": "^4.0.1",
"passport": "^0.3.2", "passport": "^0.3.2",

View File

@@ -111,23 +111,19 @@ export class ActivityRoutes {
} }
let Activity = this.db.Activity let Activity = this.db.Activity
let activityUpdates = req.body let activity = await Activity.findById(req.body._id)
const foundActivity = await Activity.findById(activityUpdates._id) if (!activity) {
if (!foundActivity) {
return next( return next(
createError.NotFound(`Activity with _id ${_id} was not found`) createError.NotFound(`Activity with _id ${req.body_id} was not found`)
) )
} }
// Strip off all BSON types let activityUpdates = new Activity(req.body)
foundActivity = JSON.parse(JSON.stringify(foundActivity))
const mergedActivity = Activity.hydrate( // Strip off all BSON types
merge(foundActivity, activityUpdates) activity.merge(activityUpdates)
) const savedActivity = await activity.save()
const savedActivity = await mergedActivity.save()
res.json(savedActivity.toClient()) res.json(savedActivity.toClient())
} }

View File

@@ -107,21 +107,17 @@ export class TeamRoutes {
} }
let Team = this.db.Team let Team = this.db.Team
let teamUpdates = null let team = await Team.findById(req.body._id)
try { if (!team) {
teamUpdates = new Team(req.body) throw createError.NotFound(`Team with _id ${req.body_id} was not found`)
} catch (err) {
throw createError.BadRequest("Invalid data")
} }
const foundTeam = await Team.findById(teamUpdates._id) let teamUpdates = new Team(req.body)
if (!foundTeam) { team.merge(teamUpdates)
throw createError.NotFound(`Team with _id ${_id} was not found`)
} const savedTeam = await team.save()
foundTeam.merge(teamUpdates)
const savedTeam = await foundTeam.save()
res.json(savedTeam.toClient()) res.json(savedTeam.toClient())
} }

View File

@@ -183,30 +183,24 @@ export class UserRoutes {
throw createError.Forbidden() throw createError.Forbidden()
} }
const User = this.db.User
let userUpdates = null
try {
userUpdates = new User(req.body)
} catch (err) {
throw createError.BadRequest("Invalid data")
}
if (isSelf && !isAdmin) { if (isSelf && !isAdmin) {
throw createError.BadRequest("Cannot modify own administrator level") throw createError.BadRequest("Cannot modify own administrator level")
} }
const foundUser = await User.findById(userUpdates._id) const User = this.db.User
const user = await User.findById(req.body._id)
if (!foundUser) { if (!user) {
throw createError.NotFound(`User with _id ${user._id} was not found`) throw createError.NotFound(`User with _id ${req.body._id} was not found`)
} }
// We don't allow direct updates to the email field so remove it if present // We don't allow direct updates to the email field so remove it if present
const userUpdates = new User(req.body)
delete userUpdates.email delete userUpdates.email
foundUser.merge(userUpdates) user.merge(userUpdates)
const savedUser = await foundUser.save() const savedUser = await user.save()
res.json(savedUser.toClient(req.user)) res.json(savedUser.toClient(req.user))
} }

View File

@@ -147,22 +147,19 @@ export class WorkItemRoutes {
} }
let WorkItem = this.db.WorkItem let WorkItem = this.db.WorkItem
let workItemUpdates = req.body let workItem = await WorkItem.findById(req.body._id)
let foundWorkItem = await WorkItem.findById(workItemUpdates._id)
if (!foundWorkItem) { if (!workItem) {
return next( return next(
createError.NotFound(`WorkItem with _id ${_id} was not found`) createError.NotFound(`WorkItem with _id ${req.body_id} was not found`)
) )
} }
// Strip off all BSON types const workItemUpdates = new WorkItem(req.body)
foundWorkItem = JSON.parse(JSON.stringify(foundWorkItem))
const mergedWorkItem = WorkItem.hydrate( workItem.merge(workItemUpdates)
merge(foundWorkItem, workItemUpdates)
) const savedWorkItem = await workItem.save()
const savedWorkItem = await mergedWorkItem.save()
res.json(savedWorkItem.toClient()) res.json(savedWorkItem.toClient())
} }

View File

@@ -1,7 +1,7 @@
import mongoose from "mongoose" import mongoose from "mongoose"
import mongodb from "mongodb" import mongodb from "mongodb"
import Grid from "gridfs-stream" import Grid from "gridfs-stream"
import merge from "mongoose-merge-plugin" import mergePlugin from "mongoose-doc-merge"
import autobind from "autobind-decorator" import autobind from "autobind-decorator"
import * as Schemas from "./schemas" import * as Schemas from "./schemas"
import util from "util" import util from "util"
@@ -9,7 +9,7 @@ import util from "util"
@autobind @autobind
export class DB { export class DB {
constructor() { constructor() {
mongoose.plugin(merge) mongoose.plugin(mergePlugin)
} }
async connect(mongoUri, isProduction) { async connect(mongoUri, isProduction) {