npm install node-parse-api
var Parse = require('node-parse-api').Parse;
var APP_ID = ...;
var MASTER_KEY = ...;
var app = new Parse(APP_ID, MASTER_KEY);
var Parse = require('node-parse-api').Parse;
var options = {
app_id:'...',
api_key:'...' // master_key:'...' could be used too
}
var app = new Parse(options);
- insert(className
string
, dataobject
, callbackfunction
)
// add a Foo object, { foo: 'bar' }
app.insert('Foo', { foo: 'bar' }, function (err, response) {
console.log(response);
});
- insertUser(data
object
, callbackfunction
)
app.insertUser({
username: 'foo',
password: 'bar'
}, function (err, response) {
console.log(response);
});
More properties can be provided, but username and password are required.
app.insertUser({
username: 'foo',
password: 'bar',
pointer/*can have any name*/: {
__type: 'Pointer',
className: <string>,
objectId: <string>
}
}, function (err, response) {
console.log(response);
});
app.insertUser({
username: 'foo',
password: 'bar',
location: {
__type: 'GeoPoint',
latitude: <int>,
longitude: <int>
}
}, function (err, response) {
console.log(response);
});
- loginUser(username
string
, passwordstring
, callbackfunction
)
Response contains all of the user fields except password, also includes a sessionToken for this user.
app.loginUser('foo', 'bar', function (error, response) {
// response = {sessionToken: '', createdAt: '', ... }
});
- me(sessionToken
string
, callbackfunction
)
app.me('sessionToken', function (error, response) {
// response is same as getUser response
});
- insertFile(fileName
string
, datastring/buffer
, contentTypestring
, callbackfunction
)
// 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(className
string
, queryobject
, callbackfunction
)
// 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.
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(className
string
, queryobject
, callbackfunction
)
// all Foo objects with foo = 'bar'
app.find('Foo', {where: {foo: 'bar'}}, 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...)
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
});
- getUser(query
object
, callbackfunction
)
app.getUser({objectId: 'someId'}, function (err, response) {
console.log(response);
});
- getUser(query
object
, callbackfunction
)
// 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);
}):
var query = {
count: 1,
limit: 0
};
app.find('Foo', query, function (error, response) {
// {
// results: [],
// count: 123
// }
});
- update(className
string
, objectIdstring
, callbackfunction
)
app.update('Foo', 'someId', {foo: 'bar'}, function (err, response) {
console.log(response);
});
- delete(className
string
, objectIdstring
, callbackfunction
)
app.delete('Foo', 'someId', function (err, response) {
// response = {}
});
- deleteAll(className
string
, callbackfunction
)
app.deleteAll('Foo', function (err, response) {
// response = [{success: {}, success: {}, ... }]
});
- deleteUser(objectId
string
, [sessionTokenstring
], callbackfunction
)
If you are using the master key you don't need any session tokens.
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.
app.deleteUser('someId', 'sessionToken', function (error, response) {
// response = {}
});
- deleteAllUsers(callback
function
)
This will only work when using the master key.
app.deleteAllUsers(function (err, response) {
// response = [{success: {}, success: {}, ... }]
});
- passwordReset(data
string
, callbackfunction
)
//email is built into Parse's special User class
app.passwordReset(email, function(err, response){
console.log(response);
});
- updateUser(objectId
string
, dataobject
, [sessionTokenstring
], callbackfunction
)
With master key
app.updateUser('someId', {email: '[email protected]'}, function(err, response){
console.log(response);
});
or with rest api key
app.updateUser('someId', {email: '[email protected]'}, 'sesstionToken', function(err, response){
console.log(response);
});
- batch(requests
array
, callbackfunction
)
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: {...}}}]
});
//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);
}
});
//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);
}
});
//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);
}
});
//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 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);
});
//pass the role object's objectId
app.getRole("<objectId>", function(err, resp){
console.log(resp);
});
//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);
});
//pass the objectId of the role
app.deleteRole("<objectId>", function(err, resp){});
app.getRoles(function(err, resp){});
var params = {
where: { name: "Administrator" }
};
app.getRoles(params, function(err, resp){
console.log(resp);
});
//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);
});
If you use node-parse-api in a node.js server environement a client might send a sessionToken with a request to your server. You can pass that sessionToken as a constructor option to perform database operations on behalf of that user session.
This allows e.g. to find objects of classes that are restricted to be read by only that user (or role).
var Parse = require('node-parse-api').Parse;
// let's assume this is a sessionToken of a user who is member of the role "moderator"
var sessionToken = '3h3gaa32bdd3h3gaa323h3gaa32bddbdd';
var options = {
app_id:'...',
api_key:'...',
session_token: sessionToken // , master_key:'...' could be used too
}
var app = new Parse(options);
// let's assume Foo is a class with read permission for "moderator"-users only
app.find('Foo', {objectId: 'someId'}, function (err, response) {
console.log(response);
});
//when inserting a data, you must use the Parse date object structure, i.e.:
{
"__type": "Date",
"iso": new Date("<year>", "<month>", "<day>").toJSON()
}
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.