@naturalcycles/db-lib 8.34.2 → 8.35.0
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/index.d.ts +1 -2
- package/dist/index.js +2 -4
- package/dist/pipeline/dbPipelineBackup.js +1 -2
- package/dist/pipeline/dbPipelineCopy.js +1 -2
- package/dist/pipeline/dbPipelineRestore.js +1 -2
- package/package.json +1 -2
- package/src/index.ts +0 -2
- package/src/pipeline/dbPipelineBackup.ts +9 -3
- package/src/pipeline/dbPipelineCopy.ts +2 -3
- package/src/pipeline/dbPipelineRestore.ts +2 -2
- package/dist/getDB.d.ts +0 -19
- package/dist/getDB.js +0 -32
- package/src/getDB.ts +0 -50
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,6 @@ import { CommonDB } from './common.db';
|
|
|
7
7
|
import { CommonDao } from './commondao/common.dao';
|
|
8
8
|
import { CommonDaoAnonymizeHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeBMToTMHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeTMToBMHook, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoCreateOptions, CommonDaoLogLevel, CommonDaoOptions, CommonDaoParseNaturalIdHook, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions } from './commondao/common.dao.model';
|
|
9
9
|
import { CommonDBAdapter, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, DBDeleteByIdsOperation, DBModelType, DBOperation, DBRelation, DBSaveBatchOperation, RunQueryResult } from './db.model';
|
|
10
|
-
import { getDB } from './getDB';
|
|
11
10
|
import { CommonKeyValueDao, CommonKeyValueDaoCfg } from './kv/commonKeyValueDao';
|
|
12
11
|
import { CommonKeyValueDB, KeyValueDBTuple } from './kv/commonKeyValueDB';
|
|
13
12
|
import { createdUpdatedFields, createdUpdatedIdFields, deserializeJsonField, serializeJsonField } from './model.util';
|
|
@@ -19,4 +18,4 @@ import { DBTransaction, RunnableDBTransaction } from './transaction/dbTransactio
|
|
|
19
18
|
import { commitDBTransactionSimple, mergeDBOperations } from './transaction/dbTransaction.util';
|
|
20
19
|
export * from './kv/commonKeyValueDaoMemoCache';
|
|
21
20
|
export type { DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBCreateOptions, CommonDB, RunQueryResult, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoParseNaturalIdHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeTMToBMHook, CommonDaoBeforeBMToTMHook, CommonDaoAnonymizeHook, InMemoryDBCfg, InMemoryKeyValueDBCfg, DBPipelineBackupOptions, DBPipelineRestoreOptions, DBPipelineCopyOptions, CommonDBAdapter, DBOperation, DBSaveBatchOperation, DBDeleteByIdsOperation, CommonKeyValueDB, CommonKeyValueDaoCfg, KeyValueDBTuple, };
|
|
22
|
-
export { DBQuery, dbQueryFilterOperatorValues, RunnableDBQuery, CommonDaoLogLevel, DBRelation, DBModelType, CommonDao, createdUpdatedFields, createdUpdatedIdFields, InMemoryDB, InMemoryKeyValueDB, queryInMemory, serializeJsonField, deserializeJsonField, dbPipelineBackup, dbPipelineRestore, dbPipelineCopy,
|
|
21
|
+
export { DBQuery, dbQueryFilterOperatorValues, RunnableDBQuery, CommonDaoLogLevel, DBRelation, DBModelType, CommonDao, createdUpdatedFields, createdUpdatedIdFields, InMemoryDB, InMemoryKeyValueDB, queryInMemory, serializeJsonField, deserializeJsonField, dbPipelineBackup, dbPipelineRestore, dbPipelineCopy, DBLibError, BaseCommonDB, DBTransaction, RunnableDBTransaction, mergeDBOperations, commitDBTransactionSimple, CommonKeyValueDao, };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CommonKeyValueDao = exports.commitDBTransactionSimple = exports.mergeDBOperations = exports.RunnableDBTransaction = exports.DBTransaction = exports.BaseCommonDB = exports.DBLibError = exports.
|
|
3
|
+
exports.CommonKeyValueDao = exports.commitDBTransactionSimple = exports.mergeDBOperations = exports.RunnableDBTransaction = exports.DBTransaction = exports.BaseCommonDB = exports.DBLibError = exports.dbPipelineCopy = exports.dbPipelineRestore = exports.dbPipelineBackup = exports.deserializeJsonField = exports.serializeJsonField = exports.queryInMemory = exports.InMemoryKeyValueDB = exports.InMemoryDB = exports.createdUpdatedIdFields = exports.createdUpdatedFields = exports.CommonDao = exports.DBModelType = exports.DBRelation = exports.CommonDaoLogLevel = exports.RunnableDBQuery = exports.dbQueryFilterOperatorValues = exports.DBQuery = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const inMemory_db_1 = require("./adapter/inmemory/inMemory.db");
|
|
6
6
|
Object.defineProperty(exports, "InMemoryDB", { enumerable: true, get: function () { return inMemory_db_1.InMemoryDB; } });
|
|
@@ -19,8 +19,6 @@ Object.defineProperty(exports, "CommonDaoLogLevel", { enumerable: true, get: fun
|
|
|
19
19
|
const db_model_1 = require("./db.model");
|
|
20
20
|
Object.defineProperty(exports, "DBModelType", { enumerable: true, get: function () { return db_model_1.DBModelType; } });
|
|
21
21
|
Object.defineProperty(exports, "DBRelation", { enumerable: true, get: function () { return db_model_1.DBRelation; } });
|
|
22
|
-
const getDB_1 = require("./getDB");
|
|
23
|
-
Object.defineProperty(exports, "getDB", { enumerable: true, get: function () { return getDB_1.getDB; } });
|
|
24
22
|
const commonKeyValueDao_1 = require("./kv/commonKeyValueDao");
|
|
25
23
|
Object.defineProperty(exports, "CommonKeyValueDao", { enumerable: true, get: function () { return commonKeyValueDao_1.CommonKeyValueDao; } });
|
|
26
24
|
const model_util_1 = require("./model.util");
|
|
@@ -44,4 +42,4 @@ Object.defineProperty(exports, "RunnableDBTransaction", { enumerable: true, get:
|
|
|
44
42
|
const dbTransaction_util_1 = require("./transaction/dbTransaction.util");
|
|
45
43
|
Object.defineProperty(exports, "commitDBTransactionSimple", { enumerable: true, get: function () { return dbTransaction_util_1.commitDBTransactionSimple; } });
|
|
46
44
|
Object.defineProperty(exports, "mergeDBOperations", { enumerable: true, get: function () { return dbTransaction_util_1.mergeDBOperations; } });
|
|
47
|
-
|
|
45
|
+
tslib_1.__exportStar(require("./kv/commonKeyValueDaoMemoCache"), exports);
|
|
@@ -5,7 +5,6 @@ const zlib_1 = require("zlib");
|
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
7
|
const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
8
|
-
const time_lib_1 = require("@naturalcycles/time-lib");
|
|
9
8
|
const fs = require("fs-extra");
|
|
10
9
|
const index_1 = require("../index");
|
|
11
10
|
/**
|
|
@@ -22,7 +21,7 @@ async function dbPipelineBackup(opt) {
|
|
|
22
21
|
const strict = errorMode !== js_lib_1.ErrorMode.SUPPRESS;
|
|
23
22
|
const gzip = opt.gzip !== false; // default to true
|
|
24
23
|
let { tables } = opt;
|
|
25
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)(
|
|
24
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
|
|
26
25
|
console.log(`>> ${(0, colors_1.dimWhite)('dbPipelineBackup')} started in ${(0, colors_1.grey)(outputDirPath)}...${sinceUpdatedStr}`);
|
|
27
26
|
fs.ensureDirSync(outputDirPath);
|
|
28
27
|
if (!tables) {
|
|
@@ -4,7 +4,6 @@ exports.dbPipelineCopy = void 0;
|
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
5
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
6
|
const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
7
|
-
const time_lib_1 = require("@naturalcycles/time-lib");
|
|
8
7
|
const dbQuery_1 = require("../query/dbQuery");
|
|
9
8
|
/**
|
|
10
9
|
* Pipeline from input stream(s) to CommonDB .saveBatch().
|
|
@@ -15,7 +14,7 @@ const dbQuery_1 = require("../query/dbQuery");
|
|
|
15
14
|
async function dbPipelineCopy(opt) {
|
|
16
15
|
const { batchSize = 100, dbInput, dbOutput, concurrency = 16, limit = 0, sinceUpdated, mapperPerTable = {}, saveOptionsPerTable = {}, transformMapOptions, errorMode = js_lib_1.ErrorMode.SUPPRESS, } = opt;
|
|
17
16
|
let { tables } = opt;
|
|
18
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)(
|
|
17
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
|
|
19
18
|
console.log(`>> ${(0, colors_1.dimWhite)('dbPipelineCopy')} started...${sinceUpdatedStr}`);
|
|
20
19
|
if (!tables) {
|
|
21
20
|
tables = await dbInput.getTables();
|
|
@@ -5,7 +5,6 @@ const zlib_1 = require("zlib");
|
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
7
|
const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
8
|
-
const time_lib_1 = require("@naturalcycles/time-lib");
|
|
9
8
|
const fs = require("fs-extra");
|
|
10
9
|
/**
|
|
11
10
|
* Pipeline from NDJSON files in a folder (optionally gzipped) to CommonDB.
|
|
@@ -18,7 +17,7 @@ async function dbPipelineRestore(opt) {
|
|
|
18
17
|
const { db, concurrency = 16, batchSize = 100, limit, sinceUpdated, inputDirPath, mapperPerTable = {}, saveOptionsPerTable = {}, transformMapOptions, errorMode = js_lib_1.ErrorMode.SUPPRESS, recreateTables = false, } = opt;
|
|
19
18
|
const strict = errorMode !== js_lib_1.ErrorMode.SUPPRESS;
|
|
20
19
|
const onlyTables = opt.tables && new Set(opt.tables);
|
|
21
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)(
|
|
20
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
|
|
22
21
|
console.log(`>> ${(0, colors_1.dimWhite)('dbPipelineRestore')} started in ${(0, colors_1.grey)(inputDirPath)}...${sinceUpdatedStr}`);
|
|
23
22
|
fs.ensureDirSync(inputDirPath);
|
|
24
23
|
const tablesToGzip = new Set();
|
package/package.json
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@naturalcycles/js-lib": "^14.0.0",
|
|
8
8
|
"@naturalcycles/nodejs-lib": "^12.0.0",
|
|
9
|
-
"@naturalcycles/time-lib": "^3.0.1",
|
|
10
9
|
"fs-extra": "^10.0.0"
|
|
11
10
|
},
|
|
12
11
|
"devDependencies": {
|
|
@@ -43,7 +42,7 @@
|
|
|
43
42
|
"engines": {
|
|
44
43
|
"node": ">=14.15"
|
|
45
44
|
},
|
|
46
|
-
"version": "8.
|
|
45
|
+
"version": "8.35.0",
|
|
47
46
|
"description": "Lowest Common Denominator API to supported Databases",
|
|
48
47
|
"keywords": [
|
|
49
48
|
"db",
|
package/src/index.ts
CHANGED
|
@@ -36,7 +36,6 @@ import {
|
|
|
36
36
|
DBSaveBatchOperation,
|
|
37
37
|
RunQueryResult,
|
|
38
38
|
} from './db.model'
|
|
39
|
-
import { getDB } from './getDB'
|
|
40
39
|
import { CommonKeyValueDao, CommonKeyValueDaoCfg } from './kv/commonKeyValueDao'
|
|
41
40
|
import { CommonKeyValueDB, KeyValueDBTuple } from './kv/commonKeyValueDB'
|
|
42
41
|
import {
|
|
@@ -117,7 +116,6 @@ export {
|
|
|
117
116
|
dbPipelineBackup,
|
|
118
117
|
dbPipelineRestore,
|
|
119
118
|
dbPipelineCopy,
|
|
120
|
-
getDB,
|
|
121
119
|
DBLibError,
|
|
122
120
|
BaseCommonDB,
|
|
123
121
|
DBTransaction,
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { createGzip, ZlibOptions } from 'zlib'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
AppError,
|
|
4
|
+
AsyncMapper,
|
|
5
|
+
ErrorMode,
|
|
6
|
+
pMap,
|
|
7
|
+
_passthroughMapper,
|
|
8
|
+
localTime,
|
|
9
|
+
} from '@naturalcycles/js-lib'
|
|
3
10
|
import {
|
|
4
11
|
NDJsonStats,
|
|
5
12
|
transformLogProgress,
|
|
@@ -11,7 +18,6 @@ import {
|
|
|
11
18
|
_pipeline,
|
|
12
19
|
} from '@naturalcycles/nodejs-lib'
|
|
13
20
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
14
|
-
import { dayjs } from '@naturalcycles/time-lib'
|
|
15
21
|
import * as fs from 'fs-extra'
|
|
16
22
|
import { CommonDB } from '../common.db'
|
|
17
23
|
import { DBQuery } from '../index'
|
|
@@ -144,7 +150,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
|
|
|
144
150
|
|
|
145
151
|
let { tables } = opt
|
|
146
152
|
|
|
147
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(
|
|
153
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
|
|
148
154
|
|
|
149
155
|
console.log(
|
|
150
156
|
`>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...${sinceUpdatedStr}`,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsyncMapper, ErrorMode, pMap, _passthroughMapper } from '@naturalcycles/js-lib'
|
|
1
|
+
import { AsyncMapper, ErrorMode, pMap, _passthroughMapper, localTime } from '@naturalcycles/js-lib'
|
|
2
2
|
import {
|
|
3
3
|
NDJsonStats,
|
|
4
4
|
transformBuffer,
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
_pipeline,
|
|
12
12
|
} from '@naturalcycles/nodejs-lib'
|
|
13
13
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
14
|
-
import { dayjs } from '@naturalcycles/time-lib'
|
|
15
14
|
import { CommonDB } from '../common.db'
|
|
16
15
|
import { CommonDBSaveOptions } from '../db.model'
|
|
17
16
|
import { DBQuery } from '../query/dbQuery'
|
|
@@ -103,7 +102,7 @@ export async function dbPipelineCopy(opt: DBPipelineCopyOptions): Promise<NDJson
|
|
|
103
102
|
|
|
104
103
|
let { tables } = opt
|
|
105
104
|
|
|
106
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(
|
|
105
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
|
|
107
106
|
|
|
108
107
|
console.log(`>> ${dimWhite('dbPipelineCopy')} started...${sinceUpdatedStr}`)
|
|
109
108
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
_mapValues,
|
|
8
8
|
_passthroughMapper,
|
|
9
9
|
SavedDBEntity,
|
|
10
|
+
localTime,
|
|
10
11
|
} from '@naturalcycles/js-lib'
|
|
11
12
|
import {
|
|
12
13
|
NDJsonStats,
|
|
@@ -24,7 +25,6 @@ import {
|
|
|
24
25
|
_pipeline,
|
|
25
26
|
} from '@naturalcycles/nodejs-lib'
|
|
26
27
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
27
|
-
import { dayjs } from '@naturalcycles/time-lib'
|
|
28
28
|
import * as fs from 'fs-extra'
|
|
29
29
|
import { CommonDB } from '../common.db'
|
|
30
30
|
import { CommonDBSaveOptions } from '../index'
|
|
@@ -133,7 +133,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
|
|
|
133
133
|
const strict = errorMode !== ErrorMode.SUPPRESS
|
|
134
134
|
const onlyTables = opt.tables && new Set(opt.tables)
|
|
135
135
|
|
|
136
|
-
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(
|
|
136
|
+
const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''
|
|
137
137
|
|
|
138
138
|
console.log(
|
|
139
139
|
`>> ${dimWhite('dbPipelineRestore')} started in ${grey(inputDirPath)}...${sinceUpdatedStr}`,
|
package/dist/getDB.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { CommonDB } from './common.db';
|
|
2
|
-
/**
|
|
3
|
-
* Returns pre-configured DB
|
|
4
|
-
*
|
|
5
|
-
* @param index defaults to 1
|
|
6
|
-
*
|
|
7
|
-
* Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
|
|
8
|
-
* Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
|
|
9
|
-
* but depends on the adapter.
|
|
10
|
-
*/
|
|
11
|
-
export declare function getDB(index?: number): CommonDB;
|
|
12
|
-
declare global {
|
|
13
|
-
namespace NodeJS {
|
|
14
|
-
interface ProcessEnv {
|
|
15
|
-
DB1?: string;
|
|
16
|
-
DB2?: string;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
package/dist/getDB.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getDB = void 0;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
|
-
const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
|
|
7
|
-
/**
|
|
8
|
-
* Returns pre-configured DB
|
|
9
|
-
*
|
|
10
|
-
* @param index defaults to 1
|
|
11
|
-
*
|
|
12
|
-
* Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
|
|
13
|
-
* Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
|
|
14
|
-
* but depends on the adapter.
|
|
15
|
-
*/
|
|
16
|
-
function getDB(index = 1) {
|
|
17
|
-
return _getDB(index);
|
|
18
|
-
}
|
|
19
|
-
exports.getDB = getDB;
|
|
20
|
-
// Extra function to provide index=1 as default (since memo doesn't work well with default arguments)
|
|
21
|
-
const _getDB = (0, js_lib_1._memoFn)((index) => {
|
|
22
|
-
const libName = process.env[`DB${index}`];
|
|
23
|
-
if (!libName) {
|
|
24
|
-
throw new Error(`getDB(${(0, colors_1.yellow)(index)}), but process.env.${(0, colors_1.white)('DB' + index)} is not defined!`);
|
|
25
|
-
}
|
|
26
|
-
const lib = require(libName);
|
|
27
|
-
if (!lib.getDBAdapter) {
|
|
28
|
-
throw new Error(`DB${index}=${libName}, but require('${libName}').getDBAdapter() is not defined`);
|
|
29
|
-
}
|
|
30
|
-
const cfg = (0, nodejs_lib_1.secretOptional)(`SECRET_DB${index}`);
|
|
31
|
-
return lib.getDBAdapter(cfg);
|
|
32
|
-
});
|
package/src/getDB.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { _memoFn } from '@naturalcycles/js-lib'
|
|
2
|
-
import { secretOptional } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import { white, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
|
|
4
|
-
import { CommonDB } from './common.db'
|
|
5
|
-
import { CommonDBAdapter } from './db.model'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Returns pre-configured DB
|
|
9
|
-
*
|
|
10
|
-
* @param index defaults to 1
|
|
11
|
-
*
|
|
12
|
-
* Requires process.env.DB${index} to be set to the name of the adapter, e.g `mysql-lib` or `db-lib/adapter/inmemory`.
|
|
13
|
-
* Requires (by most adapters) process.env.SECRET_DB${index} to contain a "connection string" to that DB. Usually a JSON.stringified object,
|
|
14
|
-
* but depends on the adapter.
|
|
15
|
-
*/
|
|
16
|
-
export function getDB(index = 1): CommonDB {
|
|
17
|
-
return _getDB(index)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Extra function to provide index=1 as default (since memo doesn't work well with default arguments)
|
|
21
|
-
const _getDB = _memoFn((index: number) => {
|
|
22
|
-
const libName = process.env[`DB${index}`]
|
|
23
|
-
|
|
24
|
-
if (!libName) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
`getDB(${yellow(index)}), but process.env.${white('DB' + index)} is not defined!`,
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const lib: CommonDBAdapter = require(libName)
|
|
31
|
-
|
|
32
|
-
if (!lib.getDBAdapter) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`DB${index}=${libName}, but require('${libName}').getDBAdapter() is not defined`,
|
|
35
|
-
)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const cfg = secretOptional(`SECRET_DB${index}`)
|
|
39
|
-
|
|
40
|
-
return lib.getDBAdapter(cfg)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
declare global {
|
|
44
|
-
namespace NodeJS {
|
|
45
|
-
interface ProcessEnv {
|
|
46
|
-
DB1?: string
|
|
47
|
-
DB2?: string
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|