Search code examples
javascriptnode.jstypescripttypeorm

TypeORM Postgres stream not outputting anything


I'm using TypeORM=^0.2.45 and pg-query-stream=^4.2.3, but I can't seem to be getting any output from the stream:

const stream = await conn
      .getRepository(Entity)
      .createQueryBuilder("e")
      .stream();
stream.on("data", (x) => {
  console.log(123);
})
stream.on("result", (x) => {
  console.log(1234);
})

How do I get a stream to work? No output, nothing. If I do a simple getMany instead of a stream, at least I get something. The stream doesn't even execute anything; not a single log.


Solution

  • Got it working. Had to dig into the source code and found out you actually need to use the pg-stream-query package:

    import { stringify } from "JSONStream";
    
    function mapSync(sync) {
      return through(function write(data) {
        let mappedData;
        try {
          mappedData = sync(data);
        } catch (e) {
          return this.emit("error", e);
        }
        if (mappedData !== undefined) {
          this.emit("data", mappedData);
        }
      });
    }
    
    
    const stream = await conn
          .getRepository(Entity)
          .createQueryBuilder("e")
          .stream();
    stream.pipe(stringify()).pipe(mapSync((data) => {
        
        data = JSON.parse(x.substring(x.indexOf("{")));
        return data;
    }));
    

    mapSync is from another library that pg-query-stream indreictly depends on.