Created
March 19, 2019 12:36
-
-
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 π
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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