Skip to content

Commit

Permalink
remove index information utility
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Mar 15, 2024
1 parent 595aae4 commit 87a4e0a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 54 deletions.
17 changes: 8 additions & 9 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
} from './mongo_types';
import type { AggregateOptions } from './operations/aggregate';
import { BulkWriteOperation } from './operations/bulk_write';
import type { IndexInformationOptions } from './operations/common_functions';
import { CountOperation, type CountOptions } from './operations/count';
import { CountDocumentsOperation, type CountDocumentsOptions } from './operations/count_documents';
import {
Expand All @@ -49,15 +48,15 @@ import {
FindOneAndUpdateOperation,
type FindOneAndUpdateOptions
} from './operations/find_and_modify';
import {
CreateIndexesOperation,
type CreateIndexesOptions,
type DropIndexesOptions,
DropIndexOperation,
type IndexDescription,
type IndexSpecification,
type ListIndexesOptions
import type {
CreateIndexesOptions,
DropIndexesOptions,
IndexDescription,
IndexInformationOptions,
IndexSpecification,
ListIndexesOptions
} from './operations/indexes';
import { CreateIndexesOperation, DropIndexOperation } from './operations/indexes';
import {
InsertManyOperation,
type InsertManyResult,
Expand Down
7 changes: 5 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { MongoClient, PkFactory } from './mongo_client';
import type { TODO_NODE_3286 } from './mongo_types';
import type { AggregateOptions } from './operations/aggregate';
import { CollectionsOperation } from './operations/collections';
import type { IndexInformationOptions } from './operations/common_functions';
import {
CreateCollectionOperation,
type CreateCollectionOptions
Expand All @@ -23,7 +22,11 @@ import {
type DropDatabaseOptions
} from './operations/drop';
import { executeOperation } from './operations/execute_operation';
import { type CreateIndexesOptions, type IndexSpecification } from './operations/indexes';
import type {
CreateIndexesOptions,
IndexInformationOptions,
IndexSpecification
} from './operations/indexes';
import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
import { ProfilingLevelOperation, type ProfilingLevelOptions } from './operations/profiling_level';
import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user';
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ export type {
CommandOperationOptions,
OperationParent
} from './operations/command';
export type { IndexInformationOptions } from './operations/common_functions';
export type { CountOptions } from './operations/count';
export type { CountDocumentsOptions } from './operations/count_documents';
export type {
Expand All @@ -466,6 +465,7 @@ export type {
FindOneAndReplaceOptions,
FindOneAndUpdateOptions
} from './operations/find_and_modify';
export type { IndexInformationOptions } from './operations/indexes';
export type {
CreateIndexesOptions,
DropIndexesOptions,
Expand Down
42 changes: 0 additions & 42 deletions src/operations/common_functions.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
import type { Document } from '../bson';
import type { Collection } from '../collection';
import type { Db } from '../db';
import type { ReadPreference } from '../read_preference';
import type { ClientSession } from '../sessions';

/** @public */
export interface IndexInformationOptions {
full?: boolean;
readPreference?: ReadPreference;
session?: ClientSession;
}
/**
* Retrieves this collections index info.
*
* @param db - The Db instance on which to retrieve the index info.
* @param name - The name of the collection.
*/
export async function indexInformation(db: Db, name: string): Promise<any>;
export async function indexInformation(
db: Db,
name: string,
options?: IndexInformationOptions
): Promise<any>;
export async function indexInformation(
db: Db,
name: string,
options?: IndexInformationOptions
): Promise<any> {
if (options == null) {
options = {};
}
// If we specified full information
const full = options.full == null ? false : options.full;
// Get the list of indexes of the specified collection
const indexes = await db.collection(name).listIndexes(options).toArray();
if (full) return indexes;

const info: Record<string, Array<[string, unknown]>> = {};
for (const index of indexes) {
info[index.name] = Object.entries(index.key);
}
return info;
}

export function maybeAddIdToDocuments(
coll: Collection,
Expand Down
8 changes: 8 additions & 0 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Document } from '../bson';
import type { Collection } from '../collection';
import { MongoCompatibilityError } from '../error';
import { type OneOrMore } from '../mongo_types';
import { type ReadPreference } from '../read_preference';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils';
Expand Down Expand Up @@ -70,6 +71,13 @@ export type IndexSpecification = OneOrMore<
| Map<string, IndexDirection>
>;

/** @public */
export interface IndexInformationOptions {
full?: boolean;
readPreference?: ReadPreference;
session?: ClientSession;
}

/** @public */
export interface IndexDescription
extends Pick<
Expand Down

0 comments on commit 87a4e0a

Please sign in to comment.