@datastax/astra-db-ts
Version:
Data API TypeScript client
164 lines (163 loc) • 7.29 kB
JavaScript
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
var _Table_httpClient, _Table_commands, _Table_db;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { TableFindCursor, TableInsertManyError } from '../../documents/index.js';
import { CommandImpls } from '../../documents/commands/command-impls.js';
import { HierarchicalLogger } from '../../lib/logging/hierarchical-logger.js';
import { $CustomInspect } from '../../lib/constants.js';
import JBI from 'json-bigint';
import { TableSerDes } from '../../documents/tables/ser-des/ser-des.js';
import { withJbiNullProtoFix } from '../../lib/api/ser-des/utils.js';
import { InternalLogger } from '../../lib/logging/internal-logger.js';
import { NonErrorError } from '../../lib/errors.js';
const jbi = JBI({ storeAsString: true });
export class Table extends HierarchicalLogger {
static schema(schema) {
return schema;
}
constructor(db, httpClient, name, rootOpts, opts) {
const loggingConfig = 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: withJbiNullProtoFix(jbi),
};
__classPrivateFieldSet(this, _Table_httpClient, httpClient.forTableSlashCollectionOrWhateverWeWouldCallTheUnionOfTheseTypes(this, opts, hack), "f");
__classPrivateFieldSet(this, _Table_commands, new CommandImpls(this, __classPrivateFieldGet(this, _Table_httpClient, "f"), new TableSerDes(TableSerDes.cfg.parse(opts?.serdes))), "f");
__classPrivateFieldSet(this, _Table_db, db, "f");
Object.defineProperty(this, $CustomInspect, {
value: () => `Table(keyspace="${this.keyspace}",name="${this.name}")`,
});
}
async insertOne(row, options) {
return __classPrivateFieldGet(this, _Table_commands, "f").insertOne(row, options);
}
async insertMany(rows, options) {
return __classPrivateFieldGet(this, _Table_commands, "f").insertMany(rows, options, TableInsertManyError);
}
async updateOne(filter, update, options) {
await __classPrivateFieldGet(this, _Table_commands, "f").updateOne(filter, update, options);
}
async deleteOne(filter, options) {
await __classPrivateFieldGet(this, _Table_commands, "f").deleteOne(filter, options);
}
async deleteMany(filter, options) {
await __classPrivateFieldGet(this, _Table_commands, "f").deleteMany(filter, options, (e) => NonErrorError.asError(e));
}
find(filter, options) {
return __classPrivateFieldGet(this, _Table_commands, "f").find(filter, options, TableFindCursor);
}
async findOne(filter, options) {
return __classPrivateFieldGet(this, _Table_commands, "f").findOne(filter, options);
}
async alter(options) {
await __classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
alterTable: {
operation: options.operation,
},
}, {
timeoutManager: __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 __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: __classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async createVectorIndex(name, column, options) {
await __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: __classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async createTextIndex(name, column, options) {
await __classPrivateFieldGet(this, _Table_httpClient, "f").executeCommand({
createTextIndex: {
name: name,
definition: {
column,
options: {
analyzer: options?.options?.analyzer,
},
},
options: {
ifNotExists: options?.ifNotExists,
},
},
}, {
timeoutManager: __classPrivateFieldGet(this, _Table_httpClient, "f").tm.single('tableAdminTimeoutMs', options),
});
}
async definition(options) {
const resp = await __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 __classPrivateFieldGet(this, _Table_db, "f").dropTable(this.name, { ...options, keyspace: this.keyspace });
}
get _httpClient() {
return __classPrivateFieldGet(this, _Table_httpClient, "f");
}
}
_Table_httpClient = new WeakMap(), _Table_commands = new WeakMap(), _Table_db = new WeakMap();