From 36b159afdf90627db752ff062f7b01b7a8c2ad5a Mon Sep 17 00:00:00 2001 From: Ryan Glover Date: Mon, 2 Mar 2015 11:00:42 -0600 Subject: [PATCH] Add server/methods directory. Add /insert, /remove, /update, and /utility with examples to /server/methods. Closes #28. --- server/methods/insert/example.js | 20 ++++++++++++++++++++ server/methods/read/example.js | 22 ++++++++++++++++++++++ server/methods/remove/example.js | 20 ++++++++++++++++++++ server/methods/update/example.js | 24 ++++++++++++++++++++++++ server/methods/utility/example.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 server/methods/insert/example.js create mode 100644 server/methods/read/example.js create mode 100644 server/methods/remove/example.js create mode 100644 server/methods/update/example.js create mode 100644 server/methods/utility/example.js diff --git a/server/methods/insert/example.js b/server/methods/insert/example.js new file mode 100644 index 0000000..1bc32d0 --- /dev/null +++ b/server/methods/insert/example.js @@ -0,0 +1,20 @@ +/* +* Methods: Insert - Example +* Example of a method used for inserting into the database. +*/ + +Meteor.methods({ + exampleInsertMethod: function(argument){ + // Check the argument. Assuming an Object type here. + check(argument, Object); + + // Perform the insert. + try { + var exampleId = Example.insert(argument); + return exampleId; + } catch(exception) { + // If an error occurs, return it to the client. + return exception; + } + } +}); diff --git a/server/methods/read/example.js b/server/methods/read/example.js new file mode 100644 index 0000000..1c54af7 --- /dev/null +++ b/server/methods/read/example.js @@ -0,0 +1,22 @@ +/* +* Methods: Read - Example +* Example of a method used for reading from the database. +*/ + +Meteor.methods({ + exampleReadMethod: function(argument){ + // Check the argument. Assuming a String type here. + check(argument, String); + + // Perform the read. + var exampleItem = Example.findOne(argument); + + // If the read fails (no documents found), throw an error. + if (!exampleItem) { + throw new Meteor.Error(500, 'Error 500: Not Found', 'No documents found.'); + } + + // Return either the result or the error. + return exampleItem; + } +}); diff --git a/server/methods/remove/example.js b/server/methods/remove/example.js new file mode 100644 index 0000000..9c8058e --- /dev/null +++ b/server/methods/remove/example.js @@ -0,0 +1,20 @@ +/* +* Methods: Remove - Example +* Example of a method used for removing a document from the database. +*/ + +Meteor.methods({ + exampleRemoveMethod: function(argument){ + // Check the argument. Assuming a String type here. + check(argument, String); + + // Perform the remove. + try { + var exampleId = Example.remove(argument); + return exampleId; + } catch(exception) { + // If an error occurs, return it to the client. + return exception; + } + } +}); diff --git a/server/methods/update/example.js b/server/methods/update/example.js new file mode 100644 index 0000000..a7cacd3 --- /dev/null +++ b/server/methods/update/example.js @@ -0,0 +1,24 @@ +/* +* Methods: Update - Example +* Example of a method used for updating a document in the database. +*/ + +Meteor.methods({ + exampleUpdateMethod: function(argument){ + // Check the argument. Assuming an Object type here. + check(argument, Object); + + // Perform the update. + try { + var exampleId = Example.update(argument._id, { + $set: { + "someKey": argument.someKey + } + }); + return exampleId; + } catch(exception) { + // If an error occurs, return it to the client. + return exception; + } + } +}); diff --git a/server/methods/utility/example.js b/server/methods/utility/example.js new file mode 100644 index 0000000..c012483 --- /dev/null +++ b/server/methods/utility/example.js @@ -0,0 +1,28 @@ +/* +* Methods: Utility - Example +* Example of a method used for performing a function on the server. +*/ + +// Example third-party API stub to call. +// This should be deleted and is only here as an example. +var chipotle = { + getBurrito: function(burrito){ + return burrito; + } +} + +Meteor.methods({ + exampleUtilityMethod: function(argument){ + // Check the argument. Assuming an Object type here. + check(argument, Object); + + // Perform the function. + try { + var apiCall = chipotle.getBurrito("Barbacoa"); + return apiCall; + } catch(exception) { + // If an error occurs, return it to the client. + return exception; + } + } +}); -- 2.0.0