A promise based node module for saving searchable plain JavaScript objects to Redis. API methods are executed as atomic Lua scripts to avoid data/index corruption. Inserted data is type checked against a predefined schema.
Node.js v4.0.0 or later required.
npm install --save rigiddb
const RigidDB = require('rigiddb');
let store = new RigidDB('mydb', 42);
store.setSchema({
cars: {
definition: {
color: { type: 'string', allowNull: true },
model: { type: 'string', allowNull: false },
mileage: 'int', // A shortcut for { type: 'int', allowNull: true }
convertible: 'boolean',
purchaseDate: 'date'
},
indices: {
first: {
uniq: true,
fields: [
{ name: 'model', caseInsensitive: true },
{ name: 'color', caseInsensitive: false }
]
},
second: {
uniq: false,
fields: [ 'color' ] // A shortcut for { name: 'color', caseInsensitive: false }
}
}
}
}).then(function(schemaId) {
return store.create('cars', {
color: 'blue',
mileage: 12345,
convertible: true,
purchaseDate: new Date('Sun Nov 01 2015 17:41:24 GMT+0000 (UTC)')
})
}).then(function(id) {
// id = 1
return store.find('cars', {
color: 'blue',
mileage: 12345
});
}).then(function(results) {
// results = [ 1 ]
});
Create a new database connection.
Argument | Description |
---|---|
dataBaseName | New or existing database name. All Redis keys will be prefixed with the name. |
revision | Revision number of the schema, integer. |
redisOptions | An object containing arguments forwarded to ioredis node module. Supported properties are: host , port , password , and db . |
All database API methods return an JavaScript object.
In success case the object contains method
(method name) and val
(actual return value) properties.
In case of an error, the object contains method
(method name) and err
(error code, string) properties.
As an example, delete
method can return:
{
method: 'delete',
err: 'notFound',
}
TODO: list all error codes.
Set a schema for the database. Schema can be set once. Future versions of this library will support schema changes and data migration. Validity of a schema is checked before it is activated and persisted. Any other API call is possible only after the schema is set.
Argument | Description |
---|---|
schemaDefinition | An object that specifies the collections, including the format of collection data. See the example above for supported data type and index definitions. |
Get the previously set schema definition.
Add a new object to collection.
Argument | Description |
---|---|
collection | Name of the collection. |
attributes | An object that includes values for all attributes listed in the schema. |
Id of the newly created object, integer.
Update an existing object in collection.
Argument | Description |
---|---|
collection | Name of the collection. |
id | Id of the object to be updated. |
attributes | An object that includes values for attributes to be updated. |
Remove an object from collection.
Argument | Description |
---|---|
collection | Name of the collection. |
id | Id of the object to be removed. |
Get an object from collection
Argument | Description |
---|---|
collection | Name of the collection. |
id | Id of the object to be fetched. |
Check if an object with an id exists
Get an object from collection
Argument | Description |
---|---|
collection | Name of the collection. |
id | Id of the object to be searched. |
Get ids of all object in a collection.
Argument | Description |
---|---|
collection | Name of the collection. |
Get the amount of objects in a collection.
Argument | Description |
---|---|
collection | Name of the collection. |
Execute several API methods in an atomic transaction. Note that the return value of a method call inside the transaction function can't be used as an argument in the following method calls.
Transaction is terminated if any of the methods return an error value. In a typical case transaction contains one or more exists() calls to make sure that the some object still exists before creating an object that points to those objects.
Argument | Description |
---|---|
transaction | Transaction function. |
Find an object using one of the specified indices.
Argument | Description |
---|---|
collection | Name of the collection. |
searchAttributes | An object that includes values to be searched. |
Print the contents of a collection.
Only usable with smalle collections during development.
Argument | Description |
---|---|
collection | Name of the collection. |
Terminate the database connection.
- Boolean ('boolean')
- Integer and float ('int')
- String ('string')
- Date ('date')
- Unix timestamp ('timestamp')