UNPKG

cassandra-codegen

Version:

Generate type definitions from a Cassandra database

52 lines (51 loc) 2.71 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const cassandra_driver_1 = require("cassandra-driver"); const codegen_1 = require("./codegen"); const path_1 = require("path"); const parseArgs = () => { const program = new commander_1.Command(); const defaultOutputDir = (0, path_1.join)(process.cwd(), 'node_modules', 'cassandra-codegen', 'dist', 'types'); program.name('cassandra-codegen') .description('Generate type definitions from a Cassandra database') .requiredOption('--host <host>', 'The Cassandra DB hostname or IP address') .option('--port <port>', 'The Cassandra DB port (optional)') .requiredOption('--datacenter <datacenter>', 'The Cassandra DB datacenter name') .option('--username <username>', 'The Cassandra DB username (optional)') .option('--password <password>', 'The Cassandra DB password (optional)') .requiredOption('--keyspace <keyspace>', 'The keyspace of the tables for which to generate type definitions') .option('--type-suffix <typeSuffix>', 'A suffix to add to the generated type names', 'Row') .option('--generate-ts-file', 'Generate a .ts file instead of the default .js & .d.ts') // To be used in conjunction with the Node.js Cassandra client is configured to use ECMAScript Map .option('--use-js-map', 'Map the Cassandra `map` type to the JavaScript `Map` type') // To be used in conjunction with the Node.js Cassandra client is configured to use ECMAScript Set .option('--use-js-set', 'Map the Cassandra `set` type to the JavaScript `Set` type') // Currently used only for tests .addOption(new commander_1.Option('--output-dir <outputDir>', 'Directory of generated files').default(defaultOutputDir).hideHelp()); program.parse(); return program.opts(); }; const main = async () => { const args = parseArgs(); const client = new cassandra_driver_1.Client({ contactPoints: [`${args.host}${args.port ? ':' + args.port : ''}`], localDataCenter: args.datacenter, ...(args.username && args.password && { credentials: { username: args.username, password: args.password, }, }) }); try { await client.connect(); await (0, codegen_1.generateTypeScriptDefinitions)(client, args.keyspace, args.outputDir, args.typeSuffix, args.generateTsFile, args.useJsMap, args.useJsSet); } catch (error) { console.error(`Encountered error: ${error?.message}`); await client.shutdown(); } }; main().then(() => process.exit());