temba 0.8.1 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -29
- package/package.json +22 -13
- package/dist/config/index.js +0 -78
- package/dist/delay/middleware.js +0 -16
- package/dist/errors/index.js +0 -21
- package/dist/errors/middleware.js +0 -14
- package/dist/logging/index.js +0 -38
- package/dist/queries/in-memory.js +0 -96
- package/dist/queries/index.js +0 -21
- package/dist/queries/mongo.js +0 -291
- package/dist/routes/delete.js +0 -82
- package/dist/routes/get.js +0 -83
- package/dist/routes/index.js +0 -95
- package/dist/routes/post.js +0 -64
- package/dist/routes/put.js +0 -90
- package/dist/routes/validator.js +0 -21
- package/dist/server.js +0 -80
- package/dist/urls/middleware.js +0 -42
- package/dist/urls/urlParser.js +0 -22
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# Temba
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/temba)
|
|
3
4
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
4
|
-
|
|
5
5
|
[](#contributors-)
|
|
6
|
-
|
|
7
6
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
8
7
|
|
|
9
8
|
**Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).**
|
|
@@ -20,6 +19,8 @@ This project is inspired by the fantastic [json-server](https://github.com/typic
|
|
|
20
19
|
|
|
21
20
|
[Getting Started](#getting-started)
|
|
22
21
|
|
|
22
|
+
[What Tema does](#what-temba-does)
|
|
23
|
+
|
|
23
24
|
[Usage](#usage)
|
|
24
25
|
|
|
25
26
|
## Temba?
|
|
@@ -39,7 +40,7 @@ Prerequisites you need to have:
|
|
|
39
40
|
|
|
40
41
|
> Wthout a database, Temba also works. However, then data is kept in memory and flushed every time the server restarts.
|
|
41
42
|
|
|
42
|
-
### Use `npx`
|
|
43
|
+
### Use the starter with `npx`
|
|
43
44
|
|
|
44
45
|
Create your own Temba server with the following command and you are up and running!
|
|
45
46
|
|
|
@@ -47,7 +48,7 @@ Create your own Temba server with the following command and you are up and runni
|
|
|
47
48
|
npx create-temba-server my-rest-api
|
|
48
49
|
```
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
This command clones the [Temba-starter](https://github.com/bouwe77/temba-starter) repository and installs all dependencies.
|
|
51
52
|
|
|
52
53
|
### Manually adding to an existing app
|
|
53
54
|
|
|
@@ -71,9 +72,7 @@ server.listen(port, () => {
|
|
|
71
72
|
|
|
72
73
|
By passing a config object to the `create` function you can customize Temba's behavior. Refer to the [config settings](#config-settings-overview) below for the various possibilities.
|
|
73
74
|
|
|
74
|
-
##
|
|
75
|
-
|
|
76
|
-
### Introduction
|
|
75
|
+
## What Temba does
|
|
77
76
|
|
|
78
77
|
Out of the box, Temba gives you a CRUD REST API to any resource name you can think of.
|
|
79
78
|
|
|
@@ -94,9 +93,21 @@ The HTTP methods that are supported are `GET`, `POST`, `PUT` and `DELETE`. For a
|
|
|
94
93
|
|
|
95
94
|
On the root URI (e.g. http://localhost:8080/) only a `GET` request is supported, which shows you a message indicating the API is working. All other HTTP methods on the root URI return a `405 Method Not Allowed` response.
|
|
96
95
|
|
|
97
|
-
###
|
|
96
|
+
### JSON
|
|
97
|
+
|
|
98
|
+
Temba supports JSON only.
|
|
99
|
+
|
|
100
|
+
Request bodies sent with a `POST` and `PUT` requests are valid when the request body is either empty, or when it's valid formatted JSON. Adding a `Content-Type: application/json` header is required. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
|
|
101
|
+
|
|
102
|
+
Any valid formatted JSON is accepted and stored. If you want to validate or even change the JSON in the request bodies, check out the [`requestBodyValidator`](#request-body-validation-or-mutation) callbacks.
|
|
103
|
+
|
|
104
|
+
IDs are auto generated when creating resources. IDs in the JSON request body are ignored for any request.
|
|
98
105
|
|
|
99
|
-
|
|
106
|
+
## Usage
|
|
107
|
+
|
|
108
|
+
### In-memory or MongoDB
|
|
109
|
+
|
|
110
|
+
By default data is stored in memory. This means the data is flushed when the server restarts. To persist your data, provide the `connectionString` config setting for your MongoDB database:
|
|
100
111
|
|
|
101
112
|
```js
|
|
102
113
|
const config = {
|
|
@@ -118,14 +129,6 @@ const server = temba.create(config)
|
|
|
118
129
|
|
|
119
130
|
Requests on these resources only give a `404 Not Found` if the ID does not exist. Requests on any other resource will always return a `404 Not Found`.
|
|
120
131
|
|
|
121
|
-
### JSON
|
|
122
|
-
|
|
123
|
-
Temba only supports JSON. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
|
|
124
|
-
|
|
125
|
-
When sending JSON data (`POST` and `PUT` requests), adding a `Content-Type: application/json` header is required.
|
|
126
|
-
|
|
127
|
-
IDs are auto generated when creating resources. IDs in the JSON request body are ignored.
|
|
128
|
-
|
|
129
132
|
### Static assets
|
|
130
133
|
|
|
131
134
|
If you want to host static assets next to the REST API, configure the `staticFolder`:
|
|
@@ -179,13 +182,13 @@ If you want to do input validation before the `POST` or `PUT` request body is sa
|
|
|
179
182
|
```js
|
|
180
183
|
const config = {
|
|
181
184
|
requestBodyValidator: {
|
|
182
|
-
post: (resourceName, requestBody) {
|
|
185
|
+
post: (resourceName, requestBody) => {
|
|
183
186
|
// Validate, or even change the requestBody
|
|
184
187
|
},
|
|
185
|
-
put: (resourceName, requestBody) {
|
|
188
|
+
put: (resourceName, requestBody) => {
|
|
186
189
|
// Validate, or even change the requestBody
|
|
187
|
-
}
|
|
188
|
-
}
|
|
190
|
+
},
|
|
191
|
+
},
|
|
189
192
|
}
|
|
190
193
|
|
|
191
194
|
const server = temba.create(config)
|
|
@@ -206,16 +209,21 @@ Example:
|
|
|
206
209
|
```js
|
|
207
210
|
const config = {
|
|
208
211
|
requestBodyValidator: {
|
|
209
|
-
post: (resourceName, requestBody) {
|
|
212
|
+
post: (resourceName, requestBody) => {
|
|
210
213
|
// Do not allow Pokemons to be created: 400 Bad Request
|
|
211
|
-
if (resourceName === 'pokemons')
|
|
214
|
+
if (resourceName === 'pokemons')
|
|
215
|
+
return 'You are not allowed to create new Pokemons'
|
|
212
216
|
|
|
213
217
|
// Add a genre to Star Trek films:
|
|
214
|
-
if (
|
|
218
|
+
if (
|
|
219
|
+
resourceName === 'movies' &&
|
|
220
|
+
requestBody.title.startsWith('Star Trek')
|
|
221
|
+
)
|
|
222
|
+
return { ...requestBody, genre: 'Science Fiction' }
|
|
215
223
|
|
|
216
224
|
// If you end up here, void will be returned, so the request will just be saved.
|
|
217
225
|
},
|
|
218
|
-
}
|
|
226
|
+
},
|
|
219
227
|
}
|
|
220
228
|
|
|
221
229
|
const server = temba.create(config)
|
|
@@ -236,13 +244,13 @@ const config = {
|
|
|
236
244
|
cacheControl: 'public, max-age=300',
|
|
237
245
|
delay: 500,
|
|
238
246
|
requestBodyValidator: {
|
|
239
|
-
post: (resourceName, requestBody) {
|
|
247
|
+
post: (resourceName, requestBody) => {
|
|
240
248
|
// Validate, or even change the requestBody
|
|
241
249
|
},
|
|
242
|
-
put: (resourceName, requestBody) {
|
|
250
|
+
put: (resourceName, requestBody) => {
|
|
243
251
|
// Validate, or even change the requestBody
|
|
244
|
-
}
|
|
245
|
-
}
|
|
252
|
+
},
|
|
253
|
+
},
|
|
246
254
|
}
|
|
247
255
|
const server = temba.create(config)
|
|
248
256
|
```
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "temba",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).",
|
|
5
|
-
"main": "dist/server.
|
|
5
|
+
"main": "dist/server.ts",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "rm -rf dist &&
|
|
8
|
-
"
|
|
7
|
+
"build": "rm -rf dist && tsc && cp package.json README.md ./dist",
|
|
8
|
+
"test": "jest --watch",
|
|
9
|
+
"lint": "eslint --ignore-path .gitignore --ext .js,.ts .",
|
|
10
|
+
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\""
|
|
9
11
|
},
|
|
10
12
|
"repository": {
|
|
11
13
|
"type": "git",
|
|
@@ -17,19 +19,26 @@
|
|
|
17
19
|
"files": [
|
|
18
20
|
"dist/**",
|
|
19
21
|
"package.json",
|
|
20
|
-
"!
|
|
22
|
+
"!tests"
|
|
21
23
|
],
|
|
22
24
|
"devDependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
25
|
+
"@types/cors": "^2.8.12",
|
|
26
|
+
"@types/express": "^4.17.13",
|
|
27
|
+
"@types/jest": "^27.4.0",
|
|
28
|
+
"@types/morgan": "^1.9.3",
|
|
29
|
+
"@types/supertest": "^2.0.11",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^5.10.0",
|
|
31
|
+
"@typescript-eslint/parser": "^5.10.0",
|
|
32
|
+
"eslint": "^8.7.0",
|
|
33
|
+
"eslint-config-prettier": "^8.3.0",
|
|
34
|
+
"jest": "^27.4.7",
|
|
35
|
+
"jest-extended": "^0.11.5",
|
|
36
|
+
"prettier": "^2.5.1",
|
|
37
|
+
"supertest": "^6.2.2",
|
|
38
|
+
"ts-jest": "^27.1.3",
|
|
39
|
+
"typescript": "^4.5.5"
|
|
30
40
|
},
|
|
31
41
|
"dependencies": {
|
|
32
|
-
"@babel/runtime": "^7.15.4",
|
|
33
42
|
"@rakered/mongo": "^1.6.0",
|
|
34
43
|
"connect-pause": "^0.1.0",
|
|
35
44
|
"cors": "^2.8.5",
|
package/dist/config/index.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.initConfig = initConfig;
|
|
9
|
-
|
|
10
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
|
-
var _logging = require("../logging");
|
|
13
|
-
|
|
14
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
17
|
-
|
|
18
|
-
var defaultConfig = {
|
|
19
|
-
resourceNames: [],
|
|
20
|
-
validateResources: false,
|
|
21
|
-
logLevel: _logging.logLevels.DEBUG,
|
|
22
|
-
staticFolder: null,
|
|
23
|
-
apiPrefix: '',
|
|
24
|
-
connectionString: null,
|
|
25
|
-
cacheControl: 'no-store',
|
|
26
|
-
delay: 0,
|
|
27
|
-
requestBodyValidator: {
|
|
28
|
-
post: function post() {},
|
|
29
|
-
put: function put() {}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function initConfig(userConfig) {
|
|
34
|
-
if (!userConfig) return defaultConfig;
|
|
35
|
-
|
|
36
|
-
var config = _objectSpread({}, defaultConfig);
|
|
37
|
-
|
|
38
|
-
if (userConfig.resourceNames && userConfig.resourceNames.length > 0) {
|
|
39
|
-
config.resourceNames = userConfig.resourceNames;
|
|
40
|
-
config.validateResources = true;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (userConfig.logLevel && userConfig.logLevel.length !== 0 && Object.keys(_logging.logLevels).includes(userConfig.logLevel.toUpperCase())) {
|
|
44
|
-
config.logLevel = userConfig.logLevel.toUpperCase();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (userConfig.staticFolder) {
|
|
48
|
-
config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (userConfig.apiPrefix) {
|
|
52
|
-
config.apiPrefix = '/' + userConfig.apiPrefix.replace(/[^a-zA-Z0-9]/g, '') + '/';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (userConfig.connectionString && userConfig.connectionString.length > 0) {
|
|
56
|
-
config.connectionString = userConfig.connectionString;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
|
|
60
|
-
config.cacheControl = userConfig.cacheControl;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (userConfig.delay && userConfig.delay.length !== 0 && typeof Number(userConfig.delay) === 'number' && Number(userConfig.delay) > 0 && Number(userConfig.delay) < 10000) {
|
|
64
|
-
config.delay = Number(userConfig.delay);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (userConfig.requestBodyValidator) {
|
|
68
|
-
if (userConfig.requestBodyValidator.post && typeof userConfig.requestBodyValidator.post === 'function') {
|
|
69
|
-
config.requestBodyValidator.post = userConfig.requestBodyValidator.post;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (userConfig.requestBodyValidator.put && typeof userConfig.requestBodyValidator.put === 'function') {
|
|
73
|
-
config.requestBodyValidator.put = userConfig.requestBodyValidator.put;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return config;
|
|
78
|
-
}
|
package/dist/delay/middleware.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createDelayMiddleware = createDelayMiddleware;
|
|
7
|
-
|
|
8
|
-
var pause = require('connect-pause');
|
|
9
|
-
|
|
10
|
-
function createDelayMiddleware(delay) {
|
|
11
|
-
return function (req, res, next) {
|
|
12
|
-
console.log('Start delay...');
|
|
13
|
-
pause(delay)(req, res, next);
|
|
14
|
-
console.log('Delay finished!');
|
|
15
|
-
};
|
|
16
|
-
}
|
package/dist/errors/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.new404NotFoundError = new404NotFoundError;
|
|
7
|
-
exports.new400BadRequestError = new400BadRequestError;
|
|
8
|
-
|
|
9
|
-
function new404NotFoundError() {
|
|
10
|
-
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Not Found';
|
|
11
|
-
var error = new Error(message);
|
|
12
|
-
error.status = 404;
|
|
13
|
-
return error;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function new400BadRequestError() {
|
|
17
|
-
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Bad Request';
|
|
18
|
-
var error = new Error(message);
|
|
19
|
-
error.status = 400;
|
|
20
|
-
return error;
|
|
21
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.errorHandler = errorHandler;
|
|
7
|
-
|
|
8
|
-
function errorHandler(err, _, res, next) {
|
|
9
|
-
console.log('errorHandler: ' + err.message);
|
|
10
|
-
if (!err.status) err.status = 500;
|
|
11
|
-
return res.status(err.status).json({
|
|
12
|
-
message: err.message
|
|
13
|
-
});
|
|
14
|
-
}
|
package/dist/logging/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createLogger = createLogger;
|
|
7
|
-
exports.logLevels = void 0;
|
|
8
|
-
var DEBUG = 'DEBUG';
|
|
9
|
-
var INFO = 'INFO'; //TODO Add 'NONE'
|
|
10
|
-
|
|
11
|
-
var logLevels = {
|
|
12
|
-
DEBUG: DEBUG,
|
|
13
|
-
INFO: INFO
|
|
14
|
-
};
|
|
15
|
-
exports.logLevels = logLevels;
|
|
16
|
-
|
|
17
|
-
function createLogger(logLevel) {
|
|
18
|
-
return {
|
|
19
|
-
debug: function debug(message) {
|
|
20
|
-
try {
|
|
21
|
-
if (logLevel === DEBUG) console.log(new Date(), 'DEBUG -', message);
|
|
22
|
-
} catch (_unused) {//swallow exceptions during logging
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
info: function info(message) {
|
|
26
|
-
try {
|
|
27
|
-
if (logLevel === DEBUG || logLevel === INFO) console.info(new Date(), 'INFO -', message);
|
|
28
|
-
} catch (_unused2) {//swallow exceptions during logging
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
error: function error(message) {
|
|
32
|
-
try {
|
|
33
|
-
console.error(new Date(), 'ERROR -', message);
|
|
34
|
-
} catch (_unused3) {//swallow exceptions during logging
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = void 0;
|
|
9
|
-
|
|
10
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
17
|
-
|
|
18
|
-
var data = {};
|
|
19
|
-
|
|
20
|
-
function connectToDatabase() {// do nothing
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getAll(resourceName) {
|
|
24
|
-
createResourceArrayIfNecessary(resourceName);
|
|
25
|
-
return new Promise(function (resolve) {
|
|
26
|
-
resolve(data[resourceName]);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function getById(resourceName, id) {
|
|
31
|
-
createResourceArrayIfNecessary(resourceName);
|
|
32
|
-
return new Promise(function (resolve) {
|
|
33
|
-
resolve(data[resourceName].find(function (item) {
|
|
34
|
-
return item.id === id;
|
|
35
|
-
}));
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function create(resourceName, item) {
|
|
40
|
-
createResourceArrayIfNecessary(resourceName);
|
|
41
|
-
|
|
42
|
-
var newItem = _objectSpread(_objectSpread({}, item), {}, {
|
|
43
|
-
id: String(new Date().getTime())
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
data[resourceName] = [].concat((0, _toConsumableArray2["default"])(data[resourceName]), [newItem]);
|
|
47
|
-
return new Promise(function (resolve) {
|
|
48
|
-
resolve(newItem);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function update(resourceName, item) {
|
|
53
|
-
createResourceArrayIfNecessary(resourceName);
|
|
54
|
-
|
|
55
|
-
var updatedItem = _objectSpread({}, item);
|
|
56
|
-
|
|
57
|
-
data[resourceName] = [].concat((0, _toConsumableArray2["default"])(data[resourceName].filter(function (r) {
|
|
58
|
-
return r.id !== item.id;
|
|
59
|
-
})), [updatedItem]);
|
|
60
|
-
return new Promise(function (resolve) {
|
|
61
|
-
resolve(updatedItem);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function deleteById(resourceName, id) {
|
|
66
|
-
createResourceArrayIfNecessary(resourceName);
|
|
67
|
-
data[resourceName] = data[resourceName].filter(function (item) {
|
|
68
|
-
return item.id !== id;
|
|
69
|
-
});
|
|
70
|
-
return new Promise(function (resolve) {
|
|
71
|
-
resolve();
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function deleteAll(resourceName) {
|
|
76
|
-
createResourceArrayIfNecessary(resourceName);
|
|
77
|
-
data[resourceName] = [];
|
|
78
|
-
return new Promise(function (resolve) {
|
|
79
|
-
resolve([]);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function createResourceArrayIfNecessary(resourceName) {
|
|
84
|
-
if (!data.hasOwnProperty(resourceName)) data[resourceName] = [];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
var _default = {
|
|
88
|
-
connectToDatabase: connectToDatabase,
|
|
89
|
-
getAll: getAll,
|
|
90
|
-
getById: getById,
|
|
91
|
-
create: create,
|
|
92
|
-
update: update,
|
|
93
|
-
deleteById: deleteById,
|
|
94
|
-
deleteAll: deleteAll
|
|
95
|
-
};
|
|
96
|
-
exports["default"] = _default;
|
package/dist/queries/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.createQueries = createQueries;
|
|
9
|
-
|
|
10
|
-
var _inMemory = _interopRequireDefault(require("./in-memory"));
|
|
11
|
-
|
|
12
|
-
var _mongo = _interopRequireDefault(require("./mongo"));
|
|
13
|
-
|
|
14
|
-
function createQueries(connectionString) {
|
|
15
|
-
if (!connectionString) {
|
|
16
|
-
return _inMemory["default"];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
var mongoQueries = (0, _mongo["default"])(connectionString);
|
|
20
|
-
return mongoQueries;
|
|
21
|
-
}
|