UNPKG

@datastax/astra-db-ts

Version:
117 lines (116 loc) 6.39 kB
// Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 var _Collection_httpClient, _Collection_commands, _Collection_db; import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib"; import { HierarchicalLogger } from '../../lib/logging/hierarchical-logger.js'; import { CommandImpls } from '../../documents/commands/command-impls.js'; import { $CustomInspect } from '../../lib/constants.js'; import { CollectionDeleteManyError, CollectionFindCursor, CollectionInsertManyError, CollectionUpdateManyError, TooManyDocumentsToCountError, } from '../../documents/index.js'; import JBI from 'json-bigint'; import { CollSerDes } from '../../documents/collections/ser-des/ser-des.js'; import { withJbiNullProtoFix } from '../../lib/api/ser-des/utils.js'; import { CollectionFindAndRerankCursor } from '../../documents/collections/cursors/rerank-cursor.js'; import { InternalLogger } from '../../lib/logging/internal-logger.js'; const jbi = JBI; export class Collection extends HierarchicalLogger { constructor(db, httpClient, name, rootOpts, opts) { const loggingConfig = 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: withJbiNullProtoFix(jbi), }; __classPrivateFieldSet(this, _Collection_httpClient, httpClient.forTableSlashCollectionOrWhateverWeWouldCallTheUnionOfTheseTypes(this, opts, hack), "f"); __classPrivateFieldSet(this, _Collection_commands, new CommandImpls(this, __classPrivateFieldGet(this, _Collection_httpClient, "f"), new CollSerDes(CollSerDes.cfg.parse(opts?.serdes))), "f"); __classPrivateFieldSet(this, _Collection_db, db, "f"); Object.defineProperty(this, $CustomInspect, { value: () => `Collection(keyspace="${this.keyspace}",name="${this.name}")`, }); } async insertOne(document, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").insertOne(document, options); } async insertMany(documents, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").insertMany(documents, options, CollectionInsertManyError); } async updateOne(filter, update, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").updateOne(filter, update, options); } async updateMany(filter, update, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").updateMany(filter, update, options, (e, result) => new CollectionUpdateManyError(e, result)); } async replaceOne(filter, replacement, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").replaceOne(filter, replacement, options); } async deleteOne(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").deleteOne(filter, options); } async deleteMany(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").deleteMany(filter, options, (e, result) => new CollectionDeleteManyError(e, result)); } find(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").find(filter, options, CollectionFindCursor); } findAndRerank(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").findAndRerank(filter, options, CollectionFindAndRerankCursor); } async findOne(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").findOne(filter, options); } async distinct(key, filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").distinct(key, filter, options, CollectionFindCursor); } async countDocuments(filter, upperBound, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").countDocuments(filter, upperBound, options, TooManyDocumentsToCountError); } async estimatedDocumentCount(options) { return __classPrivateFieldGet(this, _Collection_commands, "f").estimatedDocumentCount(options); } async findOneAndReplace(filter, replacement, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").findOneAndReplace(filter, replacement, options); } async findOneAndDelete(filter, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").findOneAndDelete(filter, options); } async findOneAndUpdate(filter, update, options) { return __classPrivateFieldGet(this, _Collection_commands, "f").findOneAndUpdate(filter, update, options); } async options(options) { const resp = await __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 __classPrivateFieldGet(this, _Collection_db, "f").dropCollection(this.name, { keyspace: this.keyspace, ...options }); } get _httpClient() { return __classPrivateFieldGet(this, _Collection_httpClient, "f"); } } _Collection_httpClient = new WeakMap(), _Collection_commands = new WeakMap(), _Collection_db = new WeakMap();