-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightDBAPI.js
More file actions
294 lines (272 loc) · 6.39 KB
/
LightDBAPI.js
File metadata and controls
294 lines (272 loc) · 6.39 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/** ****************************************************************************************************
* @file: LightDBAPI.js
* @project: LightDB
* @author Nick Soggin <iSkore@users.noreply.github.com> on 05-Jun-2018
*******************************************************************************************************/
'use strict';
const
request = require( 'request' ),
{ isSuccess } = require( 'http-response-class' );
/**
* LightDBAPI
*/
class LightDBAPI
{
/**
* constructor
* @param {string} [protocol=http] - LightDB protocol
* @param {string} [host=127.0.0.1] - LightDB host
* @param {number} [port=23000] - LightDB port
*/
constructor( protocol = 'http', host = '127.0.0.1', port = 23000 )
{
this.protocol = protocol;
this.host = host;
this.port = port;
this.basename = `${ this.protocol }://${ this.host }:${ this.port }`;
}
request( opts )
{
return new Promise(
( res, rej ) => {
if( opts.uri ) {
if( !opts.uri.startsWith( this.basename ) ) {
opts.uri = `${ this.basename }${ opts.uri }`;
}
}
opts.json = opts.json || true;
request( opts,
( e, status, data ) => e ?
rej( e ) :
isSuccess( status.statusCode ) ?
res( data ) :
rej( data )
);
}
);
}
/**
* ping
* @description
* ALL 127.0.0.1:23000/ping
* status check
* @return {string} - returns "pong"
*/
ping()
{
return this.request( { uri: '/ping' } );
}
/**
* version
* @description
* ALL 127.0.0.1:23000/version
* returns version of LightDB
* @return {string} - returns "v0.0.1"
*/
version()
{
return this.request( { uri: '/version' } );
}
/**
* kill
* @description
* GET 127.0.0.1:23000/kill
* kills LightDB server
* @return {string} - returns "v0.0.1"
*/
kill()
{
return this.request( { uri: '/kill' } );
}
/**
* databaseInfo
* @description
* GET 127.0.0.1:23000/
* returns array of database information
* @return {Object[]} - returns array of collection data
*/
databaseInfo()
{
return this.request( { uri: '/' } );
}
/**
* toJSON
* @description
* GET 127.0.0.1:23000/json
* returns array of database information, collection information, collection items, and all data
* @return {Object[]} - returns entire serializable JSON dump of the LightDB
*/
toJSON()
{
return this.request( { uri: '/json' } );
}
/**
* listCollections
* @description
* GET 127.0.0.1:23000/collections
* returns array of collection names
* @return {string[]} - returns list of collection names
*/
listCollections()
{
return this.request( { uri: '/collections' } );
}
/**
* getCollection
* @description
* GET 127.0.0.1:23000/:collection
* returns collection data
* @param {string} _id - collection identifier
* @return {Object} - returns collection data
*/
getCollection( _id )
{
return this.request( { uri: `/${ _id }` } );
}
/**
* createCollection
* @description
* POST 127.0.0.1:23000/:collection
* returns new collection data
* @param {string} _id - collection identifier
* @param {string} metadata - collection metadata
* @return {Object} - returns collection data
*/
createCollection( _id, metadata )
{
return this.request( {
method: 'POST',
uri: `/${ _id }`,
json: metadata
} );
}
/**
* updateCollection
* @description
* PUT 127.0.0.1:23000/:collection
* returns updated collection data
* @param {string} _id - collection identifier
* @param {string} metadata - collection metadata
* @return {Object} - returns collection data
*/
updateCollection( _id, metadata )
{
return this.request( {
method: 'PUT',
uri: `/${ _id }`,
json: metadata
} );
}
/**
* deleteCollection
* @description
* DELETE 127.0.0.1:23000/:collection
* returns nothing
* @param {string} _id - collection identifier
* @return {Object} - returns {}
*/
deleteCollection( _id )
{
return this.request( {
method: 'DELETE',
uri: `/${ _id }`
} );
}
/**
* createItem
* @description
* POST 127.0.0.1:23000/:collection/:id
* returns item data
* @param {string} collection - collection identifier
* @param {string} _id - item identifier
* @param {*} data - item data
* @return {Object} - returns new item information
*/
createItem( collection, _id = 'item', data )
{
return this.request( {
method: 'POST',
uri: `/${ collection }/${ _id }`,
json: data
} );
}
/**
* getItem
* @description
* GET 127.0.0.1:23000/:collection/:id
* returns item data
* @param {string} collection - collection identifier
* @param {string} _id - item identifier (specify nothing to return all items)
* @return {Object} - returns item
*/
getItem( collection, _id = 'items' )
{
return this.request( { uri: `/${ collection }/${ _id }` } );
}
/**
* getItems
* @description
* GET 127.0.0.1:23000/:collection/items
* returns all items in collection
* @param {string} collection - collection identifier
* @return {Object} - returns items in collection
*/
getItems( collection )
{
return this.getItem( collection );
}
/**
* updateItem
* @description
* PUT 127.0.0.1:23000/:collection/:id
* returns updated item data
* @param {string} collection - collection identifier
* @param {string} _id - item identifier
* @param {*} data - item data
* @return {Object} - returns new item information
*/
updateItem( collection, _id, data )
{
return this.request( {
method: 'PUT',
uri: `/${ collection }/${ _id }`,
json: data
} );
}
/**
* deleteItem
* @description
* DELETE 127.0.0.1:23000/:collection/:id
* returns nothing
* @param {string} collection - collection identifier
* @param {string} _id - item identifier
* @return {Object} - returns {}
*/
deleteItem( collection, _id )
{
return this.request( {
method: 'DELETE',
uri: `/${ collection }/${ _id }`
} );
}
/**
* task
* @description
* POST 127.0.0.1:23000/task
* @param {Object} data - operation specifications
* @param {string} data.action - action to perform (ie: createItem)
* @param {string} [data.collection] - specify which collection to perform the action
* @param {string} [data.parameters] - specify filters, sorting patters, etc.
* @param {string} [data.data] - specify the data to pass in the action
* @return {*} - returns depend on the request
*/
task( data )
{
return this.request( {
method: 'POST',
uri: '/task',
json: data
} );
}
}
module.exports = LightDBAPI;