- a cheap and easy in-memory sql database for nodejs projects that persits to S3
- built on top of the mighty alasql
- useful for hobby projects on free servers (heroku) where you don't want the hassle of a database server and don't have reliable on-server file storage
- every row in the database must have an id field (named id, _id or i). id's can be generated automatically by using the
autoId
setting
`from v1.7 onward all data is encrypted by default. if you are upgrading to v1.7 your data will get upgraded automatically but you should make a backup first.
db.select('tableName', {
where: {
email: {
$like: 'something'
}
},
page: 1
pageSize: 10
sort: 'email'
sortDir: 'ASC'
}, function(results, total) {
//do something with your data
});
db.select('tableName', {
email: {
$like: 'something'
}
}, function(results, total) {
//do something with your data
});
db.insert('tableName', objectToInsert, callbackFn);
db.update('tableName', updateObject, whereObject, callbackFn);
db.upsert('tableName', objectToUpsert, whereObject, callbackFn);
db.delete('tableName', whereObject, callbackFn);
// examples of good inserts etc
db.exec('INSERT INTO table1 VALUES ?', [obj]);
db.exec('INSERT INTO table1 SELECT * FROM ?', [[obj1, obj2, obj3]]);
db.exec('UPDATE table1 SET country=? WHERE country=?', ['Republic of China', 'China']);
db.exec('DELETE FROM table1 WHERE population > ?', [500000000]);
npm install --save ndxdb
var db = require('ndxdb')
.config({
database: 'mydb', //database name - required
tables: ['table1', 'table2'], //database tables - required
awsBucket: process.env.AWS_BUCKET, //aws info
awsRegion: process.env.AWS_REGION || 'us-east-1',
awsId: process.env.AWS_ID,
awsKey: process.env.AWS_KEY,
localStorage: 'data', //you can persist data to a local directory too
autoId: '_id', //generate id's automatically
encryptionKey: 'something random', //all data is encrypted by default
doNotEncrypt: true //turns off database encryption
})
.on('ready', function() { //database has been built/rebuilt and is ready to go
test();
}) //there are also callbacks for insert, update and delete
.start(); // call start() to get things going
var test = function() {
var vals = [
{
country: 'China'
population: 1371220000
},{
country: 'India'
population: 1311050000
},{
country: 'United States'
population: 321418000
}
];
db.insert('table1', vals);
db.select('table1', {
where: {
population: {
$gt: 500000000
}
},
sort: 'population',
sortDir: 'ASC'
}, function(results, total) {
/*
result = [
{
country: 'India'
population: 1311050000
},{
country: 'China'
population: 1371220000
}
],
total = 2
*/
});
}
if you don't set your AWS info or a local storage directory then the database will work as an in-memory database with no persistence
most of the database configuration can be set as environment variables instead
- LOCAL_STORAGE
- DATABASE
- AUTO_ID
- AUTO_DATE
- AWS_BUCKET
- AWS_REGION
- AWS_ID
- AWS_KEY
- ENCRYPTION_KEY
- DO_NOT_ENCRYPT
in which case you can simplify your code
var db = require('ndxdb')
.config({
tables: ['table1', 'table2']
})
.start();
Configure the database
Start the database
ndx.database.on 'callbackName', (args, cb) ->
#do something with args
cb true #or false if you want to cancel the operation
The database is ready to use
args.table
The database table being operated onargs.obj
The object being inserted into the databaseargs.user
The user carrying out the operation
cb(false)
to cancel the insert
args.id
The inserted object's idargs.table
The database table being operated onargs.obj
The object that was inserted into the databaseargs.user
The user carrying out the operation
args.id
The id of the object being updatedargs.table
The database table being operated onargs.where
The database queryargs.obj
The data to updateargs.oldObj
The value of the object preUpdateargs.changes
The changes to be appliedargs.user
The user carrying out the operation
cb(false)
to cancel the update
args.id
The id of the object that was updatedargs.table
The database table that was operated onargs.obj
The data that was updatedargs.oldObj
The value of the object pre updateargs.newObj
The value of the object post updateargs.changes
The changes that were appliedargs.user
The user carrying out the operation
args.table
The database table being operated onargs.args
The arguments that were passed to the select functionargs.user
The user carrying out the operation
args.table
The database table being operated onargs.objs
The objects that were selected from the databaseargs.user
The user carrying out the operation
args.table
The database table being operated onargs.where
The database queryargs.user
The user carrying out the operation
args.table
The database table being operated onargs.user
The user carrying out the operation
callbacks can be used to modify data flowing to and from the database.
see ndx-permissions and ndx-profiler for examles
Unregister a callback
Select data
Insert data
Update data
Upsert data
Delete data
Execute an SQL command
Used internally
Turn on maintenance mode
Turn off maintenance mode
Get the maintenance mode status of the database
Gets a reference to the current database
Restore the database from a backup
Cleans up data fragments and saves the main database file
Turn on maintenance mode
The current alasql instance