2.0.1 • Published 9 years ago

cooldb-promise v2.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
9 years ago

coolDB-Promise

This is a lightweight library for Client | Server which helps the CRUD actions in memory over objects / data stored in an internal JS Array using Promise.

Browser

<script src="dist/browser-cooldb.js"></script>
var cooldb = cooldb,
    coolDB = cooldb();

Node

npm install cooldb-promise
var cooldb 	= require('cooldb-promise'),
	coolDB 	= cooldb();
// Activate Individual Libraries
coolDB.activeGlobalLibs({ libs: ['Axios', 'Clone', 'CryptoJs', 'Cuid', 'jQuery', 'Lazy', 'Validate'] });

// Activate All Libraries
coolDB.activeGlobalLibs({ libs: ['All'] });

// Active Libraries During instantiation
var cooldb = cooldb,
    coolDB = cooldb({ libs: ['All'] });

// Insert Multiple coolDB.add({ item: { name: 'Blue' }, { name: 'Trunk' }, { name: 'Blue'} }) .then(function(result) { console.log(result); }) .catch(function(err) { console.log(err); });

### del
Delete the items where a key + value match with the items stored inside the cooldb Array

function del(params) params: { key (Property name) | value (Property value) } returns: Array => Object {old: Object, new: null, action: "Deleted"}, ...

``` javascript
// *** Delete Single & Multiple ***
coolDB.del({ key: 'name', value: 'Pacman' })
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.log(err);
    });

update

Update the items where a key + value match with the items stored inside the cooldb Array

function update(params)
params: { key (Property name) | value (Property value) | item (New Property values) }
returns: Array => [ Object {old: Object, new: Object, action: "Updated"}, ... ]
// *** Update Single ***
coolDB.update({ key: 'name', value: 'Mary', item: { name: 'Pingui' } })
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.log(err);
    });

// *** Update Multiple ***
coolDB.update({ key: 'name', value: 'Blue', item: { name: 'Pacman' } })
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.log(err);
    });

first

Return the first item where a key + value match with the items stored inside the cooldb Array

function first(params)
params: { key (Property name) | value (Property value) }
returns: Object { item (First object found) | count (Number of objects found) }
coolDB.first({ key:'name', value: 'Blue'})
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.log(err);
    });

get

Get the items where a key + value match with the items stored inside the cooldb Array

function get(params)
params: { key (Property name) | value (Property value) }
returns: Object { items (Array of objects found) | count (Number of objects found) }
coolDB.get({ key:'name', value: 'Blue'})
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.log(err);
    });

db

Get the cooldb Array Mirror.

function db()
returns: Array
coolDB.db()._result; // [Object, Object, Object, Object]

clone

Get the cooldb Array Clone.

function clone()
returns: Array
coolDB.clone()._result; // [Object, Object, Object, Object]

clean

Reset to empty Array the internal cooldb Array.

function clean()
returns: Array => [ Object {old: null, new: null, action: "Cleaned"} ]
coolDB.clean()._result;

changeFeed

Subscribe to the internal cooldb Array's CRUD change events.

function changeFeed(fn)
returns: Object
coolDB.changeFeed(function(change){
    console.log(change);
    // Object {old: null, new: Object, action: "Inserted"}
    // Object {old: Object, new: null, action: "Deleted"}
    // Object {old: Object, new: Object, action: "Updated"}
    // Object {old: null, new: null, action: "Cleaned"}
});

history

Get the cooldb History Array Mirror.

function history()
returns: Array
coolDB.history()._result; // [Object, Object, Object, Object]
<br />
### Undo multiple insert types
Examples:

function undo(params) params: { hcuid, hicuid } returns: Standard Object according to the action => {old: ?, new: ?, action: ?}

#### Undo simple insert
``` javascript
function displayDbItems(){
         console.log('DB ITEMS');
         console.log( clone(coolDB.db()._result) );
         return coolDB.history()._result[0];  
}

function UndoSpecific(item){

    coolDB.undo({ hcuid: item.hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO INSERT');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: { name: 'Jhon' } })
         .then(displayDbItems)
         .then(UndoSpecific)
         .catch(function(err){
            console.log(err);
        });

Undo all inserts

function displayDbItems(){
         console.log('DB ITEMS');
         console.log( clone(coolDB.db()._result) );
         return coolDB.history()._result[0];  
}

function UndoSpecific(item){
    
    coolDB.undo({ hcuid: item.hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO BUNCH OF INSERTS ');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: [{ name: 'Jhon' }, { name: 'Jane' }] })
         .then(displayDbItems)
         .then(UndoSpecific)
         .catch(function(err){
            console.log(err);
         });

Undo specific insert from bunch

function displayDbItems(){
    console.log('DB ITEMS');
    console.log( clone(coolDB.db()._result) );
    return coolDB.history()._result[0];  
}

function UndoSpecificFromBunch(item){

    coolDB.undo({ hcuid: item.hcuid, hicuid: item.item[0].hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO INSERT OVER A SPECIFIC ITEM');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: [{ name: 'Jhon' }, { name: 'Jane' }] })
        .then(displayDbItems)
        .then(UndoSpecificFromBunch)
        .catch(function(err){
            console.log(err);
        });

function changeJhon(item) { coolDB.update({ key: 'cuid', value: item.cuid, item: { name: 'Yolo'} }) .then(function(item){ console.log('RESULT -> DB ITEM UPDATE'); console.log( clone(item0.new ) );
}); }

function UndoSpecific(){

coolDB.history().then(function(item){

    coolDB.undo({ hcuid: item[1].hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO UPDATE');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
});

coolDB.add({ item: { name: 'Jhon' } }) .then(displayDbItems) .then(changeJhon) .then(UndoSpecific) .catch(function(err){ console.log(err); });

#### Undo all updates
``` javascript
function displayDbItems(){
    console.log('DB ITEMS');
    console.log( clone(coolDB.db()._result) );
    
    return clone(coolDB.db()._result[0]);
}

function changeByAge(item) {
    coolDB.update({ key: 'age', value: 20, item: { name: 'Yolo'} })
          .then(function(item){
            console.log('RESULT -> DB ITEM UPDATE');
            console.log( clone(item ) );                
          });
}

function UndoSpecific(){
    
    coolDB.history().then(function(item){
        
        coolDB.undo({ hcuid: item[1].hcuid })
                .then(function(hItem){ 
                    console.log('RESULT -> DB UNDO UPDATE');
                    console.log( coolDB.db()._result );
                })
                .catch(function(err){
                    console.log(err);
                });

    });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(displayDbItems)
        .then(changeByAge)
        .then(UndoSpecific)
        .catch(function(err){
            console.log(err);
        });

Undo specific update from bunch

function displayDbItems(){
    console.log('DB ITEMS');
    console.log( clone(coolDB.db()._result) );
    
    return clone(coolDB.db()._result[0]);
}

function changeByAge(item) {
    coolDB.update({ key: 'age', value: 20, item: { name: 'Yolo'} })
          .then(function(item){
            console.log('RESULT -> DB ITEM UPDATE');
            console.log( clone(item ) );                
          });
}

function UndoSpecificFromBunch(){
    
    coolDB.history()
          .then(function(item){
        
            coolDB.undo({ hcuid: item[1].hcuid, hicuid: item[1].item[1].hcuid })
                    .then(function(hItem){ 
                        console.log('RESULT -> DB UNDO UPDATE OVER A SPECIFIC ITEM');
                        console.log( coolDB.db()._result );
                    })
                    .catch(function(err){
                        console.log(err);
                    });
        });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(displayDbItems)
        .then(changeByAge)
        .then(UndoSpecificFromBunch)
        .catch(function(err){
            console.log(err);
        });

function displayDbItems(){ console.log('DB ITEMS'); console.log( clone(coolDB.db()._result) ); return coolDB.history()._result1;
}

function UndoSpecific(item){

// Undo Specific Item from a bunch of Inserts
coolDB.undo({ hcuid: item.hcuid })
        .then(function(hItem){ 
            console.log('RESULT -> DB UNDO DELETE');
            console.log( coolDB.db()._result );
        })
        .catch(function(err){
            console.log(err);
        });

}

coolDB.add({ item: { name: 'Jhon' } }) .then(deleteItem) .then(displayDbItems) .then(UndoSpecific) .catch(function(err){ console.log(err); });

#### Undo all deletes
``` javascript
function deleteItem() {
    var item = coolDB.db()._result[0];
    coolDB.del({ key: 'age', value: 20 })
          .catch(function(err){
            console.log(err);
          });
    
    return clone(item);
}

function displayDbItems(){
    console.log('DB ITEMS');
    console.log( clone(coolDB.db()._result) );
    return coolDB.history()._result[1];  
}

function UndoSpecific(item){

    coolDB.undo({ hcuid: item.hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO DELETES');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(deleteItem)
        .then(displayDbItems)
        .then(UndoSpecific)
        .catch(function(err){
            console.log(err);
        });

Undo specific delete from bunch

function deleteItem() {
    var item = coolDB.db()._result[0];
    coolDB.del({ key: 'age', value: 20 })
          .catch(function(err){
            console.log(err);
          });
    
    return clone(item);
}

function displayDbItems(){
    console.log('DB ITEMS');
    console.log( clone(coolDB.db()._result) );
    return coolDB.history()._result[1];  
}

function UndoSpecific(item){

    coolDB.undo({ hcuid: item.hcuid, hicuid: item.item[1].hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO SPECIFIC DELETE');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(deleteItem)
        .then(displayDbItems)
        .then(UndoSpecific)
        .catch(function(err){
            console.log(err);
        });

coolDB.add({ item: { name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 } }) .then(coolDB.clean) .then(UndoSpecific) .catch(function(err){ console.log(err); });

#### Undo specific clean from bunch
``` javascript
function UndoSpecific(){
    var item = coolDB.history()._result[1];
    
    coolDB.undo({ hcuid: item.hcuid, hicuid: item.item[1].hcuid })
            .then(function(hItem){ 
                console.log('RESULT -> DB UNDO SPECIFIC CLEAN');
                console.log( coolDB.db()._result );
            })
            .catch(function(err){
                console.log(err);
            });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(coolDB.clean)
        .then(UndoSpecific)
        .catch(function(err){
            console.log(err);
        });

coolDB.add({ item: { name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 } }) .then(cleanHistory) .catch(function(err){ console.log(err); });

<br />
### changeFeedHistory
Subscribe to the internal cooldb Array's History events.

function changeFeedHistory(fn) returns: Object

``` javascript
coolDB.changeFeedHistory(function(change){
    console.log(change);
    // { item: [{ old: ?, new: ?, action: ?, hcuid: CUID }], action: X,  hcuid: CUID }
    // action => "Inserted" | "Deleted" | "Updated" | "Cleaned".
    // ?      => Object / Array
});

coolDB.add({ item: { name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 } }) .then(postCuidDemo) .catch(function(err){ console.log(err); });

<br />
### getCuid
Ajax get request, send an internal non history item to the server by cuid.

function getCuid({ url | cuid | json (default false) | encrypt (default false) }) returns: Ajax server response

``` javascript
// Example:
function getCuidDemo() {
    
    var itemCuid = coolDB.db()._result[0].cuid;

    coolDB.getCuid({ url: '/getUrl', cuid: itemCuid, json: false }) 
          .then(function(success){
            console.log( success );
          })
          .catch(function(err){
            console.log( err );
          });
}

coolDB.add({ item: [{ name: 'Jhon', age: 20 }, { name: 'Jane', age: 20 }] })
        .then(getCuidDemo)
        .catch(function(err){
            console.log(err);
        });

coolDB.encrypt({ item: people, seconds: 15 }) .then(function(res){ console.log(res); // { item, key, count } }) .catch(function(err) { console.log( err ); });

<br />
### decrypt
It receives an Encrypted Object || Array and then all the plain text properties will be decrypted, the output will contain: 
'item' property: It is an array with the result of the decryption.
'key'  property: Encrypted Hash created used for the CoolDB-Promise to decrypt the Object / Array.
'count' property: It is the length of the returned item property.

function decrypt({ item (Object || Array) | key }) returns: { item (Array) | key | count }

``` javascript
// Example:
var people = [
  { 
	name: 'PadAMJbAE4h/', 
	age: 'dGVkX', 
	birthday: { month: 'iOX0QLSDtnPadAM', day:'1+R/DBWo', year:'8N82m9Q' }, 
	salary: '3OqybwJCp', 
	today: 'sdGVkX1/IpPX'
  },
  { 
	name: 'kX1/JiTNg0pa', 
	age: 'dGVkX', 
	birthday: { month: 'iOX0QLSDtnPadAM', day:'1+R/DBWo', year:'8N82m9Q' }, 
	salary: '3OqybwJCp', 
	today: 'sdGVkX1/IpPX'
  }
];

coolDB.decrypt({ item: people, key: 'U2FsdGVkX1/JiT1U7Bv2WZSMmKgANlKQ2bWFk1RF46dvYWko24DtUbHa136tgQ==' })
  .then(function(res){
    console.log(res); // { item, key, count }
  })
  .catch(function(err) {
    console.log( err );
  });
2.0.1

9 years ago

2.0.0

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago