forked from valkey-io/valkey-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndjson-writer.js
More file actions
23 lines (17 loc) · 760 Bytes
/
ndjson-writer.js
File metadata and controls
23 lines (17 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import fs from "fs"
import path from "path"
import * as R from "ramda"
const dayStr = ts => new Date(ts).toISOString().slice(0, 10).replace(/-/g, "")
export const makeNdjsonWriter = ({ dataDir, filePrefix }) => {
const fileFor = ts => path.join(dataDir, `${filePrefix}_${dayStr(ts, filePrefix)}.ndjson`)
const appendRows = async (rows = []) => {
if (R.isEmpty(rows.length)) return
const ts = Number.isFinite(rows[0]?.ts) ? rows[0].ts : Date.now()
const file = fileFor(ts)
await fs.promises.mkdir(path.dirname(file), { recursive: true })
const lines = rows.map(r => JSON.stringify(r)).join("\n").concat("\n")
await fs.promises.appendFile(file, lines, "utf8")
}
const close = async () => {}
return { appendRows, close }
}