Skip to content

Commit

Permalink
Expose oldDoc in the server events
Browse files Browse the repository at this point in the history
  • Loading branch information
lukejagodzinski committed Sep 24, 2019
1 parent dfc7e35 commit b238fbc
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 199 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
4 changes: 2 additions & 2 deletions .versions
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ htmljs@1.0.11
id-map@1.1.0
insecure@1.0.7
inter-process-messaging@0.1.0
jagi:astronomy@2.7.1
jagi:astronomy@2.7.2
jquery@1.11.10
local-test:jagi:astronomy@2.7.1
local-test:jagi:astronomy@2.7.2
logging@1.1.20
mdg:validation-error@0.5.1
meteor@1.9.3
Expand Down
72 changes: 39 additions & 33 deletions lib/modules/storage/class_prototype_methods/save.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import _defaults from 'lodash/defaults';
import _each from 'lodash/each';
import _extend from 'lodash/extend';
import _omit from 'lodash/omit';
import { DDP } from 'meteor/ddp';
import documentInsert from '../utils/document_insert';
import documentUpdate from '../utils/document_update';
import isRemote from '../utils/is_remote';
import callMeteorMethod from '../utils/call_meteor_method';
import rawAll from '../../fields/utils/rawAll';
import castNested from '../../fields/utils/castNested';
import getModifier from '../../storage/utils/getModifier';
import _defaults from "lodash/defaults";
import _each from "lodash/each";
import _extend from "lodash/extend";
import _omit from "lodash/omit";
import { DDP } from "meteor/ddp";
import documentInsert from "../utils/document_insert";
import documentUpdate from "../utils/document_update";
import isRemote from "../utils/is_remote";
import callMeteorMethod from "../utils/call_meteor_method";
import rawAll from "../../fields/utils/rawAll";
import castNested from "../../fields/utils/castNested";
import getModifier from "../../storage/utils/getModifier";

function save(options = {}, callback) {
const doc = this;
Expand All @@ -26,15 +26,15 @@ function save(options = {}, callback) {
_defaults(options, {
stopOnFirstError: true,
simulation: true,
forceUpdate: false
forceUpdate: false,
});

// Cast all fields.
if (options.cast) {
_each(Class.getFields(), (field) => {
_each(Class.getFields(), field => {
doc[field.name] = field.castValue(doc[field.name], {
clone: false,
cast: true
cast: true,
});
});
}
Expand All @@ -43,8 +43,8 @@ function save(options = {}, callback) {
castNested({
doc,
options: {
clone: false
}
clone: false,
},
});
}

Expand All @@ -71,7 +71,7 @@ function save(options = {}, callback) {
// If we are dealing with a remote collection and we are not on the server.
if (isRemote(Class)) {
// Prepare meteor method name to be called.
const methodName = '/Astronomy/' + (inserting ? 'insert' : 'update');
const methodName = "/Astronomy/" + (inserting ? "insert" : "update");
// Prepare arguments for meteor method.
const methodArgs = {
className: Class.getName(),
Expand All @@ -82,30 +82,35 @@ function save(options = {}, callback) {
if (inserting) {
_extend(methodArgs, {
rawDoc: rawAll(doc, {
transient: false
})
transient: false,
}),
});
}
// Updating.
else {
// If the "forceUpdate" option was set then we don't run the "getModifier"
// function to get modifier and instead we just take entire raw object and
// remove the "_id" field as we can't update it.
const modifier = options.forceUpdate ? _omit(doc.raw(), ['_id']) : getModifier({doc});
const modifier = options.forceUpdate
? _omit(doc.raw(), ["_id"])
: getModifier({ doc });
_extend(methodArgs, {
selector: {
_id: doc._id
_id: doc._id,
},
modifier,
options: {},
fields: options.fields
fields: options.fields,
});
}

try {
// Run Meteor method.
const result = callMeteorMethod(
Class, methodName, [methodArgs], callback
Class,
methodName,
[methodArgs],
callback,
);
if (result && inserting) {
// In the insert operation the value return from the meteor method is
Expand All @@ -115,9 +120,8 @@ function save(options = {}, callback) {
// Document is not new anymore.
doc._isNew = false;
return result;
}
// Catch stub exceptions.
catch (err) {
} catch (err) {
// Catch stub exceptions.
if (callback) {
callback(err);
return null;
Expand All @@ -134,25 +138,27 @@ function save(options = {}, callback) {
doc,
stopOnFirstError: options.stopOnFirstError,
simulation: options.simulation,
trusted: true
trusted: true,
};
if (inserting) {
let result = documentInsert(methodArgs);
if (callback) {
callback(undefined, result);
}
return result;
}
else {
} else {
methodArgs.fields = options.fields;
// On the server the `oldDoc` argument is empty to we have to provide it.
if (!methodArgs.oldDoc) {
methodArgs.oldDoc = Class.findOne(doc._id, { defaults: false });
}
let result = documentUpdate(methodArgs);
if (callback) {
callback(undefined, result);
}
return result;
}
}
catch (err) {
} catch (err) {
if (callback) {
callback(err);
return null;
Expand All @@ -161,4 +167,4 @@ function save(options = {}, callback) {
}
}

export default save;
export default save;
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: "jagi:astronomy",
version: "2.7.2",
version: "2.7.3",
summary: "Model layer for Meteor",
git: "https://github.com/jagi/meteor-astronomy.git"
});
Expand Down
Loading

0 comments on commit b238fbc

Please sign in to comment.