Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 53 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage
The ideal way to use this library is to have a db.js in your applications somewhere. Which will be required.

**DB.js**

```
var RNDBModel = require('react-native-db-models')

Expand All @@ -47,7 +48,7 @@ var {
View,
Image
} = React;

// Only "all" event emitter is available

DBEvents.on("all", function(){
Expand All @@ -56,8 +57,10 @@ DBEvents.on("all", function(){

var App = React.createClass({
get_users: function(){
DB.users.get_all(function(result){
console.log(result);
DB.users.get_all(function(err, result){
if(!err) {
console.log(result);
}
})
},
render: function(){
Expand All @@ -80,9 +83,12 @@ You can check all the returned data from the callback. The returned data is more
> query_data: The data to be matched. (eg. {name: "John Doe"})

Example

```
DB.users.get({first_name: "Rishabh"}, function(results){
console.log(results);
DB.users.get({first_name: "Rishabh"}, function(err, results){
if(!err) {
console.log(results);
}
})
```
----------
Expand All @@ -92,9 +98,12 @@ DB.users.get({first_name: "Rishabh"}, function(results){
> id: ID of the object to be fetched.

Example

```
DB.users.get_id(10, function(results){
console.log(results);
DB.users.get_id(10, function(err, results){
if(!err) {
console.log(results);
}
})
```

Expand All @@ -107,9 +116,11 @@ DB.users.get_id(10, function(results){
Example

```
DB.users.get_all(function(result){
console.log(result);
})
DB.users.get_all(function(err, result){
if(!err) {
console.log(results);
}
})
```

----------
Expand All @@ -119,9 +130,12 @@ DB.users.get_all(function(result){
> query_data: The data to be matched. (eg. {name: "John Doe"})

Example

```
DB.users.remove({first_name: "Rishabh"}, function(removed_data){
console.log(removed_data);
DB.users.remove({first_name: "Rishabh"}, function(err, removed_data){
if(!err) {
console.log(results);
}
})
```

Expand All @@ -132,9 +146,12 @@ DB.users.remove({first_name: "Rishabh"}, function(removed_data){
> id: ID of the object to be deleted.

Example

```
DB.users.remove({first_name: "Rishabh"}, function(removed_data){
console.log(removed_data);
DB.users.remove({first_name: "Rishabh"}, function(err, removed_data){
if(!err) {
console.log(results);
}
})
```
----------
Expand All @@ -144,9 +161,12 @@ DB.users.remove({first_name: "Rishabh"}, function(removed_data){
> data: The data to be added. (eg. {name: "John Doe", age: 56})

Example

```
DB.users.add({first_name: "Rishabh", age: 25}, function(added_data){
console.log(added_data);
DB.users.add({first_name: "Rishabh", age: 25}, function(err, added_data){
if(!err) {
console.log(results);
}
})
```

Expand All @@ -159,9 +179,12 @@ DB.users.add({first_name: "Rishabh", age: 25}, function(added_data){
> new_data: The data to be updated. (eg. {age: 12})

Example

```
DB.users.update({first_name: "Rishabh"}, {age: 25}, function(updated_table){
console.log(updated_table);
DB.users.update({first_name: "Rishabh"}, {age: 25}, function(err, updated_table){
if(!err) {
console.log(results);
}
})
```

Expand All @@ -173,9 +196,12 @@ DB.users.update({first_name: "Rishabh"}, {age: 25}, function(updated_table){
> new_data: The data to be updated. (eg. {name: "Ken"})

Example

```
DB.users.update_id(3, {name: "Ken", age: 12}, function(updated_table){
console.log(updated_table);
DB.users.update_id(3, {name: "Ken", age: 12}, function(err, updated_table){
if(!err) {
console.log(results);
}
})
```
----------
Expand All @@ -185,14 +211,16 @@ DB.users.update_id(3, {name: "Ken", age: 12}, function(updated_table){
> Erases the complete table.

Example

```
DB.users.erase_db(function(removed_data){
console.log(removed_data);
DB.users.erase_db(function(err, removed_data){
if(!err) {
console.log(results);
}
})
```


*More methods and features are gonna be added soon. Such as update, replace, constraints*

----------

20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.where(query_data).find();
if(callback){
callback(results)
callback(null, results)
}
});
};
Expand All @@ -32,7 +32,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.get(id);
if(callback){
callback(results)
callback(null, results)
}
});
};
Expand All @@ -45,7 +45,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.databaseData[me.db_name];
if(callback){
callback(results)
callback(null, results)
}
});
};
Expand All @@ -60,7 +60,7 @@ RNDBModel.create_db = function(db){
// Add Data
collection.add(data_to_add, function(added_data_id){
if(callback){
callback(added_data_id)
callback(null, added_data_id)
}
RNDBModel.DBEvents.emit("all")
});
Expand Down Expand Up @@ -95,7 +95,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.where(query_data).remove(function(data_removed){
if(callback){
callback(data_removed);
callback(null, data_removed);
}
});
});
Expand All @@ -110,7 +110,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.removeById(id, function(data_removed){
if(callback){
callback(data_removed);
callback(null, data_removed);
}
RNDBModel.DBEvents.emit("all")
});
Expand All @@ -125,7 +125,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.remove(function(data_removed){
if(callback){
callback(data_removed);
callback(null, data_removed);
}
RNDBModel.DBEvents.emit("all")
});
Expand All @@ -141,7 +141,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.where(query_data).update(replace_data, function(data){
if(callback){
callback(data);
callback(null, data);
}
RNDBModel.DBEvents.emit("all")
});
Expand All @@ -158,7 +158,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.updateById(id, replace_data, function(data){
if(callback){
callback(data);
callback(null, data);
}
RNDBModel.DBEvents.emit("all")
});
Expand All @@ -174,7 +174,7 @@ RNDBModel.create_db = function(db){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.removeById(id, function(data_removed){
if(callback){
callback(data_removed);
callback(null, data_removed);
}
RNDBModel.DBEvents.emit("all")
});
Expand Down