UNPKG

@datastax/astra-db-ts

Version:
328 lines (327 loc) 17.1 kB
"use strict"; // Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 var _Db_defaultOpts, _Db_httpClient, _Db_keyspace, _Db_id, _Db_region; Object.defineProperty(exports, "__esModule", { value: true }); exports.Db = void 0; const tslib_1 = require("tslib"); const index_js_1 = require("../documents/collections/index.js"); const index_js_2 = require("../lib/api/index.js"); const astra_db_admin_js_1 = require("../administration/astra-db-admin.js"); const utils_js_1 = require("../documents/utils.js"); const data_api_db_admin_js_1 = require("../administration/data-api-db-admin.js"); const data_api_http_client_js_1 = require("../lib/api/clients/data-api-http-client.js"); const index_js_3 = require("../documents/index.js"); const constants_js_1 = require("../lib/api/constants.js"); const constants_js_2 = require("../lib/constants.js"); const errors_js_1 = require("../db/errors.js"); const ser_des_js_1 = require("../documents/collections/ser-des/ser-des.js"); const ser_des_js_2 = require("../documents/tables/ser-des/ser-des.js"); const admin_opts_handler_js_1 = require("../client/opts-handlers/admin-opts-handler.js"); const db_opts_handler_js_1 = require("../client/opts-handlers/db-opts-handler.js"); const environment_cfg_handler_js_1 = require("../client/opts-handlers/environment-cfg-handler.js"); const index_js_4 = require("../lib/index.js"); class Db extends index_js_4.HierarchicalLogger { static userDefinedTypeSchema(schema) { return schema; } constructor(rootOpts, endpoint, dbOpts) { var _a, _b; const defaultOpts = { ...rootOpts, dbOptions: db_opts_handler_js_1.DbOptsHandler.concat([rootOpts.dbOptions, dbOpts]), adminOptions: admin_opts_handler_js_1.AdminOptsHandler.concatParse([rootOpts.adminOptions], { adminToken: index_js_4.TokenProvider.opts.parseWithin(dbOpts, 'token'), }), }; super(rootOpts.client, defaultOpts.dbOptions.logging); _Db_defaultOpts.set(this, void 0); _Db_httpClient.set(this, void 0); _Db_keyspace.set(this, void 0); _Db_id.set(this, void 0); _Db_region.set(this, void 0); Object.defineProperty(this, "endpoint", { enumerable: true, configurable: true, writable: true, value: void 0 }); tslib_1.__classPrivateFieldSet(this, _Db_defaultOpts, defaultOpts, "f"); tslib_1.__classPrivateFieldSet(this, _Db_keyspace, { ref: (rootOpts.environment === 'astra') ? tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.keyspace ?? index_js_2.DEFAULT_KEYSPACE : tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.keyspace ?? undefined, }, "f"); endpoint = (endpoint.endsWith('/')) ? endpoint.replace(/\/+$/, "") : endpoint; tslib_1.__classPrivateFieldSet(this, _Db_httpClient, new data_api_http_client_js_1.DataAPIHttpClient({ baseUrl: endpoint, tokenProvider: tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.token, baseApiPath: tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.dataApiPath || constants_js_1.DEFAULT_DATA_API_PATHS[rootOpts.environment], logger: this, fetchCtx: rootOpts.fetchCtx, keyspace: tslib_1.__classPrivateFieldGet(this, _Db_keyspace, "f"), caller: rootOpts.caller, emissionStrategy: data_api_http_client_js_1.EmissionStrategy.Normal, additionalHeaders: tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").additionalHeaders, timeoutDefaults: tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.timeoutDefaults, }), "f"); _a = this, _b = this, [({ set value(_c) { tslib_1.__classPrivateFieldSet(_a, _Db_id, _c, "f"); } }).value, ({ set value(_c) { tslib_1.__classPrivateFieldSet(_b, _Db_region, _c, "f"); } }).value] = (0, utils_js_1.extractDbComponentsFromAstraUrl)(endpoint); Object.defineProperty(this, 'endpoint', { value: endpoint, }); Object.defineProperty(this, constants_js_2.$CustomInspect, { value: () => `Db(endpoint="${this.endpoint}",keyspace="${this.keyspace}")`, }); } get keyspace() { if (!tslib_1.__classPrivateFieldGet(this, _Db_keyspace, "f").ref) { throw new Error('No keyspace set for DB (can\'t do db.keyspace, or perform any operation requiring it). Use `db.useKeyspace`, or pass the keyspace as an option parameter explicitly.'); } return tslib_1.__classPrivateFieldGet(this, _Db_keyspace, "f").ref; } get id() { if (tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment !== 'astra') { throw new errors_js_1.InvalidEnvironmentError('db.id', tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment, ['astra'], 'non-Astra databases have no appropriate ID'); } if (!tslib_1.__classPrivateFieldGet(this, _Db_id, "f")) { throw new Error(`Unexpected AstraDB endpoint URL '${this.endpoint}'—database ID unable to be parsed`); } return tslib_1.__classPrivateFieldGet(this, _Db_id, "f"); } get region() { if (tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment !== 'astra') { throw new errors_js_1.InvalidEnvironmentError('db.region', tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment, ['astra'], 'non-Astra databases have no appropriate region'); } if (!tslib_1.__classPrivateFieldGet(this, _Db_region, "f")) { throw new Error(`Unexpected AstraDB endpoint URL '${this.endpoint}'—database region unable to be parsed`); } return tslib_1.__classPrivateFieldGet(this, _Db_region, "f"); } useKeyspace(keyspace) { tslib_1.__classPrivateFieldGet(this, _Db_keyspace, "f").ref = keyspace; } admin(options) { const environment = environment_cfg_handler_js_1.EnvironmentCfgHandler.parseWithin(options, 'options.environment'); const parsedOpts = admin_opts_handler_js_1.AdminOptsHandler.parse(options, 'options'); if (tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment !== environment) { throw new errors_js_1.InvalidEnvironmentError('db.admin()', environment, [tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment], 'environment option is not the same as set in the DataAPIClient'); } if (environment === 'astra') { return new astra_db_admin_js_1.AstraDbAdmin(this, tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f"), parsedOpts, tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.token, this.endpoint); } if ((0, utils_js_1.extractDbComponentsFromAstraUrl)(this.endpoint).length !== 0) { throw new errors_js_1.InvalidEnvironmentError('db.admin()', environment, [tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment], 'environment option must be "astra" or unset for this database'); } return new data_api_db_admin_js_1.DataAPIDbAdmin(this, tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").client, tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f"), tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f"), parsedOpts); } async info(options) { if (tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment !== 'astra') { throw new errors_js_1.InvalidEnvironmentError('db.info()', tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").environment, ['astra'], 'info() is only available for Astra databases'); } const data = await this.admin().info(options); const region = this.endpoint .split('.')[0] .split('https://')[1] .split('-') .slice(5) .join('-'); return { id: data.id, name: data.name, keyspaces: data.keyspaces, status: data.status, environment: data.environment, cloudProvider: data.cloudProvider, region: region, apiEndpoint: this.endpoint, raw: data.raw.info, }; } collection(name, options) { return new index_js_1.Collection(this, tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f"), name, tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f"), { ...options, serdes: ser_des_js_1.CollSerDes.cfg.concatParse([tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.collSerdes], options?.serdes), }); } table(name, options) { return new index_js_3.Table(this, tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f"), name, tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f"), { ...options, serdes: ser_des_js_2.TableSerDes.cfg.concatParse([tslib_1.__classPrivateFieldGet(this, _Db_defaultOpts, "f").dbOptions.tableSerdes], options?.serdes), }); } async createCollection(name, options) { const command = { createCollection: { name: name, options: { defaultId: options?.defaultId, indexing: options?.indexing, vector: options?.vector, lexical: options?.lexical, rerank: options?.rerank, }, }, }; await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('collectionAdminTimeoutMs', { timeout: { collectionAdminTimeoutMs: (typeof options?.timeout === 'number') ? options.timeout : options?.timeout?.collectionAdminTimeoutMs, requestTimeoutMs: 0, }, }), keyspace: options?.keyspace, extraLogInfo: { name }, }); return this.collection(name, options); } async createTable(name, options) { const command = { createTable: { name: name, definition: options.definition, options: { ifNotExists: options.ifNotExists, }, }, }; await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name, ifNotExists: options.ifNotExists ?? false }, keyspace: options?.keyspace, }); return this.table(name, options); } async createType(name, options) { const command = { createType: { name: name, definition: options.definition, options: { ifNotExists: options.ifNotExists, }, }, }; await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name, ifNotExists: options.ifNotExists ?? false }, keyspace: options?.keyspace, }); } async dropCollection(name, options) { await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand({ deleteCollection: { name } }, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('collectionAdminTimeoutMs', options), keyspace: options?.keyspace, extraLogInfo: { name }, }); } async dropTable(name, options) { await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand({ dropTable: { name, options: { ifExists: options?.ifExists } } }, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name, ifExists: options?.ifExists ?? false }, keyspace: options?.keyspace, }); } async dropType(name, options) { await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand({ dropType: { name, options: { ifExists: options?.ifExists } } }, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name, ifExists: options?.ifExists ?? false }, keyspace: options?.keyspace, }); } async dropTableIndex(name, options) { const dropOpts = (options?.ifExists) ? { ifExists: true } : undefined; await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand({ dropIndex: { name, options: dropOpts } }, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name, ifExists: options?.ifExists ?? false }, keyspace: options?.keyspace, }); } async listCollections(options) { const explain = options?.nameOnly !== true; const command = { findCollections: { options: { explain }, }, }; const resp = await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('collectionAdminTimeoutMs', options), extraLogInfo: { nameOnly: !explain }, keyspace: options?.keyspace, }); const colls = resp.status.collections; if (explain) { for (let i = 0, n = colls.length; i < n; i++) { colls[i].definition = colls[i].options; delete colls[i].options; } } return colls; } async listTables(options) { const explain = options?.nameOnly !== true; const command = { listTables: { options: { explain }, }, }; const resp = await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { nameOnly: !explain }, keyspace: options?.keyspace, }); return resp.status.tables; } async listTypes(options) { const explain = options?.nameOnly !== true; const command = { listTypes: { options: { explain }, }, }; const resp = await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { nameOnly: !explain }, keyspace: options?.keyspace, }); const types = resp.status.types; if (explain) { for (let i = 0, n = types.length; i < n; i++) { types[i].name = types[i].udtName; delete types[i].udtName; delete types[i].type; } } return types; } async alterType(name, options) { const command = { alterType: { name: name, ...options.operation, // TODO yes or no? }, }; await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('tableAdminTimeoutMs', options), extraLogInfo: { name }, keyspace: options?.keyspace, }); } async command(command, options) { return await tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").executeCommand(command, { timeoutManager: tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f").tm.single('generalMethodTimeoutMs', options), keyspace: options?.keyspace, collection: options?.collection, extraLogInfo: options?.extraLogInfo ?? { source: 'db.command' }, table: options?.table, }); } get _httpClient() { return tslib_1.__classPrivateFieldGet(this, _Db_httpClient, "f"); } } exports.Db = Db; _Db_defaultOpts = new WeakMap(), _Db_httpClient = new WeakMap(), _Db_keyspace = new WeakMap(), _Db_id = new WeakMap(), _Db_region = new WeakMap();