@naturalcycles/db-lib 8.24.0 → 8.24.4

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.
@@ -33,9 +33,7 @@ export interface CacheDBCfg {
33
33
  */
34
34
  logDownstream?: boolean;
35
35
  /**
36
- * Pass noopLogger (or undefined) to skip logging completely.
37
- *
38
- * @default console
36
+ * Defaults to `console`.
39
37
  */
40
38
  logger?: CommonLogger;
41
39
  }
@@ -19,7 +19,7 @@ export interface FileDBCfg {
19
19
  */
20
20
  sortObjects?: boolean;
21
21
  /**
22
- * @default console
22
+ * Defaults to `console`.
23
23
  */
24
24
  logger?: CommonLogger;
25
25
  /**
@@ -31,7 +31,7 @@ export interface InMemoryDBCfg {
31
31
  */
32
32
  persistZip: boolean;
33
33
  /**
34
- * @default console
34
+ * Defaults to `console`.
35
35
  */
36
36
  logger?: CommonLogger;
37
37
  }
@@ -1,4 +1,4 @@
1
- import { AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, Saved, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, ObjectSchemaTyped, ReadableTyped } from '@naturalcycles/nodejs-lib';
3
3
  import { DBModelType, RunQueryResult } from '../db.model';
4
4
  import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
@@ -137,7 +137,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId>, DBM extends Obj
137
137
  *
138
138
  * Does NOT mutate the object.
139
139
  */
140
- validateAndConvert<IN, OUT = IN>(obj: IN, schema?: ObjectSchemaTyped<IN> | AjvSchema<IN>, modelType?: DBModelType, opt?: CommonDaoOptions): OUT;
140
+ validateAndConvert<IN, OUT = IN>(obj: Partial<IN>, schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined, modelType: DBModelType, opt?: CommonDaoOptions): OUT;
141
141
  getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
142
142
  createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
143
143
  /**
@@ -8,6 +8,8 @@ const db_model_1 = require("../db.model");
8
8
  const dbQuery_1 = require("../query/dbQuery");
9
9
  const common_dao_model_1 = require("./common.dao.model");
10
10
  /* eslint-disable no-dupe-class-members */
11
+ const isGAE = !!process.env['GAE_INSTANCE'];
12
+ const isCI = !!process.env['CI'];
11
13
  /**
12
14
  * Lowest common denominator API between supported Databases.
13
15
  *
@@ -19,7 +21,10 @@ class CommonDao {
19
21
  constructor(cfg) {
20
22
  this.cfg = cfg;
21
23
  this.cfg = {
22
- logLevel: common_dao_model_1.CommonDaoLogLevel.OPERATIONS,
24
+ // Default is to NOT log in AppEngine and in CI,
25
+ // otherwise to log Operations
26
+ // e.g in Dev (local machine), Test - it will log operations (useful for debugging)
27
+ logLevel: isGAE || isCI ? common_dao_model_1.CommonDaoLogLevel.NONE : common_dao_model_1.CommonDaoLogLevel.OPERATIONS,
23
28
  createdUpdated: true,
24
29
  logger: console,
25
30
  ...cfg,
@@ -732,8 +737,6 @@ class CommonDao {
732
737
  async ping() {
733
738
  await this.cfg.db.ping();
734
739
  }
735
- // todo: logging
736
- // todo: bmToDBM, etc. How?
737
740
  // transaction(): DBTransaction {
738
741
  // return this.cfg.db.transaction()
739
742
  // }
@@ -745,7 +748,7 @@ class CommonDao {
745
748
  if (Array.isArray(res)) {
746
749
  logRes = `${res.length} row(s)`;
747
750
  if (res.length && this.cfg.logLevel >= common_dao_model_1.CommonDaoLogLevel.DATA_FULL) {
748
- args.push('\n', res.slice(0, 10)); // max 10 items
751
+ args.push('\n', ...res.slice(0, 10)); // max 10 items
749
752
  }
750
753
  }
751
754
  else if (res) {
@@ -757,7 +760,7 @@ class CommonDao {
757
760
  else {
758
761
  logRes = `undefined`;
759
762
  }
760
- this.cfg.logger?.log(...[`<< ${table}.${op}: ${logRes} in ${(0, js_lib_1._since)(started)}`].concat(args));
763
+ this.cfg.logger?.log(`<< ${table}.${op}: ${logRes} in ${(0, js_lib_1._since)(started)}`, ...args);
761
764
  }
762
765
  logSaveResult(started, op, table) {
763
766
  if (!this.cfg.logLevel)
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib';
1
+ import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
@@ -34,11 +34,20 @@ export declare enum CommonDaoLogLevel {
34
34
  * Same as undefined
35
35
  */
36
36
  NONE = 0,
37
+ /**
38
+ * Log operations (e.g "getById returned 1 row"), but not data
39
+ */
37
40
  OPERATIONS = 10,
41
+ /**
42
+ * Log operations and data for single operations (e.g getById), but not batch operations.
43
+ */
38
44
  DATA_SINGLE = 20,
45
+ /**
46
+ * Log EVERYTHING - all data passing in and out (max 10 rows). Very verbose!
47
+ */
39
48
  DATA_FULL = 30
40
49
  }
41
- export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId, TM> {
50
+ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId = Saved<BM>, TM = BM> {
42
51
  db: CommonDB;
43
52
  table: string;
44
53
  /**
@@ -54,9 +63,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
54
63
  */
55
64
  readOnly?: boolean;
56
65
  /**
57
- * Pass undefined (or noopLogger) to disable logging completely.
58
- *
59
- * @default console
66
+ * Defaults to `console`
60
67
  */
61
68
  logger?: CommonLogger;
62
69
  /**
@@ -7,7 +7,16 @@ var CommonDaoLogLevel;
7
7
  * Same as undefined
8
8
  */
9
9
  CommonDaoLogLevel[CommonDaoLogLevel["NONE"] = 0] = "NONE";
10
+ /**
11
+ * Log operations (e.g "getById returned 1 row"), but not data
12
+ */
10
13
  CommonDaoLogLevel[CommonDaoLogLevel["OPERATIONS"] = 10] = "OPERATIONS";
14
+ /**
15
+ * Log operations and data for single operations (e.g getById), but not batch operations.
16
+ */
11
17
  CommonDaoLogLevel[CommonDaoLogLevel["DATA_SINGLE"] = 20] = "DATA_SINGLE";
18
+ /**
19
+ * Log EVERYTHING - all data passing in and out (max 10 rows). Very verbose!
20
+ */
12
21
  CommonDaoLogLevel[CommonDaoLogLevel["DATA_FULL"] = 30] = "DATA_FULL";
13
22
  })(CommonDaoLogLevel = exports.CommonDaoLogLevel || (exports.CommonDaoLogLevel = {}));
@@ -78,7 +78,7 @@ async function dbPipelineRestore(opt) {
78
78
  ...opt,
79
79
  metric: table,
80
80
  }),
81
- (0, nodejs_lib_1.transformLimit)(limit),
81
+ (0, nodejs_lib_1.transformLimit)({ limit }),
82
82
  ...(sinceUpdated
83
83
  ? [(0, nodejs_lib_1.transformFilterSync)(r => r.updated >= sinceUpdated)]
84
84
  : []),
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "engines": {
43
43
  "node": ">=14.15"
44
44
  },
45
- "version": "8.24.0",
45
+ "version": "8.24.4",
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
48
48
  "db",
@@ -40,9 +40,7 @@ export interface CacheDBCfg {
40
40
  logDownstream?: boolean
41
41
 
42
42
  /**
43
- * Pass noopLogger (or undefined) to skip logging completely.
44
- *
45
- * @default console
43
+ * Defaults to `console`.
46
44
  */
47
45
  logger?: CommonLogger
48
46
  }
@@ -24,7 +24,7 @@ export interface FileDBCfg {
24
24
  sortObjects?: boolean
25
25
 
26
26
  /**
27
- * @default console
27
+ * Defaults to `console`.
28
28
  */
29
29
  logger?: CommonLogger
30
30
 
@@ -65,7 +65,7 @@ export interface InMemoryDBCfg {
65
65
  persistZip: boolean
66
66
 
67
67
  /**
68
- * @default console
68
+ * Defaults to `console`.
69
69
  */
70
70
  logger?: CommonLogger
71
71
  }
@@ -1,4 +1,4 @@
1
- import { CommonLogger, ErrorMode, ObjectWithId } from '@naturalcycles/js-lib'
1
+ import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib'
2
2
  import {
3
3
  AjvSchema,
4
4
  AjvValidationError,
@@ -46,12 +46,25 @@ export enum CommonDaoLogLevel {
46
46
  * Same as undefined
47
47
  */
48
48
  NONE = 0,
49
+ /**
50
+ * Log operations (e.g "getById returned 1 row"), but not data
51
+ */
49
52
  OPERATIONS = 10,
53
+ /**
54
+ * Log operations and data for single operations (e.g getById), but not batch operations.
55
+ */
50
56
  DATA_SINGLE = 20,
57
+ /**
58
+ * Log EVERYTHING - all data passing in and out (max 10 rows). Very verbose!
59
+ */
51
60
  DATA_FULL = 30,
52
61
  }
53
62
 
54
- export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends ObjectWithId, TM> {
63
+ export interface CommonDaoCfg<
64
+ BM extends Partial<ObjectWithId>,
65
+ DBM extends ObjectWithId = Saved<BM>,
66
+ TM = BM,
67
+ > {
55
68
  db: CommonDB
56
69
  table: string
57
70
 
@@ -71,9 +84,7 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId>, DBM extends Obje
71
84
  readOnly?: boolean
72
85
 
73
86
  /**
74
- * Pass undefined (or noopLogger) to disable logging completely.
75
- *
76
- * @default console
87
+ * Defaults to `console`
77
88
  */
78
89
  logger?: CommonLogger
79
90
 
@@ -1,20 +1,21 @@
1
1
  import {
2
+ _filterNullishValues,
3
+ _filterUndefinedValues,
4
+ _passthroughPredicate,
5
+ _since,
6
+ _truncate,
7
+ _uniqBy,
2
8
  AppError,
3
9
  AsyncMapper,
4
10
  ErrorMode,
5
11
  JsonSchemaObject,
6
- _passthroughPredicate,
7
- _since,
8
- _truncate,
9
- pMap,
10
12
  JsonSchemaRootObject,
11
- Saved,
12
- _uniqBy,
13
13
  ObjectWithId,
14
- _filterUndefinedValues,
15
- _filterNullishValues,
14
+ pMap,
15
+ Saved,
16
16
  } from '@naturalcycles/js-lib'
17
17
  import {
18
+ _pipeline,
18
19
  AjvSchema,
19
20
  AjvValidationError,
20
21
  getValidationResult,
@@ -22,14 +23,13 @@ import {
22
23
  ObjectSchemaTyped,
23
24
  ReadableTyped,
24
25
  stringId,
26
+ transformBuffer,
25
27
  transformLogProgress,
26
28
  transformMap,
27
29
  transformMapSimple,
28
30
  transformMapSync,
29
31
  transformTap,
30
32
  writableVoid,
31
- _pipeline,
32
- transformBuffer,
33
33
  } from '@naturalcycles/nodejs-lib'
34
34
  import { DBLibError } from '../cnst'
35
35
  import { DBModelType, RunQueryResult } from '../db.model'
@@ -46,6 +46,9 @@ import {
46
46
 
47
47
  /* eslint-disable no-dupe-class-members */
48
48
 
49
+ const isGAE = !!process.env['GAE_INSTANCE']
50
+ const isCI = !!process.env['CI']
51
+
49
52
  /**
50
53
  * Lowest common denominator API between supported Databases.
51
54
  *
@@ -60,7 +63,10 @@ export class CommonDao<
60
63
  > {
61
64
  constructor(public cfg: CommonDaoCfg<BM, DBM, TM>) {
62
65
  this.cfg = {
63
- logLevel: CommonDaoLogLevel.OPERATIONS,
66
+ // Default is to NOT log in AppEngine and in CI,
67
+ // otherwise to log Operations
68
+ // e.g in Dev (local machine), Test - it will log operations (useful for debugging)
69
+ logLevel: isGAE || isCI ? CommonDaoLogLevel.NONE : CommonDaoLogLevel.OPERATIONS,
64
70
  createdUpdated: true,
65
71
  logger: console,
66
72
  ...cfg,
@@ -897,9 +903,9 @@ export class CommonDao<
897
903
  * Does NOT mutate the object.
898
904
  */
899
905
  validateAndConvert<IN, OUT = IN>(
900
- obj: IN,
901
- schema?: ObjectSchemaTyped<IN> | AjvSchema<IN>,
902
- modelType?: DBModelType,
906
+ obj: Partial<IN>,
907
+ schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined,
908
+ modelType: DBModelType,
903
909
  opt: CommonDaoOptions = {},
904
910
  ): OUT {
905
911
  // `raw` option completely bypasses any processing
@@ -922,12 +928,12 @@ export class CommonDao<
922
928
 
923
929
  // Pre-validation hooks
924
930
  if (modelType === DBModelType.DBM) {
925
- obj = this.cfg.hooks!.beforeDBMValidate!(obj as any) as any
931
+ obj = this.cfg.hooks!.beforeDBMValidate!(obj as any) as IN
926
932
  }
927
933
 
928
934
  // Return as is if no schema is passed or if `skipConversion` is set
929
935
  if (!schema || opt.skipConversion) {
930
- return obj as any
936
+ return obj as OUT
931
937
  }
932
938
 
933
939
  // This will Convert and Validate
@@ -941,12 +947,12 @@ export class CommonDao<
941
947
  // Ajv schema
942
948
  convertedValue = obj // because Ajv mutates original object
943
949
 
944
- error = schema.getValidationError(obj, {
950
+ error = schema.getValidationError(obj as IN, {
945
951
  objectName,
946
952
  })
947
953
  } else {
948
954
  // Joi
949
- const vr = getValidationResult<IN, OUT>(obj, schema, objectName)
955
+ const vr = getValidationResult<IN, OUT>(obj as IN, schema, objectName)
950
956
  error = vr.error
951
957
  convertedValue = vr.value
952
958
  }
@@ -977,8 +983,6 @@ export class CommonDao<
977
983
  await this.cfg.db.ping()
978
984
  }
979
985
 
980
- // todo: logging
981
- // todo: bmToDBM, etc. How?
982
986
  // transaction(): DBTransaction {
983
987
  // return this.cfg.db.transaction()
984
988
  // }
@@ -992,7 +996,7 @@ export class CommonDao<
992
996
  if (Array.isArray(res)) {
993
997
  logRes = `${res.length} row(s)`
994
998
  if (res.length && this.cfg.logLevel >= CommonDaoLogLevel.DATA_FULL) {
995
- args.push('\n', res.slice(0, 10)) // max 10 items
999
+ args.push('\n', ...res.slice(0, 10)) // max 10 items
996
1000
  }
997
1001
  } else if (res) {
998
1002
  logRes = `1 row`
@@ -1003,7 +1007,7 @@ export class CommonDao<
1003
1007
  logRes = `undefined`
1004
1008
  }
1005
1009
 
1006
- this.cfg.logger?.log(...[`<< ${table}.${op}: ${logRes} in ${_since(started)}`].concat(args))
1010
+ this.cfg.logger?.log(`<< ${table}.${op}: ${logRes} in ${_since(started)}`, ...args)
1007
1011
  }
1008
1012
 
1009
1013
  protected logSaveResult(started: number, op: string, table: string): void {
@@ -209,7 +209,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
209
209
  ...opt,
210
210
  metric: table,
211
211
  }),
212
- transformLimit(limit),
212
+ transformLimit({ limit }),
213
213
  ...(sinceUpdated
214
214
  ? [transformFilterSync<SavedDBEntity>(r => r.updated >= sinceUpdated)]
215
215
  : []),