Skip to content

Commit

Permalink
fix: use branded UnixTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Nov 9, 2024
1 parent b654ce0 commit 33750b2
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 116 deletions.
7 changes: 4 additions & 3 deletions src/adapter/file/file.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
generateJsonSchemaFromData,
JsonSchemaRootObject,
ObjectWithId,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import { dimGrey, readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib'
import {
Expand Down Expand Up @@ -240,14 +241,14 @@ export class FileDB extends BaseCommonDB implements CommonDB {
return rows
}

private logStarted(op: string): number {
private logStarted(op: string): UnixTimestampMillis {
if (this.cfg.logStarted) {
this.cfg.logger?.log(`>> ${op}`)
}
return Date.now()
return Date.now() as UnixTimestampMillis
}

private logFinished(started: number, op: string): void {
private logFinished(started: UnixTimestampMillis, op: string): void {
if (!this.cfg.logFinished) return
this.cfg.logger?.log(`<< ${op} ${dimGrey(`in ${_since(started)}`)}`)
}
Expand Down
5 changes: 3 additions & 2 deletions src/adapter/inmemory/inMemory.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ObjectWithId,
pMap,
StringMap,
UnixTimestampMillis,
} from '@naturalcycles/js-lib'
import {
_pipeline,
Expand Down Expand Up @@ -314,7 +315,7 @@ export class InMemoryDB implements CommonDB {
_assert(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false')
const { persistentStoragePath, persistZip } = this.cfg

const started = Date.now()
const started = Date.now() as UnixTimestampMillis

await fs2.emptyDirAsync(persistentStoragePath)

Expand Down Expand Up @@ -343,7 +344,7 @@ export class InMemoryDB implements CommonDB {
_assert(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false')
const { persistentStoragePath } = this.cfg

const started = Date.now()
const started = Date.now() as UnixTimestampMillis

await fs2.ensureDirAsync(persistentStoragePath)

Expand Down
4 changes: 2 additions & 2 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CommonLogger,
ErrorMode,
Promisable,
UnixTimestampNumber,
UnixTimestamp,
ZodError,
ZodSchema,
} from '@naturalcycles/js-lib'
Expand Down Expand Up @@ -257,7 +257,7 @@ export interface CommonDaoReadOptions extends CommonDaoOptions {
* If provided (and supported by the DB) - will read the data at that point in time (aka "Time machine" feature).
* This feature is named PITR (point-in-time-recovery) query in Datastore.
*/
readAt?: UnixTimestampNumber
readAt?: UnixTimestamp
}

export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseDBEntity>
Expand Down
14 changes: 7 additions & 7 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
pMap,
SKIP,
StringMap,
UnixTimestampMillisNumber,
UnixTimestampMillis,
Unsaved,
ZodSchema,
ZodValidationError,
Expand Down Expand Up @@ -1326,7 +1326,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
}
}

protected logResult(started: number, op: string, res: any, table: string): void {
protected logResult(started: UnixTimestampMillis, op: string, res: any, table: string): void {
if (!this.cfg.logLevel) return

let logRes: any
Expand All @@ -1349,19 +1349,19 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
this.cfg.logger?.log(`<< ${table}.${op}: ${logRes} in ${_since(started)}`, ...args)
}

protected logSaveResult(started: number, op: string, table: string): void {
protected logSaveResult(started: UnixTimestampMillis, op: string, table: string): void {
if (!this.cfg.logLevel) return
this.cfg.logger?.log(`<< ${table}.${op} in ${_since(started)}`)
}

protected logStarted(op: string, table: string, force = false): UnixTimestampMillisNumber {
protected logStarted(op: string, table: string, force = false): UnixTimestampMillis {
if (this.cfg.logStarted || force) {
this.cfg.logger?.log(`>> ${table}.${op}`)
}
return Date.now()
return Date.now() as UnixTimestampMillis
}

protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillisNumber {
protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillis {
if (this.cfg.logStarted) {
const args: any[] = [`>> ${table}.${op}`]
if (Array.isArray(items)) {
Expand All @@ -1379,7 +1379,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
this.cfg.logger?.log(...args)
}

return Date.now()
return Date.now() as UnixTimestampMillis
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/db.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectWithId, UnixTimestampNumber } from '@naturalcycles/js-lib'
import { ObjectWithId, UnixTimestamp } from '@naturalcycles/js-lib'
import { CommonDB } from './common.db'

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface CommonDBReadOptions extends CommonDBOptions {
* If provided (and supported by the DB) - will read the data at that point in time (aka "Time machine" feature).
* This feature is named PITR (point-in-time-recovery) query in Datastore.
*/
readAt?: UnixTimestampNumber
readAt?: UnixTimestamp
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/kv/commonKeyValueDB.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Integer, UnixTimestampNumber } from '@naturalcycles/js-lib'
import { Integer, UnixTimestamp } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { CommonDBCreateOptions } from '../db.model'

Expand Down Expand Up @@ -72,7 +72,7 @@ export interface CommonKeyValueDBSaveBatchOptions {
* If set (and if it's implemented by the driver) - will set expiry TTL for each key of the batch.
* E.g EXAT in Redis.
*/
expireAt?: UnixTimestampNumber
expireAt?: UnixTimestamp
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/kv/commonKeyValueDaoMemoCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { AsyncMemoCache, localTime, MISS, NumberOfSeconds } from '@naturalcycles/js-lib'
import {
AsyncMemoCache,
localTime,
MISS,
NumberOfSeconds,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import { CommonKeyValueDao } from './commonKeyValueDao'

export interface CommonKeyValueDaoMemoCacheCfg<VALUE> {
Expand Down Expand Up @@ -26,7 +32,9 @@ export class CommonKeyValueDaoMemoCache<VALUE> implements AsyncMemoCache<string,
}

async set(k: string, v: VALUE): Promise<void> {
const opt = this.cfg.ttl ? { expireAt: localTime.nowUnix() + this.cfg.ttl } : undefined
const opt = this.cfg.ttl
? { expireAt: (localTime.nowUnix() + this.cfg.ttl) as UnixTimestamp }
: undefined

await this.cfg.dao.save(k, v, opt)
}
Expand Down
6 changes: 3 additions & 3 deletions src/pipeline/dbPipelineBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
localTime,
pMap,
StringMap,
UnixTimestampNumber,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import {
_pipeline,
Expand Down Expand Up @@ -61,13 +61,13 @@ export interface DBPipelineBackupOptions extends TransformLogProgressOptions {
/**
* If set - will do "incremental backup" (not full), only for entities that updated >= `sinceUpdated`
*/
sinceUpdated?: UnixTimestampNumber
sinceUpdated?: UnixTimestamp

/**
* 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>
sinceUpdatedPerTable?: StringMap<UnixTimestamp>

/**
* By default, dbPipelineBackup creates a Query based on sinceUpdated.
Expand Down
3 changes: 2 additions & 1 deletion src/pipeline/dbPipelineCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ErrorMode,
localTime,
pMap,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import {
_pipeline,
Expand Down Expand Up @@ -68,7 +69,7 @@ export interface DBPipelineCopyOptions extends TransformLogProgressOptions {
*
* @default undefined
*/
sinceUpdated?: number
sinceUpdated?: UnixTimestamp

/**
* Optionally you can provide mapper that is going to run for each table.
Expand Down
3 changes: 2 additions & 1 deletion src/pipeline/dbPipelineRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
JsonSchemaObject,
localTime,
pMap,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import {
_pipeline,
Expand Down Expand Up @@ -80,7 +81,7 @@ export interface DBPipelineRestoreOptions extends TransformLogProgressOptions {
*
* @default undefined
*/
sinceUpdated?: number
sinceUpdated?: UnixTimestamp

/**
* @default false
Expand Down
10 changes: 8 additions & 2 deletions src/testing/test.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { _range, BaseDBEntity, jsonSchema, JsonSchemaObject } from '@naturalcycles/js-lib'
import {
_range,
BaseDBEntity,
jsonSchema,
JsonSchemaObject,
UnixTimestamp,
} from '@naturalcycles/js-lib'
import {
baseDBEntitySchema,
binarySchema,
Expand All @@ -8,7 +14,7 @@ import {
stringSchema,
} from '@naturalcycles/nodejs-lib'

const MOCK_TS_2018_06_21 = 1529539200
const MOCK_TS_2018_06_21 = 1529539200 as UnixTimestamp

export const TEST_TABLE = 'TEST_TABLE'

Expand Down
Loading

0 comments on commit 33750b2

Please sign in to comment.