@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.
- package/dist/adapter/file/localFile.persistence.plugin.js +3 -3
- package/dist/adapter/inmemory/inMemory.db.js +2 -2
- package/dist/common.db.d.ts +1 -1
- package/dist/pipeline/dbPipelineBackup.js +4 -4
- package/dist/pipeline/dbPipelineRestore.js +2 -2
- package/package.json +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +4 -5
- package/src/adapter/inmemory/inMemory.db.ts +3 -4
- package/src/common.db.ts +1 -1
- package/src/pipeline/dbPipelineBackup.ts +5 -8
- package/src/pipeline/dbPipelineRestore.ts +3 -4
|
@@ -26,10 +26,10 @@ class LocalFilePersistencePlugin {
|
|
|
26
26
|
.map(f => f.split('.ndjson')[0]);
|
|
27
27
|
}
|
|
28
28
|
async loadFile(table) {
|
|
29
|
-
await
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
package/dist/common.db.d.ts
CHANGED
|
@@ -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:
|
|
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
|
-
|
|
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 &&
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
@@ -9,8 +9,7 @@ import {
|
|
|
9
9
|
transformToNDJson,
|
|
10
10
|
writablePushToArray,
|
|
11
11
|
_pipeline,
|
|
12
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
320
|
+
await fs2.ensureDirAsync(persistentStoragePath)
|
|
322
321
|
|
|
323
322
|
this.data = {} // empty it in the beginning!
|
|
324
323
|
|
package/src/common.db.ts
CHANGED
|
@@ -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
|
-
|
|
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 &&
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
187
|
+
const schema = await fs2.readJsonAsync<JsonSchemaObject<any>>(schemaFilePath)
|
|
189
188
|
await db.createTable(table, schema, { dropIfExists: true })
|
|
190
189
|
})
|
|
191
190
|
}
|