@quillsql/node
v0.9.17
Published
Quill Server SDK for Node.js
Downloads
3,232
Readme
Quill Node SDK
Installation
$ npm install @quillsql/node
# Or, if you prefer yarn
$ yarn add @quillsql/nodeUsage
Instantiate quill with your credentials and add the below POST endpoint.
Note that we assume you have an organization id on the user returned by your auth middleware. Queries will not work properly without the organization id.
const quill = require("@quillsql/node")({
privateKey: process.env.QULL_PRIVATE_KEY,
databaseConnectionString: process.env.POSTGRES_READ,
});
// "authenticateJWT" is your own pre-existing auth middleware
app.post("/quill", authenticateJWT, async (req, res) => {
// assuming user fetched via auth middleware has an organizationId
const { organizationId } = req.user;
const { metadata } = req.body;
const result = await quill.query({
tenants: [{ tenantField: "organization_id", tenantIds: [organizationId] }],
metadata,
});
res.send(result);
});Streaming
const quill = require("@quillsql/node")({
privateKey: process.env.QULL_PRIVATE_KEY,
databaseConnectionString: process.env.POSTGRES_READ,
});
app.use(express.json());
app.post("/quillStream", authenticateJWT, async (req, res) => {
// assuming user fetched via auth middleware has an organizationId
const { organizationId } = req.user;
const { metadata } = req.body;
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
try {
const stream = await quill.stream({
tenants: [
{ tenantField: "organization_id", tenantIds: [organizationId] },
],
metadata,
});
for await (const event of stream) {
if (event.type === "start") {
// pass
} else if (event.type === "text-delta") {
res.write(`data: ${JSON.stringify(event.delta)}\n\n`);
} else if (event.type === "error") {
res.write(`data: ${JSON.stringify({ error: event.errorText })}\n\n`);
} else if (event.type === "finish") {
res.write("data: [DONE]\n\n");
res.end();
}
}
} catch (err) {
console.log("error in stream", err)
res.write(`data: ${JSON.stringify({ error: err })}\n\n`);
}
});For local testing (dev purposes only)
Create an .env file with the following key-value pairs:
QUILL_PRIVATE_KEY=
DB_URL=
ENV=development
BACKEND_URL=Use the following commands to start a locally hosted dev server.
npm install
npm run dev-serverYou should be able to ping your local server at http://localhost:3000.
