@naturalcycles/db-lib 8.60.0 → 8.60.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.
@@ -26,10 +26,10 @@ class LocalFilePersistencePlugin {
26
26
  .map(f => f.split('.ndjson')[0]);
27
27
  }
28
28
  async loadFile(table) {
29
- await (0, nodejs_lib_1._ensureDir)(this.cfg.storagePath);
29
+ await nodejs_lib_1.fs2.ensureDirAsync(this.cfg.storagePath);
30
30
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
31
31
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
32
- if (!(await (0, nodejs_lib_1._pathExists)(filePath)))
32
+ if (!(await nodejs_lib_1.fs2.pathExistsAsync(filePath)))
33
33
  return [];
34
34
  const transformUnzip = this.cfg.gzip ? [(0, node_zlib_1.createUnzip)()] : [];
35
35
  const rows = [];
@@ -46,7 +46,7 @@ class LocalFilePersistencePlugin {
46
46
  await (0, js_lib_1.pMap)(ops, async (op) => await this.saveFile(op.table, op.rows), { concurrency: 16 });
47
47
  }
48
48
  async saveFile(table, rows) {
49
- await (0, nodejs_lib_1._ensureDir)(this.cfg.storagePath);
49
+ await nodejs_lib_1.fs2.ensureDirAsync(this.cfg.storagePath);
50
50
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`;
51
51
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`;
52
52
  const transformZip = this.cfg.gzip ? [(0, node_zlib_1.createGzip)()] : [];
@@ -167,7 +167,7 @@ class InMemoryDB {
167
167
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false');
168
168
  const { persistentStoragePath, persistZip } = this.cfg;
169
169
  const started = Date.now();
170
- await (0, nodejs_lib_1._emptyDir)(persistentStoragePath);
170
+ await nodejs_lib_1.fs2.emptyDirAsync(persistentStoragePath);
171
171
  const transformZip = persistZip ? [(0, node_zlib_1.createGzip)()] : [];
172
172
  let tables = 0;
173
173
  // infinite concurrency for now
@@ -193,7 +193,7 @@ class InMemoryDB {
193
193
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false');
194
194
  const { persistentStoragePath } = this.cfg;
195
195
  const started = Date.now();
196
- await (0, nodejs_lib_1._ensureDir)(persistentStoragePath);
196
+ await nodejs_lib_1.fs2.ensureDirAsync(persistentStoragePath);
197
197
  this.data = {}; // empty it in the beginning!
198
198
  const files = (await promises_1.default.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'));
199
199
  // infinite concurrency for now
@@ -31,7 +31,7 @@ export interface CommonDB {
31
31
  * Order of items returned is not guaranteed to match order of ids.
32
32
  * (Such limitation exists because Datastore doesn't support it).
33
33
  */
34
- getByIds: <ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: CommonDBOptions) => Promise<ROW[]>;
34
+ getByIds: <ROW extends ObjectWithId>(table: string, ids: string[], opt?: CommonDBOptions) => Promise<ROW[]>;
35
35
  /**
36
36
  * Order by 'id' is not supported by all implementations (for example, Datastore doesn't support it).
37
37
  */
@@ -23,7 +23,7 @@ async function dbPipelineBackup(opt) {
23
23
  const gzip = opt.gzip !== false; // default to true
24
24
  let { tables } = opt;
25
25
  console.log(`>> ${(0, nodejs_lib_1.dimWhite)('dbPipelineBackup')} started in ${(0, nodejs_lib_1.grey)(outputDirPath)}...`);
26
- (0, nodejs_lib_1._ensureDirSync)(outputDirPath);
26
+ nodejs_lib_1.fs2.ensureDir(outputDirPath);
27
27
  tables ||= await db.getTables();
28
28
  console.log(`${(0, nodejs_lib_1.yellow)(tables.length)} ${(0, nodejs_lib_1.boldWhite)('table(s)')}:\n` + tables.join('\n'));
29
29
  const statsPerTable = {};
@@ -46,16 +46,16 @@ async function dbPipelineBackup(opt) {
46
46
  }
47
47
  const filePath = `${outputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '');
48
48
  const schemaFilePath = `${outputDirPath}/${table}.schema.json`;
49
- if (protectFromOverwrite && (0, nodejs_lib_1._pathExistsSync)(filePath)) {
49
+ if (protectFromOverwrite && nodejs_lib_1.fs2.pathExists(filePath)) {
50
50
  throw new js_lib_1.AppError(`dbPipelineBackup: output file exists: ${filePath}`);
51
51
  }
52
52
  const started = Date.now();
53
53
  let rows = 0;
54
- (0, nodejs_lib_1._ensureFileSync)(filePath);
54
+ nodejs_lib_1.fs2.ensureFile(filePath);
55
55
  // console.log(`>> ${grey(filePath)} started...`)
56
56
  if (emitSchemaFromDB) {
57
57
  const schema = await db.getTableSchema(table);
58
- await (0, nodejs_lib_1._writeJson)(schemaFilePath, schema, { spaces: 2 });
58
+ await nodejs_lib_1.fs2.writeJsonAsync(schemaFilePath, schema, { spaces: 2 });
59
59
  console.log(`>> ${(0, nodejs_lib_1.grey)(schemaFilePath)} saved (generated from DB)`);
60
60
  }
61
61
  await (0, nodejs_lib_1._pipeline)([
@@ -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, nodejs_lib_1.grey)((0, js_lib_1.localTime)(sinceUpdated).toPretty()) : '';
21
21
  console.log(`>> ${(0, nodejs_lib_1.dimWhite)('dbPipelineRestore')} started in ${(0, nodejs_lib_1.grey)(inputDirPath)}...${sinceUpdatedStr}`);
22
- (0, nodejs_lib_1._ensureDirSync)(inputDirPath);
22
+ nodejs_lib_1.fs2.ensureDir(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 (0, nodejs_lib_1._readJson)(schemaFilePath);
57
+ const schema = await nodejs_lib_1.fs2.readJsonAsync(schemaFilePath);
58
58
  await db.createTable(table, schema, { dropIfExists: true });
59
59
  });
60
60
  }
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "engines": {
41
41
  "node": ">=18.12"
42
42
  },
43
- "version": "8.60.0",
43
+ "version": "8.60.1",
44
44
  "description": "Lowest Common Denominator API to supported Databases",
45
45
  "keywords": [
46
46
  "db",
@@ -9,8 +9,7 @@ import {
9
9
  transformToNDJson,
10
10
  writablePushToArray,
11
11
  _pipeline,
12
- _ensureDir,
13
- _pathExists,
12
+ fs2,
14
13
  } from '@naturalcycles/nodejs-lib'
15
14
  import { DBSaveBatchOperation } from '../../db.model'
16
15
  import { FileDBPersistencePlugin } from './file.db.model'
@@ -50,11 +49,11 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
50
49
  }
51
50
 
52
51
  async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
53
- await _ensureDir(this.cfg.storagePath)
52
+ await fs2.ensureDirAsync(this.cfg.storagePath)
54
53
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
55
54
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
56
55
 
57
- if (!(await _pathExists(filePath))) return []
56
+ if (!(await fs2.pathExistsAsync(filePath))) return []
58
57
 
59
58
  const transformUnzip = this.cfg.gzip ? [createUnzip()] : []
60
59
 
@@ -76,7 +75,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
76
75
  }
77
76
 
78
77
  async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
79
- await _ensureDir(this.cfg.storagePath)
78
+ await fs2.ensureDirAsync(this.cfg.storagePath)
80
79
  const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
81
80
  const filePath = `${this.cfg.storagePath}/${table}.${ext}`
82
81
  const transformZip = this.cfg.gzip ? [createGzip()] : []
@@ -25,10 +25,9 @@ import {
25
25
  transformToNDJson,
26
26
  writablePushToArray,
27
27
  _pipeline,
28
- _emptyDir,
29
- _ensureDir,
30
28
  dimGrey,
31
29
  yellow,
30
+ fs2,
32
31
  } from '@naturalcycles/nodejs-lib'
33
32
  import { CommonDB, DBIncrement, DBPatch, DBTransaction, queryInMemory } from '../..'
34
33
  import {
@@ -283,7 +282,7 @@ export class InMemoryDB implements CommonDB {
283
282
 
284
283
  const started = Date.now()
285
284
 
286
- await _emptyDir(persistentStoragePath)
285
+ await fs2.emptyDirAsync(persistentStoragePath)
287
286
 
288
287
  const transformZip = persistZip ? [createGzip()] : []
289
288
  let tables = 0
@@ -318,7 +317,7 @@ export class InMemoryDB implements CommonDB {
318
317
 
319
318
  const started = Date.now()
320
319
 
321
- await _ensureDir(persistentStoragePath)
320
+ await fs2.ensureDirAsync(persistentStoragePath)
322
321
 
323
322
  this.data = {} // empty it in the beginning!
324
323
 
package/src/common.db.ts CHANGED
@@ -50,7 +50,7 @@ export interface CommonDB {
50
50
  */
51
51
  getByIds: <ROW extends ObjectWithId>(
52
52
  table: string,
53
- ids: ROW['id'][],
53
+ ids: string[],
54
54
  opt?: CommonDBOptions,
55
55
  ) => Promise<ROW[]>
56
56
 
@@ -20,14 +20,11 @@ import {
20
20
  transformTap,
21
21
  transformToNDJson,
22
22
  _pipeline,
23
- _ensureDirSync,
24
- _pathExistsSync,
25
- _ensureFileSync,
26
- _writeJson,
27
23
  boldWhite,
28
24
  dimWhite,
29
25
  grey,
30
26
  yellow,
27
+ fs2,
31
28
  } from '@naturalcycles/nodejs-lib'
32
29
  import { CommonDB } from '../common.db'
33
30
  import { DBQuery } from '../index'
@@ -180,7 +177,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
180
177
 
181
178
  console.log(`>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...`)
182
179
 
183
- _ensureDirSync(outputDirPath)
180
+ fs2.ensureDir(outputDirPath)
184
181
 
185
182
  tables ||= await db.getTables()
186
183
 
@@ -213,20 +210,20 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
213
210
  const filePath = `${outputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '')
214
211
  const schemaFilePath = `${outputDirPath}/${table}.schema.json`
215
212
 
216
- if (protectFromOverwrite && _pathExistsSync(filePath)) {
213
+ if (protectFromOverwrite && fs2.pathExists(filePath)) {
217
214
  throw new AppError(`dbPipelineBackup: output file exists: ${filePath}`)
218
215
  }
219
216
 
220
217
  const started = Date.now()
221
218
  let rows = 0
222
219
 
223
- _ensureFileSync(filePath)
220
+ fs2.ensureFile(filePath)
224
221
 
225
222
  // console.log(`>> ${grey(filePath)} started...`)
226
223
 
227
224
  if (emitSchemaFromDB) {
228
225
  const schema = await db.getTableSchema(table)
229
- await _writeJson(schemaFilePath, schema, { spaces: 2 })
226
+ await fs2.writeJsonAsync(schemaFilePath, schema, { spaces: 2 })
230
227
  console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`)
231
228
  }
232
229
 
@@ -25,12 +25,11 @@ import {
25
25
  transformTap,
26
26
  writableForEach,
27
27
  _pipeline,
28
- _ensureDirSync,
29
- _readJson,
30
28
  boldWhite,
31
29
  dimWhite,
32
30
  grey,
33
31
  yellow,
32
+ fs2,
34
33
  } from '@naturalcycles/nodejs-lib'
35
34
  import { CommonDB } from '../common.db'
36
35
  import { CommonDBSaveOptions } from '../index'
@@ -145,7 +144,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
145
144
  `>> ${dimWhite('dbPipelineRestore')} started in ${grey(inputDirPath)}...${sinceUpdatedStr}`,
146
145
  )
147
146
 
148
- _ensureDirSync(inputDirPath)
147
+ fs2.ensureDir(inputDirPath)
149
148
 
150
149
  const tablesToGzip = new Set<string>()
151
150
  const sizeByTable: Record<string, number> = {}
@@ -185,7 +184,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
185
184
  return
186
185
  }
187
186
 
188
- const schema = await _readJson<JsonSchemaObject<any>>(schemaFilePath)
187
+ const schema = await fs2.readJsonAsync<JsonSchemaObject<any>>(schemaFilePath)
189
188
  await db.createTable(table, schema, { dropIfExists: true })
190
189
  })
191
190
  }