Skip to content

Commit

Permalink
feat: sinceUpdated for each table (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: kirillgroshkov <[email protected]>
  • Loading branch information
flo-monin and kirillgroshkov authored Oct 3, 2023
1 parent 1e8f45c commit cbdc18f
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/pipeline/dbPipelineBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
pMap,
_passthroughMapper,
localTime,
UnixTimestampNumber,
StringMap,
} from '@naturalcycles/js-lib'
import {
NDJsonStats,
Expand Down Expand Up @@ -65,10 +67,14 @@ export interface DBPipelineBackupOptions extends TransformLogProgressOptions {

/**
* If set - will do "incremental backup" (not full), only for entities that updated >= `sinceUpdated`
*
* @default undefined
*/
sinceUpdated?: number
sinceUpdated?: UnixTimestampNumber

/**
* Map for each table a `sinceUpdated` timestamp, or `undefined`.
* If set - will do "incremental backup" (not full), only for entities that updated >= `sinceUpdated` (on a per table basis)
*/
sinceUpdatedPerTable?: StringMap<UnixTimestampNumber>

/**
* Directory path to store dumped files. Will create `${tableName}.ndjson` (or .ndjson.gz if gzip=true) files.
Expand Down Expand Up @@ -100,7 +106,7 @@ export interface DBPipelineBackupOptions extends TransformLogProgressOptions {
* @default `{}`
* Default mappers will be "passthroughMapper" (pass all data as-is).
*/
mapperPerTable?: Record<string, AsyncMapper>
mapperPerTable?: StringMap<AsyncMapper>

/**
* You can alter default `transformMapOptions` here.
Expand Down Expand Up @@ -143,7 +149,6 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
db,
concurrency = 16,
limit = 0,
sinceUpdated,
outputDirPath,
protectFromOverwrite = false,
zlibOptions,
Expand All @@ -158,11 +163,7 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND

let { tables } = opt

const sinceUpdatedStr = sinceUpdated ? ' since ' + grey(localTime(sinceUpdated).toPretty()) : ''

console.log(
`>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...${sinceUpdatedStr}`,
)
console.log(`>> ${dimWhite('dbPipelineBackup')} started in ${grey(outputDirPath)}...`)

_ensureDirSync(outputDirPath)

Expand All @@ -175,6 +176,14 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
await pMap(
tables,
async table => {
const sinceUpdated = opt.sinceUpdatedPerTable?.[table] || opt.sinceUpdated

const sinceUpdatedStr = sinceUpdated
? ' since ' + grey(localTime(sinceUpdated).toPretty())
: ''

console.log(`>> ${grey(table)}${sinceUpdatedStr}`)

let q = DBQuery.create(table).limit(limit)

if (sinceUpdated) {
Expand Down

0 comments on commit cbdc18f

Please sign in to comment.