@naturalcycles/db-lib 10.5.0 → 10.6.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.
@@ -1,5 +1,5 @@
1
1
  import { _assert, _by, _deepEquals, _filterUndefinedValues, _since, _sortBy, _sortObjectDeep, _stringMapValues, generateJsonSchemaFromData, localTime, } from '@naturalcycles/js-lib';
2
- import { dimGrey } from '@naturalcycles/nodejs-lib';
2
+ import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
3
3
  import { readableCreate } from '@naturalcycles/nodejs-lib/stream';
4
4
  import { BaseCommonDB } from '../../base.common.db.js';
5
5
  import { commonDBFullSupport } from '../../common.db.js';
@@ -1,7 +1,7 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { pMap } from '@naturalcycles/js-lib';
3
- import { fs2 } from '@naturalcycles/nodejs-lib/fs';
4
- import { _pipeline } from '@naturalcycles/nodejs-lib/stream';
3
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
4
+ import { _pipeline, createReadStreamAsNDJSON, createWriteStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
5
5
  /**
6
6
  * Persists in local filesystem as ndjson.
7
7
  */
@@ -26,7 +26,7 @@ export class LocalFilePersistencePlugin {
26
26
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
27
27
  if (!(await fs2.pathExistsAsync(filePath)))
28
28
  return [];
29
- return await fs2.createReadStreamAsNDJSON(filePath).toArray();
29
+ return await createReadStreamAsNDJSON(filePath).toArray();
30
30
  }
31
31
  async saveFiles(ops) {
32
32
  await pMap(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 32 });
@@ -35,6 +35,6 @@ export class LocalFilePersistencePlugin {
35
35
  await fs2.ensureDirAsync(this.cfg.storagePath);
36
36
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
37
37
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
38
- await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(filePath)]);
38
+ await _pipeline([Readable.from(rows), ...createWriteStreamAsNDJSON(filePath)]);
39
39
  }
40
40
  }
@@ -1,8 +1,8 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { _assert, _by, _deepCopy, _isEmptyObject, _since, _sortObjectDeep, _stringMapEntries, _stringMapValues, generateJsonSchemaFromData, localTime, pMap, } from '@naturalcycles/js-lib';
3
- import { dimGrey, yellow } from '@naturalcycles/nodejs-lib';
4
- import { fs2 } from '@naturalcycles/nodejs-lib/fs';
5
- import { _pipeline, bufferReviver } from '@naturalcycles/nodejs-lib/stream';
3
+ import { dimGrey, yellow } from '@naturalcycles/nodejs-lib/colors';
4
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
5
+ import { _pipeline, bufferReviver, createReadStreamAsNDJSON, createWriteStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
6
6
  import { commonDBFullSupport, CommonDBType } from '../../common.db.js';
7
7
  import { queryInMemory } from './queryInMemory.js';
8
8
  export class InMemoryDB {
@@ -183,7 +183,7 @@ export class InMemoryDB {
183
183
  return; // 0 rows
184
184
  tables++;
185
185
  const fname = `${persistentStoragePath}/${table}.ndjson${persistZip ? '.gz' : ''}`;
186
- await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(fname)]);
186
+ await _pipeline([Readable.from(rows), ...createWriteStreamAsNDJSON(fname)]);
187
187
  });
188
188
  this.cfg.logger.log(`flushToDisk took ${dimGrey(_since(started))} to save ${yellow(tables)} tables`);
189
189
  }
@@ -201,7 +201,7 @@ export class InMemoryDB {
201
201
  await pMap(files, async (file) => {
202
202
  const fname = `${persistentStoragePath}/${file}`;
203
203
  const table = file.split('.ndjson')[0];
204
- const rows = await fs2.createReadStreamAsNDJSON(fname).toArray();
204
+ const rows = await createReadStreamAsNDJSON(fname).toArray();
205
205
  this.data[table] = _by(rows, r => r.id);
206
206
  });
207
207
  this.cfg.logger.log(`restoreFromDisk took ${dimGrey(_since(started))} to read ${yellow(files.length)} tables`);
@@ -1,5 +1,5 @@
1
1
  import { AppError, pMap } from '@naturalcycles/js-lib';
2
- import { deflateString, inflateToString } from '@naturalcycles/nodejs-lib';
2
+ import { deflateString, inflateToString } from '@naturalcycles/nodejs-lib/zip';
3
3
  export const commonKeyValueDaoDeflatedJsonTransformer = {
4
4
  valueToBuffer: async (v) => await deflateString(JSON.stringify(v)),
5
5
  bufferToValue: async (buf) => JSON.parse(await inflateToString(buf)),
@@ -1,6 +1,6 @@
1
1
  import type { AsyncMapper, StringMap, UnixTimestamp } from '@naturalcycles/js-lib';
2
2
  import { ErrorMode } from '@naturalcycles/js-lib';
3
- import type { TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
3
+ import { type TransformLogProgressOptions, type TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
4
4
  import { NDJsonStats } from '@naturalcycles/nodejs-lib/stream';
5
5
  import type { CommonDB } from '../common.db.js';
6
6
  import { DBQuery } from '../query/dbQuery.js';
@@ -1,6 +1,7 @@
1
1
  import { _passthroughMapper, AppError, ErrorMode, localTime, pMap } from '@naturalcycles/js-lib';
2
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib';
3
- import { fs2 } from '@naturalcycles/nodejs-lib/fs';
2
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
3
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
4
+ import { createWriteStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
4
5
  import { _pipeline, NDJsonStats, transformLogProgress, transformMap, transformTap, } from '@naturalcycles/nodejs-lib/stream';
5
6
  import { DBQuery } from '../query/dbQuery.js';
6
7
  /**
@@ -68,7 +69,7 @@ export async function dbPipelineBackup(opt) {
68
69
  transformTap(() => {
69
70
  rows++;
70
71
  }),
71
- ...fs2.createWriteStreamAsNDJSON(filePath),
72
+ ...createWriteStreamAsNDJSON(filePath),
72
73
  ]);
73
74
  const { size: sizeBytes } = await fs2.statAsync(filePath);
74
75
  const stats = NDJsonStats.create({
@@ -1,5 +1,5 @@
1
1
  import { _passthroughMapper, ErrorMode, localTime, pMap } from '@naturalcycles/js-lib';
2
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib';
2
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
3
3
  import { _pipeline, NDJsonStats, transformChunk, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
4
4
  import { DBQuery } from '../query/dbQuery.js';
5
5
  /**
@@ -1,6 +1,6 @@
1
1
  import type { AsyncMapper, UnixTimestamp } from '@naturalcycles/js-lib';
2
2
  import { ErrorMode } from '@naturalcycles/js-lib';
3
- import type { TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
3
+ import { type TransformLogProgressOptions, type TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
4
4
  import { NDJsonStats } from '@naturalcycles/nodejs-lib/stream';
5
5
  import type { CommonDB } from '../common.db.js';
6
6
  import type { CommonDBSaveOptions } from '../db.model.js';
@@ -1,6 +1,7 @@
1
1
  import { _hb, _mapValues, _passthroughMapper, ErrorMode, localTime, pMap, } from '@naturalcycles/js-lib';
2
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib';
3
- import { fs2 } from '@naturalcycles/nodejs-lib/fs';
2
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
3
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
4
+ import { createReadStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
4
5
  import { _pipeline, NDJsonStats, transformChunk, transformFilterSync, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
5
6
  /**
6
7
  * Pipeline from NDJSON files in a folder (optionally gzipped) to CommonDB.
@@ -62,7 +63,7 @@ export async function dbPipelineRestore(opt) {
62
63
  const sizeBytes = sizeByTable[table];
63
64
  console.log(`<< ${grey(filePath)} ${dimWhite(_hb(sizeBytes))} started...`);
64
65
  await _pipeline([
65
- fs2.createReadStreamAsNDJSON(filePath).take(limit || Number.POSITIVE_INFINITY),
66
+ createReadStreamAsNDJSON(filePath).take(limit || Number.POSITIVE_INFINITY),
66
67
  transformTap(() => rows++),
67
68
  transformLogProgress({
68
69
  logEvery: 1000,
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.5.0",
4
+ "version": "10.6.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
8
8
  },
9
9
  "devDependencies": {
10
- "@naturalcycles/dev-lib": "19.5.0"
10
+ "@naturalcycles/dev-lib": "19.6.0"
11
11
  },
12
12
  "files": [
13
13
  "dist",
@@ -11,7 +11,7 @@ import {
11
11
  generateJsonSchemaFromData,
12
12
  localTime,
13
13
  } from '@naturalcycles/js-lib'
14
- import { dimGrey } from '@naturalcycles/nodejs-lib'
14
+ import { dimGrey } from '@naturalcycles/nodejs-lib/colors'
15
15
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
16
16
  import { readableCreate } from '@naturalcycles/nodejs-lib/stream'
17
17
  import { BaseCommonDB } from '../../base.common.db.js'
@@ -1,8 +1,12 @@
1
1
  import { Readable } from 'node:stream'
2
2
  import type { ObjectWithId } from '@naturalcycles/js-lib'
3
3
  import { pMap } from '@naturalcycles/js-lib'
4
- import { fs2 } from '@naturalcycles/nodejs-lib/fs'
5
- import { _pipeline } from '@naturalcycles/nodejs-lib/stream'
4
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
5
+ import {
6
+ _pipeline,
7
+ createReadStreamAsNDJSON,
8
+ createWriteStreamAsNDJSON,
9
+ } from '@naturalcycles/nodejs-lib/stream'
6
10
  import type { DBSaveBatchOperation } from '../../db.model.js'
7
11
  import type { FileDBPersistencePlugin } from './file.db.model.js'
8
12
 
@@ -47,7 +51,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
47
51
 
48
52
  if (!(await fs2.pathExistsAsync(filePath))) return []
49
53
 
50
- return await fs2.createReadStreamAsNDJSON(filePath).toArray()
54
+ return await createReadStreamAsNDJSON(filePath).toArray()
51
55
  }
52
56
 
53
57
  async saveFiles(ops: DBSaveBatchOperation<any>[]): Promise<void> {
@@ -59,6 +63,6 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
59
63
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
60
64
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
61
65
 
62
- await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(filePath)])
66
+ await _pipeline([Readable.from(rows), ...createWriteStreamAsNDJSON(filePath)])
63
67
  }
64
68
  }
@@ -20,9 +20,15 @@ import {
20
20
  localTime,
21
21
  pMap,
22
22
  } from '@naturalcycles/js-lib'
23
- import { dimGrey, yellow } from '@naturalcycles/nodejs-lib'
24
- import { fs2 } from '@naturalcycles/nodejs-lib/fs'
25
- import { _pipeline, bufferReviver, type ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
23
+ import { dimGrey, yellow } from '@naturalcycles/nodejs-lib/colors'
24
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
25
+ import {
26
+ _pipeline,
27
+ bufferReviver,
28
+ createReadStreamAsNDJSON,
29
+ createWriteStreamAsNDJSON,
30
+ type ReadableTyped,
31
+ } from '@naturalcycles/nodejs-lib/stream'
26
32
  import type { CommonDB, CommonDBSupport } from '../../common.db.js'
27
33
  import { commonDBFullSupport, CommonDBType } from '../../common.db.js'
28
34
  import type {
@@ -329,7 +335,7 @@ export class InMemoryDB implements CommonDB {
329
335
  tables++
330
336
  const fname = `${persistentStoragePath}/${table}.ndjson${persistZip ? '.gz' : ''}`
331
337
 
332
- await _pipeline([Readable.from(rows), ...fs2.createWriteStreamAsNDJSON(fname)])
338
+ await _pipeline([Readable.from(rows), ...createWriteStreamAsNDJSON(fname)])
333
339
  })
334
340
 
335
341
  this.cfg.logger!.log(
@@ -357,7 +363,7 @@ export class InMemoryDB implements CommonDB {
357
363
  const fname = `${persistentStoragePath}/${file}`
358
364
  const table = file.split('.ndjson')[0]!
359
365
 
360
- const rows = await fs2.createReadStreamAsNDJSON(fname).toArray()
366
+ const rows = await createReadStreamAsNDJSON(fname).toArray()
361
367
 
362
368
  this.data[table] = _by(rows, r => r.id)
363
369
  })
@@ -1,7 +1,7 @@
1
1
  import type { CommonLogger, KeyValueTuple } from '@naturalcycles/js-lib'
2
2
  import { AppError, pMap } from '@naturalcycles/js-lib'
3
- import { deflateString, inflateToString } from '@naturalcycles/nodejs-lib'
4
3
  import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
4
+ import { deflateString, inflateToString } from '@naturalcycles/nodejs-lib/zip'
5
5
  import type { CommonDaoLogLevel } from '../commondao/common.dao.model.js'
6
6
  import type { CommonDBCreateOptions } from '../db.model.js'
7
7
  import type {
@@ -1,10 +1,11 @@
1
1
  import type { AsyncMapper, StringMap, UnixTimestamp } from '@naturalcycles/js-lib'
2
2
  import { _passthroughMapper, AppError, ErrorMode, localTime, pMap } from '@naturalcycles/js-lib'
3
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib'
4
- import { fs2 } from '@naturalcycles/nodejs-lib/fs'
5
- import type {
6
- TransformLogProgressOptions,
7
- TransformMapOptions,
3
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
4
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
5
+ import {
6
+ createWriteStreamAsNDJSON,
7
+ type TransformLogProgressOptions,
8
+ type TransformMapOptions,
8
9
  } from '@naturalcycles/nodejs-lib/stream'
9
10
  import {
10
11
  _pipeline,
@@ -223,7 +224,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
223
224
  transformTap(() => {
224
225
  rows++
225
226
  }),
226
- ...fs2.createWriteStreamAsNDJSON(filePath),
227
+ ...createWriteStreamAsNDJSON(filePath),
227
228
  ])
228
229
 
229
230
  const { size: sizeBytes } = await fs2.statAsync(filePath)
@@ -1,6 +1,6 @@
1
1
  import type { AsyncMapper, BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib'
2
2
  import { _passthroughMapper, ErrorMode, localTime, pMap } from '@naturalcycles/js-lib'
3
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib'
3
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
4
4
  import type {
5
5
  TransformLogProgressOptions,
6
6
  TransformMapOptions,
@@ -12,11 +12,12 @@ import {
12
12
  localTime,
13
13
  pMap,
14
14
  } from '@naturalcycles/js-lib'
15
- import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib'
16
- import { fs2 } from '@naturalcycles/nodejs-lib/fs'
17
- import type {
18
- TransformLogProgressOptions,
19
- TransformMapOptions,
15
+ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
16
+ import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
17
+ import {
18
+ createReadStreamAsNDJSON,
19
+ type TransformLogProgressOptions,
20
+ type TransformMapOptions,
20
21
  } from '@naturalcycles/nodejs-lib/stream'
21
22
  import {
22
23
  _pipeline,
@@ -200,7 +201,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
200
201
  console.log(`<< ${grey(filePath)} ${dimWhite(_hb(sizeBytes))} started...`)
201
202
 
202
203
  await _pipeline([
203
- fs2.createReadStreamAsNDJSON(filePath).take(limit || Number.POSITIVE_INFINITY),
204
+ createReadStreamAsNDJSON(filePath).take(limit || Number.POSITIVE_INFINITY),
204
205
  transformTap(() => rows++),
205
206
  transformLogProgress({
206
207
  logEvery: 1000,