From b05bb8bc354a03649490e1838e387ce00b2e1273 Mon Sep 17 00:00:00 2001 From: Douglas Ferguson Date: Sun, 15 Feb 2015 19:09:54 -0600 Subject: [PATCH] Added node commandline example --- .gitignore | 1 + examples/node/README.md | 23 +++++++++ examples/node/index.js | 96 ++++++++++++++++++++++++++++++++++++++ examples/node/json | 9 ++++ examples/node/package.json | 20 ++++++++ 5 files changed, 149 insertions(+) create mode 100644 examples/node/README.md create mode 100644 examples/node/index.js create mode 100644 examples/node/json create mode 100644 examples/node/package.json diff --git a/.gitignore b/.gitignore index 21e6589..62ed143 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store node_modules/ .lone/ +.idea/ npm-debug.log *.pyc diff --git a/examples/node/README.md b/examples/node/README.md new file mode 100644 index 0000000..b0cdca4 --- /dev/null +++ b/examples/node/README.md @@ -0,0 +1,23 @@ +```bash +# Requirements: you must have curl installed and the json module for pretty formatting: +npm install -g json; + +# Start our REST API server +npm start; + +# Create the pets +curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":1,\"type\":\"dog\"}" | json -i; +curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":2,\"type\":\"cat\"}" | json -i; +curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":3,\"type\":\"seabeast\"}" | json -i; + +# View them +curl -s -X GET -H "Accept: application/json" http://localhost:3000/api/v1/pets | json -i; + +# Use json patches to name them +curl -s -X PUT -H "Content-Type: application/json" http://localhost:3000/api/v1/pets/1 -d "[{\"op\":\"add\",\"path\":\"/name\",\"value\":\"Arlo\"}]" | json -i; +curl -s -X PUT -H "Content-Type: application/json" http://localhost:3000/api/v1/pets/2 -d "[{\"op\":\"add\",\"path\":\"/name\",\"value\":\"Basil\"}]" | json -i; +curl -s -X PUT -H "Content-Type: application/json" http://localhost:3000/api/v1/pets/3 -d "[{\"op\":\"test\",\"path\":\"/type\",\"value\":\"seabeast\"},{\"op\":\"replace\",\"path\":\"/type\",\"value\":\"cat\"},{\"op\":\"add\",\"path\":\"/name\",\"value\":\"Kochka\"}]" | json -i; + +# Now view them again: +curl -s -X GET -H "Accept: application/json" http://localhost:3000/api/v1/pets | json -i; +``` diff --git a/examples/node/index.js b/examples/node/index.js new file mode 100644 index 0000000..d773075 --- /dev/null +++ b/examples/node/index.js @@ -0,0 +1,96 @@ +var mongoose = require('mongoose'); +var jiff = require('jiff'); +var async = require('async'); +var patchesToMongo = require('../..'); + +mongoose.set('debug', true); + +// Models +var Pet = mongoose.model('Pet', { + _id: Number, + name: String, + type: String +}, 'pets'); + + +function getPet(id, callback){ + Pet.findOne({_id: id}, function(err, pet){ + if(err) return callback(err); + callback(null, pet.toJSON()); + }); +}; + +function updatePet(id, from, to){ + return function(done){ + console.log('update pet'); + var body = jiff.diff(from, to); //).replace(/"/g, '\\"'); + console.log(body); + getPet(id, function(err, pet){ + var patched = jiff.patch(body, pet); + var update = patchesToMongo(body); + Pet.update({_id: id}, update, function(err){ + console.log(err || patched); + done() + }); + }); + }; +} + +function createPet(body){ + return function(done) { + console.log('create pet'); + var pet = new Pet(body); + pet.save(function (err) { + console.log(err || pet); + done() + }); + } +} + +function listPets(callback){ + console.log('list pets'); + Pet.find(function(err, pet){ + console.log(err || pet); + callback() + }); +} + +// Bin +mongoose.connect('mongodb://localhost/test'); + +function cleanup(done){ + Pet.remove({}, function(err) { + console.log('Pet collection removed'); + done(err); + }); +} + +function setup(done) { + console.log('# Create the pets'); + async.series([ + createPet({_id: 1, type: 'dog'}), + createPet({_id: 2, type: 'cat'}), + createPet({_id: 3, type: 'seabeast'}) + ], function(err, result){ + done(err); + }); + +} + +function update(done) { + console.log('# Use json patches to name them'); + async.series([ + updatePet(1, {type: 'dog'}, {name: 'Arlo', type: 'dog'}), + updatePet(2, {type: 'cat'}, {name: 'Basil', type: 'cat'}), + updatePet(3, {type: 'seabeast'}, {name: 'Kochka', type: 'cat'}) + ], function(err, result){ + done(err); + }); +} + +async.series([listPets, cleanup, listPets, setup, listPets, update, listPets], + function(err, result){ + console.log('all done'); + mongoose.connection.close(); + }); + diff --git a/examples/node/json b/examples/node/json new file mode 100644 index 0000000..3b54d31 --- /dev/null +++ b/examples/node/json @@ -0,0 +1,9 @@ +HTTP/1.1 200 OK +X-Powered-By: Express +Content-Type: application/json; charset=utf-8 +Content-Length: 129 +ETag: W/"81-17478c4d" +Date: Tue, 06 Jan 2015 18:33:10 GMT +Connection: keep-alive + +[{"_id":1,"name":"","type":"dog","__v":0},{"_id":2,"name":"","type":"cat","__v":0},{"_id":3,"name":"","type":"seabeast","__v":0}] \ No newline at end of file diff --git a/examples/node/package.json b/examples/node/package.json new file mode 100644 index 0000000..da7579d --- /dev/null +++ b/examples/node/package.json @@ -0,0 +1,20 @@ +{ + "name": "jsonpatch-to-mongodb-example-express", + "version": "0.0.0", + "description": "Using jsonpatch-to-mongodb with express", + "dependencies": { + "jiff": "~0.7.0", + "mongoose": "~3.8.21", + "async": "~0.9.0" + }, + "scripts": { + "start": "node index.js" + }, + "author": "Lucas Hrabovsky (http://imlucas.com)", + "license": "MIT", + "homepage": "http://github.com/imlucas/jsonpatch-to-mongodb", + "repository": { + "type": "git", + "url": "git://github.com/imlucas/jsonpatch-to-mongodb.git" + } +}