-
Notifications
You must be signed in to change notification settings - Fork 2
/
suiteql.js
59 lines (54 loc) · 1.62 KB
/
suiteql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";
var NetsuiteRest = require("netsuite-rest");
const Readable = require("stream").Readable;
module.exports = class suiteql extends NetsuiteRest {
constructor(options) {
if (typeof options !== "object")
throw new TypeError("Please provide netsuite api credentials");
super(options);
}
async connect() {
return await this.request({
path: "*",
method: "OPTIONS",
});
}
async query(string, limit = 1000, offset = 0) {
let queryresult = {};
if (typeof string !== "string")
throw new TypeError("Query is not a string");
if (limit > 1000) throw new Error("Max limit is 1000");
// replace all \t with spaces as suggested in #5
string = string.replace(/\t/g, ' ');
string = string.replace(/\r?\n|\r/gm, "");
let bodycontent = `{"q": "${string}" }`;
await this.request({
path: `query/v1/suiteql?limit=${limit}&offset=${offset}`,
method: "POST",
body: bodycontent,
}).then(async (response) => {
queryresult.items = response.data.items;
queryresult.hasMore = response.data.hasMore;
});
return queryresult;
}
queryAll(string, limit = 1000) {
const stream = new Readable({
objectMode: true,
read() {},
});
let offset = 0;
const getNextPage = async () => {
let hasMore = true;
while (hasMore === true) {
let sqlresult = await this.query(string, limit, offset);
sqlresult.items.forEach((item) => stream.push(item));
hasMore = sqlresult.hasMore;
offset = offset + limit;
}
stream.push(null);
};
getNextPage();
return stream;
}
};