UNPKG

@fivetrandevelopers/zetasql

Version:
76 lines 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZetaSQLClient = void 0; const grpc = require("@grpc/grpc-js"); const protoLoader = require("@grpc/proto-loader"); const util_1 = require("util"); class ZetaSQLClient { static getInstance() { if (!ZetaSQLClient.INSTANCE) { throw new Error('You have to call init first'); } return ZetaSQLClient.INSTANCE; } static init(port) { ZetaSQLClient.INSTANCE = new ZetaSQLClient(); const packageDefinition = protoLoader.loadSync('local_service.proto', { defaults: true, oneofs: true, includeDirs: [`${__dirname}/zetasql/protos`], }); const proto = grpc.loadPackageDefinition(packageDefinition); ZetaSQLClient.API = new proto.zetasql.local_service.ZetaSqlLocalService(`${ZetaSQLClient.HOST}:${port}`, grpc.credentials.createInsecure(), { 'grpc.max_send_message_length': ZetaSQLClient.SIZE_LIMIT_BYTES, 'grpc.max_receive_message_length': ZetaSQLClient.SIZE_LIMIT_BYTES, }); } constructor() { // To prevent instantiation from outside } async testConnection() { for (let i = 0; i < 15; i++) { try { await this.formatSql({ sql: 'SELECT 1;' }); return true; } catch (e) { if (e.code !== 14) { return false; } } await this.delay(500); } return false; } delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } registerCatalog(request) { return this.promisify(request, ZetaSQLClient.API.registerCatalog.bind(ZetaSQLClient.API)); } unRegisterCatalog(request) { return this.promisify(request, ZetaSQLClient.API.unregisterCatalog.bind(ZetaSQLClient.API)); } analyze(request) { return this.promisify(request, ZetaSQLClient.API.analyze.bind(ZetaSQLClient.API)); } getLanguageOptions(request) { return this.promisify(request, ZetaSQLClient.API.getLanguageOptions.bind(ZetaSQLClient.API)); } getBuiltinFunctions(request) { return this.promisify(request, ZetaSQLClient.API.getBuiltinFunctions.bind(ZetaSQLClient.API)); } formatSql(request) { return this.promisify(request, ZetaSQLClient.API.formatSql.bind(ZetaSQLClient.API)); } extractTableNamesFromStatement(request) { return this.promisify(request, ZetaSQLClient.API.extractTableNamesFromStatement.bind(ZetaSQLClient.API)); } promisify(request, fn) { return (0, util_1.promisify)(fn)(request); } } exports.ZetaSQLClient = ZetaSQLClient; ZetaSQLClient.SIZE_LIMIT_BYTES = 500 * 1024 * 1024; ZetaSQLClient.HOST = 'localhost'; //# sourceMappingURL=ZetaSQLClient.js.map