@naturalcycles/db-lib 8.21.0 → 8.21.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [8.21.1](https://github.com/NaturalCycles/db-lib/compare/v8.21.0...v8.21.1) (2021-10-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * test for saving/loading undefined values ([e7a8dcf](https://github.com/NaturalCycles/db-lib/commit/e7a8dcff296ba7c8c23c4157510dd79fe7ef1727))
7
+
1
8
  # [8.21.0](https://github.com/NaturalCycles/db-lib/compare/v8.20.1...v8.21.0) (2021-10-18)
2
9
 
3
10
 
@@ -41,7 +41,7 @@ class FileDB extends __1.BaseCommonDB {
41
41
  async saveBatch(table, rows, _opt) {
42
42
  if (!rows.length)
43
43
  return; // save some api calls
44
- // 1. Load the whole file from gh
44
+ // 1. Load the whole file
45
45
  const byId = (0, js_lib_1._by)(await this.loadFile(table), r => r.id);
46
46
  // 2. Merge with new data (using ids)
47
47
  let saved = 0;
@@ -173,6 +173,7 @@ class FileDB extends __1.BaseCommonDB {
173
173
  * Mutates
174
174
  */
175
175
  sortRows(rows) {
176
+ rows.forEach(r => (0, js_lib_1._filterUndefinedValues)(r, true));
176
177
  if (this.cfg.sortOnSave) {
177
178
  (0, js_lib_1._sortBy)(rows, r => r[this.cfg.sortOnSave.name], true);
178
179
  if (this.cfg.sortOnSave.descending)
@@ -22,7 +22,7 @@ function runCommonDaoTest(db, features = {}, quirks = {}) {
22
22
  // tableSchemas = true,
23
23
  createTable = true, dbQueryFilter = true,
24
24
  // dbQueryFilterIn = true,
25
- dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, } = features;
25
+ dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, nullValues = true, } = features;
26
26
  // const {
27
27
  // allowExtraPropertiesInResponse,
28
28
  // allowBooleansAsUndefined,
@@ -72,15 +72,28 @@ function runCommonDaoTest(db, features = {}, quirks = {}) {
72
72
  expect(await dao.getByIds(['abc', 'abcd'])).toEqual([]);
73
73
  });
74
74
  // SAVE
75
- test('should allow to save and load null values', async () => {
75
+ if (nullValues) {
76
+ test('should allow to save and load null values', async () => {
77
+ const item3 = {
78
+ ...(0, test_model_1.createTestItemBM)(3),
79
+ k2: null,
80
+ };
81
+ await dao.save(item3);
82
+ const item3Loaded = await dao.requireById(item3.id);
83
+ (0, dbTest_1.expectMatch)([item3], [item3Loaded], quirks);
84
+ expect(item3Loaded.k2).toBe(null);
85
+ });
86
+ }
87
+ test('undefined values should not be saved/loaded', async () => {
76
88
  const item3 = {
77
89
  ...(0, test_model_1.createTestItemBM)(3),
78
- k2: null,
90
+ k2: undefined,
79
91
  };
80
92
  await dao.save(item3);
81
93
  const item3Loaded = await dao.requireById(item3.id);
82
94
  (0, dbTest_1.expectMatch)([item3], [item3Loaded], quirks);
83
- expect(item3Loaded.k2).toBe(null);
95
+ expect(item3Loaded.k2).toBe(undefined);
96
+ expect(Object.keys(item3Loaded)).not.toContain('k2');
84
97
  });
85
98
  test('saveBatch test items', async () => {
86
99
  const itemsSaved = await dao.saveBatch(items);
@@ -17,6 +17,7 @@ export interface CommonDBImplementationFeatures {
17
17
  strongConsistency?: boolean;
18
18
  streaming?: boolean;
19
19
  bufferSupport?: boolean;
20
+ nullValues?: boolean;
20
21
  }
21
22
  /**
22
23
  * All options default to `false`.
@@ -12,7 +12,7 @@ const test_util_1 = require("./test.util");
12
12
  function runCommonDBTest(db, features = {}, quirks = {}) {
13
13
  const { querying = true, tableSchemas = true, createTable = true, dbQueryFilter = true,
14
14
  // dbQueryFilterIn = true,
15
- dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, bufferSupport = true, } = features;
15
+ dbQueryOrder = true, dbQuerySelectFields = true, streaming = true, strongConsistency = true, bufferSupport = true, nullValues = true, } = features;
16
16
  // const {
17
17
  // allowExtraPropertiesInResponse,
18
18
  // allowBooleansAsUndefined,
@@ -58,15 +58,28 @@ function runCommonDBTest(db, features = {}, quirks = {}) {
58
58
  expect(await db.getByIds(test_model_1.TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
59
59
  });
60
60
  // SAVE
61
- test('should allow to save and load null values', async () => {
61
+ if (nullValues) {
62
+ test('should allow to save and load null values', async () => {
63
+ const item3 = {
64
+ ...(0, test_model_1.createTestItemDBM)(3),
65
+ k2: null,
66
+ };
67
+ await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
68
+ const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
69
+ expectMatch([item3], [item3Loaded], quirks);
70
+ expect(item3Loaded.k2).toBe(null);
71
+ });
72
+ }
73
+ test('undefined values should not be saved/loaded', async () => {
62
74
  const item3 = {
63
75
  ...(0, test_model_1.createTestItemDBM)(3),
64
- k2: null,
76
+ k2: undefined,
65
77
  };
66
78
  await db.saveBatch(test_model_1.TEST_TABLE, [item3]);
67
79
  const item3Loaded = (await db.getByIds(test_model_1.TEST_TABLE, [item3.id]))[0];
68
80
  expectMatch([item3], [item3Loaded], quirks);
69
- expect(item3Loaded.k2).toBe(null);
81
+ expect(item3Loaded.k2).toBe(undefined);
82
+ expect(Object.keys(item3Loaded)).not.toContain('k2');
70
83
  });
71
84
  test('saveBatch test items', async () => {
72
85
  await db.saveBatch(test_model_1.TEST_TABLE, items);
package/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "engines": {
43
43
  "node": ">=12.13"
44
44
  },
45
- "version": "8.21.0",
45
+ "version": "8.21.1",
46
46
  "description": "Lowest Common Denominator API to supported Databases",
47
47
  "keywords": [
48
48
  "db",
@@ -10,6 +10,7 @@ import {
10
10
  _stringMapValues,
11
11
  _uniq,
12
12
  JsonSchemaRootObject,
13
+ _filterUndefinedValues,
13
14
  } from '@naturalcycles/js-lib'
14
15
  import { Debug, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
15
16
  import { dimGrey } from '@naturalcycles/nodejs-lib/dist/colors'
@@ -82,7 +83,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
82
83
  ): Promise<void> {
83
84
  if (!rows.length) return // save some api calls
84
85
 
85
- // 1. Load the whole file from gh
86
+ // 1. Load the whole file
86
87
  const byId = _by(await this.loadFile<ROW>(table), r => r.id)
87
88
 
88
89
  // 2. Merge with new data (using ids)
@@ -261,6 +262,8 @@ export class FileDB extends BaseCommonDB implements CommonDB {
261
262
  * Mutates
262
263
  */
263
264
  sortRows<ROW>(rows: ROW[]): ROW[] {
265
+ rows.forEach(r => _filterUndefinedValues(r, true))
266
+
264
267
  if (this.cfg.sortOnSave) {
265
268
  _sortBy(rows, r => r[this.cfg.sortOnSave!.name], true)
266
269
  if (this.cfg.sortOnSave.descending) rows.reverse() // mutates
@@ -39,6 +39,7 @@ export function runCommonDaoTest(
39
39
  dbQuerySelectFields = true,
40
40
  streaming = true,
41
41
  strongConsistency = true,
42
+ nullValues = true,
42
43
  } = features
43
44
 
44
45
  // const {
@@ -102,15 +103,29 @@ export function runCommonDaoTest(
102
103
  })
103
104
 
104
105
  // SAVE
105
- test('should allow to save and load null values', async () => {
106
+ if (nullValues) {
107
+ test('should allow to save and load null values', async () => {
108
+ const item3 = {
109
+ ...createTestItemBM(3),
110
+ k2: null,
111
+ }
112
+ await dao.save(item3)
113
+ const item3Loaded = await dao.requireById(item3.id)
114
+ expectMatch([item3], [item3Loaded], quirks)
115
+ expect(item3Loaded.k2).toBe(null)
116
+ })
117
+ }
118
+
119
+ test('undefined values should not be saved/loaded', async () => {
106
120
  const item3 = {
107
121
  ...createTestItemBM(3),
108
- k2: null,
122
+ k2: undefined,
109
123
  }
110
124
  await dao.save(item3)
111
125
  const item3Loaded = await dao.requireById(item3.id)
112
126
  expectMatch([item3], [item3Loaded], quirks)
113
- expect(item3Loaded.k2).toBe(null)
127
+ expect(item3Loaded.k2).toBe(undefined)
128
+ expect(Object.keys(item3Loaded)).not.toContain('k2')
114
129
  })
115
130
 
116
131
  test('saveBatch test items', async () => {
@@ -34,6 +34,7 @@ export interface CommonDBImplementationFeatures {
34
34
  streaming?: boolean
35
35
 
36
36
  bufferSupport?: boolean
37
+ nullValues?: boolean
37
38
  }
38
39
 
39
40
  /**
@@ -76,6 +77,7 @@ export function runCommonDBTest(
76
77
  streaming = true,
77
78
  strongConsistency = true,
78
79
  bufferSupport = true,
80
+ nullValues = true,
79
81
  } = features
80
82
 
81
83
  // const {
@@ -135,15 +137,29 @@ export function runCommonDBTest(
135
137
  })
136
138
 
137
139
  // SAVE
138
- test('should allow to save and load null values', async () => {
140
+ if (nullValues) {
141
+ test('should allow to save and load null values', async () => {
142
+ const item3 = {
143
+ ...createTestItemDBM(3),
144
+ k2: null,
145
+ }
146
+ await db.saveBatch(TEST_TABLE, [item3])
147
+ const item3Loaded = (await db.getByIds<TestItemDBM>(TEST_TABLE, [item3.id]))[0]!
148
+ expectMatch([item3], [item3Loaded], quirks)
149
+ expect(item3Loaded.k2).toBe(null)
150
+ })
151
+ }
152
+
153
+ test('undefined values should not be saved/loaded', async () => {
139
154
  const item3 = {
140
155
  ...createTestItemDBM(3),
141
- k2: null,
156
+ k2: undefined,
142
157
  }
143
158
  await db.saveBatch(TEST_TABLE, [item3])
144
159
  const item3Loaded = (await db.getByIds<TestItemDBM>(TEST_TABLE, [item3.id]))[0]!
145
160
  expectMatch([item3], [item3Loaded], quirks)
146
- expect(item3Loaded.k2).toBe(null)
161
+ expect(item3Loaded.k2).toBe(undefined)
162
+ expect(Object.keys(item3Loaded)).not.toContain('k2')
147
163
  })
148
164
 
149
165
  test('saveBatch test items', async () => {