Kinvey JavaScript Library for Node.js. Kinvey is a Backend as a Service platform that makes it ridiculously easy for developers to setup and operate backends for their mobile, tablet and web apps.
To use the library, sign up for Kinvey if you have not already done so. Go to the sign up page, and follow the steps provided.
You can setup the library in two ways. You can either install the module directly through npm
:
npm install kinvey
Or alternatively, clone the repository and create a symbolic link to the module:
git clone git@github.com:Kinvey/kinvey-nodejs.git
cd kinvey-nodejs
npm link
Now, the library is available for use in your project. Use require
to import the library. require
will return the Kinvey namespace.
var Kinvey = require('kinvey');
Once you have imported the library, it is time to initialize it. To do so, you’ll need the App Key
and App Secret
of your application. You can find these keys when you click on an the application in the console.
Kinvey.init({
appKey: '<your-app-key>',
appSecret: '<your-app-secret>'
});
As a first example, we will show you how to save an item on Kinvey. The following code snippet saves a book. We use the first parameter of Kinvey.Entity
to specify in which collection we want to save this book.
var book = new Kinvey.Entity({
title: 'Awesome Arms',
author: 'Robert Kennedy'
}, 'book');
book.save({
success: function(response) {
// response is the book object.
},
error: function(error) {
// error contains a error and description field indicating what exactly went wrong.
}
});
At a later stage, you might want to retrieve all books written by a certain author. Therefore, you can use the Kinvey.Collection
class.
// First, build a query to match the author.
var query = new Kinvey.Query();
query.on('author').equal('Robert Kennedy');
// Create a collection, and pass in the query.
var bookCollection = new Kinvey.Collection('book', { query: query });
bookCollection.fetch({
success: function(list) {
// list is an array of books written by Kennedy.
},
error: function(error) {
// error contains a error and description field indicating what exactly went wrong.
}
});
The example shown above only shows a very small subset of all features the library offers. To learn more:
- Read about our APIs in the JavaScript Developer’s Guide
- Try saving and loading data using the JavaScript Appdata API
- Consult the JavaScript API Docs for detailed information on all available methods