@naturalcycles/db-lib 8.54.0 → 8.54.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.
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LocalFilePersistencePlugin = void 0;
4
+ const fs = require("node:fs");
4
5
  const node_stream_1 = require("node:stream");
6
+ const fsp = require("node:fs/promises");
5
7
  const node_zlib_1 = require("node:zlib");
6
8
  const js_lib_1 = require("@naturalcycles/js-lib");
7
9
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
- const fs = require("fs-extra");
9
10
  /**
10
11
  * Persists in local filesystem as ndjson.
11
12
  */
@@ -19,15 +20,15 @@ class LocalFilePersistencePlugin {
19
20
  }
20
21
  async ping() { }
21
22
  async getTables() {
22
- return (await fs.readdir(this.cfg.storagePath))
23
+ return (await fsp.readdir(this.cfg.storagePath))
23
24
  .filter(f => f.includes('.ndjson'))
24
25
  .map(f => f.split('.ndjson')[0]);
25
26
  }
26
27
  async loadFile(table) {
27
- await fs.ensureDir(this.cfg.storagePath);
28
+ await (0, nodejs_lib_1._ensureDir)(this.cfg.storagePath);
28
29
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
29
30
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
30
- if (!(await fs.pathExists(filePath)))
31
+ if (!(await (0, nodejs_lib_1._pathExists)(filePath)))
31
32
  return [];
32
33
  const transformUnzip = this.cfg.gzip ? [(0, node_zlib_1.createUnzip)()] : [];
33
34
  const rows = [];
@@ -44,7 +45,7 @@ class LocalFilePersistencePlugin {
44
45
  await (0, js_lib_1.pMap)(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 16 });
45
46
  }
46
47
  async saveFile(table, rows) {
47
- await fs.ensureDir(this.cfg.storagePath);
48
+ await (0, nodejs_lib_1._ensureDir)(this.cfg.storagePath);
48
49
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
49
50
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
50
51
  const transformZip = this.cfg.gzip ? [(0, node_zlib_1.createGzip)()] : [];
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryDB = void 0;
4
+ const fs = require("node:fs");
5
+ const fsp = require("node:fs/promises");
4
6
  const node_stream_1 = require("node:stream");
5
7
  const node_zlib_1 = require("node:zlib");
6
8
  const js_lib_1 = require("@naturalcycles/js-lib");
7
9
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
8
10
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
9
- const fs = require("fs-extra");
10
11
  const __1 = require("../..");
11
12
  const dbQuery_1 = require("../../query/dbQuery");
12
13
  class InMemoryDB {
@@ -166,7 +167,7 @@ class InMemoryDB {
166
167
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false');
167
168
  const { persistentStoragePath, persistZip } = this.cfg;
168
169
  const started = Date.now();
169
- await fs.emptyDir(persistentStoragePath);
170
+ await (0, nodejs_lib_1._emptyDir)(persistentStoragePath);
170
171
  const transformZip = persistZip ? [(0, node_zlib_1.createGzip)()] : [];
171
172
  let tables = 0;
172
173
  // infinite concurrency for now
@@ -192,9 +193,9 @@ class InMemoryDB {
192
193
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false');
193
194
  const { persistentStoragePath } = this.cfg;
194
195
  const started = Date.now();
195
- await fs.ensureDir(persistentStoragePath);
196
+ await (0, nodejs_lib_1._ensureDir)(persistentStoragePath);
196
197
  this.data = {}; // empty it in the beginning!
197
- const files = (await fs.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'));
198
+ const files = (await fsp.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'));
198
199
  // infinite concurrency for now
199
200
  await (0, js_lib_1.pMap)(files, async (file) => {
200
201
  const fname = `${persistentStoragePath}/${file}`;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dbPipelineBackup = void 0;
4
+ const fs = require("node:fs");
5
+ const fsp = require("node:fs/promises");
4
6
  const node_zlib_1 = require("node:zlib");
5
7
  const js_lib_1 = require("@naturalcycles/js-lib");
6
8
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
9
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
8
- const fs = require("fs-extra");
9
10
  const index_1 = require("../index");
10
11
  /**
11
12
  * Pipeline from input stream(s) to a NDJSON file (optionally gzipped).
@@ -23,7 +24,7 @@ async function dbPipelineBackup(opt) {
23
24
  let { tables } = opt;
24
25
  const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
25
26
  console.log(`>> ${(0, colors_1.dimWhite)('dbPipelineBackup')} started in ${(0, colors_1.grey)(outputDirPath)}...${sinceUpdatedStr}`);
26
- fs.ensureDirSync(outputDirPath);
27
+ (0, nodejs_lib_1._ensureDirSync)(outputDirPath);
27
28
  tables ||= await db.getTables();
28
29
  console.log(`${(0, colors_1.yellow)(tables.length)} ${(0, colors_1.boldWhite)('table(s)')}:\n` + tables.join('\n'));
29
30
  const statsPerTable = {};
@@ -34,16 +35,16 @@ async function dbPipelineBackup(opt) {
34
35
  }
35
36
  const filePath = `${outputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '');
36
37
  const schemaFilePath = `${outputDirPath}/${table}.schema.json`;
37
- if (protectFromOverwrite && (await fs.pathExists(filePath))) {
38
+ if (protectFromOverwrite && (0, nodejs_lib_1._pathExistsSync)(filePath)) {
38
39
  throw new js_lib_1.AppError(`dbPipelineBackup: output file exists: ${filePath}`);
39
40
  }
40
41
  const started = Date.now();
41
42
  let rows = 0;
42
- await fs.ensureFile(filePath);
43
+ (0, nodejs_lib_1._ensureFileSync)(filePath);
43
44
  console.log(`>> ${(0, colors_1.grey)(filePath)} started...`);
44
45
  if (emitSchemaFromDB) {
45
46
  const schema = await db.getTableSchema(table);
46
- await fs.writeJson(schemaFilePath, schema, { spaces: 2 });
47
+ await (0, nodejs_lib_1._writeJsonFile)(schemaFilePath, schema, { spaces: 2 });
47
48
  console.log(`>> ${(0, colors_1.grey)(schemaFilePath)} saved (generated from DB)`);
48
49
  }
49
50
  await (0, nodejs_lib_1._pipeline)([
@@ -66,7 +67,7 @@ async function dbPipelineBackup(opt) {
66
67
  ...(gzip ? [(0, node_zlib_1.createGzip)(zlibOptions)] : []),
67
68
  fs.createWriteStream(filePath),
68
69
  ]);
69
- const { size: sizeBytes } = await fs.stat(filePath);
70
+ const { size: sizeBytes } = await fsp.stat(filePath);
70
71
  const stats = nodejs_lib_1.NDJsonStats.create({
71
72
  tookMillis: Date.now() - started,
72
73
  rows,
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dbPipelineRestore = void 0;
4
+ const fs = require("node:fs");
4
5
  const node_zlib_1 = require("node:zlib");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
7
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
8
  const colors_1 = require("@naturalcycles/nodejs-lib/dist/colors");
8
- const fs = require("fs-extra");
9
9
  /**
10
10
  * Pipeline from NDJSON files in a folder (optionally gzipped) to CommonDB.
11
11
  * Allows to define a mapper and a predicate to map/filter objects between input and output.
@@ -19,7 +19,7 @@ async function dbPipelineRestore(opt) {
19
19
  const onlyTables = opt.tables && new Set(opt.tables);
20
20
  const sinceUpdatedStr = sinceUpdated ? ' since ' + (0, colors_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
21
21
  console.log(`>> ${(0, colors_1.dimWhite)('dbPipelineRestore')} started in ${(0, colors_1.grey)(inputDirPath)}...${sinceUpdatedStr}`);
22
- fs.ensureDirSync(inputDirPath);
22
+ (0, nodejs_lib_1._ensureDirSync)(inputDirPath);
23
23
  const tablesToGzip = new Set();
24
24
  const sizeByTable = {};
25
25
  const statsPerTable = {};
@@ -54,7 +54,7 @@ async function dbPipelineRestore(opt) {
54
54
  console.warn(`${schemaFilePath} does not exist!`);
55
55
  return;
56
56
  }
57
- const schema = await fs.readJson(schemaFilePath);
57
+ const schema = await (0, nodejs_lib_1._readJsonFile)(schemaFilePath);
58
58
  await db.createTable(table, schema, { dropIfExists: true });
59
59
  });
60
60
  }
package/package.json CHANGED
@@ -5,13 +5,12 @@
5
5
  },
6
6
  "dependencies": {
7
7
  "@naturalcycles/js-lib": "^14.116.0",
8
- "@naturalcycles/nodejs-lib": "^12.0.0",
9
- "fs-extra": "^11.1.0"
8
+ "@naturalcycles/nodejs-lib": "^12.0.0"
10
9
  },
11
10
  "devDependencies": {
12
11
  "@naturalcycles/bench-lib": "^1.0.0",
13
12
  "@naturalcycles/dev-lib": "^13.0.0",
14
- "@types/node": "^18.0.3",
13
+ "@types/node": "^20.2.1",
15
14
  "jest": "^29.0.0"
16
15
  },
17
16
  "files": [
@@ -41,7 +40,7 @@
41
40
  "engines": {
42
41
  "node": ">=18.12"
43
42
  },
44
- "version": "8.54.0",
43
+ "version": "8.54.1",
45
44
  "description": "Lowest Common Denominator API to supported Databases",
46
45
  "keywords": [
47
46
  "db",
@@ -1,4 +1,6 @@
1
+ import * as fs from 'node:fs'
1
2
  import { Readable } from 'node:stream'
3
+ import * as fsp from 'node:fs/promises'
2
4
  import { createGzip, createUnzip } from 'node:zlib'
3
5
  import { pMap, ObjectWithId } from '@naturalcycles/js-lib'
4
6
  import {
@@ -7,8 +9,9 @@ import {
7
9
  transformToNDJson,
8
10
  writablePushToArray,
9
11
  _pipeline,
12
+ _ensureDir,
13
+ _pathExists,
10
14
  } from '@naturalcycles/nodejs-lib'
11
- import * as fs from 'fs-extra'
12
15
  import { DBSaveBatchOperation } from '../../db.model'
13
16
  import { FileDBPersistencePlugin } from './file.db.model'
14
17
 
@@ -41,17 +44,17 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
41
44
  async ping(): Promise<void> {}
42
45
 
43
46
  async getTables(): Promise<string[]> {
44
- return (await fs.readdir(this.cfg.storagePath))
47
+ return (await fsp.readdir(this.cfg.storagePath))
45
48
  .filter(f => f.includes('.ndjson'))
46
49
  .map(f => f.split('.ndjson')[0]!)
47
50
  }
48
51
 
49
52
  async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
50
- await fs.ensureDir(this.cfg.storagePath)
53
+ await _ensureDir(this.cfg.storagePath)
51
54
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
52
55
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
53
56
 
54
- if (!(await fs.pathExists(filePath))) return []
57
+ if (!(await _pathExists(filePath))) return []
55
58
 
56
59
  const transformUnzip = this.cfg.gzip ? [createUnzip()] : []
57
60
 
@@ -73,7 +76,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
73
76
  }
74
77
 
75
78
  async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
76
- await fs.ensureDir(this.cfg.storagePath)
79
+ await _ensureDir(this.cfg.storagePath)
77
80
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
78
81
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
79
82
  const transformZip = this.cfg.gzip ? [createGzip()] : []
@@ -1,3 +1,5 @@
1
+ import * as fs from 'node:fs'
2
+ import * as fsp from 'node:fs/promises'
1
3
  import { Readable } from 'node:stream'
2
4
  import { createGzip, createUnzip } from 'node:zlib'
3
5
  import {
@@ -23,9 +25,10 @@ import {
23
25
  transformToNDJson,
24
26
  writablePushToArray,
25
27
  _pipeline,
28
+ _emptyDir,
29
+ _ensureDir,
26
30
  } from '@naturalcycles/nodejs-lib'
27
31
  import { dimGrey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
28
- import * as fs from 'fs-extra'
29
32
  import { CommonDB, DBIncrement, DBPatch, DBTransaction, queryInMemory } from '../..'
30
33
  import {
31
34
  CommonDBCreateOptions,
@@ -279,7 +282,7 @@ export class InMemoryDB implements CommonDB {
279
282
 
280
283
  const started = Date.now()
281
284
 
282
- await fs.emptyDir(persistentStoragePath)
285
+ await _emptyDir(persistentStoragePath)
283
286
 
284
287
  const transformZip = persistZip ? [createGzip()] : []
285
288
  let tables = 0
@@ -314,11 +317,11 @@ export class InMemoryDB implements CommonDB {
314
317
 
315
318
  const started = Date.now()
316
319
 
317
- await fs.ensureDir(persistentStoragePath)
320
+ await _ensureDir(persistentStoragePath)
318
321
 
319
322
  this.data = {} // empty it in the beginning!
320
323
 
321
- const files = (await fs.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'))
324
+ const files = (await fsp.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'))
322
325
 
323
326
  // infinite concurrency for now
324
327
  await pMap(files, async file => {
@@ -1,3 +1,5 @@
1
+ import * as fs from 'node:fs'
2
+ import * as fsp from 'node:fs/promises'
1
3
  import { createGzip, ZlibOptions } from 'node:zlib'
2
4
  import {
3
5
  AppError,
@@ -16,9 +18,12 @@ import {
16
18
  transformTap,
17
19
  transformToNDJson,
18
20
  _pipeline,
21
+ _ensureDirSync,
22
+ _pathExistsSync,
23
+ _ensureFileSync,
24
+ _writeJsonFile,
19
25
  } from '@naturalcycles/nodejs-lib'
20
26
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
21
- import * as fs from 'fs-extra'
22
27
  import { CommonDB } from '../common.db'
23
28
  import { DBQuery } from '../index'
24
29
 
@@ -156,7 +161,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
156
161
  `>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...${sinceUpdatedStr}`,
157
162
  )
158
163
 
159
- fs.ensureDirSync(outputDirPath)
164
+ _ensureDirSync(outputDirPath)
160
165
 
161
166
  tables ||= await db.getTables()
162
167
 
@@ -176,20 +181,20 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
176
181
  const filePath = `${outputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '')
177
182
  const schemaFilePath = `${outputDirPath}/${table}.schema.json`
178
183
 
179
- if (protectFromOverwrite && (await fs.pathExists(filePath))) {
184
+ if (protectFromOverwrite && _pathExistsSync(filePath)) {
180
185
  throw new AppError(`dbPipelineBackup: output file exists: ${filePath}`)
181
186
  }
182
187
 
183
188
  const started = Date.now()
184
189
  let rows = 0
185
190
 
186
- await fs.ensureFile(filePath)
191
+ _ensureFileSync(filePath)
187
192
 
188
193
  console.log(`>> ${grey(filePath)} started...`)
189
194
 
190
195
  if (emitSchemaFromDB) {
191
196
  const schema = await db.getTableSchema(table)
192
- await fs.writeJson(schemaFilePath, schema, { spaces: 2 })
197
+ await _writeJsonFile(schemaFilePath, schema, { spaces: 2 })
193
198
  console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`)
194
199
  }
195
200
 
@@ -214,7 +219,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
214
219
  fs.createWriteStream(filePath),
215
220
  ])
216
221
 
217
- const { size: sizeBytes } = await fs.stat(filePath)
222
+ const { size: sizeBytes } = await fsp.stat(filePath)
218
223
 
219
224
  const stats = NDJsonStats.create({
220
225
  tookMillis: Date.now() - started,
@@ -1,3 +1,4 @@
1
+ import * as fs from 'node:fs'
1
2
  import { createUnzip } from 'node:zlib'
2
3
  import {
3
4
  AsyncMapper,
@@ -8,6 +9,7 @@ import {
8
9
  _passthroughMapper,
9
10
  SavedDBEntity,
10
11
  localTime,
12
+ JsonSchemaObject,
11
13
  } from '@naturalcycles/js-lib'
12
14
  import {
13
15
  NDJsonStats,
@@ -23,9 +25,10 @@ import {
23
25
  transformTap,
24
26
  writableForEach,
25
27
  _pipeline,
28
+ _ensureDirSync,
29
+ _readJsonFile,
26
30
  } from '@naturalcycles/nodejs-lib'
27
31
  import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
28
- import * as fs from 'fs-extra'
29
32
  import { CommonDB } from '../common.db'
30
33
  import { CommonDBSaveOptions } from '../index'
31
34
 
@@ -139,7 +142,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
139
142
  `>> ${dimWhite('dbPipelineRestore')} started in ${grey(inputDirPath)}...${sinceUpdatedStr}`,
140
143
  )
141
144
 
142
- fs.ensureDirSync(inputDirPath)
145
+ _ensureDirSync(inputDirPath)
143
146
 
144
147
  const tablesToGzip = new Set<string>()
145
148
  const sizeByTable: Record<string, number> = {}
@@ -179,7 +182,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
179
182
  return
180
183
  }
181
184
 
182
- const schema = await fs.readJson(schemaFilePath)
185
+ const schema = await _readJsonFile<JsonSchemaObject<any>>(schemaFilePath)
183
186
  await db.createTable(table, schema, { dropIfExists: true })
184
187
  })
185
188
  }