@naturalcycles/backend-lib 4.18.7 → 4.18.8
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/dist/db/httpDB.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
1
|
+
import { FetcherOptions, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
2
2
|
import { BaseCommonDB, CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
|
|
3
|
-
import {
|
|
4
|
-
export interface HttpDBCfg extends
|
|
5
|
-
|
|
3
|
+
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
|
+
export interface HttpDBCfg extends FetcherOptions {
|
|
5
|
+
baseUrl: string;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Implementation of CommonDB that proxies all requests via HTTP to "httpDBRequestHandler".
|
|
@@ -11,7 +11,7 @@ export declare class HttpDB extends BaseCommonDB implements CommonDB {
|
|
|
11
11
|
cfg: HttpDBCfg;
|
|
12
12
|
constructor(cfg: HttpDBCfg);
|
|
13
13
|
setCfg(cfg: HttpDBCfg): void;
|
|
14
|
-
private
|
|
14
|
+
private fetcher;
|
|
15
15
|
ping(): Promise<void>;
|
|
16
16
|
getTables(): Promise<string[]>;
|
|
17
17
|
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
package/dist/db/httpDB.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HttpDB = void 0;
|
|
4
4
|
const node_stream_1 = require("node:stream");
|
|
5
|
+
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
6
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
6
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
7
|
/**
|
|
8
8
|
* Implementation of CommonDB that proxies all requests via HTTP to "httpDBRequestHandler".
|
|
9
9
|
*/
|
|
@@ -14,53 +14,47 @@ class HttpDB extends db_lib_1.BaseCommonDB {
|
|
|
14
14
|
this.setCfg(cfg);
|
|
15
15
|
}
|
|
16
16
|
setCfg(cfg) {
|
|
17
|
-
this.
|
|
17
|
+
this.fetcher = (0, js_lib_1.getFetcher)(cfg);
|
|
18
18
|
}
|
|
19
19
|
async ping() {
|
|
20
|
-
await this.
|
|
20
|
+
await this.fetcher.getVoid(`ping`);
|
|
21
21
|
}
|
|
22
22
|
async getTables() {
|
|
23
|
-
return await this.
|
|
23
|
+
return await this.fetcher.get(`tables`);
|
|
24
24
|
}
|
|
25
25
|
async getTableSchema(table) {
|
|
26
|
-
return await this.
|
|
26
|
+
return await this.fetcher.get(`${table}/schema`);
|
|
27
27
|
}
|
|
28
28
|
async resetCache(table = '') {
|
|
29
|
-
await this.
|
|
29
|
+
await this.fetcher.putVoid(`resetCache/${table}`);
|
|
30
30
|
}
|
|
31
31
|
async getByIds(table, ids, opt) {
|
|
32
|
-
return await this.
|
|
33
|
-
.put(`getByIds`, {
|
|
32
|
+
return await this.fetcher.put(`getByIds`, {
|
|
34
33
|
json: {
|
|
35
34
|
table,
|
|
36
35
|
ids,
|
|
37
36
|
opt,
|
|
38
37
|
},
|
|
39
|
-
})
|
|
40
|
-
.json();
|
|
38
|
+
});
|
|
41
39
|
}
|
|
42
40
|
async runQuery(query, opt) {
|
|
43
|
-
return await this.
|
|
44
|
-
.put(`runQuery`, {
|
|
41
|
+
return await this.fetcher.put(`runQuery`, {
|
|
45
42
|
json: {
|
|
46
43
|
query,
|
|
47
44
|
opt,
|
|
48
45
|
},
|
|
49
|
-
})
|
|
50
|
-
.json();
|
|
46
|
+
});
|
|
51
47
|
}
|
|
52
48
|
async runQueryCount(query, opt) {
|
|
53
|
-
return await this.
|
|
54
|
-
.put(`runQueryCount`, {
|
|
49
|
+
return await this.fetcher.put(`runQueryCount`, {
|
|
55
50
|
json: {
|
|
56
51
|
query,
|
|
57
52
|
opt,
|
|
58
53
|
},
|
|
59
|
-
})
|
|
60
|
-
.json();
|
|
54
|
+
});
|
|
61
55
|
}
|
|
62
56
|
async saveBatch(table, rows, opt) {
|
|
63
|
-
await this.
|
|
57
|
+
await this.fetcher.putVoid(`saveBatch`, {
|
|
64
58
|
json: {
|
|
65
59
|
table,
|
|
66
60
|
rows,
|
|
@@ -69,25 +63,21 @@ class HttpDB extends db_lib_1.BaseCommonDB {
|
|
|
69
63
|
});
|
|
70
64
|
}
|
|
71
65
|
async deleteByIds(table, ids, opt) {
|
|
72
|
-
return await this.
|
|
73
|
-
.put(`deleteByIds`, {
|
|
66
|
+
return await this.fetcher.put(`deleteByIds`, {
|
|
74
67
|
json: {
|
|
75
68
|
table,
|
|
76
69
|
ids,
|
|
77
70
|
opt,
|
|
78
71
|
},
|
|
79
|
-
})
|
|
80
|
-
.json();
|
|
72
|
+
});
|
|
81
73
|
}
|
|
82
74
|
async deleteByQuery(query, opt) {
|
|
83
|
-
return await this.
|
|
84
|
-
.put(`deleteByQuery`, {
|
|
75
|
+
return await this.fetcher.put(`deleteByQuery`, {
|
|
85
76
|
json: {
|
|
86
77
|
query,
|
|
87
78
|
opt,
|
|
88
79
|
},
|
|
89
|
-
})
|
|
90
|
-
.json();
|
|
80
|
+
});
|
|
91
81
|
}
|
|
92
82
|
streamQuery(_q, _opt) {
|
|
93
83
|
console.warn(`streamQuery not implemented`);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Fetcher, FetcherOptions } from '@naturalcycles/js-lib';
|
|
2
2
|
import { BackendApplication, DefaultAppCfg } from '../index';
|
|
3
3
|
import { BackendRequestHandlerCfg } from '../server/createDefaultApp.model';
|
|
4
|
-
export interface ExpressApp extends
|
|
4
|
+
export interface ExpressApp extends Fetcher {
|
|
5
5
|
close: () => Promise<void>;
|
|
6
6
|
}
|
|
7
7
|
declare class ExpressTestService {
|
|
8
|
-
createAppFromResource(resource: BackendRequestHandlerCfg, opt?:
|
|
9
|
-
createAppFromResources(resources: BackendRequestHandlerCfg[], opt?:
|
|
10
|
-
createApp(app: BackendApplication, opt?:
|
|
8
|
+
createAppFromResource(resource: BackendRequestHandlerCfg, opt?: FetcherOptions, defaultAppCfg?: DefaultAppCfg): ExpressApp;
|
|
9
|
+
createAppFromResources(resources: BackendRequestHandlerCfg[], opt?: FetcherOptions): ExpressApp;
|
|
10
|
+
createApp(app: BackendApplication, opt?: FetcherOptions): ExpressApp;
|
|
11
11
|
/**
|
|
12
12
|
* Creates a "Default Express App" with provided resources.
|
|
13
13
|
* Starts an http server on '127.0.0.1' and random available port.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.expressTestService = void 0;
|
|
4
|
-
const
|
|
4
|
+
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
5
|
const index_1 = require("../index");
|
|
6
|
+
const nativeFetchFn = async (url, init) => await globalThis.fetch(url, init);
|
|
6
7
|
// Example:
|
|
7
8
|
// const app = expressTestService.createApp([ debugResource ])
|
|
8
9
|
// afterAll(async () => {
|
|
@@ -23,21 +24,31 @@ class ExpressTestService {
|
|
|
23
24
|
createApp(app, opt) {
|
|
24
25
|
const server = this.createTestServer(app);
|
|
25
26
|
const { port } = server.address();
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
27
|
+
const baseUrl = `http://127.0.0.1:${port}`;
|
|
28
|
+
const fetcher = (0, js_lib_1.getFetcher)({
|
|
29
|
+
baseUrl,
|
|
29
30
|
responseType: 'json',
|
|
30
|
-
retry: 0,
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
retry: { count: 0 },
|
|
32
|
+
logRequest: true,
|
|
33
|
+
logResponse: true,
|
|
34
|
+
logWithBaseUrl: false,
|
|
35
|
+
fetchFn: nativeFetchFn,
|
|
33
36
|
...opt,
|
|
37
|
+
}).onAfterResponse(async () => {
|
|
38
|
+
// This "empty" hook exists to act like `await pDelay`,
|
|
39
|
+
// so tests using it can reply on `void someAnalyticsService.doSmth()` promises
|
|
40
|
+
// to be done by that time.
|
|
41
|
+
// Smart, huh?
|
|
42
|
+
await (0, js_lib_1.pDelay)();
|
|
34
43
|
});
|
|
35
|
-
|
|
44
|
+
fetcher.close = async () => {
|
|
45
|
+
const started = Date.now();
|
|
36
46
|
await new Promise(resolve => server.close(resolve));
|
|
47
|
+
console.log(`close took ${(0, js_lib_1._since)(started)}`); // todo: investigate why it takes ~5 seconds!
|
|
37
48
|
// server.destroy()
|
|
38
49
|
// await pDelay(1000)
|
|
39
50
|
};
|
|
40
|
-
return
|
|
51
|
+
return fetcher;
|
|
41
52
|
}
|
|
42
53
|
/**
|
|
43
54
|
* Creates a "Default Express App" with provided resources.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/backend-lib",
|
|
3
|
-
"version": "4.18.
|
|
3
|
+
"version": "4.18.8",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"serve": "APP_ENV=dev nodemon",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@naturalcycles/db-lib": "^8.0.2",
|
|
20
20
|
"@naturalcycles/js-lib": "^14.27.0",
|
|
21
|
-
"@naturalcycles/nodejs-lib": "^
|
|
21
|
+
"@naturalcycles/nodejs-lib": "^13.1.0",
|
|
22
22
|
"@types/cookie-parser": "^1.4.1",
|
|
23
23
|
"@types/cors": "^2.8.4",
|
|
24
24
|
"@types/express": "^4.16.1",
|
package/src/db/httpDB.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { Readable } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Fetcher,
|
|
4
|
+
FetcherOptions,
|
|
5
|
+
getFetcher,
|
|
6
|
+
JsonSchemaRootObject,
|
|
7
|
+
ObjectWithId,
|
|
8
|
+
} from '@naturalcycles/js-lib'
|
|
3
9
|
import {
|
|
4
10
|
BaseCommonDB,
|
|
5
11
|
CommonDB,
|
|
@@ -9,10 +15,10 @@ import {
|
|
|
9
15
|
DBQuery,
|
|
10
16
|
RunQueryResult,
|
|
11
17
|
} from '@naturalcycles/db-lib'
|
|
12
|
-
import {
|
|
18
|
+
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
13
19
|
|
|
14
|
-
export interface HttpDBCfg extends
|
|
15
|
-
|
|
20
|
+
export interface HttpDBCfg extends FetcherOptions {
|
|
21
|
+
baseUrl: string
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
/**
|
|
@@ -25,27 +31,27 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
|
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
setCfg(cfg: HttpDBCfg): void {
|
|
28
|
-
this.
|
|
34
|
+
this.fetcher = getFetcher(cfg)
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
private
|
|
37
|
+
private fetcher!: Fetcher
|
|
32
38
|
|
|
33
39
|
override async ping(): Promise<void> {
|
|
34
|
-
await this.
|
|
40
|
+
await this.fetcher.getVoid(`ping`)
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
override async getTables(): Promise<string[]> {
|
|
38
|
-
return await this.
|
|
44
|
+
return await this.fetcher.get(`tables`)
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
override async getTableSchema<ROW extends ObjectWithId>(
|
|
42
48
|
table: string,
|
|
43
49
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
44
|
-
return await this.
|
|
50
|
+
return await this.fetcher.get(`${table}/schema`)
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
async resetCache(table = ''): Promise<void> {
|
|
48
|
-
await this.
|
|
54
|
+
await this.fetcher.putVoid(`resetCache/${table}`)
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
override async getByIds<ROW extends ObjectWithId>(
|
|
@@ -53,43 +59,37 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
|
|
|
53
59
|
ids: ROW['id'][],
|
|
54
60
|
opt?: CommonDBOptions,
|
|
55
61
|
): Promise<ROW[]> {
|
|
56
|
-
return await this.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
64
|
-
.json()
|
|
62
|
+
return await this.fetcher.put(`getByIds`, {
|
|
63
|
+
json: {
|
|
64
|
+
table,
|
|
65
|
+
ids,
|
|
66
|
+
opt,
|
|
67
|
+
},
|
|
68
|
+
})
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
override async runQuery<ROW extends ObjectWithId>(
|
|
68
72
|
query: DBQuery<ROW>,
|
|
69
73
|
opt?: CommonDBOptions,
|
|
70
74
|
): Promise<RunQueryResult<ROW>> {
|
|
71
|
-
return await this.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
})
|
|
78
|
-
.json()
|
|
75
|
+
return await this.fetcher.put(`runQuery`, {
|
|
76
|
+
json: {
|
|
77
|
+
query,
|
|
78
|
+
opt,
|
|
79
|
+
},
|
|
80
|
+
})
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
override async runQueryCount<ROW extends ObjectWithId>(
|
|
82
84
|
query: DBQuery<ROW>,
|
|
83
85
|
opt?: CommonDBOptions,
|
|
84
86
|
): Promise<number> {
|
|
85
|
-
return await this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
})
|
|
92
|
-
.json()
|
|
87
|
+
return await this.fetcher.put(`runQueryCount`, {
|
|
88
|
+
json: {
|
|
89
|
+
query,
|
|
90
|
+
opt,
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
@@ -97,7 +97,7 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
|
|
|
97
97
|
rows: ROW[],
|
|
98
98
|
opt?: CommonDBSaveOptions<ROW>,
|
|
99
99
|
): Promise<void> {
|
|
100
|
-
await this.
|
|
100
|
+
await this.fetcher.putVoid(`saveBatch`, {
|
|
101
101
|
json: {
|
|
102
102
|
table,
|
|
103
103
|
rows,
|
|
@@ -107,29 +107,25 @@ export class HttpDB extends BaseCommonDB implements CommonDB {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
async deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number> {
|
|
110
|
-
return await this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
})
|
|
118
|
-
.json()
|
|
110
|
+
return await this.fetcher.put(`deleteByIds`, {
|
|
111
|
+
json: {
|
|
112
|
+
table,
|
|
113
|
+
ids,
|
|
114
|
+
opt,
|
|
115
|
+
},
|
|
116
|
+
})
|
|
119
117
|
}
|
|
120
118
|
|
|
121
119
|
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
122
120
|
query: DBQuery<ROW>,
|
|
123
121
|
opt?: CommonDBOptions,
|
|
124
122
|
): Promise<number> {
|
|
125
|
-
return await this.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
})
|
|
132
|
-
.json()
|
|
123
|
+
return await this.fetcher.put(`deleteByQuery`, {
|
|
124
|
+
json: {
|
|
125
|
+
query,
|
|
126
|
+
opt,
|
|
127
|
+
},
|
|
128
|
+
})
|
|
133
129
|
}
|
|
134
130
|
|
|
135
131
|
override streamQuery<ROW extends ObjectWithId>(
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { Server } from 'node:http'
|
|
2
2
|
import { AddressInfo } from 'node:net'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
_since,
|
|
5
|
+
Fetcher,
|
|
6
|
+
FetcherOptions,
|
|
7
|
+
FetchFunction,
|
|
8
|
+
getFetcher,
|
|
9
|
+
pDelay,
|
|
10
|
+
} from '@naturalcycles/js-lib'
|
|
4
11
|
import { BackendApplication, createDefaultApp, DefaultAppCfg } from '../index'
|
|
5
12
|
import { BackendRequestHandlerCfg } from '../server/createDefaultApp.model'
|
|
6
13
|
|
|
7
|
-
|
|
14
|
+
const nativeFetchFn: FetchFunction = async (url, init) => await globalThis.fetch(url, init)
|
|
15
|
+
|
|
16
|
+
export interface ExpressApp extends Fetcher {
|
|
8
17
|
close: () => Promise<void>
|
|
9
18
|
}
|
|
10
19
|
|
|
@@ -17,7 +26,7 @@ export interface ExpressApp extends Got {
|
|
|
17
26
|
class ExpressTestService {
|
|
18
27
|
createAppFromResource(
|
|
19
28
|
resource: BackendRequestHandlerCfg,
|
|
20
|
-
opt?:
|
|
29
|
+
opt?: FetcherOptions,
|
|
21
30
|
defaultAppCfg?: DefaultAppCfg,
|
|
22
31
|
): ExpressApp {
|
|
23
32
|
return this.createApp(
|
|
@@ -29,7 +38,7 @@ class ExpressTestService {
|
|
|
29
38
|
)
|
|
30
39
|
}
|
|
31
40
|
|
|
32
|
-
createAppFromResources(resources: BackendRequestHandlerCfg[], opt?:
|
|
41
|
+
createAppFromResources(resources: BackendRequestHandlerCfg[], opt?: FetcherOptions): ExpressApp {
|
|
33
42
|
return this.createApp(
|
|
34
43
|
createDefaultApp({
|
|
35
44
|
resources,
|
|
@@ -38,27 +47,37 @@ class ExpressTestService {
|
|
|
38
47
|
)
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
createApp(app: BackendApplication, opt?:
|
|
50
|
+
createApp(app: BackendApplication, opt?: FetcherOptions): ExpressApp {
|
|
42
51
|
const server = this.createTestServer(app)
|
|
43
52
|
const { port } = server.address() as AddressInfo
|
|
44
|
-
const
|
|
53
|
+
const baseUrl = `http://127.0.0.1:${port}`
|
|
45
54
|
|
|
46
|
-
const
|
|
47
|
-
|
|
55
|
+
const fetcher = getFetcher({
|
|
56
|
+
baseUrl,
|
|
48
57
|
responseType: 'json',
|
|
49
|
-
retry: 0,
|
|
50
|
-
|
|
51
|
-
|
|
58
|
+
retry: { count: 0 },
|
|
59
|
+
logRequest: true,
|
|
60
|
+
logResponse: true,
|
|
61
|
+
logWithBaseUrl: false, // for stable error snapshots
|
|
62
|
+
fetchFn: nativeFetchFn, // this is to make it NOT mockable
|
|
52
63
|
...opt,
|
|
64
|
+
}).onAfterResponse(async () => {
|
|
65
|
+
// This "empty" hook exists to act like `await pDelay`,
|
|
66
|
+
// so tests using it can reply on `void someAnalyticsService.doSmth()` promises
|
|
67
|
+
// to be done by that time.
|
|
68
|
+
// Smart, huh?
|
|
69
|
+
await pDelay()
|
|
53
70
|
}) as ExpressApp
|
|
54
71
|
|
|
55
|
-
|
|
72
|
+
fetcher.close = async () => {
|
|
73
|
+
const started = Date.now()
|
|
56
74
|
await new Promise(resolve => server.close(resolve))
|
|
75
|
+
console.log(`close took ${_since(started)}`) // todo: investigate why it takes ~5 seconds!
|
|
57
76
|
// server.destroy()
|
|
58
77
|
// await pDelay(1000)
|
|
59
78
|
}
|
|
60
79
|
|
|
61
|
-
return
|
|
80
|
+
return fetcher
|
|
62
81
|
}
|
|
63
82
|
|
|
64
83
|
/**
|