-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlokiDb.js
165 lines (140 loc) · 4.18 KB
/
lokiDb.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
var lokijs = require('lokijs'),
lokiNativescriptAdapter = require('lokijs/src/loki-nativescript-adapter'),
dummyjson = require('dummy-json'),
_ = require('lodash');
var LokiDb = (function () {
// private variable
var texto = 'default';
var _db;
var _users;
var _dashboard;
// private function
function log() {
console.log('doo!');
}
function setText(text) {
texto = text;
}
function getText() {
return texto;
}
function getUsers() {
return _users;
}
function getDashboard() {
return _dashboard;
}
function upsert(collection, idField, record) {
var query = {};
query[idField] = record[idField];
var existingRecord = collection.findOne(query);
if (existingRecord) {
// The record exists. Do an update.
var updatedRecord = existingRecord;
// Only update the fields contained in `record`. All fields not contained
// in `record` remain unchanged.
_.forEach(record, function (value, key) {
updatedRecord[key] = value;
});
collection.update(updatedRecord);
} else {
// The record doesn't exist. Do an insert.
collection.insert(record);
}
}
function _init() {
_db = new lokijs('uda.json', {
adapter: new lokiNativescriptAdapter()
});
_users = _db.addCollection('users', {
indices: ['id']
});
_dashboard = _db.addCollection('dashboard', {
indices: ['id']
});
var myHelpers = {
rol: function () {
// Use randomArrayItem to ensure the seeded random number generator is used
return dummyjson.utils.randomArrayItem(['administrador', 'desarrollador', 'espectador', 'informador', 'manager']);
}
};
var template = '[ \
{{#repeat 1000}} \
{\
"id":"{{@index}}",\
"nombre":"{{firstName}}",\
"apellido1":"{{lastName}}",\
"apellido2":"{{lastName}}",\
"ejie":"{{int 0 1}} ",\
"fechaAlta":"{{date \'2010\' \'2014\' \'DD/MM/YYYY\'}}",\
"fechaBaja":"{{date \'2015\' \'2016\' \'DD/MM/YYYY\'}}",\
"rol":"{{rol}}"\
} \
{{/repeat}} \
]';
// for (var i=0;i<10;i++){
var result = dummyjson.parse(template, {
helpers: myHelpers
}); // Returns a string
var obj = JSON.parse(result);
for (var i = 0; i < obj.length; i++) {
_users.insert(obj[i]);
}
// Dashboard
_dashboard.insert({
'id': '1',
'nombre': 'Escritorio 1',
'serializedArray': '[]'
});
_dashboard.insert({
'id': '2',
'nombre': 'Escritorio 2',
'serializedArray': '[]'
});
_dashboard.insert({
'id': '3',
'nombre': 'Escritorio 3',
'serializedArray': '[]'
});
_dashboard.insert({
'id': '4',
'nombre': 'Escritorio 4',
'serializedArray': '[]'
});
_dashboard.insert({
'id': '5',
'nombre': 'Escritorio 5',
'serializedArray': '[]'
});
}
// return public interface
return {
log: function () {
// we have access to the private function here
// as well as the private variable (btw)
log();
},
setText: function (text) {
setText(text);
},
initialize: function () {
_init();
},
getText: function () {
return getText();
},
getUsers: function () {
return getUsers();
},
upsert: function (collection, idField, record) {
return upsert(collection, idField, record);
},
// Dashboard
dashboard: {
getAll: function (userId) {
return getDashboard();
}
}
};
}()); // we import jQuery as a global symbol
module.exports = LokiDb;