forked from yamayamasan/realm-first-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
88 lines (77 loc) · 1.87 KB
/
main.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
'use strict';
const model = new require('./user.js')();
const program = require('commander');
program
.version('1.0.0')
.option('-a, --add [value]', 'add item')
.option('-g, --get [value]', 'get item')
.option('-d, --delete [value]', 'delete item')
.option('-r, --role [value]', 'role')
.option('-f, --filtered [value]', 'filtered')
.option('-u, --update [value]', 'update')
.parse(process.argv);
if (program.add) {
const args = program.add.split(',');
const obj = {};
args.forEach((arg) => {
const argArr = arg.split(':');
const key = argArr[0].trim();
const val = argArr[1];
obj[key] = val.trim();
});
obj['uuid'] = getUnixTime();
obj['age'] = parseInt(obj.age);
obj['created_at'] = new Date();
console.log(obj);
model.add(obj);
process.exit();
}
if (program.get) {
const datas = model.get();
console.log('datas:', datas.toJSON());
process.exit();
}
if (program.delete) {
const arg = program.delete;
const datas = model.get();
let data = null;
datas.forEach((_data) => {
if (_data.uuid === arg) {
data = _data;
}
});
model.delete(data);
process.exit();
}
if (program.role) {
const datas = model.filteredRoleUser();
console.log('datas:', datas.toJSON());
process.exit();
}
if (program.filtered) {
const arg = program.filtered;
const datas = model.filtered(arg);
console.log('datas:', datas.toJSON());
process.exit();
}
if (program.update) {
const args = program.update.split(',');
const obj = {};
args.forEach((arg) => {
const argArr = arg.split(':');
const key = argArr[0].trim();
const val = argArr[1];
obj[key] = val.trim();
});
if (obj.age) {
obj['age'] = parseInt(obj.age);
}
const uuid = obj.uuid;
delete obj.uuid;
const datas = model.update(uuid, obj);
process.exit();
}
function getUnixTime() {
const date = new Date();
return 'uuid_' + date.getTime();
}