@datastax/astra-db-ts
Version:
Data API TypeScript client
168 lines (167 loc) • 7.75 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
var _Table_httpClient, _Table_commands, _Table_db;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
const tslib_1 = require("tslib");
const index_js_1 = require("../../documents/index.js");
const command_impls_js_1 = require("../../documents/commands/command-impls.js");
const hierarchical_logger_js_1 = require("../../lib/logging/hierarchical-logger.js");
const constants_js_1 = require("../../lib/constants.js");
const json_bigint_1 = tslib_1.__importDefault(require("json-bigint"));
const ser_des_js_1 = require("../../documents/tables/ser-des/ser-des.js");
const utils_js_1 = require("../../lib/api/ser-des/utils.js");
const internal_logger_js_1 = require("../../lib/logging/internal-logger.js");
const errors_js_1 = require("../../lib/errors.js");
const jbi = (0, json_bigint_1.default)({ storeAsString: true });
class Table extends hierarchical_logger_js_1.HierarchicalLogger {
static schema(schema) {
return schema;
}
constructor(db, httpClient, name, rootOpts, opts) {
const loggingConfig = internal_logger_js_1.InternalLogger.cfg.concatParseWithin([rootOpts.dbOptions.logging], opts, 'logging');
super(db, loggingConfig);
_Table_httpClient.set(this, void 0);
_Table_commands.set(this, void 0);
_Table_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,
});
Object.defineProperty(this, 'keyspace', {
value: opts?.keyspace ?? db.keyspace,
});
const hack = {
parseWithBigNumbers(json) {
return json.includes('{"type":"varint"}') || json.includes('{"type":"decimal"}') || json.includes('{"type":"bigint"}') || json.includes('{"type":"counter"}');
},
parser: (0, utils_js_1.withJbiNullProtoFix)(jbi),
};
tslib_1.__classPrivateFieldSet(this, _Table_httpClient, httpClient.forTableSlashCollectionOrWhateverWeWouldCallTheUnionOfTheseTypes(this, opts, hack), "f");
tslib_1.__classPrivateFieldSet(this, _Table_commands, new command_impls_js_1.CommandImpls(this, tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f"), new ser_des_js_1.TableSerDes(ser_des_js_1.TableSerDes.cfg.parse(opts?.serdes))), "f");
tslib_1.__classPrivateFieldSet(this, _Table_db, db, "f");
Object.defineProperty(this, constants_js_1.$CustomInspect, {
value: () => `Table(keyspace="${this.keyspace}",name="${this.name}")`,
});
}
async insertOne(row, options) {
return tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").insertOne(row, options);
}
async insertMany(rows, options) {
return tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").insertMany(rows, options, index_js_1.TableInsertManyError);
}
async updateOne(filter, update, options) {
await tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").updateOne(filter, update, options);
}
async deleteOne(filter, options) {
await tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").deleteOne(filter, options);
}
async deleteMany(filter, options) {
await tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").deleteMany(filter, options, (e) => errors_js_1.NonErrorError.asError(e));
}
find(filter, options) {
return tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").find(filter, options, index_js_1.TableFindCursor);
}
async findOne(filter, options) {
return tslib_1.__classPrivateFieldGet(this, _Table_commands, "f").findOne(filter, options);
}
async alter(options) {
await tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
alterTable: {
operation: options.operation,
},
}, {
timeoutManager: tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
return this;
}
async createIndex(name, column, options) {
const includeOptions = !!options?.options && Object.keys(options.options).length > 0;
await tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
createIndex: {
name: name,
definition: {
column,
options: includeOptions ? {
caseSensitive: options.options?.caseSensitive,
normalize: options.options?.normalize,
ascii: options.options?.ascii,
} : undefined,
},
options: {
ifNotExists: options?.ifNotExists,
},
},
}, {
timeoutManager: tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async createVectorIndex(name, column, options) {
await tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
createVectorIndex: {
name: name,
definition: {
column,
options: {
sourceModel: options?.options?.sourceModel,
metric: options?.options?.metric,
},
},
options: {
ifNotExists: options?.ifNotExists,
},
},
}, {
timeoutManager: tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async createTextIndex(name, column, options) {
await tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
createTextIndex: {
name: name,
definition: {
column,
options: {
analyzer: options?.options?.analyzer,
},
},
options: {
ifNotExists: options?.ifNotExists,
},
},
}, {
timeoutManager: tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async definition(options) {
const resp = await tslib_1.__classPrivateFieldGet(this, _Table_db, "f").listTables({
timeout: options?.timeout,
keyspace: this.keyspace,
});
const table = resp.find((t) => t.name === this.name);
if (!table) {
throw new Error(`Can not get definition for table '${this.keyspace}.${this.name}'; table not found. Did you use the right keyspace?`);
}
return table.definition;
}
async drop(options) {
await tslib_1.__classPrivateFieldGet(this, _Table_db, "f").dropTable(this.name, { ...options, keyspace: this.keyspace });
}
get _httpClient() {
return tslib_1.__classPrivateFieldGet(this, _Table_httpClient, "f");
}
}
exports.Table = Table;
_Table_httpClient = new WeakMap(), _Table_commands = new WeakMap(), _Table_db = new WeakMap();