-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (146 loc) · 4.02 KB
/
index.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
import axios from "axios";
export default class Database {
#instance;
#meta;
constructor(url = "http://localhost:8080", { user, password } = {}) {
return (async () => {
this.#instance = axios.create({
baseURL: url,
});
if (user && password) {
let token = await this.#instance
.post("/auth", {
user,
password,
})
.then((res) => res.data);
/*if (!token) {
throw new Error("Authentication not possible");
}*/
this.#instance.defaults.headers.common[
"Authorization"
] = `Bearer ${token}`;
}
await this.start();
return this;
})();
}
async start() {
let meta = await this.#instance.get("/meta").then((res) => res.data);
this.#meta = meta;
}
async close() {}
get meta() {
return this.#meta;
}
table(name) {
if (!name) {
throw new Error("no tableName provided");
}
if (!this.meta.tables[name]) {
throw new Error(`table "${name}" does not exist in this database`);
}
return new Table(this.meta.tables[name], this.#instance);
}
}
class Table {
#meta;
#instance;
constructor(meta, instance) {
this.#meta = meta;
this.#instance = instance;
}
get meta() {
return this.#meta;
}
async get(id) {
return this.#instance
.get(`/table/${this.meta.name}/data/${id}`)
.then((res) => res.data);
}
async ensureIndex(name) {
throw new Error("not implemented");
}
async removeIndex(name) {
throw new Error("not implemented");
}
async find(query, context = {}) {
let result = await this.#instance
.post(`/table/${this.meta.name}/data/`, {
query: query.toString(),
context,
})
.then((res) => res.data);
return result;
}
filter(func, context = {}) {
return new Query(func, context, this.meta.name, this.#instance);
}
async save(data) {
let id = await this.#instance
.put(`/table/${this.meta.name}/data/`, data)
.then((res) => res.data);
return id;
}
async remove(id) {
return this.#instance
.delete(`/table/${this.meta.name}/data/${id}`)
.then((res) => res.data);
}
}
class Query {
#tableName;
#instance;
#actions = [];
constructor(query, context, tableName, instance) {
this.#actions.push({
type: actionTypes.FILTER,
data: { query: query.toString(), context },
});
this.#tableName = tableName;
this.#instance = instance;
}
filter(query, context) {
this.#actions.push({
type: actionTypes.FILTER,
data: { query: query.toString(), context },
});
return this;
}
sort(query, context) {
this.#actions.push({
type: actionTypes.SORT,
data: { query: query.toString(), context },
});
return this;
}
map(query, context) {
this.#actions.push({
type: actionTypes.MAP,
data: { query: query.toString(), context },
});
return this;
}
reduce(query, initVal, context) {
this.#actions.push({
type: actionTypes.REDUCE,
data: { query: query.toString(), context, initVal },
});
return this;
}
async then(resolve, error) {
let result = await this.#instance
.post(`/table/${this.#tableName}/data/`, {
type: "chain",
chain: this.#actions,
})
.then((res) => res.data);
resolve(result);
}
}
export let actionTypes = {
FILTER: 0,
SORT: 1,
MAP: 2,
REDUCE: 3,
};