dynamo-bao 0.7.3 → 0.8.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/README.md +7 -1
- package/package.json +1 -1
- package/src/dynamodb-client.js +17 -17
- package/src/fields.js +0 -6
- package/src/index.js +1 -1
- package/src/mixins/batch-loading-mixin.js +89 -39
- package/src/mixins/mutation-mixin.js +623 -453
- package/src/mixins/query-mixin.js +15 -25
- package/src/model.js +229 -103
- package/src/utils/logger.js +18 -3
|
@@ -185,29 +185,26 @@ const QueryMethods = {
|
|
|
185
185
|
},
|
|
186
186
|
);
|
|
187
187
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
188
|
+
if (logger.isDebugEnabled()) {
|
|
189
|
+
logger.debug("DynamoDB Query Params:", {
|
|
190
|
+
TableName: params.TableName,
|
|
191
|
+
IndexName: params.IndexName,
|
|
192
|
+
KeyConditionExpression: params.KeyConditionExpression,
|
|
193
|
+
ExpressionAttributeNames: params.ExpressionAttributeNames,
|
|
194
|
+
ExpressionAttributeValues: params.ExpressionAttributeValues,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
196
197
|
|
|
197
198
|
const response = await retryOperation(() =>
|
|
198
199
|
this.documentClient.send(new QueryCommand(params)),
|
|
199
200
|
);
|
|
200
201
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
category: item.category,
|
|
208
|
-
status: item.status,
|
|
209
|
-
})),
|
|
210
|
-
});
|
|
202
|
+
if (logger.isDebugEnabled()) {
|
|
203
|
+
logger.debug("DynamoDB Response:", {
|
|
204
|
+
Count: response.Count,
|
|
205
|
+
ScannedCount: response.ScannedCount,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
211
208
|
|
|
212
209
|
let totalItems;
|
|
213
210
|
if (options.countOnly) {
|
|
@@ -231,13 +228,6 @@ const QueryMethods = {
|
|
|
231
228
|
const expressionValues = { ":pk": pkValue };
|
|
232
229
|
|
|
233
230
|
if (skCondition) {
|
|
234
|
-
logger.log("Building key condition for:", {
|
|
235
|
-
condition: skCondition,
|
|
236
|
-
gsiSortKeyName: options.gsiIndexId
|
|
237
|
-
? `_${options.gsiIndexId}_sk`
|
|
238
|
-
: "_sk",
|
|
239
|
-
});
|
|
240
|
-
|
|
241
231
|
const skExpr = keyBuilder.buildKeyCondition(
|
|
242
232
|
this,
|
|
243
233
|
options.indexName || "primary",
|
package/src/model.js
CHANGED
|
@@ -29,6 +29,20 @@ const {
|
|
|
29
29
|
} = require("./model-config");
|
|
30
30
|
|
|
31
31
|
const GID_SEPARATOR = "##__SK__##";
|
|
32
|
+
|
|
33
|
+
// Passed to the constructor by _createFromDyItem: the instance is about to
|
|
34
|
+
// take its data from a stored row, so skip computing initial field values
|
|
35
|
+
// (which for auto-assigned ULID and version fields means generating ids
|
|
36
|
+
// that would be thrown away).
|
|
37
|
+
const HYDRATE = Symbol("dynamo-bao.hydrate");
|
|
38
|
+
|
|
39
|
+
// Set on each model class once its field accessors have been defined on the
|
|
40
|
+
// prototype; holds the `fields` object they were built from.
|
|
41
|
+
const FIELD_ACCESSORS_FOR = Symbol("dynamo-bao.fieldAccessorsFor");
|
|
42
|
+
|
|
43
|
+
// System attributes (_pk, _sk, GSI keys, ...) are plain strings with no
|
|
44
|
+
// per-model options, so one shared field definition serves them all.
|
|
45
|
+
const SYSTEM_STRING_FIELD = StringField();
|
|
32
46
|
const {
|
|
33
47
|
UNIQUE_CONSTRAINT_KEY,
|
|
34
48
|
SYSTEM_FIELDS,
|
|
@@ -107,7 +121,7 @@ class BaoModel {
|
|
|
107
121
|
static _getField(fieldName) {
|
|
108
122
|
let fieldDef;
|
|
109
123
|
if (SYSTEM_FIELDS.includes(fieldName) || fieldName === "modelPrefix") {
|
|
110
|
-
fieldDef =
|
|
124
|
+
fieldDef = SYSTEM_STRING_FIELD;
|
|
111
125
|
} else {
|
|
112
126
|
fieldDef = this.fields[fieldName];
|
|
113
127
|
}
|
|
@@ -325,24 +339,98 @@ class BaoModel {
|
|
|
325
339
|
return this.iterable ? this.iterationBuckets : 1;
|
|
326
340
|
}
|
|
327
341
|
|
|
342
|
+
/**
|
|
343
|
+
* @description
|
|
344
|
+
* Iterate every row of an `iterable` model, yielding hydrated batches.
|
|
345
|
+
* Within a bucket the next page of keys is fetched while the current page
|
|
346
|
+
* is hydrated. Buckets are walked one at a time unless `concurrency` is
|
|
347
|
+
* raised, in which case that many buckets are read at once and their
|
|
348
|
+
* batches are yielded as they arrive (bucket order is then interleaved).
|
|
349
|
+
*
|
|
350
|
+
* @param {Object} [options]
|
|
351
|
+
* @param {number} [options.batchSize=100] - Keys per iteration-index Query,
|
|
352
|
+
* and so the upper bound on each yielded batch.
|
|
353
|
+
* @param {Object} [options.filter] - Filter on attributes projected onto
|
|
354
|
+
* the iteration index.
|
|
355
|
+
* @param {number} [options.concurrency=1] - Buckets to read concurrently.
|
|
356
|
+
* @yields {BaoModel[]} One batch of loaded instances at a time.
|
|
357
|
+
*/
|
|
328
358
|
static async *iterateAll(options = {}) {
|
|
329
359
|
if (!this.iterable) {
|
|
330
360
|
throw new Error(`Model ${this.name} is not configured as iterable`);
|
|
331
361
|
}
|
|
332
362
|
|
|
333
|
-
const { batchSize = 100, filter = null } = options;
|
|
363
|
+
const { batchSize = 100, filter = null, concurrency = 1 } = options;
|
|
364
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) {
|
|
365
|
+
throw new Error("concurrency must be a positive integer");
|
|
366
|
+
}
|
|
334
367
|
|
|
335
368
|
if (this.iterationBuckets === 1) {
|
|
336
|
-
yield* this._iterateSingleBucket(null, {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
369
|
+
yield* this._iterateSingleBucket(null, { batchSize, filter });
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const buckets = Array.from({ length: this.iterationBuckets }, (_, b) => b);
|
|
374
|
+
if (concurrency === 1) {
|
|
375
|
+
for (const bucket of buckets) {
|
|
376
|
+
yield* this._iterateSingleBucket(bucket, { batchSize, filter });
|
|
377
|
+
}
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
yield* this._interleaveGenerators(
|
|
382
|
+
buckets.map(
|
|
383
|
+
(bucket) => () =>
|
|
384
|
+
this._iterateSingleBucket(bucket, { batchSize, filter }),
|
|
385
|
+
),
|
|
386
|
+
concurrency,
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Drive up to `limit` async generators at once, yielding each value as
|
|
392
|
+
* soon as any of them produces one. Each generator has exactly one
|
|
393
|
+
* `next()` outstanding, so no value is lost between races. On error the
|
|
394
|
+
* remaining generators are closed and the error propagates.
|
|
395
|
+
* @private
|
|
396
|
+
*/
|
|
397
|
+
static async *_interleaveGenerators(factories, limit) {
|
|
398
|
+
const pending = [...factories];
|
|
399
|
+
const inFlight = new Map(); // generator -> promise of { gen, result }
|
|
400
|
+
|
|
401
|
+
const start = () => {
|
|
402
|
+
const gen = pending.shift()();
|
|
403
|
+
inFlight.set(
|
|
404
|
+
gen,
|
|
405
|
+
gen.next().then((result) => ({ gen, result })),
|
|
406
|
+
);
|
|
407
|
+
};
|
|
408
|
+
const pull = (gen) => {
|
|
409
|
+
inFlight.set(
|
|
410
|
+
gen,
|
|
411
|
+
gen.next().then((result) => ({ gen, result })),
|
|
412
|
+
);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
while (pending.length > 0 && inFlight.size < limit) start();
|
|
416
|
+
|
|
417
|
+
try {
|
|
418
|
+
while (inFlight.size > 0) {
|
|
419
|
+
const { gen, result } = await Promise.race(inFlight.values());
|
|
420
|
+
if (result.done) {
|
|
421
|
+
inFlight.delete(gen);
|
|
422
|
+
if (pending.length > 0) start();
|
|
423
|
+
} else {
|
|
424
|
+
pull(gen);
|
|
425
|
+
yield result.value;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
} finally {
|
|
429
|
+
// Reached on error or on an early break by the consumer: don't leave
|
|
430
|
+
// the other generators' in-flight requests as unhandled rejections.
|
|
431
|
+
for (const [gen, promise] of inFlight) {
|
|
432
|
+
promise.catch(() => {});
|
|
433
|
+
gen.return().catch(() => {});
|
|
346
434
|
}
|
|
347
435
|
}
|
|
348
436
|
}
|
|
@@ -889,66 +977,80 @@ class BaoModel {
|
|
|
889
977
|
const iterPk = this._getIterationPk(bucketNum);
|
|
890
978
|
const indexName = this.manager.getIterationIndexName();
|
|
891
979
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
};
|
|
904
|
-
|
|
905
|
-
if (lastEvaluatedKey) {
|
|
906
|
-
params.ExclusiveStartKey = lastEvaluatedKey;
|
|
907
|
-
}
|
|
980
|
+
// The query is the same for every page except ExclusiveStartKey, so
|
|
981
|
+
// build it once.
|
|
982
|
+
const baseParams = {
|
|
983
|
+
TableName: this.table,
|
|
984
|
+
IndexName: indexName,
|
|
985
|
+
KeyConditionExpression: "#pk = :pk",
|
|
986
|
+
ExpressionAttributeNames: { "#pk": ITERATION_PK_FIELD },
|
|
987
|
+
ExpressionAttributeValues: { ":pk": iterPk },
|
|
988
|
+
Limit: batchSize,
|
|
989
|
+
ReturnConsumedCapacity: "TOTAL",
|
|
990
|
+
};
|
|
908
991
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
Object.assign(
|
|
925
|
-
params.ExpressionAttributeNames,
|
|
926
|
-
filterExpression.ExpressionAttributeNames,
|
|
927
|
-
);
|
|
928
|
-
Object.assign(
|
|
929
|
-
params.ExpressionAttributeValues,
|
|
930
|
-
filterExpression.ExpressionAttributeValues,
|
|
931
|
-
);
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
if (searchPredicate) {
|
|
935
|
-
filterParts.push(searchPredicate.FilterExpression);
|
|
992
|
+
const filterParts = [];
|
|
993
|
+
if (filter) {
|
|
994
|
+
const { FilterExpressionBuilder } = require("./filter-expression");
|
|
995
|
+
const filterBuilder = new FilterExpressionBuilder();
|
|
996
|
+
const filterExpression = filterBuilder.build(filter, this);
|
|
997
|
+
if (filterExpression) {
|
|
998
|
+
// Preflight: filter attributes must be in the GSI's projection.
|
|
999
|
+
// Otherwise DynamoDB returns an opaque "does not project one or
|
|
1000
|
+
// more filter attributes" ValidationException; we surface a clearer
|
|
1001
|
+
// error before the round-trip.
|
|
1002
|
+
this._assertFilterFitsIterIndex(
|
|
1003
|
+
indexName,
|
|
1004
|
+
filterExpression.ExpressionAttributeNames,
|
|
1005
|
+
);
|
|
1006
|
+
filterParts.push(filterExpression.FilterExpression);
|
|
936
1007
|
Object.assign(
|
|
937
|
-
|
|
938
|
-
|
|
1008
|
+
baseParams.ExpressionAttributeNames,
|
|
1009
|
+
filterExpression.ExpressionAttributeNames,
|
|
939
1010
|
);
|
|
940
1011
|
Object.assign(
|
|
941
|
-
|
|
942
|
-
|
|
1012
|
+
baseParams.ExpressionAttributeValues,
|
|
1013
|
+
filterExpression.ExpressionAttributeValues,
|
|
943
1014
|
);
|
|
944
1015
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1016
|
+
}
|
|
1017
|
+
if (searchPredicate) {
|
|
1018
|
+
filterParts.push(searchPredicate.FilterExpression);
|
|
1019
|
+
Object.assign(
|
|
1020
|
+
baseParams.ExpressionAttributeNames,
|
|
1021
|
+
searchPredicate.ExpressionAttributeNames,
|
|
1022
|
+
);
|
|
1023
|
+
Object.assign(
|
|
1024
|
+
baseParams.ExpressionAttributeValues,
|
|
1025
|
+
searchPredicate.ExpressionAttributeValues,
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
if (filterParts.length > 0) {
|
|
1029
|
+
baseParams.FilterExpression = filterParts
|
|
1030
|
+
.map((p) => `(${p})`)
|
|
1031
|
+
.join(" AND ");
|
|
1032
|
+
}
|
|
950
1033
|
|
|
951
|
-
|
|
1034
|
+
const fetchPage = (lastEvaluatedKey) => {
|
|
1035
|
+
const params = lastEvaluatedKey
|
|
1036
|
+
? { ...baseParams, ExclusiveStartKey: lastEvaluatedKey }
|
|
1037
|
+
: baseParams;
|
|
1038
|
+
const page = this._runIterationQuery(params, indexName);
|
|
1039
|
+
// The page is awaited below, but if the consumer stops iterating first
|
|
1040
|
+
// a failed prefetch must not become an unhandled rejection.
|
|
1041
|
+
page.catch(() => {});
|
|
1042
|
+
return page;
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
// Keep one key page in flight ahead of hydration: the next Query is
|
|
1046
|
+
// issued as soon as the current page's LastEvaluatedKey is known, so
|
|
1047
|
+
// the index read and the BatchGet overlap instead of alternating.
|
|
1048
|
+
let nextPage = fetchPage(null);
|
|
1049
|
+
while (nextPage) {
|
|
1050
|
+
const response = await nextPage;
|
|
1051
|
+
nextPage = response.LastEvaluatedKey
|
|
1052
|
+
? fetchPage(response.LastEvaluatedKey)
|
|
1053
|
+
: null;
|
|
952
1054
|
|
|
953
1055
|
if (response.Items && response.Items.length > 0) {
|
|
954
1056
|
const objectIds = response.Items.map(
|
|
@@ -962,9 +1064,7 @@ class BaoModel {
|
|
|
962
1064
|
yield batch;
|
|
963
1065
|
}
|
|
964
1066
|
}
|
|
965
|
-
|
|
966
|
-
lastEvaluatedKey = response.LastEvaluatedKey;
|
|
967
|
-
} while (lastEvaluatedKey);
|
|
1067
|
+
}
|
|
968
1068
|
}
|
|
969
1069
|
|
|
970
1070
|
/**
|
|
@@ -973,37 +1073,62 @@ class BaoModel {
|
|
|
973
1073
|
* @param {Object} [jsData] - The initial data for the model.
|
|
974
1074
|
*/
|
|
975
1075
|
constructor(jsData = {}) {
|
|
1076
|
+
const hydrating = jsData === HYDRATE;
|
|
1077
|
+
|
|
976
1078
|
this._dyData = {};
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
1079
|
+
if (!hydrating) {
|
|
1080
|
+
SYSTEM_FIELDS.forEach((key) => {
|
|
1081
|
+
if (jsData[key] !== undefined) {
|
|
1082
|
+
this._dyData[key] = jsData[key];
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
982
1086
|
|
|
983
1087
|
this._loadedDyData = {};
|
|
984
1088
|
this._changes = new Set();
|
|
985
1089
|
this._relatedObjects = {};
|
|
986
1090
|
this._consumedCapacity = [];
|
|
987
1091
|
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1092
|
+
this.constructor._ensureFieldAccessors();
|
|
1093
|
+
|
|
1094
|
+
if (!hydrating) {
|
|
1095
|
+
// Convert initial values to DynamoDB format
|
|
1096
|
+
for (const [fieldName, field] of Object.entries(
|
|
1097
|
+
this.constructor.fields,
|
|
1098
|
+
)) {
|
|
1099
|
+
const value =
|
|
1100
|
+
jsData[fieldName] === undefined
|
|
1101
|
+
? field.getInitialValue()
|
|
1102
|
+
: jsData[fieldName];
|
|
1103
|
+
this._dyData[fieldName] = field.toDy(value);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Define a getter/setter for each field on this class's prototype, once
|
|
1110
|
+
* per class. Every instance reads and writes its own `_dyData` through
|
|
1111
|
+
* them, so they don't need to be per-instance properties. Keyed by an own
|
|
1112
|
+
* property so a subclass with its own `fields` gets its own accessors.
|
|
1113
|
+
* @private
|
|
1114
|
+
*/
|
|
1115
|
+
static _ensureFieldAccessors() {
|
|
1116
|
+
if (Object.prototype.hasOwnProperty.call(this, FIELD_ACCESSORS_FOR)) {
|
|
1117
|
+
if (this[FIELD_ACCESSORS_FOR] === this.fields) return;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
for (const [fieldName, field] of Object.entries(this.fields)) {
|
|
1121
|
+
Object.defineProperty(this.prototype, fieldName, {
|
|
1122
|
+
configurable: true,
|
|
1123
|
+
enumerable: false,
|
|
1124
|
+
get() {
|
|
1000
1125
|
// For StringSetField, pass model context to enable proxy
|
|
1001
1126
|
if (field instanceof StringSetFieldClass) {
|
|
1002
1127
|
return field.fromDy(this._dyData[fieldName], this, fieldName);
|
|
1003
1128
|
}
|
|
1004
1129
|
return field.fromDy(this._dyData[fieldName]);
|
|
1005
1130
|
},
|
|
1006
|
-
set
|
|
1131
|
+
set(newValue) {
|
|
1007
1132
|
const oldDyValue = this._dyData[fieldName];
|
|
1008
1133
|
const newDyValue = field.toDy(newValue);
|
|
1009
1134
|
if (newDyValue !== oldDyValue) {
|
|
@@ -1015,16 +1140,20 @@ class BaoModel {
|
|
|
1015
1140
|
}
|
|
1016
1141
|
},
|
|
1017
1142
|
});
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
Object.defineProperty(this, FIELD_ACCESSORS_FOR, {
|
|
1146
|
+
value: this.fields,
|
|
1147
|
+
configurable: true,
|
|
1148
|
+
enumerable: false,
|
|
1149
|
+
writable: true,
|
|
1018
1150
|
});
|
|
1019
1151
|
}
|
|
1020
1152
|
|
|
1021
1153
|
static _createFromDyItem(dyItem) {
|
|
1022
|
-
const newObj = new this();
|
|
1154
|
+
const newObj = new this(HYDRATE);
|
|
1023
1155
|
newObj._dyData = dyItem;
|
|
1024
1156
|
newObj._resetChangeTracking();
|
|
1025
|
-
|
|
1026
|
-
logger.debug("_createFromDyItem", dyItem, newObj);
|
|
1027
|
-
logger.debug("_createFromDyItem.name", newObj.name);
|
|
1028
1157
|
return newObj;
|
|
1029
1158
|
}
|
|
1030
1159
|
|
|
@@ -1169,20 +1298,12 @@ class BaoModel {
|
|
|
1169
1298
|
// Get only changed fields - convert from Dynamo to JS format
|
|
1170
1299
|
_getChanges() {
|
|
1171
1300
|
const changes = {};
|
|
1172
|
-
logger.debug("_changes Set contains:", Array.from(this._changes));
|
|
1173
1301
|
for (const field of this._changes) {
|
|
1174
1302
|
const fieldDef = this.constructor._getField(field);
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
const dyValue = this._dyData[field];
|
|
1180
|
-
logger.debug("Converting value:", {
|
|
1181
|
-
field,
|
|
1182
|
-
dyValue,
|
|
1183
|
-
fromDyExists: typeof fieldDef.fromDy === "function",
|
|
1184
|
-
});
|
|
1185
|
-
changes[field] = fieldDef.fromDy(dyValue);
|
|
1303
|
+
changes[field] = fieldDef.fromDy(this._dyData[field]);
|
|
1304
|
+
}
|
|
1305
|
+
if (logger.isDebugEnabled()) {
|
|
1306
|
+
logger.debug("_getChanges", Array.from(this._changes), changes);
|
|
1186
1307
|
}
|
|
1187
1308
|
return changes;
|
|
1188
1309
|
}
|
|
@@ -1203,7 +1324,12 @@ class BaoModel {
|
|
|
1203
1324
|
* @returns {boolean} True if the object has been loaded, false otherwise.
|
|
1204
1325
|
*/
|
|
1205
1326
|
isLoaded() {
|
|
1206
|
-
|
|
1327
|
+
for (const key in this._loadedDyData) {
|
|
1328
|
+
if (Object.prototype.hasOwnProperty.call(this._loadedDyData, key)) {
|
|
1329
|
+
return true;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
return false;
|
|
1207
1333
|
}
|
|
1208
1334
|
|
|
1209
1335
|
// Reset tracking after successful save
|
package/src/utils/logger.js
CHANGED
|
@@ -16,11 +16,26 @@ class Logger {
|
|
|
16
16
|
this.currentLevel = userConfig.logging.level.toUpperCase();
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Whether a message at `level` would be printed. Check this before
|
|
21
|
+
* building a log argument that is expensive to compute (a JSON dump, a
|
|
22
|
+
* mapped array), so the cost is only paid when the output is wanted.
|
|
23
|
+
* @param {"ERROR"|"WARN"|"INFO"|"DEBUG"} level
|
|
24
|
+
* @returns {boolean}
|
|
25
|
+
*/
|
|
26
|
+
isLevelEnabled(level) {
|
|
27
|
+
return (
|
|
21
28
|
this.currentLevel !== "OFF" &&
|
|
22
29
|
LOG_LEVELS[level] <= LOG_LEVELS[this.currentLevel]
|
|
23
|
-
)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isDebugEnabled() {
|
|
34
|
+
return this.isLevelEnabled("DEBUG");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
log(level, ...args) {
|
|
38
|
+
if (this.isLevelEnabled(level)) {
|
|
24
39
|
const prefix = `[${level}] ${this.context}:`;
|
|
25
40
|
console.log(prefix, ...args);
|
|
26
41
|
}
|