Skip to content

Commit cafe2c5

Browse files
authored
Merge pull request #34 from oslabs-beta/ja/npmPackage
Ja/npm package
2 parents 5830c81 + 31c76b0 commit cafe2c5

File tree

10 files changed

+82
-6214
lines changed

10 files changed

+82
-6214
lines changed

.gitignore

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,5 @@ dist
103103
# TernJS port file
104104
.tern-port
105105

106-
<<<<<<< HEAD
107-
# Local
108-
ksqljsTest.js
109-
package-lock.json
110-
local_ignore/
111-
.gitignore
112-
113-
# KSQLDB docker server settings
114-
ksqldb_server_config/
115-
116-
=======
117-
# Local files
118-
./ksqljs.js
119-
>>>>>>> jonTest
106+
107+

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Test Files
2+
/__tests__/
3+
.yml

__tests__/integrationtests.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { default: waitForExpect } = require('wait-for-expect');
2-
const ksqljs = require('../ksqljs/ksqlJS.js');
2+
const ksqldb = require('../ksqldb/ksqldb');
33
// Pre-requisite: start a docker container
44
/* To add to README: Prior to running test with 'npm test', please start the ksqlDB
55
server using the command 'docker-compose up'. This will spin up a ksqlDB server on
@@ -16,7 +16,7 @@ need to be removed first.
1616
describe('--Integration Tests--', () => {
1717
describe('--Method Tests--', () => {
1818
beforeAll((done) => {
19-
client = new ksqljs({ ksqldbURL: 'http://localhost:8088' });
19+
client = new ksqldb({ ksqldbURL: 'http://localhost:8088' });
2020
done();
2121
});
2222

@@ -61,7 +61,6 @@ describe('--Integration Tests--', () => {
6161
const data = [];
6262
await client.push('SELECT * FROM TESTJESTSTREAM EMIT CHANGES;', async (chunk) => {
6363
data.push(JSON.parse(chunk));
64-
console.log(data);
6564
if (data[1]) {
6665
client.terminate(data[0].queryId);
6766
expect(data[1]).toEqual(["stab-rabbit", "123@mail.com", 100])
@@ -158,7 +157,7 @@ describe('--Integration Tests--', () => {
158157

159158
describe('--Health Tests--', () => {
160159
beforeAll((done) => {
161-
client = new ksqljs({ ksqldbURL: 'http://localhost:8088' });
160+
client = new ksqldb({ ksqldbURL: 'http://localhost:8088' });
162161
done();
163162
});
164163

__tests__/queryBuilderTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const queryBuilder = require('../ksqljs/queryBuilder.js');
2-
const { QueryBuilderError, EmptyQueryError, NumParamsError, InappropriateStringParamError } = require('../ksqljs/customErrors.js');
1+
const queryBuilder = require('../ksqldb/queryBuilder.js');
2+
const { QueryBuilderError, EmptyQueryError, NumParamsError, InappropriateStringParamError } = require('../ksqldb/customErrors.js');
33

44

55
describe('Unit tests for query builder class', () => {

klip_64_ksqldb_js_client.md

Lines changed: 0 additions & 63 deletions
This file was deleted.
File renamed without changes.

ksqljs/ksqlJS.js renamed to ksqldb/ksqldb.js

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ const { ksqlDBError } = require("./customErrors.js");
55
const queryBuilder = require('./queryBuilder.js');
66
const builder = new queryBuilder();
77

8-
class ksqljs {
8+
class ksqldb {
9+
/**
10+
* Constructor
11+
* @param {object} config
12+
*
13+
* Config object can have these properties
14+
*
15+
* ksqldbURL: Connection URL or address
16+
*
17+
* API: Username or API key for basic authentication
18+
*
19+
* secret: Password or secret for basic authentication
20+
*
21+
* httpsAgent: httpsAgent for setting TLS properties
22+
*/
923
constructor(config) {
1024
this.ksqldbURL = config.ksqldbURL;
1125
this.API = config.API ? config.API : null;
@@ -261,7 +275,16 @@ class ksqljs {
261275
// utilize query with variables to build actual query
262276
const query = builder.build(builderQuery, [streamName], ...propertiesArgs, [sourceStream]);
263277

264-
return axios.post(this.ksqldbURL + '/ksql', { ksql: query })
278+
return axios.post(this.ksqldbURL + '/ksql', { ksql: query }, {
279+
headers:
280+
this.API && this.secret ?
281+
{
282+
"Authorization": `Basic ${Buffer.from(this.API + ":" + this.secret, 'utf8').toString('base64')}`,
283+
}
284+
:
285+
{},
286+
httpsAgent: this.httpsAgentAxios ? this.httpsAgentAxios : null,
287+
})
265288
.then(res => res.data[0].commandStatus.queryId)
266289
.catch(error => console.log(error));
267290
}
@@ -331,34 +354,34 @@ class ksqljs {
331354
partitions: 1
332355
};
333356
Object.assign(defaultProps, propertiesObj);
334-
357+
335358
// if there's no properties Obj, assign them all default values
336359

337360
// expect user to input a conditions object of format {WHERE: condition, GROUP_BY: condition, HAVING: condition};
338361
// generate conditions string based on object
339362
// const builder = new queryBuilder();
340363

341364
let conditionQuery = '';
342-
if (conditionsObj){
365+
if (conditionsObj) {
343366
const conditionsArr = ['WHERE', 'GROUP_BY', 'HAVING'];
344367
const sqlClauses = [];
345-
368+
346369
let i = 0;
347-
while (conditionsArr.length){
348-
if (conditionsObj[conditionsArr[0]]){
349-
sqlClauses[i] = [conditionsArr[0].replace('_',' ')]; // clause values are set as arrays for query builder
350-
sqlClauses[i+1] =[' ' + conditionsObj[conditionsArr[0]] + ' '];
370+
while (conditionsArr.length) {
371+
if (conditionsObj[conditionsArr[0]]) {
372+
sqlClauses[i] = [conditionsArr[0].replace('_', ' ')]; // clause values are set as arrays for query builder
373+
sqlClauses[i + 1] = [' ' + conditionsObj[conditionsArr[0]] + ' '];
351374
}
352375
else {
353376
sqlClauses[i] = [''];
354-
sqlClauses[i+1] = [''];
377+
sqlClauses[i + 1] = [''];
355378
}
356-
i+=2;
379+
i += 2;
357380
conditionsArr.shift()
358381
}
359382
conditionQuery = builder.build('??????', sqlClauses[0], sqlClauses[1], sqlClauses[2], sqlClauses[3], sqlClauses[4], sqlClauses[5]);
360383
}
361-
384+
362385

363386
// reformat for builder
364387
tableName = [tableName];
@@ -368,21 +391,30 @@ class ksqljs {
368391

369392

370393
const query = builder.build(`CREATE TABLE ? WITH (kafka_topic=?, value_format=?, partitions=?) AS SELECT ? FROM ? ?EMIT CHANGES;`, tableName, defaultProps.topic, defaultProps.value_format, defaultProps.partitions, selectColStr, source, conditionQuery)
371-
return axios.post(this.ksqldbURL + '/ksql', { ksql: query })
372-
.catch(error => console.log(error));
394+
return axios.post(this.ksqldbURL + '/ksql', { ksql: query }, {
395+
headers:
396+
this.API && this.secret ?
397+
{
398+
"Authorization": `Basic ${Buffer.from(this.API + ":" + this.secret, 'utf8').toString('base64')}`,
399+
}
400+
:
401+
{},
402+
httpsAgent: this.httpsAgentAxios ? this.httpsAgentAxios : null,
403+
})
404+
.catch(error => console.log(error));
373405
}
374406

375-
/**
376-
* Inserts rows of data into a stream.
377-
*
378-
* <p>This method may be used to insert new rows of data into a stream.
379-
*
380-
* <p>This method is sql injection protected with the use of queryBuilder.
381-
*
382-
* @param {string} stream the name of the stream to insert data into.
383-
* @param {object} rows an array that contains data that is being inserted into the stream.
384-
* @return {Promise} this method returns a promise that resolves into an array describing the status of the row inserted.
385-
*/
407+
/**
408+
* Inserts rows of data into a stream.
409+
*
410+
* <p>This method may be used to insert new rows of data into a stream.
411+
*
412+
* <p>This method is sql injection protected with the use of queryBuilder.
413+
*
414+
* @param {string} stream the name of the stream to insert data into.
415+
* @param {object} rows an array that contains data that is being inserted into the stream.
416+
* @return {Promise} this method returns a promise that resolves into an array describing the status of the row inserted.
417+
*/
386418
//---------------------Insert Rows Into Existing Streams-----------------
387419
insertStream = (stream, rows) => {
388420
return new Promise((resolve, reject) => {
@@ -603,4 +635,4 @@ class ksqljs {
603635
}
604636
};
605637

606-
module.exports = ksqljs;
638+
module.exports = ksqldb;
File renamed without changes.

0 commit comments

Comments
 (0)