-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
219 lines (204 loc) · 5.75 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
/*
* SQLite-backed table storage service
*/
// global includes
const spec = require('restbase-mod-table-spec').spec;
class RBSQLite {
constructor(options) {
this.options = options;
this.conf = options.conf;
this.log = options.log;
this.setup = this.setup.bind(this);
this.store = null;
this.handler = {
spec,
operations: {
createTable: this.createTable.bind(this),
dropTable: this.dropTable.bind(this),
getTableSchema: this.getTableSchema.bind(this),
get: this.get.bind(this),
put: this.put.bind(this),
delete: this.delete.bind(this)
}
};
}
createTable(rb, req) {
const store = this.store;
req.body.table = req.params.table;
const domain = req.params.domain;
// check if the domains table exists
return store.createTable(domain, req.body)
.then(() => ({
// created
status: 201,
body: {
type: 'table_created',
title: 'Table was created.',
domain: req.params.domain,
table: req.params.table
}
}))
.catch((e) => {
this.log('sqlite/error', e);
if (e.status >= 400) {
return {
status: e.status,
body: e.body
};
}
return {
status: 500,
body: {
type: 'table_creation_error',
title: 'Internal error while creating a ' +
'table within the SQLite storage backend',
stack: e.stack,
err: e,
req
}
};
});
}
// Query a table
get(rb, req) {
const rp = req.params;
if (!rp.rest && !req.body) {
req.body = {
table: rp.table,
limit: 10
};
}
const domain = req.params.domain;
return this.store.get(domain, req.body)
.then((res) => ({
status: res.items.length ? 200 : 404,
body: res
}))
.catch((e) => {
this.log('sqlite/error', e);
return {
status: 500,
body: {
type: 'query_error',
title: 'Error in SQLite table storage backend',
stack: e.stack,
err: e,
req
}
};
});
}
// Update a table
put(rb, req) {
const domain = req.params.domain;
return this.store.put(domain, req.body)
.then(() => ({
// created
status: 201
}))
.catch((e) => {
this.log('sqlite/error', e);
return {
status: 500,
body: {
type: 'update_error',
title: 'Internal error in SQLite table storage backend',
stack: e.stack,
err: e,
req
}
};
});
}
dropTable(rb, req) {
const domain = req.params.domain;
return this.store.dropTable(domain, req.params.table)
.then(() => ({
// done
status: 204
}))
.catch((e) => {
this.log('sqlite/error', e);
return {
status: 500,
body: {
type: 'delete_error',
title: 'Internal error in SQLite table storage backend',
stack: e.stack,
err: e,
req
}
};
});
}
getTableSchema(rb, req) {
const domain = req.params.domain;
return this.store.getTableSchema(domain, req.params.table)
.then((res) => ({
status: 200,
body: res.schema
}))
.catch((e) => {
this.log('sqlite/error', e);
return {
status: 500,
body: {
type: 'schema_query_error',
title: 'Internal error querying table schema in SQLite storage backend',
stack: e.stack,
err: e,
req
}
};
});
}
delete(rb, req) {
const domain = req.params.domain;
// XXX: Use the path to determine the primary key?
return this.store.delete(domain, req.body)
.thenReturn({
// deleted
status: 204
})
.catch((e) => ({
status: 500,
body: {
type: 'delete_error',
title: 'Internal error in SQLite table storage backend',
stack: e.stack,
err: e,
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
}
/*
* Setup / startup
*
* @return {Promise<registry>}
*/
setup() {
// Set up storage backend
const createDB = require('./lib/db');
return createDB(this.options)
.then((store) => {
this.store = store;
return this.handler;
});
}
}
/**
* Factory
* @param {Object} options
* @return {Promise<registration>} with registration being the registration
* object
*/
function makeRBSQLite(options) {
const rb = new RBSQLite(options);
return rb.setup();
}
module.exports = makeRBSQLite;