-
Notifications
You must be signed in to change notification settings - Fork 75
/
db.js
124 lines (105 loc) · 2.75 KB
/
db.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
let _messages = [];
let _data = {
todos: [],
todoTypes: [],
todoTypeMapping: []
};
function insert(table, row) {
let id = uuidv4();
let fields = Object.keys(row);
sendMessages(
fields.map(k => {
return {
dataset: table,
row: row.id || id,
column: k,
value: row[k],
timestamp: Timestamp.send(getClock()).toString()
};
})
);
return id;
}
function update(table, params) {
let fields = Object.keys(params).filter(k => k !== 'id');
sendMessages(
fields.map(k => {
return {
dataset: table,
row: params.id,
column: k,
value: params[k],
timestamp: Timestamp.send(getClock()).toString()
};
})
);
}
function delete_(table, id) {
sendMessages([
{
dataset: table,
row: id,
column: 'tombstone',
value: 1,
timestamp: Timestamp.send(getClock()).toString()
}
]);
}
function _resolveTodos(todos) {
todos = todos.map(todo => ({
...todo,
type: todo.type ? getTodoType(todo.type) : null
}));
todos.sort((t1, t2) => {
if (t1.order < t2.order) {
return 1;
} else if (t1.order > t2.order) {
return -1;
}
return 0;
});
return todos;
}
function getTodos() {
return _resolveTodos(_data.todos.filter(todo => todo.tombstone !== 1));
}
function getDeletedTodos() {
return _resolveTodos(_data.todos.filter(todo => todo.tombstone === 1));
}
function getAllTodos() {
return _resolveTodos(_data.todos);
}
function getTodoType(id) {
// Go through the mapping table, which is a layer of indirection. In
// SQL you could think of doing a LEFT JOIN onto this table and
// using the id from the mapping table instead of the raw id
let mapping = _data.todoTypeMapping.find(m => m.id === id);
let type =
mapping && _data.todoTypes.find(type => type.id === mapping.targetId);
return type && type.tombstone !== 1 ? type : null;
}
function getNumTodos() {
return _data.todos.length;
}
function getTodoTypes() {
return _data.todoTypes.filter(todoType => todoType.tombstone !== 1);
}
function insertTodoType({ name, color }) {
let id = insert('todoTypes', { name, color });
// Create an entry in the mapping table that points it to itself
insert('todoTypeMapping', { id, targetId: id });
}
function deleteTodoType(id, targetId) {
if (targetId) {
// We need to update all the pointers the point to the type that
// we are deleting and point it to the new type. This already
// includes the type we are deleting (when created, it creates a
// mapping to itself)
for (let mapping of _data.todoTypeMapping) {
if (mapping.targetId === id) {
update('todoTypeMapping', { id: mapping.id, targetId });
}
}
}
delete_('todoTypes', id);
}