@powerkraut/data-utils-js
v1.6.0
Published
JS Data Utils for PowerKraut Data Projects
Downloads
67
Readme
NPM Package for PowerKraut Data Utils.
Utility library to be used by PowerKraut data projects.
Installation
npm install @powerkraut/data-utils-js --save
Available Modules
GoogleCloud\Logging\Logger
This allows logging to stackdriver logging API, with support for labels and severity levels.GoogleCloud\Functions\sendMessage
A helper binary to allow sending test to your local cloud-functions framework.
The documentation of the individual modules can be found in the class document blocks.
Config installation / loader
This packages contains several classes to support with config installation / loading. To allow the installation
of a config, create a js script to handle the installation, for example: cli/installConfig.js
. The default
install script in this package can be used to create a basic config installation.
import {InstallConfig} from "@powerkraut/data-utils-js";
const installer = new InstallConfig({
configDefaultPath: `path/to/config/output`,
});
// Execute the script
installer.handle();
The install script can be extended to adopt custom installation logic if necessary. The ConfigLoader
class can then
be used to read the configuration and load them into environment variables. To create an instance of this class
use the following code:
import {ConfigLoader} from "@powerkraut/data-utils-js";
const loader = new ConfigLoader({
configDefaultFile: '[config-file-name].js',
configDefaultPath: 'path/to/config/[config-file-name].js'
});
This class is also used by the AbstractCli
class, used to create CLI commands.
CLI commands
Creating CLI commands you should extend the AbstractCli
class from this packages. For now this class
only allows access to the config loader. Be sure to call the super constructor with the correct
config info.
import {AbstractCli} from "@powerkraut/data-utils.js";
class SayHello extends AbstractCli {
constructor() {
super({
configDefaultFile: '[config-file-name].js',
configDefaultPath: 'path/to/config/[config-file-name].js'
});
}
handle() {
console.log('Hello from: ' + super.configLoader.getCwdDir());
}
}
GCloud / Deployment
This package exposes several classes to help with deployments. To start with, the GCloudCli
class allows for the
execution of commands to google cloud. The class contains several pre-coded commands and has a method to execute
any command. The gcloud
prefix should be omitted.
import {GCloudCli} from "@powerkraut/data-utils.js";
const cli = new GCloudCli();
cli.getCurrentProject();
cli.setProject('[project]');
cli.getIdentityToken();
// Or execute any other command:
cli.exec(['config', 'get', 'project']);
The GCloudCli
is used by this package to handle deployments. The GoogleDeploymentManager
class extends the
GCloudCli
class and adds CRUD methods to manage deployments. Combining the CLI and deployment classes, the
DeployCloudFunction
class adds helper methods to assist with the creation of a deployment script.
Like the installation of the config file, a js file should be created to allow cli execution, for example:
cli/deployCloudFunction.js
. A most basic implementation could look like this:
DeploymentScript.js
import {DeployCloudFunction} from "@powerkraut/data-utils-js";
export default class DeploymentScript extends DeployCloudFunction {
constructor() {
super(
{
npmRcDistPath: './../../../.npmrc.dist',
cloudFunctionsPath: `path/to/cloud-function`
},
{
configDefaultFile: '[config-file-name].js',
configDefaultPath: 'path/to/config/[config-file-name].js'
}
);
}
/**
* @return {Promise<void>}
*/
async handle() {
await super.configLoader.loadActiveConfigIntoProcessEnv();
await this.deployCloudFunction();
}
/**
* @return {Promise<void>}
*/
async deployCloudFunction() {
await super.createDeploymentFolder();
await super.writeEnvFile();
await super.writeGoogleApplicationCredentialsFile();
const functionArgs = [];
const workingDir = process.cwd();
functionArgs.push('--runtime=nodejs16');
functionArgs.push(`--trigger-topic=${process.env["PUBSUB_TOPIC"]}`);
functionArgs.push(`--entry-point=${process.env["FUNCTION_NAME"]}`);
functionArgs.push('--region=europe-west1');
functionArgs.push('--env-vars-file=env.yaml');
functionArgs.push('--source=.');
process.chdir(super.getTempDeployDirectory());
console.log(chalk.yellow('Deploying with the following args'));
console.log(chalk.yellow(functionArgs.toString()));
(new GCloudCli()).exec(['beta', 'functions', 'deploy', `${process.env["FUNCTION_NAME_REST"]}`, ...functionArgs]);
process.chdir(workingDir);
}
}
deployCloudFunctions.js
import DeploymentScript from './DeploymentScript.js';
(new DeploymentScript()).handle();
Background events
To help with background events spawned by Google Cloud you can use the PubSubBackgroundEventController
class.
By extending this class you can easily access the data from the event, and flag the event as failed or aborted.
See the src/Functions/PubSubBackgroundEventController.js
file for all the functionality.