UNPKG

@datastax/astra-db-ts

Version:
121 lines (120 loc) 6.82 kB
"use strict"; // Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 var _Collection_httpClient, _Collection_commands, _Collection_db; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = void 0; const tslib_1 = require("tslib"); const hierarchical_logger_js_1 = require("../../lib/logging/hierarchical-logger.js"); const command_impls_js_1 = require("../../documents/commands/command-impls.js"); const constants_js_1 = require("../../lib/constants.js"); const index_js_1 = require("../../documents/index.js"); const json_bigint_1 = tslib_1.__importDefault(require("json-bigint")); const ser_des_js_1 = require("../../documents/collections/ser-des/ser-des.js"); const utils_js_1 = require("../../lib/api/ser-des/utils.js"); const rerank_cursor_js_1 = require("../../documents/collections/cursors/rerank-cursor.js"); const internal_logger_js_1 = require("../../lib/logging/internal-logger.js"); const jbi = json_bigint_1.default; class Collection extends hierarchical_logger_js_1.HierarchicalLogger { constructor(db, httpClient, name, rootOpts, opts) { const loggingConfig = internal_logger_js_1.InternalLogger.cfg.concatParseWithin([rootOpts.dbOptions.logging], opts, 'logging'); super(db, loggingConfig); _Collection_httpClient.set(this, void 0); _Collection_commands.set(this, void 0); _Collection_db.set(this, void 0); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "keyspace", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, 'name', { value: name, writable: false, }); Object.defineProperty(this, 'keyspace', { value: opts?.keyspace ?? db.keyspace, writable: false, }); const hack = { parseWithBigNumbers: () => !!opts?.serdes?.enableBigNumbers, parser: (0, utils_js_1.withJbiNullProtoFix)(jbi), }; tslib_1.__classPrivateFieldSet(this, _Collection_httpClient, httpClient.forTableSlashCollectionOrWhateverWeWouldCallTheUnionOfTheseTypes(this, opts, hack), "f"); tslib_1.__classPrivateFieldSet(this, _Collection_commands, new command_impls_js_1.CommandImpls(this, tslib_1.__classPrivateFieldGet(this, _Collection_httpClient, "f"), new ser_des_js_1.CollSerDes(ser_des_js_1.CollSerDes.cfg.parse(opts?.serdes))), "f"); tslib_1.__classPrivateFieldSet(this, _Collection_db, db, "f"); Object.defineProperty(this, constants_js_1.$CustomInspect, { value: () => `Collection(keyspace="${this.keyspace}",name="${this.name}")`, }); } async insertOne(document, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").insertOne(document, options); } async insertMany(documents, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").insertMany(documents, options, index_js_1.CollectionInsertManyError); } async updateOne(filter, update, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").updateOne(filter, update, options); } async updateMany(filter, update, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").updateMany(filter, update, options, (e, result) => new index_js_1.CollectionUpdateManyError(e, result)); } async replaceOne(filter, replacement, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").replaceOne(filter, replacement, options); } async deleteOne(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").deleteOne(filter, options); } async deleteMany(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").deleteMany(filter, options, (e, result) => new index_js_1.CollectionDeleteManyError(e, result)); } find(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").find(filter, options, index_js_1.CollectionFindCursor); } findAndRerank(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").findAndRerank(filter, options, rerank_cursor_js_1.CollectionFindAndRerankCursor); } async findOne(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").findOne(filter, options); } async distinct(key, filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").distinct(key, filter, options, index_js_1.CollectionFindCursor); } async countDocuments(filter, upperBound, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").countDocuments(filter, upperBound, options, index_js_1.TooManyDocumentsToCountError); } async estimatedDocumentCount(options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").estimatedDocumentCount(options); } async findOneAndReplace(filter, replacement, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").findOneAndReplace(filter, replacement, options); } async findOneAndDelete(filter, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").findOneAndDelete(filter, options); } async findOneAndUpdate(filter, update, options) { return tslib_1.__classPrivateFieldGet(this, _Collection_commands, "f").findOneAndUpdate(filter, update, options); } async options(options) { const resp = await tslib_1.__classPrivateFieldGet(this, _Collection_db, "f").listCollections({ ...options, keyspace: this.keyspace }); const collection = resp.find((c) => c.name === this.name); if (!collection) { throw new Error(`Can not get options for collection '${this.keyspace}.${this.name}'; collection not found. Did you use the right keyspace?`); } return collection.definition; } async drop(options) { await tslib_1.__classPrivateFieldGet(this, _Collection_db, "f").dropCollection(this.name, { keyspace: this.keyspace, ...options }); } get _httpClient() { return tslib_1.__classPrivateFieldGet(this, _Collection_httpClient, "f"); } } exports.Collection = Collection; _Collection_httpClient = new WeakMap(), _Collection_commands = new WeakMap(), _Collection_db = new WeakMap();