Skip to content

Commit

Permalink
Create Meteor methods even for local collections
Browse files Browse the repository at this point in the history
  • Loading branch information
lukejagodzinski committed Feb 15, 2018
1 parent 92eefad commit 1f6e484
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
13 changes: 8 additions & 5 deletions lib/modules/methods/class_prototype_methods/applyMethod.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import callStandardMeteorMethod from '../../storage/utils/callStandardMeteorMethod';
import rawAll from '../../fields/utils/rawAll';
import callStandardMeteorMethod from "../../storage/utils/callStandardMeteorMethod";
import rawAll from "../../fields/utils/rawAll";

function applyMethod(methodName, methodArgs, callback) {
const doc = this;
Expand All @@ -16,8 +16,11 @@ function applyMethod(methodName, methodArgs, callback) {
};

return callStandardMeteorMethod(
Class, '/Astronomy/execute', [meteorMethodArgs], callback
Class,
"/Astronomy/execute",
[meteorMethodArgs],
callback
);
};
}

export default applyMethod;
export default applyMethod;
31 changes: 14 additions & 17 deletions lib/modules/methods/hooks/onFinalizeClass.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _zipObject from 'lodash/zipObject';
import _each from 'lodash/each';
import wrapMethod from '../utils/wrapMethod';
import hasMeteorMethod from '../../storage/utils/has_meteor_method';
import _zipObject from "lodash/zipObject";
import _each from "lodash/each";
import wrapMethod from "../utils/wrapMethod";
import hasMeteorMethod from "../../storage/utils/has_meteor_method";
import astronomyExecute from "../meteor_methods/astronomyExecute";
import applyMethod from "../class_prototype_methods/applyMethod";
import callMethod from "../class_prototype_methods/callMethod";
Expand All @@ -11,18 +11,15 @@ function onFinalizeClass(Class, className) {

if (schema.collection) {
const Collection = schema.collection;
const connection = Collection._connection;
const connection =
Collection._connection || Meteor.connection || Meteor.server;
if (connection) {
// Prepare meteor methods to be added.
const astroMethods = {
'/Astronomy/execute': astronomyExecute,
};
_each(astroMethods, (astroMethod, astroMethodName) => {
if (!hasMeteorMethod(connection, astroMethodName)) {
// Add Meteor method.
connection.methods(_zipObject([astroMethodName], [astroMethod]));
}
});
if (!hasMeteorMethod(connection, "/Astronomy/execute")) {
// Add Meteor method.
connection.methods({
"/Astronomy/execute": astronomyExecute
});
}
}

// Add Meteor methods to the class.
Expand All @@ -35,6 +32,6 @@ function onFinalizeClass(Class, className) {
Class.prototype.applyMethod = applyMethod;
Class.prototype.callMethod = callMethod;
}
};
}

export default onFinalizeClass;
export default onFinalizeClass;
17 changes: 8 additions & 9 deletions lib/modules/methods/utils/wrapMethod.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _last from 'lodash/last';
import callMeteorMethod from '../../storage/utils/call_meteor_method';
import rawAll from '../../fields/utils/rawAll';
import _last from "lodash/last";
import callMeteorMethod from "../../storage/utils/call_meteor_method";
import rawAll from "../../fields/utils/rawAll";

function wrapMethod(methodName) {
return function(...methodArgs) {
Expand All @@ -11,17 +11,16 @@ function wrapMethod(methodName) {
let callback = _last(methodArgs);
// If the last argument is a callback function, then remove the last
// argument.
if (typeof callback === 'function') {
if (typeof callback === "function") {
methodArgs.pop();
}
// If the last argument is not a callback function, then clear the
// "callback" variable.
else {
} else {
// If the last argument is not a callback function, then clear the
// "callback" variable.
callback = undefined;
}
// Call the "/Astronomy/execute" method.
return doc.applyMethod(methodName, methodArgs, callback);
};
}

export default wrapMethod;
export default wrapMethod;
4 changes: 2 additions & 2 deletions lib/modules/storage/utils/callStandardMeteorMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ function callStandardMeteorMethod(Class, methodName, methodArgs, callback) {
const Collection = Class.getCollection();
let connection = Collection && Collection._connection;
if (!connection && (!Collection || !Collection._name)) {
connection = Meteor.connection;
connection = Meteor.connection || Meteor.server;
}
return connection.apply(methodName, methodArgs, callback);
}

export default callStandardMeteorMethod;
export default callStandardMeteorMethod;

0 comments on commit 1f6e484

Please sign in to comment.