Skip to content

Commit

Permalink
support listTables with nameOnly option
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 4, 2025
1 parent ca6ab11 commit 05fef52
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/driver/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ function _updateHasKey(update: Record<string, any>, key: string) {
}

/**
* Mongoose expects listIndexes() to return a cursor but Astra returns an array.
* Mongoose expects listIndexes() to return a cursor but Astra returns an array. Mongoose itself doesn't support
* returning a cursor from Model.listIndexes(), so all we need to return is an object with a toArray() function.
*/

class AsyncCursorPlaceholder<ValueType = unknown> {
Expand Down
13 changes: 10 additions & 3 deletions src/driver/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import { Collection } from './collection';
import { AstraAdmin, CreateTableDefinition, DataAPIDbAdmin, RawDataAPIResponse } from '@datastax/astra-db-ts';
import { AstraAdmin, CreateTableDefinition, DataAPIDbAdmin, ListTablesOptions, RawDataAPIResponse, TableDescriptor } from '@datastax/astra-db-ts';
import { Db } from './db';
import { default as MongooseConnection } from 'mongoose/lib/connection';
import { STATES } from 'mongoose';
Expand Down Expand Up @@ -157,9 +157,16 @@ export class Connection extends MongooseConnection {
/**
* List all tables in the database
*/
async listTables() {

async listTables(options: ListTablesOptions & { nameOnly: true }): Promise<string[]>;
async listTables(options: ListTablesOptions & { nameOnly: false }): Promise<TableDescriptor[]>;

async listTables(options: ListTablesOptions) {
await this._waitForClient();
return this.db!.listTables();
if (options.nameOnly) {
return this.db!.listTables({ ...options, nameOnly: true });
}
return this.db!.listTables({ ...options, nameOnly: false });
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/driver/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { Db as AstraDb, CreateTableDefinition, RawDataAPIResponse } from '@datastax/astra-db-ts';
import { Db as AstraDb, CreateTableDefinition, ListTablesOptions, RawDataAPIResponse } from '@datastax/astra-db-ts';

export class Db {
astraDb: AstraDb;
Expand Down Expand Up @@ -100,8 +100,11 @@ export class Db {
/**
* List all tables in the database.
*/
async listTables() {
return this.astraDb.listTables();
async listTables(options: ListTablesOptions) {
if (options.nameOnly) {
return this.astraDb.listTables({ ...options, nameOnly: true });
}
return this.astraDb.listTables({ ...options, nameOnly: false });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/driver/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ describe('Mongoose Model API level tests', async () => {
it('API ops tests createConnection() with uri and options', async function() {
const connection = mongooseInstance.createConnection(testClient!.uri, testClient!.options) as unknown as StargateMongooseDriver.Connection;
await connection.asPromise();
const promise = process.env.DATA_API_TABLES ? connection.listTables() : connection.listCollections();
const promise = process.env.DATA_API_TABLES ? connection.listTables({ nameOnly: false }) : connection.listCollections();
assert.ok((await promise.then(res => res.map(obj => obj.name))).includes(Product.collection.collectionName));

await assert.rejects(
Expand Down Expand Up @@ -1024,7 +1024,7 @@ describe('Mongoose Model API level tests', async () => {
});
it('API ops tests createConnection() with queueing', async function() {
const connection = mongooseInstance.createConnection() as unknown as StargateMongooseDriver.Connection;
const promise = process.env.DATA_API_TABLES ? connection.listTables() : connection.listCollections();
const promise = process.env.DATA_API_TABLES ? connection.listTables({ nameOnly: false }) : connection.listCollections();

await connection.openUri(testClient!.uri, testClient!.options);
assert.ok((await promise.then(res => res.map(obj => obj.name))).includes(Product.collection.collectionName));
Expand Down
5 changes: 2 additions & 3 deletions tests/driver/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ describe('Driver based tests', async () => {
});
const Person = mongooseInstance.model('Person', personSchema, TEST_COLLECTION_NAME);
before(async function () {
const tables = await mongooseInstance.connection.listTables();
const tableNames = tables.map(t => t.name);
const tableNames = await mongooseInstance.connection.listTables({ nameOnly: true });

if (process.env.DATA_API_TABLES) {
await mongooseInstance.connection.dropTable(TEST_COLLECTION_NAME);
Expand Down Expand Up @@ -234,7 +233,7 @@ describe('Driver based tests', async () => {
const mongooseInstance = await createMongooseInstance();
const TestModel = mongooseInstance.model('Person', Person.schema, TEST_COLLECTION_NAME);
if (process.env.DATA_API_TABLES) {
const tableNames = await mongooseInstance.connection.listTables().then(t => t.map(t => t.name));
const tableNames = await mongooseInstance.connection.listTables({ nameOnly: true });
if (!tableNames.includes(TEST_COLLECTION_NAME)) {
await mongooseInstance.connection.createTable(TEST_COLLECTION_NAME, {
primaryKey: '_id',
Expand Down
2 changes: 1 addition & 1 deletion tests/driver/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('tables', function() {
await mongooseInstance.connection.dropTable(TEST_TABLE_NAME);

await mongooseInstance.connection.createTable(TEST_TABLE_NAME, { primaryKey: '_id', columns: { _id: 'text' } });
const tableNames = await mongooseInstance.connection.listTables().then(tables => tables.map(t => t.name));
const tableNames = await mongooseInstance.connection.listTables({ nameOnly: false }).then(tables => tables.map(t => t.name));
assert.ok(tableNames.includes(TEST_TABLE_NAME));
await mongooseInstance.connection.dropTable(TEST_TABLE_NAME);

Expand Down
3 changes: 1 addition & 2 deletions tests/mongooseFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ async function createNamespace() {
export async function createMongooseCollections() {
await createNamespace();

const tables = await mongooseInstance.connection.listTables();
const tableNames = tables.map(t => t.name);
const tableNames = await mongooseInstance.connection.listTables({ nameOnly: true });

if (useTables) {
await mongooseInstance.connection.dropTable(Cart.collection.collectionName);
Expand Down

0 comments on commit 05fef52

Please sign in to comment.