rdfjs / N3.js

Lightning fast, spec-compatible, streaming RDF for JavaScript
http://rdf.js.org/N3.js/
Other
676 stars 127 forks source link

How to use streams to write quads (without a ReadStream) #379

Closed flyon closed 3 months ago

flyon commented 3 months ago

In the documentation and the examples I'm finding the StreamWriter is always combined with a StreamReader.

But what if I already have my quads and I want to write them to a file with a stream? I have the following working code, but I want to switch it streams (and I'm happy to switch to another format then N3 since that's not supported for streaming).

function writeQuadsToPath(quads,path)
    let writeStream = fs.createWriteStream(path, {flags: 'w'});
    const writer = new N3.Writer(writeStream, {
      end: false,
      prefixes,
    }); 
    return new Promise<boolean>((resolve, reject) => {
      quads.forEach((quad) => {
        writer.addQuad(quad);
      });
      writer.end((error) => {
        if (error) {
          console.error('Error writing data to file:', error);
          reject(false);
        }
        resolve(true);
      });
    });
}
RubenVerborgh commented 3 months ago

HI @flyon!

the examples I'm finding the StreamWriter is always combined with a StreamReader

So instead of the N3.StreamReader, you'll have a stream of quads of your own. Just plug that stream into the writer and it works.

I.e., it's the same as https://github.com/rdfjs/N3.js?tab=readme-ov-file#from-a-quad-stream-to-an-rdf-stream, except inputStream = createMyOwnStreamOfQuads(), and then inputStream.pipe(streamWriter).

flyon commented 3 months ago

ah right like that. Perfect. Thank you @RubenVerborgh