/
index.js
199 lines (186 loc) · 4.89 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
"use strict";
/*
* Cassandra-backed table storage service
*/
// global includes
var spec = require('restbase-mod-table-spec').spec;
function RBCassandra(options) {
this.options = options;
this.conf = options.conf;
this.log = options.log;
this.setup = this.setup.bind(this);
this.store = null;
this.handler = {
spec: 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)
}
};
}
RBCassandra.prototype.createTable = function(rb, req) {
var store = this.store;
// XXX: decide on the interface
req.body.table = req.params.table;
var domain = req.params.domain;
// check if the domains table exists
return store.createTable(domain, req.body)
.then(function() {
return {
status: 201, // created
body: {
type: 'table_created',
title: 'Table was created.',
domain: req.params.domain,
table: req.params.table
}
};
})
.catch(function(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 cassandra storage backend',
stack: e.stack,
err: e,
req: req
}
};
});
};
// Query a table
RBCassandra.prototype.get = function(rb, req) {
var rp = req.params;
if (!rp.rest && !req.body) {
// Return the entire table
// XXX: Only list the hash keys?
req.body = {
table: rp.table,
limit: 10
};
}
var domain = req.params.domain;
return this.store.get(domain, req.body)
.then(function(res) {
return {
status: res.items.length ? 200 : 404,
body: res
};
})
.catch(function(e) {
return {
status: 500,
body: {
type: 'query_error',
title: 'Error in Cassandra table storage backend',
stack: e.stack,
err: e,
req: req
}
};
});
};
// Update a table
RBCassandra.prototype.put = function(rb, req) {
var domain = req.params.domain;
// XXX: Use the path to determine the primary key?
return this.store.put(domain, req.body)
.then(function(res) {
return {
status: 201 // created
};
})
.catch(function(e) {
return {
status: 500,
body: {
type: 'update_error',
title: 'Internal error in Cassandra table storage backend',
stack: e.stack,
err: e,
req: req
}
};
});
};
RBCassandra.prototype.dropTable = function(rb, req) {
var domain = req.params.domain;
return this.store.dropTable(domain, req.params.table)
.then(function(res) {
return {
status: 204 // done
};
})
.catch(function(e) {
return {
status: 500,
body: {
type: 'delete_error',
title: 'Internal error in Cassandra table storage backend',
stack: e.stack,
err: e,
req: req
}
};
});
};
RBCassandra.prototype.getTableSchema = function(rb, req) {
var domain = req.params.domain;
return this.store.getTableSchema(domain, req.params.table)
.then(function(res) {
return {
status: 200,
headers: { etag: res.tid.toString() },
body: res.schema
};
})
.catch(function(e) {
return {
status: 500,
body: {
type: 'schema_query_error',
title: 'Internal error querying table schema in Cassandra storage backend',
stack: e.stack,
err: e,
req: req
}
};
});
};
/*
* Setup / startup
*
* @return {Promise<registry>}
*/
RBCassandra.prototype.setup = function setup() {
var self = this;
// Set up storage backend
var backend = require('./lib/index');
return backend(self.options)
.then(function(store) {
self.store = store;
// console.log('RB setup complete', self.handler);
return self.handler;
});
};
/**
* Factory
* @param options
* @return {Promise<registration>} with registration being the registration
* object
*/
function makeRBCassandra(options) {
var rb = new RBCassandra(options);
return rb.setup();
}
module.exports = makeRBCassandra;