Dev Helpers
← Back

Find documents with a single revision

Find documents with a single revision. Parse newline-delimited records from a readable stream before applying the transformation.

Open this example in the interactive library

Source code

import { Readable } from "node:stream";
import { createInterface } from "node:readline";
const input = Readable.from([
  '{"doc":"guide","rev":1,"author":"Ada"}\n',
  '{"doc":"api","rev":1,"author":"Leo"}\n',
  '{"doc":"guide","rev":3,"author":"Kim"}\n',
  '{"doc":"guide","rev":4,"author":"Ada"}\n',
]);
const rows = [];
for await (const line of createInterface({ input, crlfDelay: Infinity })) {
  rows.push(JSON.parse(line));
}
const result = [...new Set(rows.map((r) => r.doc))].filter(
  (doc) => rows.filter((r) => r.doc === doc).length === 1,
);
console.log(JSON.stringify(result, null, 2));

Sample output

[
  "api"
]