Skip to content

Commit

Permalink
#8 added addDataSources convenience method for sequential addDataSour…
Browse files Browse the repository at this point in the history
…ce requests
  • Loading branch information
arawinters committed Feb 14, 2025
1 parent 66cd33f commit e557374
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type":"module",
"scripts": {
"szconfig/addDataSource.ts": "tsx szconfig/addDataSource.ts",
"szconfig/addDataSources.ts": "tsx szconfig/addDataSources.ts",
"szconfig/constructor.ts": "tsx szconfig/constructor.ts",
"szconfig/createAndClose.ts": "tsx szconfig/createAndClose.ts",
"szconfig/createExportImportClose.ts": "tsx szconfig/createExportImportClose.ts",
Expand Down
17 changes: 17 additions & 0 deletions examples/szconfig/addDataSources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SzGrpcEnvironment } from '@senzing/sz-sdk-typescript-grpc';

const szEnvironment = new SzGrpcEnvironment({connectionString: `0.0.0.0:8261`});
const DATASOURCES_TO_ADD = ['CUSTOMERS', 'REFERENCE', 'WATCHLIST'];

// create new config and get handle
szEnvironment.getConfig().createConfig().then((configHandle) => {
// now add datasources
szEnvironment.getConfig().addDataSources(configHandle as number, DATASOURCES_TO_ADD).then((results) => {
console.log(`Added Data Sources: \n\r`, results);
}).
catch((err) => {
console.error(err);
});
}).catch((err) => {
console.error(err);
})
77 changes: 77 additions & 0 deletions src/szGrpcConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,83 @@ export class SzGrpcConfig extends SzGrpcBase implements SzConfig {
})
});
}
/**
* Adds multiple data sources to an existing in-memory configuration.
* @param {number} configHandle
* @param {string[]} dataSourceCodes
* @returns {Promise<string[]>} JSON documents for each datasource listing the newly created data source
*/
addDataSources(configHandle: number, dataSourceCodes: string[]) {
/**
* Attempting to create multiple datasources asynchonously ends up causing a connection closed error.
* JavaScript/TypeScript does not have a good paradigm for synchronously requesting promises one after the other.
* (technically we could use generators but google's protoc generated files do not support ESM which would be required for
* using "async" and "await" so that leaves us with recursive function calls). Asking the end user to come up
* with that logic on their own might be a bit daunting so I'm providing this convenience method that
* handles it for them.
*/
/** private class used for managing sequential requests */
let addDataSourcesSequentially = (dataSources: string[], configHandle: number, callback?: Function) => {
let _dataSources = dataSources;
let _configHandle = configHandle;
let _callback: Function | undefined;
let _onComplete: Promise<string[]>;
let _responses: string[] = [];

// define subs
let next = () => {
let dsName = _dataSources.shift();
if(!dsName || dsName == undefined) {
// we're done
onComplete();
return;
}
//return new Promise((resolve, reject) => {
this.addDataSource(_configHandle as number, dsName as string)
.then((resp) => {
// add response to results
_responses.push(resp);
// call "getNextRequest" again
next();
})
//})
}

let onComplete = () => {
// when we're done call this method
if(_callback) {
_callback.call(this, _responses);
}
}

// initialization logic
if(callback) {
_callback = callback;
}
// set up onComplete promise now that we know the number of requests
_onComplete = new Promise<string[]>((resolve, reject) => {
if(_responses.length >= _dataSources.length) {
resolve(_responses);
}
});
// if there's a callback attach it to the _onComplete promise chain
if(callback) {
_onComplete.then(callback as any)
}
// kick off the first request. (recursive method chain)
if(_dataSources && _dataSources.length > 0) {
next();
}
}
return new Promise<string[]>((resolve, reject) => {
let addDataSources = addDataSourcesSequentially(dataSourceCodes, configHandle as number, (results: string[], error?: Error | undefined) => {
if(error) {
reject(error);
}
resolve(results);
})
});
}
/**
* Cleans up the Senzing SzConfig object pointed to by the config_handle.
* @param configHandle An identifier of an in-memory configuration. Usually created by the {@link SzGrpcConfig#createConfig} or {@link SzGrpcConfig#importConfig} methods.
Expand Down

0 comments on commit e557374

Please sign in to comment.