-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
executable file
·186 lines (171 loc) · 5.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
var ReactNativeStore = require('./asyncstore');
var Events = require('eventemitter3')
var RNDBModel = {};
RNDBModel.DBEvents = new Events()
RNDBModel.create_db = function(db){
var me = this;
me.db_name = db;
/**
* @description Finds all the objects based on the query
* @param query_data
* @param callback
*/
me.get = function(query_data, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.where(query_data).find();
if(callback){
callback(results)
}
});
};
/**
* @description Finds by ID
* @param id
* @param callback
*/
me.get_id = function(id, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.get(id);
if(callback){
callback(results)
}
});
};
/**
* @description Gets all the data of the table
* @param callback
*/
me.get_all = function( callback){
ReactNativeStore.table(me.db_name).then(function(collection){
var results = collection.databaseData[me.db_name];
if(callback){
callback(results)
}
});
};
/**
* @description Adds data to the Table in the DB
* @param data_to_add
* @param callback
*/
me.add = function(data_to_add, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
// Add Data
collection.add(data_to_add, function(added_data_id){
if(callback){
callback(added_data_id)
}
RNDBModel.DBEvents.emit("all")
});
});
};
/**
* @description Adds array of data (objects) to the Table in the DB
* @param data_to_add
* @param callback
*/
me.add_all = function(data_to_add, callback){
var self = this;
ReactNativeStore.table(me.db_name).then(function(collection){
// Add Data
collection.multiAdd(data_to_add, function(added_data){
if(callback){
callback(added_data)
}
RNDBModel.DBEvents.emit("all")
});
});
};
/**
* @description Removes all the objects matching the query
* @param query_data
* @param callback
*/
me.remove = function(query_data, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.where(query_data).remove(function(data_removed){
if(callback){
callback(data_removed);
}
});
});
};
/**
* @description Removed object by ID
* @param id
* @param callback
*/
me.remove_id = function(id, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.removeById(id, function(data_removed){
if(callback){
callback(data_removed);
}
RNDBModel.DBEvents.emit("all")
});
});
};
/**
* @description Erases the complete DB
* @param callback
*/
me.erase_db = function(callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.remove(function(data_removed){
if(callback){
callback(data_removed);
}
RNDBModel.DBEvents.emit("all")
});
});
}
/**
* @description Updates the Table with the query
* @param query_data
* @param replace_data
* @param callback
*/
me.update = function(query_data, replace_data, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.where(query_data).update(replace_data, function(data){
if(callback){
callback(data);
}
RNDBModel.DBEvents.emit("all")
});
});
};
/**
* @description Updates the DB Object by ID
* @param id
* @param replace_data
* @param callback
*/
me.update_id = function(id, replace_data, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.updateById(id, replace_data, function(data){
if(callback){
callback(data);
}
RNDBModel.DBEvents.emit("all")
});
});
};
/**
* @description Removed object by ID
* @param id
* @param callback
*/
me.remove_id = function(id, callback){
ReactNativeStore.table(me.db_name).then(function(collection){
collection.removeById(id, function(data_removed){
if(callback){
callback(data_removed);
}
RNDBModel.DBEvents.emit("all")
});
});
};
};
module.exports = RNDBModel;