Localoose is a simple localStorage Object Data Mapping (ODM) -> Still under development
npm install <package>
<script src="path/to/localoose.js"></script>
var localoose = new Localoose();
We define schema like this localoose.Schema();
Example:
var MySchema = localoose.Schema({
name: String,
lastName: String,
age: {
type: Number,
required: true
}
});
We define model like this localoose.Model('ModelName', MySchema);
Example
var MyModel = localoose.Model('ModelName', MySchema);
Once we have our model, we can then instantiate it passing to constructor the values:
var instance = new MyModel({
name: "John",
lastName: "Lee",
age: 22
});
String
, Number
, Date
, Boolean
and Array
Example:
localoose.Schema({
field: String,
field: Number,
field: Date,
field: {
type: Boolean
},
field: {
type: Array
}
});
Type | Supported values | Default value | Description |
---|---|---|---|
Required | Boolean | False | Validate field as required |
Min | Number or Array | Null | Validate a minimum value for the field |
Max | Number or Array | Null | Validate a maximum value for the field |
Default | Not empty value | Null | When the field is empty or null, then apply the default value |
Note: The types 'min' and 'max' can be number or array. If array or number, look:
localoose.Schema({
field: {
min: [3, "Message if be invalid"],
max: 6 // Note: this not have a invalid custom message
}
});
Example with all types:
localoose.Schema({
name: {
type: String,
required: true
},
age: {
type: Number,
min: [1, "The minimum is one!"]
},
birthday: Date,
haveDog: Boolean,
brothers: {
type: Array,
default: [""]
}
});
save([callback(err, model)]);
Persists the model on localStorage
MyModel.save(function( err, my_model ) {
if ( err )
console.error(err);
else
console.info('Saved!', my_model);
});
findAll([callback(err, result)]);
Return all data from the model storaged on localStorage
MyModel.findAll(function( err, result ) {
if ( err )
console.error(err);
else
console.log('All results', result);
});
findById([id], [callback(err, found)]);
Return one data based on id parameter
MyModel.findById(id, function( err, found ) {
if ( err )
console.error(err);
else
console.log(found.name);
});
find([conditions], [callback(err, found)]);
return data based on object condition parameter
MyModel.find({ age: 18 }, function( err, found ) {
if ( err )
console.error(err);
else
console.log(found);
});
delete(id, [callback(err)]);
delete a data based on id parameter
MyModel.delete('5da1765bd9845e5680551d8aa4a61a43', function( err ) {
if ( err )
console.error(err);
else
console.info('Deleted!');
});
update(id, [options], [callback(err)]);
update a data based on id and options object
MyModel.update('5da1765bd9845e5680551d8aa4a61a43', {
name: 'John Travolta',
age: 62
}, function( err ) {
if ( err )
console.error(err);
else
console.info('Updated!');
});
find()delete()update()- findOne()
- Tests