Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Added node commandline example
Browse files Browse the repository at this point in the history
  • Loading branch information
thedug committed Feb 16, 2015
1 parent 96f1703 commit b05bb8b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules/
.lone/
.idea/
npm-debug.log
*.pyc
23 changes: 23 additions & 0 deletions examples/node/README.md
Original file line number Diff line number Diff line change
@@ -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;
```
96 changes: 96 additions & 0 deletions examples/node/index.js
Original file line number Diff line number Diff line change
@@ -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();
});

9 changes: 9 additions & 0 deletions examples/node/json
Original file line number Diff line number Diff line change
@@ -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}]
20 changes: 20 additions & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (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"
}
}

0 comments on commit b05bb8b

Please sign in to comment.