Skip to content

Commit

Permalink
fix: deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 21, 2023
1 parent aa0a196 commit 9806cc3
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 571 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
},
"dependencies": {
"@naturalcycles/js-lib": "^14.116.0",
"@naturalcycles/nodejs-lib": "^12.0.0",
"fs-extra": "^11.1.0"
"@naturalcycles/nodejs-lib": "^12.0.0"
},
"devDependencies": {
"@naturalcycles/bench-lib": "^1.0.0",
"@naturalcycles/dev-lib": "^13.0.0",
"@types/node": "^18.0.3",
"@types/node": "^20.2.1",
"jest": "^29.0.0"
},
"files": [
Expand Down
13 changes: 8 additions & 5 deletions src/adapter/file/localFile.persistence.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from 'node:fs'
import { Readable } from 'node:stream'
import * as fsp from 'node:fs/promises'
import { createGzip, createUnzip } from 'node:zlib'
import { pMap, ObjectWithId } from '@naturalcycles/js-lib'
import {
Expand All @@ -7,8 +9,9 @@ import {
transformToNDJson,
writablePushToArray,
_pipeline,
_ensureDir,
_pathExists,
} from '@naturalcycles/nodejs-lib'
import * as fs from 'fs-extra'
import { DBSaveBatchOperation } from '../../db.model'
import { FileDBPersistencePlugin } from './file.db.model'

Expand Down Expand Up @@ -41,17 +44,17 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
async ping(): Promise<void> {}

async getTables(): Promise<string[]> {
return (await fs.readdir(this.cfg.storagePath))
return (await fsp.readdir(this.cfg.storagePath))
.filter(f => f.includes('.ndjson'))
.map(f => f.split('.ndjson')[0]!)
}

async loadFile<ROW extends ObjectWithId>(table: string): Promise<ROW[]> {
await fs.ensureDir(this.cfg.storagePath)
await _ensureDir(this.cfg.storagePath)
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
const filePath = `${this.cfg.storagePath}/${table}.${ext}`

if (!(await fs.pathExists(filePath))) return []
if (!(await _pathExists(filePath))) return []

const transformUnzip = this.cfg.gzip ? [createUnzip()] : []

Expand All @@ -73,7 +76,7 @@ export class LocalFilePersistencePlugin implements FileDBPersistencePlugin {
}

async saveFile<ROW extends ObjectWithId>(table: string, rows: ROW[]): Promise<void> {
await fs.ensureDir(this.cfg.storagePath)
await _ensureDir(this.cfg.storagePath)
const ext = `ndjson${this.cfg.gzip ? '.gz' : ''}`
const filePath = `${this.cfg.storagePath}/${table}.${ext}`
const transformZip = this.cfg.gzip ? [createGzip()] : []
Expand Down
11 changes: 7 additions & 4 deletions src/adapter/inmemory/inMemory.db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import { Readable } from 'node:stream'
import { createGzip, createUnzip } from 'node:zlib'
import {
Expand All @@ -23,9 +25,10 @@ import {
transformToNDJson,
writablePushToArray,
_pipeline,
_emptyDir,
_ensureDir,
} from '@naturalcycles/nodejs-lib'
import { dimGrey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
import * as fs from 'fs-extra'
import { CommonDB, DBIncrement, DBPatch, DBTransaction, queryInMemory } from '../..'
import {
CommonDBCreateOptions,
Expand Down Expand Up @@ -279,7 +282,7 @@ export class InMemoryDB implements CommonDB {

const started = Date.now()

await fs.emptyDir(persistentStoragePath)
await _emptyDir(persistentStoragePath)

const transformZip = persistZip ? [createGzip()] : []
let tables = 0
Expand Down Expand Up @@ -314,11 +317,11 @@ export class InMemoryDB implements CommonDB {

const started = Date.now()

await fs.ensureDir(persistentStoragePath)
await _ensureDir(persistentStoragePath)

this.data = {} // empty it in the beginning!

const files = (await fs.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'))
const files = (await fsp.readdir(persistentStoragePath)).filter(f => f.includes('.ndjson'))

// infinite concurrency for now
await pMap(files, async file => {
Expand Down
17 changes: 11 additions & 6 deletions src/pipeline/dbPipelineBackup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import { createGzip, ZlibOptions } from 'node:zlib'
import {
AppError,
Expand All @@ -16,9 +18,12 @@ import {
transformTap,
transformToNDJson,
_pipeline,
_ensureDirSync,
_pathExistsSync,
_ensureFileSync,
_writeJsonFile,
} from '@naturalcycles/nodejs-lib'
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
import * as fs from 'fs-extra'
import { CommonDB } from '../common.db'
import { DBQuery } from '../index'

Expand Down Expand Up @@ -156,7 +161,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
`>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...${sinceUpdatedStr}`,
)

fs.ensureDirSync(outputDirPath)
_ensureDirSync(outputDirPath)

tables ||= await db.getTables()

Expand All @@ -176,20 +181,20 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
const filePath = `${outputDirPath}/${table}.ndjson` + (gzip ? '.gz' : '')
const schemaFilePath = `${outputDirPath}/${table}.schema.json`

if (protectFromOverwrite && (await fs.pathExists(filePath))) {
if (protectFromOverwrite && _pathExistsSync(filePath)) {
throw new AppError(`dbPipelineBackup: output file exists: ${filePath}`)
}

const started = Date.now()
let rows = 0

await fs.ensureFile(filePath)
_ensureFileSync(filePath)

console.log(`>> ${grey(filePath)} started...`)

if (emitSchemaFromDB) {
const schema = await db.getTableSchema(table)
await fs.writeJson(schemaFilePath, schema, { spaces: 2 })
await _writeJsonFile(schemaFilePath, schema, { spaces: 2 })
console.log(`>> ${grey(schemaFilePath)} saved (generated from DB)`)
}

Expand All @@ -214,7 +219,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
fs.createWriteStream(filePath),
])

const { size: sizeBytes } = await fs.stat(filePath)
const { size: sizeBytes } = await fsp.stat(filePath)

const stats = NDJsonStats.create({
tookMillis: Date.now() - started,
Expand Down
9 changes: 6 additions & 3 deletions src/pipeline/dbPipelineRestore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'node:fs'
import { createUnzip } from 'node:zlib'
import {
AsyncMapper,
Expand All @@ -8,6 +9,7 @@ import {
_passthroughMapper,
SavedDBEntity,
localTime,
JsonSchemaObject,
} from '@naturalcycles/js-lib'
import {
NDJsonStats,
Expand All @@ -23,9 +25,10 @@ import {
transformTap,
writableForEach,
_pipeline,
_ensureDirSync,
_readJsonFile,
} from '@naturalcycles/nodejs-lib'
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/dist/colors'
import * as fs from 'fs-extra'
import { CommonDB } from '../common.db'
import { CommonDBSaveOptions } from '../index'

Expand Down Expand Up @@ -139,7 +142,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
`>> ${dimWhite('dbPipelineRestore')} started in ${grey(inputDirPath)}...${sinceUpdatedStr}`,
)

fs.ensureDirSync(inputDirPath)
_ensureDirSync(inputDirPath)

const tablesToGzip = new Set<string>()
const sizeByTable: Record<string, number> = {}
Expand Down Expand Up @@ -179,7 +182,7 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
return
}

const schema = await fs.readJson(schemaFilePath)
const schema = await _readJsonFile<JsonSchemaObject<any>>(schemaFilePath)
await db.createTable(table, schema, { dropIfExists: true })
})
}
Expand Down
Loading

0 comments on commit 9806cc3

Please sign in to comment.