Setting up persistence.js using

Installing persistence.js on node is easy using npm:

npm install persistencejs

Alternatively, you can clone the repository manually:

git clone git://github.com/zefhemel/persistencejs.git

If installed manually, you also need to install node-mysql, the MySQL driver that the persistence store depends on.

Setup

You need to require two modules, the persistence.js library itself and the store module.

var persistence = require('persistencejs/persistence').persistence;
// For MySQL:
var persistenceStore = require('persistencejs/persistence.store.mysql');
// For SQLite:
var persistenceStore = require('persistencejs/persistence.store.sqlite');

Alternatively, if you cloned persistence.js using git, use a relative path to the modules instead, e.g.:

var persistence = require('./persistencejs/persistence').persistence;
var persistenceStore = require('./persistencejs/persistence.store.mysql');
// or
var persistenceStore = require('./persistencejs/persistence.store.sqlite');

Then, you configure the database settings to use:

// MySQL
persistenceStore.config(persistence, 'localhost', 3306, 'dbname', 'username', 'password');
// SQLite
persistenceStore.config(persistence, __dirname + '/mydb.db');

Subsequently, for every connection you handle (assuming you’re building a sever), you call the persistenceStore.getSession() method:

var session = persistenceStore.getSession();
See also  Understanding JavaScript expressions and declarations