Skip to content

Localoose is a localStorage Object Data Mapping (ODM)

Notifications You must be signed in to change notification settings

k4fer74/localoose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Localoose

Localoose is a simple localStorage Object Data Mapping (ODM) -> Still under development

Installation

Install with npm

npm install <package>

Install manually

<script src="path/to/localoose.js"></script>

Overview

Getting started

var localoose = new Localoose();

Defining a Schema

We define schema like this localoose.Schema();

Example:

var MySchema = localoose.Schema({
    name: String,
    lastName: String,
    age: {
        type: Number,
        required: true
    }
});

Defining a Model

We define model like this localoose.Model('ModelName', MySchema);

Example

var MyModel = localoose.Model('ModelName', MySchema);

Accessing a Model

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
});

Doc

Schema Types

String, Number, Date, Boolean and Array

Example:

localoose.Schema({
    field: String,
    field: Number,
    field: Date,
    field: {
        type: Boolean
    },
    field: {
        type: Array
    }
});

Schema Validator Types

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: [""]
    }
});

Methods

save();

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();

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();

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();

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();

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();

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!');
});

findOne();

Todo

  • find()
  • delete()
  • update()
  • findOne()
  • Tests

About

Localoose is a localStorage Object Data Mapping (ODM)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published