Created
June 5, 2012 18:50
-
-
Save zuk/2876909 to your computer and use it in GitHub Desktop.
DrowsyDromedary usage with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This line might not be necessary, but try it if you see weird HTTP errors. | |
jQuery.support.cors = true; | |
var drowsyUrl = "http://localhost:9292"; | |
/** List available databases **/ | |
jQuery.ajax(drowsyUrl, { | |
type: 'get', | |
success: function (dbs) { | |
console.log(dbs); | |
} | |
}); | |
/** List available collections in a database **/ | |
var dbName = 'fridge'; | |
jQuery.ajax(drowsyUrl + '/' + dbName, { | |
type: 'get', | |
success: function (collections) { | |
console.log(collections); | |
} | |
}); | |
/** List all items in a collection **/ | |
var collName = 'vegetables'; | |
jQuery.ajax(drowsyUrl + '/' + dbName + '/' + collName, { | |
type: 'get', | |
success: function (items) { | |
console.log(items); | |
} | |
}); | |
/** List a subset of items in a collection, sorted by the property 'colour' in descending order **/ | |
var match = { colour: "green", quantity: { '$gt': 10 }}; | |
var sort = ['colour','DESC']; | |
jQuery.ajax(drowsyUrl + '/' + dbName + '/' + collName, { | |
type: 'get', | |
data: { selector: JSON.stringify(match), sort: JSON.stringify(sort) }, | |
success: function (items) { | |
console.log(items); | |
} | |
}); | |
/** Add an item to a collection **/ | |
var newItem = { colour: "green", quantity: 1, type: "broccoli"}; | |
jQuery.ajax(drowsyUrl + '/' + dbName + '/' + collName, { | |
type: 'post', | |
data: newItem, | |
success: function (item) { | |
console.log(item); | |
} | |
}); | |
/** Replace an item in a collection **/ | |
var item = { colour: "green", quantity: 1, type: "broccoli"}; | |
var itemId = '4deeb1d9349c85523b000002'; | |
jQuery.ajax(drowsyUrl + '/' + dbName + '/' + collName + '/' + itemId, { | |
type: 'put', | |
data: item, | |
success: function (item) { | |
console.log(item); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment