Created
April 24, 2019 09:25
-
-
Save wolf4ood/c3c00bc3e782069c70d726a00bb29a8f to your computer and use it in GitHub Desktop.
OrientJS pipe exsample
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
const OrientDBClient = require("orientjs").OrientDBClient; | |
const fs = require("fs"); | |
const { Transform } = require("stream"); | |
const dbConnectionData = { | |
host: "localhost", | |
port: 2424 | |
}; | |
const dbData = { | |
username: "admin", | |
password: "admin", | |
name: "demodb" | |
}; | |
const writeStream = (session, path, query) => { | |
return new Promise((resolve, reject) => { | |
const file = fs.createWriteStream(path); | |
let counter = 0; | |
let transform = new Transform({ | |
objectMode: true, | |
transform(transaction, encoding, callback) { | |
callback(null, JSON.stringify(transaction)); | |
} | |
}); | |
session | |
.query(query) | |
.on("data", () => counter++) | |
.pipe(transform) | |
.pipe(file); | |
file.on("finish", () => { | |
resolve(counter); | |
}); | |
}); | |
}; | |
(async () => { | |
const client = await OrientDBClient.connect(dbConnectionData); | |
const session = await client.session(dbData); | |
const results = await writeStream(session, "big.json", "select from V"); | |
await session.close(); | |
await client.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment