Permalink
Browse files

added roles, counting, and push notifications

  • Loading branch information...
Leveton committed Feb 12, 2013
1 parent ddf0a33 commit 2f0c43f947321621d982f11895c0bdb7c2496266
Showing with 169 additions and 3 deletions.
  1. +48 −1 lib/Parse.js
  2. +121 −2 readme.md
View
@@ -33,7 +33,7 @@ Parse.prototype = {
// get a collection of objects
findMany: function (className, query, callback) {
- if (typeof query === 'string') {
+ if (typeof(query) === 'string') {
parseRequest.call(this, 'GET', '/1/classes/' + className + '/' + query, null, callback);
} else {
parseRequest.call(this, 'GET', '/1/classes/' + className, { where: JSON.stringify(query) }, callback);
@@ -124,6 +124,49 @@ Parse.prototype = {
data = { deviceType: deviceType, installationId: deviceToken, timeZone: timeZone, channels: channels }
}
parseRequest.call(this, 'POST', '/1/installations/', data, callback);
+ },
+
+ countObjects: function (className, query, callback) {
+ if (typeof(query) === "function") {
+ parseRequest.call(this, 'GET', '/1/classes/' + className, null, query);
+ }
+ if (typeof(query) === "string") {
+ parseRequest.call(this, 'GET', '/1/classes/' + className + '/' + query, null, callback);
+ } else {
+ parseRequest.call(this, 'GET', '/1/classes/' + className, { where: JSON.stringify(query) }, callback);
+ }
+ },
+
+ insertRole: function (data, callback) {
+ parseRequest.call(this, 'POST', '/1/roles/', data, callback);
+ },
+
+ getRole: function (objectId, callback) {
+ parseRequest.call(this, 'GET', '/1/roles/' + objectId, null, callback);
+ },
+
+ getRoles: function (params, callback) {
+ if (typeof(params) === "function") {
+ parseRequest.call(this, 'GET', '/1/roles/', null, params);
+ }
+ if (typeof(params) === "string") {
+ parseRequest.call(this, 'GET', '/1/roles/' + params, null, callback);
+ } else {
+ params = JSON.stringify(params);
+ parseRequest.call(this, 'GET', '/1/roles/', params, callback);
+ }
+ },
+
+ updateRole: function (objectId, data, callback) {
+ parseRequest.call(this, 'PUT', '/1/roles/' + objectId, data, callback);
+ },
+
+ deleteRole: function (objectId, callback) {
+ parseRequest.call(this, 'DELETE', '/1/roles/' + objectId, callback);
+ },
+
+ sendPush: function (data, callback) {
+ parseRequest.call(this, 'POST', '/1/push/', data, callback);
}
};
@@ -178,6 +221,10 @@ function parseRequest(method, path, data, callback, contentType) {
return callback(err);
}
+ // if ((!err) && (res.statusCode === 200 || res.statusCode === 201)) {
+ // res.success = res.statusCode;
+ // }
+
var json = '';
res.setEncoding('utf8');
View
123 readme.md
@@ -18,13 +18,25 @@ examples
var app = new Parse(APP_ID, MASTER_KEY);
-### insert
+### insert an object
// add a Foo object, { foo: 'bar' }
app.insert('Foo', { foo: 'bar' }, function (err, response) {
console.log(response);
});
+### insert a User
+
+ app.insertCustom('users', { foo: 'bar' }, function (err, response) {
+ console.log(response);
+ });
+
+### insert a User with GeoPoints
+
+ app.insertCustom('users', { foo: 'bar', location: {__type: 'GeoPoint', latitude: <int>, longitude: <int>} }, function (err, response) {
+ console.log(response);
+ });
+
### insert a file
app.insertFile(fileName, data, fileType, function (err, response) {
@@ -48,6 +60,13 @@ examples
console.log(response);
});
+### count the number of objects
+
+ //just use findMany, and call results.length on the response
+ app.findMany('Foo', { user: '<objectId>' }, function (err, response) {
+ console.log(response.results.length);
+});
+
### update
app.update('Foo', 'someId', { foo: 'fubar' }, function (err, response) {
@@ -122,4 +141,104 @@ examples
} else {
console.log(response);
}
- });
+ });
+
+### create a role for a particular user
+
+ //create a data object that links the user object's objectId to the role
+
+var data = {
+ name: 'Administrator',
+ ACL: {
+ "*": {
+ "read": true
+ }
+ },
+ roles: {
+ "__op": "AddRelation",
+ "objects": [
+ {
+ "__type": "Pointer",
+ "className": "_Role",
+ "objectId": "<objectId>"
+ }
+ ]
+ },
+ users: {
+ "__op": "AddRelation",
+ "objects": [
+ {
+ "__type": "Pointer",
+ "className": "_User",
+ "objectId": "<objectId>"
+ }
+ ]
+ }
+};
+
+ app.insertRole(data, function(err, resp){
+ console.log(resp);
+ });
+
+### get a role
+
+ //pass the role object's objectId
+ app.getRole("<objectId>", function(err, resp){
+ console.log(resp);
+ });
+
+### update a role
+ //pass the objectId of the role, data contains the user's objectId
+
+var data = {
+ users: {
+ "__op": "RemoveRelation",
+ "objects": [
+ {
+ "__type": "Pointer",
+ "className": "_User",
+ "objectId": "<objectId>"
+ }
+ ]
+ }
+};
+
+ app.updateRole("<objectId>", data, function(err, resp){
+ console.log(resp);
+ });
+
+### delete a role
+
+ //pass the objectId of the role
+ app.deleteRole("<objectId>", function(err, resp){
+
+ });
+
+### get all the roles
+
+ app.getRoles(function(err, resp){ //like so
+ console.log(resp);
+
+ });
+
+### get a role against a cetain param
+
+var params = {
+ where: { name: "Administrator" }
+};
+
+ app.getRoles(params, function(err, resp){
+ console.log(resp);
+ });
+
+### send a push notification
+
+var notification = {
+ channels: [''],
+ data: {
+ alert: "sending too many push notifications is obnoxious"
+ }
+};
+ app.sendPush(notification, function(err, resp){
+ console.log(resp);
+ });

0 comments on commit 2f0c43f

Please sign in to comment.