diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b7e07d..36c8bfbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ This driver uses semantic versioning: ### Changed -- Split the Collection type parameter into result and input types [#807](https://github.com/arangodb/arangojs/issues/807) +- Split the Collection type parameter into result and input types ([#807](https://github.com/arangodb/arangojs/issues/807)) It is now possible to specify a separate type for the data passed when creating or modifying documents in addition to the type of the data returned @@ -31,6 +31,11 @@ This driver uses semantic versioning: This option was introduced in 3.12.1 and allows skipping the fast lock round. +- Added non-specific `EnsureIndexOptions` type and `ensureIndex` method + signature ([#778](https://github.com/arangodb/arangojs/issues/778)) + + This allows creating indexes without narrowing the index type. + ## [9.0.0-preview.4] - 2024-06-18 ### Added diff --git a/src/collection.ts b/src/collection.ts index a3f97d8e..edb9aa65 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -40,6 +40,7 @@ import { TtlIndex, MdiIndex, _indexHandle, + EnsureIndexOptions, } from "./indexes.js"; import { COLLECTION_NOT_FOUND, DOCUMENT_NOT_FOUND } from "./lib/codes.js"; @@ -2006,6 +2007,27 @@ export interface DocumentCollection< ensureIndex( details: EnsureInvertedIndexOptions ): Promise>; + /** + * Creates an index on the collection if it does not already exist. + * + * @param details - Options for creating the index. + * + * @example + * ```js + * const db = new Database(); + * const collection = db.collection("some-collection"); + * // Create a unique index for looking up documents by username + * await collection.ensureIndex({ + * type: "persistent", + * fields: ["username"], + * name: "unique-usernames", + * unique: true + * }); + * ``` + */ + ensureIndex( + details: EnsureIndexOptions + ): Promise>; /** * Deletes the index with the given name or `id` from the database. * diff --git a/src/indexes.ts b/src/indexes.ts index dd7014c1..8c824ea2 100644 --- a/src/indexes.ts +++ b/src/indexes.ts @@ -518,6 +518,16 @@ export type EnsureInvertedIndexOptions = { optimizeTopK?: string[]; }; +/** + * Options for creating an index. + */ +export type EnsureIndexOptions = + | EnsurePersistentIndexOptions + | EnsureGeoIndexOptions + | EnsureTtlIndexOptions + | EnsureMdiIndexOptions + | EnsureInvertedIndexOptions; + /** * Shared attributes of all index types. */