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

Commit b05bb8b

Browse files
committed
Added node commandline example
1 parent 96f1703 commit b05bb8b

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
node_modules/
33
.lone/
4+
.idea/
45
npm-debug.log
56
*.pyc

examples/node/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```bash
2+
# Requirements: you must have curl installed and the json module for pretty formatting:
3+
npm install -g json;
4+
5+
# Start our REST API server
6+
npm start;
7+
8+
# Create the pets
9+
curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":1,\"type\":\"dog\"}" | json -i;
10+
curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":2,\"type\":\"cat\"}" | json -i;
11+
curl -s -X POST -H "Content-Type: application/json" http://localhost:3000/api/v1/pets -d "{\"_id\":3,\"type\":\"seabeast\"}" | json -i;
12+
13+
# View them
14+
curl -s -X GET -H "Accept: application/json" http://localhost:3000/api/v1/pets | json -i;
15+
16+
# Use json patches to name them
17+
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;
18+
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;
19+
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;
20+
21+
# Now view them again:
22+
curl -s -X GET -H "Accept: application/json" http://localhost:3000/api/v1/pets | json -i;
23+
```

examples/node/index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var mongoose = require('mongoose');
2+
var jiff = require('jiff');
3+
var async = require('async');
4+
var patchesToMongo = require('../..');
5+
6+
mongoose.set('debug', true);
7+
8+
// Models
9+
var Pet = mongoose.model('Pet', {
10+
_id: Number,
11+
name: String,
12+
type: String
13+
}, 'pets');
14+
15+
16+
function getPet(id, callback){
17+
Pet.findOne({_id: id}, function(err, pet){
18+
if(err) return callback(err);
19+
callback(null, pet.toJSON());
20+
});
21+
};
22+
23+
function updatePet(id, from, to){
24+
return function(done){
25+
console.log('update pet');
26+
var body = jiff.diff(from, to); //).replace(/"/g, '\\"');
27+
console.log(body);
28+
getPet(id, function(err, pet){
29+
var patched = jiff.patch(body, pet);
30+
var update = patchesToMongo(body);
31+
Pet.update({_id: id}, update, function(err){
32+
console.log(err || patched);
33+
done()
34+
});
35+
});
36+
};
37+
}
38+
39+
function createPet(body){
40+
return function(done) {
41+
console.log('create pet');
42+
var pet = new Pet(body);
43+
pet.save(function (err) {
44+
console.log(err || pet);
45+
done()
46+
});
47+
}
48+
}
49+
50+
function listPets(callback){
51+
console.log('list pets');
52+
Pet.find(function(err, pet){
53+
console.log(err || pet);
54+
callback()
55+
});
56+
}
57+
58+
// Bin
59+
mongoose.connect('mongodb://localhost/test');
60+
61+
function cleanup(done){
62+
Pet.remove({}, function(err) {
63+
console.log('Pet collection removed');
64+
done(err);
65+
});
66+
}
67+
68+
function setup(done) {
69+
console.log('# Create the pets');
70+
async.series([
71+
createPet({_id: 1, type: 'dog'}),
72+
createPet({_id: 2, type: 'cat'}),
73+
createPet({_id: 3, type: 'seabeast'})
74+
], function(err, result){
75+
done(err);
76+
});
77+
78+
}
79+
80+
function update(done) {
81+
console.log('# Use json patches to name them');
82+
async.series([
83+
updatePet(1, {type: 'dog'}, {name: 'Arlo', type: 'dog'}),
84+
updatePet(2, {type: 'cat'}, {name: 'Basil', type: 'cat'}),
85+
updatePet(3, {type: 'seabeast'}, {name: 'Kochka', type: 'cat'})
86+
], function(err, result){
87+
done(err);
88+
});
89+
}
90+
91+
async.series([listPets, cleanup, listPets, setup, listPets, update, listPets],
92+
function(err, result){
93+
console.log('all done');
94+
mongoose.connection.close();
95+
});
96+

examples/node/json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
HTTP/1.1 200 OK
2+
X-Powered-By: Express
3+
Content-Type: application/json; charset=utf-8
4+
Content-Length: 129
5+
ETag: W/"81-17478c4d"
6+
Date: Tue, 06 Jan 2015 18:33:10 GMT
7+
Connection: keep-alive
8+
9+
[{"_id":1,"name":"","type":"dog","__v":0},{"_id":2,"name":"","type":"cat","__v":0},{"_id":3,"name":"","type":"seabeast","__v":0}]

examples/node/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "jsonpatch-to-mongodb-example-express",
3+
"version": "0.0.0",
4+
"description": "Using jsonpatch-to-mongodb with express",
5+
"dependencies": {
6+
"jiff": "~0.7.0",
7+
"mongoose": "~3.8.21",
8+
"async": "~0.9.0"
9+
},
10+
"scripts": {
11+
"start": "node index.js"
12+
},
13+
"author": "Lucas Hrabovsky <[email protected]> (http://imlucas.com)",
14+
"license": "MIT",
15+
"homepage": "http://github.com/imlucas/jsonpatch-to-mongodb",
16+
"repository": {
17+
"type": "git",
18+
"url": "git://github.com/imlucas/jsonpatch-to-mongodb.git"
19+
}
20+
}

0 commit comments

Comments
 (0)