Skip to content

Instantly share code, notes, and snippets.

@radex
Created March 19, 2019 12:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radex/a0a27761ac348f4a5552ecaf227d500c to your computer and use it in GitHub Desktop.
Save radex/a0a27761ac348f4a5552ecaf227d500c to your computer and use it in GitHub Desktop.
Example Sync logger and censor for WatermelonDB Sync https://github.com/Nozbe/WatermelonDB/blob/master/docs/Advanced/Sync.md πŸ‰
// @flow
import { map, is } from 'rambdax'
import type { DirtyRaw } from '@nozbe/watermelondb/RawRecord'
import type { SyncLog } from '@nozbe/watermelondb/sync'
// beginning, end, length
const censorValue = (value: string): string =>
`${value.slice(0, 2)}***${value.slice(-2)}(${value.length})`
const shouldCensorKey = (key: string): boolean =>
key !== 'id' && !key.endsWith('_id') && key !== '_status' && key !== '_changed'
const censorRaw: DirtyRaw => DirtyRaw = map((value, key) =>
shouldCensorKey(key) && is(String, value) ? censorValue(value) : value,
)
const censorConflicts = map(map(censorRaw))
const censorLog = (log: SyncLog): SyncLog => ({
...log,
...(log.resolvedConflicts ? { resolvedConflicts: censorConflicts(log.resolvedConflicts) } : {}),
})
const censorLogs = map(censorLog)
export default class SyncLogger {
_logs: SyncLog[] = []
newLog(): SyncLog {
const log: SyncLog = {}
this._logs.push(log)
return log
}
get logs(): SyncLog[] {
// censor logs before viewing them
return censorLogs(this._logs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment