Socket
Socket
Sign inDemoInstall

node-parse-api

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.5 to 0.3.6

test/testRestApiKey.js

269

lib/Parse.js

@@ -8,7 +8,7 @@ var qs = require('querystring');

function Parse(options_or_application_id, master_key) {
this._options = {}
if(master_key){
this._options = {};
if (master_key) {
this._options.app_id = options_or_application_id;
this._options.master_key = master_key;
}else{
} else {
this._options = options_or_application_id;

@@ -33,27 +33,58 @@ }

// create a new user
insertUser: function (data, callback) {
parseRequest.call(this, 'POST', '/1/users/', data, callback);
},
// get an object from the class store
find: function (className, query, callback) {
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);
var url = '/1/' + (className === '_User' ? 'users' : 'classes/' + className);
var queryType = typeof query;
if ( queryType === 'string' ) {
url += '/' + query;
} else if ( queryType === 'object' ) {
// if the user wants to add 'include' or 'key' (or other types of) constraints while getting only one object
// objectId can be added to the query object and is deleted after it's appended to the url
if ( query.hasOwnProperty('objectId') ) {
url += '/' + query.objectId;
delete query.objectId;
}
// check to see if there is a 'where' object in the query object
// the 'where' object need to be stringified by JSON.stringify(), not querystring
if ( query.hasOwnProperty('where') ) {
url += '?where=' + JSON.stringify(query.where);
delete query.where;
}
// if there are no more constraints left in the query object 'remainingQuery' will be an empty string
var remainingQuery = qs.stringify(query);
if ( remainingQuery ) {
url += ( url.indexOf('?') === -1 ? '?' : '&' ) + remainingQuery;
}
}
parseRequest.call(this, 'GET', url, null, callback);
},
// get a collection of objects
findMany: function (className, query, order, limit, callback) {
// also supports the following api - findMany: function (className, query, callback)
findMany: function (className, query, order, limit, skip, callback) {
console.warn('"findMany" is deprecated, use "find" instead.');
// if only 3 arguments exist, third argument is the callback, order and limit are undefined
if (arguments.length === 3) {
var callback = order;
var order = undefined;
var limit = 9999;
switch ( arguments.length ) {
case 3: callback = order;
break;
case 5: query.order = order;
query.limit = limit;
callback = skip;
break;
case 6: query.order = order;
query.limit = limit;
query.skip = skip;
break;
default: throw new Error('Unexpected number of arguments.');
}
if (typeof(query) === 'string') {
parseRequest.call(this, 'GET', '/1/classes/' + className + '/' + query, null, callback);
} else {
parseRequest.call(this, 'GET', '/1/classes/' + className, {limit: limit, order:order, where: JSON.stringify(query) }, callback);
}
this.find(className, query, callback);
},

@@ -66,14 +97,44 @@

// get a user from the Parse's special User class. See https://parse.com/questions/why-does-querying-for-a-user-create-a-second-user-class
getUser: function (userName, passWord, callback) {
parseRequest.call(this, 'GET', '/1/login/?username=' + userName + '&password=' + passWord, null, callback);
// user login
loginUser: function (username, password, callback) {
parseRequest.call(this, 'GET', '/1/login/?username=' + username + '&password=' + password, null, callback);
},
// retrieve current user
me: function (sessionToken, callback) {
parseRequest.call(this, 'GET', '/1/users/me', null, callback, null, sessionToken);
},
// retrieve contents of one or more user objects
findUser: function (query, callback) {
if ( arguments.length !== 2 ) {
throw new Error('Unexpected number of arguments.');
} else {
this.find('_User', query, callback);
}
},
// get an object belonging to a certain User
getFileByUser: function(userId, className, callback) {
queryString = 'where={"user":' + '"' + userId + '"' + '}'
findFileByUser: function(userId, className, callback) {
queryString = 'where={"user":' + '"' + userId + '"' + '}';
encodedString = encodeURIComponent(queryString);
parseRequest.call(this, 'GET', '/1/classes/' + className + '?' + encodedString, null, callback)
parseRequest.call(this, 'GET', '/1/classes/' + className + '?' + encodedString, null, callback);
},
getUser: function (query, callback, deprecatedCallback) {
console.warn('getUser is deprecated, user findUser instead.');
if ( arguments.length === 3 ) {
console.warn('Logging in with "getUser" is deprecated, use loginUser instead.');
console.warn('Use "findUser" to retrieve one or more user object contents.');
this.loginUser(query, callback, deprecatedCallback);
} else {
this.findUser(query, callback);
}
},
getFileByUser: function(userId, className, callback) {
console.warn('getFileByUser is deprecated, user findFileByUser instead.');
this.findFileByUser(userId, className, callback);
},
// insert an object into Parse

@@ -90,17 +151,40 @@ insertCustom: function (className, object, callback) {

// update a User object's email address
updateUserEmail: function(objectId, data, callback) {
data = { email: data }
parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
updateUserEmail: function(objectId, data, sessionToken, callback) {
switch ( arguments.length ) {
case 3: callback = sessionToken;
break;
case 4: break;
default: throw new Error('Unexpected number of arguments.');
}
data = { email: data };
parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback, null, sessionToken);
},
// update a User object's username*
updateUserName: function(objectId, data, callback) {
data = { username: data }
parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback)
updateUserName: function(objectId, data, sessionToken, callback) {
switch ( arguments.length ) {
case 3: callback = sessionToken;
break;
case 4: break;
default: throw new Error('Unexpected number of arguments.');
}
data = { username: data };
parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback, null, sessionToken);
},
// update any keys of a User object
updateUser: function (objectId, data, sessionToken, callback) {
switch ( arguments.length ) {
case 3: callback = sessionToken;
break;
case 4: break;
default: throw new Error('Unexpected number of arguments.');
}
parseRequest.call(this, 'PUT', '/1/users/' + objectId, data, callback, null, sessionToken);
},
// reset a User object's password
passwordReset: function (data, callback) {
data = { email: data }
parseRequest.call(this, 'POST', '/1/requestPasswordReset/', data, callback)
data = { email: data };
parseRequest.call(this, 'POST', '/1/requestPasswordReset/', data, callback);
},

@@ -113,9 +197,36 @@

// remove an object from the class store
deleteUser: function (objectId, sessionToken, callback) {
switch ( arguments.length ) {
case 2: callback = sessionToken;
break;
case 3: break;
default: throw new Error('Unexpected number of arguments');
}
parseRequest.call(this, 'DELETE', '/1/users/' + objectId, null, callback, null, sessionToken);
},
deleteAll: function(className, callback){
var that = this;
this.find(className, '', function (err, response) {
var requests = toDeleteOps(className, response.results);
that.batch(requests, callback);
});
},
deleteAllUsers: function(callback){
var that = this;
this.find('_User', '', function (err, response) {
var requests = toDeleteOps('_User', response.results);
that.batch(requests, callback);
});
},
// upload installation data
insertInstallationData: function (deviceType, deviceToken, callback) {
if (deviceType === 'ios'){
data = { deviceType: deviceType, deviceToken: deviceToken }
data = { deviceType: deviceType, deviceToken: deviceToken };
}
else {
data = { deviceType: deviceType, installationId: deviceToken }
data = { deviceType: deviceType, installationId: deviceToken };
}

@@ -127,6 +238,6 @@ parseRequest.call(this, 'POST', '/1/installations/', data, callback);

if (deviceType === 'ios'){
data = { deviceType: deviceType, deviceToken: deviceToken, timeZone: timeZone }
data = { deviceType: deviceType, deviceToken: deviceToken, timeZone: timeZone };
}
else {
data = { deviceType: deviceType, installationId: deviceToken, timeZone: timeZone }
data = { deviceType: deviceType, installationId: deviceToken, timeZone: timeZone };
}

@@ -138,6 +249,6 @@ parseRequest.call(this, 'POST', '/1/installations/', data, callback);

if (deviceType === 'ios'){
data = { deviceType: deviceType, deviceToken: deviceToken, channels: channels }
data = { deviceType: deviceType, deviceToken: deviceToken, channels: channels };
}
else {
data = { deviceType: deviceType, installationId: deviceToken, channels: channels }
data = { deviceType: deviceType, installationId: deviceToken, channels: channels };
}

@@ -149,6 +260,6 @@ parseRequest.call(this, 'POST', '/1/installations/', data, callback);

if (deviceType === 'ios'){
data = { deviceType: deviceType, deviceToken: deviceToken, timeZone: timeZone, channels: channels }
data = { deviceType: deviceType, deviceToken: deviceToken, timeZone: timeZone, channels: channels };
}
else {
data = { deviceType: deviceType, installationId: deviceToken, timeZone: timeZone, channels: channels }
data = { deviceType: deviceType, installationId: deviceToken, timeZone: timeZone, channels: channels };
}

@@ -164,2 +275,6 @@ parseRequest.call(this, 'POST', '/1/installations/', data, callback);

getInstallationData: function(callback) {
parseRequest.call(this, 'GET', '/1/installations', null, callback);
},
getInstallationDataForDeviceToken: function(deviceToken, callback) {

@@ -169,2 +284,12 @@ parseRequest.call(this, 'GET', '/1/installations?where={"deviceToken":"'+deviceToken+'"}', null, callback);

deleteInstallation: function(objectId, callback) {
parseRequest.call(this, 'DELETE', '/1/installations/' + objectId, null, callback);
},
upsertInstallation: function(deviceType, deviceToken, data, callback) {
data.deviceType = deviceType;
data.deviceToken = deviceToken;
parseRequest.call(this, 'POST', '/1/installations', data, callback);
},
insertOrUpdateInstallationDataWithChannels: function(deviceType, deviceToken, channels, callback) {

@@ -193,3 +318,3 @@ var that = this;

addRelation: function( relationName, className1, objectId1, className2, objectId2, callback) {
data = {}
data = {};
data[relationName] = { __op:"AddRelation",objects:[{__type:"Pointer",className:className2,objectId:objectId2}]};

@@ -200,3 +325,3 @@ parseRequest.call(this,'PUT','/1/classes/' + className1+'/'+objectId1,data,callback);

removeRelation: function( relationName, className1, objectId1, className2, objectId2, callback) {
data = {}
data = {};
data[relationName] = { __op:"RemoveRelation",objects:[{__type:"Pointer",className:className2,objectId:objectId2}]};

@@ -237,10 +362,2 @@ parseRequest.call(this,'PUT','/1/classes/' + className1+'/'+objectId1,data,callback);

parseRequest.call(this, 'POST', '/1/push/', data, callback);
},
deleteAll: function(modelName, callback){
var that = this;
this.findMany(modelName, '', function(err, response){
var requests = toDeleteOps(modelName, response.results);
that.batch(requests, callback);
});
}

@@ -250,3 +367,3 @@ };

// Parse.com https api request
function parseRequest(method, path, data, callback, contentType) {
function parseRequest(method, path, data, callback, contentType, sessionToken) {
var headers = {

@@ -259,5 +376,11 @@ Connection: 'Keep-alive'

headers.Authorization = auth;
if ( sessionToken ) {
throw new Error('Can\'t use session tokens while using the master key.');
}
}else if(this._options.api_key){
headers['X-Parse-Application-Id'] = this._options.app_id;
headers['X-Parse-REST-API-Key'] = this._options.api_key;
if ( sessionToken ) {
headers['X-Parse-Session-Token'] = sessionToken;
}
}

@@ -275,7 +398,7 @@

case 'POST':
body = typeof data === 'object' ? JSON.stringify(data) : data;
headers['Content-length'] = Buffer.byteLength(body);
case 'PUT':
body = typeof data === 'object' ? JSON.stringify(data) : data;
headers['Content-length'] = Buffer.byteLength(body);
body = contentType ? data : typeof data === 'object' ? JSON.stringify(data) : data;
if ( !contentType ) {
headers['Content-length'] = Buffer.byteLength(body);
}
headers['Content-type'] = contentType || 'application/json';

@@ -303,15 +426,2 @@ break;

if (res.statusCode < 200 || res.statusCode >= 300) {
var err = new Error('HTTP error ' + res.statusCode);
err.arguments = arguments;
err.type = res.statusCode;
err.options = options;
err.body = body;
return callback(err);
}
// if ((!err) && (res.statusCode === 200 || res.statusCode === 201)) {
// res.success = res.statusCode;
// }
var json = '';

@@ -325,9 +435,13 @@ res.setEncoding('utf8');

res.on('end', function () {
var err = null;
var data = null;
var data;
try {
var data = JSON.parse(json);
} catch (err) {
data = JSON.parse(json);
if ( data.code || data.error ) {
throw (data);
}
callback(null, data);
}
callback(err, data);
catch (err) {
callback(err);
}
});

@@ -352,6 +466,5 @@

method: 'DELETE',
path: '/1/classes/' + className + '/' + object.objectId,
body: {}
}
path: '/1/' + ( className === '_User' ? 'users/' : 'classes/' + className + '/' ) + object.objectId
};
});
}
}
{
"name": "node-parse-api",
"description": "A Parse.com REST API client for Node.js",
"version": "0.3.5",
"version": "0.3.6",
"author": "Chris Johnson <tenorviol@yahoo.com>, Michael Leveton <mleveton@prepcloud.com>, Seth Gholson",
"contributors": [
"Daniel Gasienica <daniel@gasienica.ch>",
"Jakub Knejzlík"
"Jakub Knejzlík",
"Sam Saccone",
"Arnaud Rinquin",
"Charles Julian Knight",
"Barrington Haynes",
"Christian Monaghan",
"Christoffer Niska",
"Rodrigo Martell",
"Joe Bruggeman",
"Omar A",
"Andrey",
"Rafael Galdêncio",
"Akhmad Fathonih",
"Kody J. Peterson",
"Jo Jordens",
"Appsaloon"
],

@@ -10,0 +25,0 @@ "main": "index",

@@ -7,269 +7,533 @@ Node Parse API

npm install node-parse-api
```
npm install node-parse-api
```
examples
--------
### setup with MASTER_KEY (old way)
### setup with MASTER_KEY
var Parse = require('node-parse-api').Parse;
```javascript
var Parse = require('node-parse-api').Parse;
var APP_ID = ...;
var MASTER_KEY = ...;
var APP_ID = ...;
var MASTER_KEY = ...;
var app = new Parse(APP_ID, MASTER_KEY);
var app = new Parse(APP_ID, MASTER_KEY);
```
### setup with API_KEY (new way)
### setup with API_KEY
var Parse = require('node-parse-api').Parse;
```javascript
var Parse = require('node-parse-api').Parse;
var options = {
app_id:'...',
api_key:'...' // master_key:'...' could be used too
}
var options = {
app_id:'...',
api_key:'...' // master_key:'...' could be used too
}
var app = new Parse(options);
var app = new Parse(options);
```
### insert an object
// add a Foo object, { foo: 'bar' }
app.insert('Foo', { foo: 'bar' }, function (err, response) {
console.log(response);
});
* insert(className `string`, data `object`, callback `function`)
```javascript
// add a Foo object, { foo: 'bar' }
app.insert('Foo', { foo: 'bar' }, function (err, response) {
console.log(response);
});
```
### insert a User
app.insertCustom('users', {
username: 'tom@j.com',
password: 'wow'
}, function (err, response) {
console.log(response);
});
* insertUser(data `object`, callback `function`)
```javascript
app.insertUser({
username: 'foo',
password: 'bar'
}, function (err, response) {
console.log(response);
});
```
More properties can be provided, but username and password are required.
### insert a User with a Pointer
```javascript
app.insertUser({
username: 'foo',
password: 'bar',
pointer/*can have any name*/: {
__type: 'Pointer',
className: <string>,
objectId: <string>
}
}, 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);
});
```javascript
app.insertUser({
username: 'foo',
password: 'bar',
location: {
__type: 'GeoPoint',
latitude: <int>,
longitude: <int>
}
}, function (err, response) {
console.log(response);
});
```
### user login
* loginUser(username `string`, password `string`, callback `function`)
Response contains all of the user fields except password, also includes a sessionToken for this user.
```javascript
app.loginUser('foo', 'bar', function (error, response) {
// response = {sessionToken: '', createdAt: '', ... }
});
```
### me
* me(sessionToken `string`, callback `function`)
```javascript
app.me('sessionToken', function (error, response) {
// response is same as getUser response
});
```
### insert a file
app.insertFile(fileName, data, fileType, function (err, response) {
fileLink = response.url;
parseName = response.name;
app.insert('Foo', { "foo" : fileLink, "bar" : parseName }, function(erro, res){
})
});
* insertFile(fileName `string`, data `string/buffer`, contentType `string`, callback `function`)
```javascript
// first upload the file to the parse cloud
app.insertFile('foo.txt', 'bar', 'text/plain', function (err, response) {
// then insert a new object with the link to the new file
app.insert('MyFile', {__type: 'File', "name": response.name }, function (error, response) {
});
});
```
### find one
// the Foo with id = 'someId'
app.find('Foo', 'someId', function (err, response) {
console.log(response);
});
* find(className `string`, query `object`, callback `function`)
```javascript
// the Foo with id = 'someId'
app.find('Foo', {objectId: 'someId'}, function (err, response) {
console.log(response);
});
```
Returned fields can be restricted with the 'keys' query.
```javascript
var query = {
objectId: 'someId',
keys: 'foo,bar'
};
app.find('Foo', query, function (error, response) {
//response object will only contain foo and bar fields, as well as the special built-in fields (objectId, createdAt and updatedAt)
});
```
### find many
// all Foo objects with foo = 'bar'
app.findMany('Foo', { foo: 'bar' }, function (err, response) {
console.log(response);
});
* find(className `string`, query `object`, callback `function`)
```javascript
// all Foo objects with foo = 'bar'
app.find('Foo', {where: {foo: 'bar'}}, function (err, response) {
console.log(response);
});
// all Foo objects
app.findMany('Foo', '', function (err, response) {
console.log(response);
}):
// all Foo objects
// '', null, undefined or any other falsy value will work
app.find('Foo', '', function (err, response) {
console.log(response);
}):
```
All types of query constraints Parse provides can be added to the query object as properties. (order, limit, keys, count, include...)
```javascript
var query = {
where: {
foo: 'bar',
baz: 'qux'
},
limit: 10,
skip: 5,
order: '-createdAt'
};
app.find('Foo', query, function (error, response ) {
// the first 5 results will be ignored and the next 10 results will be returned
// response.results will contain up to 10 objects with foo = 'bar' and baz = 'qux', sorted from latest to oldest
});
```
### find one user
* getUser(query `object`, callback `function`)
```javascript
app.find({objectId: 'someId'}, function (err, response) {
console.log(response);
});
```
### find many users
* getUser(query `object`, callback `function`)
```javascript
// all users with foo = 'bar'
app.find({where: {foo: 'bar'}}, function (err, response) {
console.log(response);
});
// all users
// '', null, undefined or any other falsy value will work
app.find('', function (err, response) {
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);
});
```javascript
### update
var query = {
count: 1,
limit: 0
};
app.find('Foo', query, function (error, response) {
// {
// results: [],
// count: 123
// }
});
```
app.update('Foo', 'someId', { foo: 'fubar' }, function (err, response) {
console.log(response);
});
### edit an object
### delete
* update(className `string`, objectId `string`, callback `function`)
app.delete('Foo', 'someId', function (err) {
// nothing to see here
});
```javascript
app.update('Foo', 'someId', {foo: 'bar'}, function (err, response) {
console.log(response);
});
```
### deleteAll
### delete an object
app.deleteAll('Foo', function (err) {
// nothing to see here
});
* delete(className `string`, objectId `string`, callback `function`)
```javascript
app.delete('Foo', 'someId', function (err, response) {
// response = {}
});
```
### delete all objects in a class
* deleteAll(className `string`, callback `function`)
```javascript
app.deleteAll('Foo', function (err, response) {
// response = [{success: {}, success: {}, ... }]
});
```
### delete user
* deleteUser(objectId `string`, [sessionToken `string`], callback `function`)
If you are using the master key you don't need any session tokens.
```javascript
app.deleteUser('someId', function (err, response) {
// response = {}
});
```
If you're using the rest api key you will need a session token and will only be able to delete the user object of the matching user.
```javascript
app.deleteUser('someId', 'sessionToken', function (error, response) {
// response = {}
});
```
### delete all users
* deleteAllUsers(callback `function`)
This will only work when using the master key.
```javascript
app.deleteAllUsers(function (err, response) {
// response = [{success: {}, success: {}, ... }]
});
```
### reset a password
//email is built into Parse's special User class
app.passwordReset(email, function(err, response){
console.log(response);
});
* passwordReset(data `string`, callback `function`)
### update User email
```javascript
//email is built into Parse's special User class
app.passwordReset(email, function(err, response){
console.log(response);
});
```
//email is built into Parse's special User class
app.updateUserEmail(objectId, email, function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
### edit a user object
* updateUser(objectId `string`, data `object`, [sessionToken `string`], callback `function`)
With master key
```javascript
app.updateUser('someId', {email: 'foo@example.com'}, function(err, response){
console.log(response);
});
```
or with rest api key
```javascript
app.updateUser('someId', {email: 'foo@example.com'}, 'sesstionToken', function(err, response){
console.log(response);
});
```
### batch requests
* batch(requests `array`, callback `function`)
```javascript
var requests = [
{
method: 'POST',
path: '/1/classes/Foo',
body: {
foo: 'bar1',
baz: 'qux1'
}
},
{
method: 'POST',
path: '/1/classes/Foo',
body: {
foo: 'bar2',
baz: 'qux2'
}
}
];
app.batch(requests, function (error, response) {
// response = [{success: {createdAt: '', objectId: ''}, {success: {...}}}]
});
```
### insert installation data
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId.
app.insertInstallationData("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```javascript
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId.
app.insertInstallationData("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```
### insert installation data with timeZone
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the timezone string.
app.insertInstallationDataWithTimeZone("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "EST", function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```javascript
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the timezone string.
app.insertInstallationDataWithTimeZone("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "EST", function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```
### insert installation data with channels
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the channels array.
arr = ["news", "sports"];
app.insertInstallationDataWithChannels("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", arr, function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```javascript
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the channels array.
arr = ["news", "sports"];
app.insertInstallationDataWithChannels("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", arr, function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```
### insert installation data with timeZone and channels
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the timezone string. 4th is the channels array.
arr = ["news", "sports"];
app.insertInstallationDataWithTimeZoneAndChannels("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "EST", arr, function(err, response){
if (err) {
console.log(err);
} else {
console.log(response);
}
});
```javascript
//first arg is either 'ios' or 'android'. second arg is either the Apple deviceToken or the Android installationId. Third arg is the timezone string. 4th is the channels array.
arr = ["news", "sports"];
app.insertInstallationDataWithTimeZoneAndChannels("ios", "0123456784abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "EST", arr, function(err, response){
if (err) {
console.log(err);
} 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
```javascript
//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>"
}
]
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);
});
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);
});
```javascript
//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>"
}
]
```javascript
//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);
});
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){});
```javascript
//pass the objectId of the role
app.deleteRole("<objectId>", function(err, resp){});
```
### get all the roles
app.getRoles(function(err, resp){});
```javascript
app.getRoles(function(err, resp){});
```
### get a role against a cetain param
var params = {
where: { name: "Administrator" }
};
```javascript
var params = {
where: { name: "Administrator" }
};
app.getRoles(params, function(err, resp){
console.log(resp);
});
app.getRoles(params, function(err, resp){
console.log(resp);
});
```
### send a push notification
//The data param has to follow the data structure as described in the [Parse REST API](https://www.parse.com/docs/rest#push)
var notification = {
channels: [''],
data: {
alert: "sending too many push notifications is obnoxious"
}
};
app.sendPush(notification, function(err, resp){
console.log(resp);
});
```javascript
//The data param has to follow the data structure as described in the [Parse REST API](https://www.parse.com/docs/rest#push)
var notification = {
channels: [''],
data: {
alert: "sending too many push notifications is obnoxious"
}
};
app.sendPush(notification, function(err, resp){
console.log(resp);
});
```
### note on sending dates
//when inserting a data, you must use the Parse date object structure, i.e.:
{
"__type": "Date",
"iso": new Date("<year>", "<month>", "<day>").toJSON()
}
```javascript
//when inserting a data, you must use the Parse date object structure, i.e.:
{
"__type": "Date",
"iso": new Date("<year>", "<month>", "<day>").toJSON()
}
```
# License
node-parse-api is available under the MIT license.
Copyright © 2015 Mike Leveton and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@@ -0,1 +1,2 @@

// this file runs tests against the master key
var Parse = require('../index').Parse;

@@ -19,113 +20,267 @@

var className2 = 'NodeParseApiRelationTest';
var object = { foo: Math.floor(Math.random() * 10000) }; // ERROR: if you change the type
var object2 = { foo: Math.floor(Math.random() * 10000) }; // ERROR: if you change the type
var object3 = { foo: Math.floor(Math.random() * 10000) }; // ERROR: if you change the type
var object4 = { foo: Math.floor(Math.random() * 10000) }; // ERROR: if you change the type
var stub;
var stub1;
var stub2;
var stub3;
var stubRelation;
exports.insert = function (assert) {
parse.insert(className, object, function (err, response) {
err && console.log(err);
assert.ok(response);
stub = response;
assert.done();
exports.insert = function (test) {
var data = {
foo: 'bar0',
baz: 'qux0',
quux: 'quuux'
};
parse.insert(className, data, function (error, response) {
test.expect(1);
test.ok(!error, 'There shoudn\'t be an error object.');
stub1 = response;
test.done();
});
};
exports.batchInsert = function (test) {
var batchRequests = [
{
method: 'POST',
path: '/1/classes/' + className,
body: {
foo: 'bar1',
baz: 'qux1',
quux: 'quuux'
}
},
{
method: 'POST',
path: '/1/classes/' + className,
body: {
foo: 'bar2',
baz: 'qux2',
quux: 'quuux'
}
}
];
parse.batch(batchRequests, function (error, response) {
test.expect(1);
test.ok(!error, 'There shoudn\'t be an error object.');
stub2 = response[0].success;
stub3 = response[1].success;
test.done();
});
};
exports.find = function (test) {
parse.find(className, stub1.objectId, function (err, response) {
test.equal(stub1.objectId, response.objectId);
test.done();
});
};
exports['insert 2'] = function(assert){
parse.insert(className2,object2, function(err,response){
err && console.log(err);
assert.ok(response);
stub2 = response;
assert.done();
exports.findManyNoConstraints = function (test) {
parse.find(className, '', function (error, response) {
test.expect(3);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response.results.length === 3, 'There should be 3 objects in response.results.');
test.equal(stub1.objectId, response.results[0].objectId, 'The first object should have the same objectId as the stub object.');
test.done();
});
}
};
// order limit skip keys include
exports.findManyWithConstraints = {
order: function (test) {
var query = {
order: '-foo'
};
parse.find(className, query, function (error, response) {
test.expect(4);
test.ok(!error, 'There shoudn\'t be an error object.');
test.equal('bar0', response.results[2].foo, 'response.results[2].foo should be "bar0".');
test.equal('bar1', response.results[1].foo, 'response.results[1].foo should be "bar1".');
test.equal('bar2', response.results[0].foo, 'response.results[0].foo should be "bar2".');
test.done();
});
},
'order keys skip': function (test) {
var query = {
order: 'foo',
keys: 'baz',
skip: 2
};
parse.find(className, query, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.equal('qux2', response.results[0].baz, 'response.results[0].baz should be "qux2".');
test.done();
});
},
'order limit': function (test) {
var query = {
order: '-foo',
limit: 2
};
parse.find(className, query, function (error, response) {
test.expect(4);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response.results.length === 2, 'There should be 2 objects in response.results.');
test.equal('bar1', response.results[1].foo, 'response.results[1].foo should be "bar1".');
test.equal('qux1', response.results[1].baz, 'response.results[1].baz should be "qux1".');
test.done();
});
}
};
exports.find = function (assert) {
parse.find(className, stub.objectId, function (err, response) {
assert.equal(object.foo, response.foo);
assert.done();
});
exports.deprecatedFindMany = {
setUp: function (callback) {
this.query = {
quux: 'quuux'
};
callback();
},
'3 arguments': function (test) {
parse.findMany(className, this.query, function (error, response) {
test.expect(2);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response.results.length === 3, 'There should be 3 objects in response.results.');
test.done();
});
},
'5 arguments': function (test) {
parse.findMany(className, this.query, 'foo', 2, function (error, response) {
test.expect(3);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response.results.length === 2, 'There should be 2 objects in response.results.');
test.equal('bar0', response.results[0].foo, 'response.results[0].foo should be "bar0".');
test.done();
});
},
'6 arguments': function (test) {
parse.findMany(className, this.query, 'foo', 2, 1, function (error, response) {
test.expect(3);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response.results.length === 2, 'There should be 2 objects in response.results.');
test.equal('bar1', response.results[0].foo, 'response.results[0].foo should be "bar1".');
test.done();
});
},
'invalid number of arguments': function (test) {
test.expect(1);
test.throws(function () {parse.findMany('foo', 'bar', 'baz', 'qux');});
test.done();
}
};
exports['find with limit and order'] = function (assert) {
parse.findMany(className,{},'-createdAt',1, function (err1,res1){
assert.equal(1, res1.results.length);
assert.equal(stub.objectId, res1.results[0].objectId);
assert.equal(stub.createdAt, res1.results[0].createdAt);
assert.done();
exports.update = function (test) {
stub1.foo = 'bar00';
parse.update(className, stub1.objectId, {foo: 'bar00'}, function (error, response) {
test.expect(4);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response);
parse.find(className, stub1.objectId, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.equal(stub1.foo, response.foo, 'response.foo should be "bar00".');
test.done();
});
});
}
};
exports['find many'] = function (assert) {
parse.find(className, stub, function (err, response) {
assert.equal(1, response.results.length);
assert.equal(stub.objectId, response.results[0].objectId);
assert.equal(stub.createdAt, response.results[0].createdAt);
assert.equal(object.foo, response.results[0].foo);
assert.done();
exports.insertClass2 = function (test) {
parse.insert(className2, {foo: 'bar'}, function (error, response) {
test.expect(1);
test.ok(!error, 'There shoudn\'t be an error object.');
stubRelation = response;
test.done();
});
};
exports.addRelation = function (test) {
parse.addRelation("secondObject", className2, stubRelation.objectId, className, stub1.objectId, function (error, response) {
test.expect(3);
test.ok(!error, 'There shoudn\'t be an error object.');
var query = {
where: {
$relatedTo: {
object: {
__type: 'Pointer',
className: className2,
objectId: stubRelation.objectId
},
key: 'secondObject'
}
}
};
parse.find(className, query, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.equal(stub1.foo, response.results[0].foo, 'The response object should contain the related object.');
test.done();
});
});
};
exports.deleteOne = function (test) {
parse.delete(className2, stubRelation.objectId, function (error, response) {
test.expect(3);
test.ok(!error, 'There shouldn\'t be an error object.');
parse.find(className2, stubRelation.objectId, function (error, response) {
test.ok(error, 'There should be an error object.');
test.equal(101, error.code, 'error.code should be 101.');
test.done();
});
});
};
exports.update = function (assert) {
do {
var num = Math.floor(Math.random() * 10000);
} while (num == object.foo);
object.foo = num;
parse.update(className, stub.objectId, object, function (err, response) {
err && console.log(err);
assert.ok(response);
exports.find(assert); // retest find on the updated object
exports.deleteAll = function (test) {
parse.deleteAll(className, function (error, response) {
test.expect(2);
test.ok(!error, 'There shoudn\'t be an error object.');
test.ok(response[0].hasOwnProperty('success'));
test.done();
});
};
exports.installationTests = {
upsertInstallation: function(test) {
parse.upsertInstallation('ios', '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', {userID: 'jenny'}, function(error, response) {
test.ok(!error, 'There shouldn\'t be an error object');
test.done();
});
},
exports['add relation'] = function (assert) {
parse.addRelation("secondObject",className,stub.objectId,className2,stub2.objectId, function (err ,response){
err && console.log(response);
assert.ok(response);
assert.done();
});
deleteInstallation: function(test) {
parse.getInstallationDataForDeviceToken('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', function(error, response) {
var id = response.results[0].objectId;
parse.deleteInstallation(id, function(error, response){
test.ok(!error, 'There shouldn\'t be an error obejct');
test.done();
});
});
}
}
exports['batch'] = function (assert) {
requests = [{"method":"PUT","path": "/1/classes/"+className+'/'+stub.objectId, "body": object3},{"method":"PUT","path": "/1/classes/"+className2+'/'+stub2.objectId, "body": object4} ];
parse.batch(requests, function(err,response){
err && console.log(response);
assert.ok(response);
assert.done();
});
}
exports['delete'] = function (assert) {
parse['delete'](className, stub.objectId, function (err) {
err && console.log(err);
assert.ok(!err);
parse.find(className, stub.objectId, function (err, response) {
assert.equal(404, err.type);
assert.done();
exports.userTests = {
insertUser : function (test) {
test.expect(1);
parse.insertUser({username: 'foo', password: 'bar'}, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.done();
});
});
},
getUser: function (test) {
test.expect(2);
parse.getUser({where:{username: 'foo'}}, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.equal('foo', response.results[0].username, 'response.results[0].username should be foo.');
test.done();
});
},
deleteUser: function (test) {
test.expect(1);
parse.getUser({where:{username: 'foo'}}, function (error, response) {
parse.deleteUser(response.results[0].objectId, function (error, response) {
test.ok(!error, 'There shoudn\'t be an error object.');
test.done();
});
});
}
};
exports['delete all'] = function(assert){
parse.insert(className2,object2, function(err,response){
parse.deleteAll(className2, function(){
parse.findMany(className2, '', function(err, response){
assert.equal(0, response.results.length);
assert.done();
});
});
});
}
exports['push notification error'] = function (assert) {
exports.pushNotificationError = function (test) {
parse.sendPush({

@@ -136,8 +291,9 @@ channels: ['foobar'],

}
}, function (error, result) {
assert.ok(error);
assert.equal(result, null);
assert.equal(error.message, 'Missing the push data. (Code: 115)');
assert.done();
}, function (error, response) {
test.expect(3);
test.ok(error);
test.equal(response, null);
test.equal(error.code, 115, 'error.code should be 115.');
test.done();
});
}
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc