-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey-val.js
68 lines (61 loc) · 1.49 KB
/
key-val.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
import Realm from 'realm';
const KeyValSchema = {
name: 'KeyVal',
primaryKey: 'key_field',
properties: {
key_field: 'string',
key: {
type: 'string',
indexed: true
},
field: {
type: 'string',
indexed: true
},
rel: {
type: 'string',
optional: true
},
val: {
type: 'string',
optional: true
},
valType: {
type: 'int',
optional: true
},
state: {
type: 'int',
optional: false
}
}
};
const type = function(val) {
let type = -1;
switch (true) {
case (typeof val === 'string'):
type = 0;
break;
case (typeof val === 'number'):
type = 1;
break;
case (typeof val === 'boolean'):
type = 2;
case (val === null):
type = 3;
}
return type;
}
module.exports = new Realm({
schema: [KeyValSchema],
schemaVersion: 1,
migration: (oldRealm, newRealm) => {
// only apply this change if upgrading to schemaVersion 1
const oldObjects = oldRealm.objects('KeyVal');
const newObjects = newRealm.objects('KeyVal');
// loop through all objects and set the name property in the new schema
for (let i = 0; i < oldObjects.length; i++) {
newObjects[i].valType = type(oldObjects[0].val);
}
}
});