forked from yamayamasan/realm-first-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
61 lines (50 loc) · 1.24 KB
/
user.js
File metadata and controls
61 lines (50 loc) · 1.24 KB
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
'use strict';
const Realm = require('realm');
function User() {
if (!(this instanceof User)) return new User();
this.schema = {
name: 'Users',
primaryKey: 'uuid',
properties: {
uuid: 'string',
username: 'string',
age: 'int',
role: 'string',
created_at: 'date'
}
};
this.realm = new Realm({
path: 'db/realm',
schema: [this.schema]
});
}
User.prototype.add = function(input) {
this.realm.write(() => {
const datas = this.realm.create(this.schema.name, input);
});
}
User.prototype.update = function(uuid, inputs) {
const data = this.filtered(`uuid == '${uuid}'`);
this.realm.write(() => {
Object.keys(inputs).forEach((key) => {
data[0][key] = inputs[key];
});
});
}
User.prototype.get = function() {
return this.realm.objects(this.schema.name);
}
User.prototype.filteredRoleUser = function() {
const datas = this.realm.objects(this.schema.name);
return datas.filtered('role == "user"');
}
User.prototype.filtered = function(filterString) {
const datas = this.realm.objects(this.schema.name);
return datas.filtered(filterString);
}
User.prototype.delete = function(object) {
this.realm.write(() => {
this.realm.delete(object);
});
}
module.exports = User;