UNPKG

arangojs

Version:

The official ArangoDB JavaScript driver.

123 lines 3.2 kB
import { isArangoError } from "./error.js"; import { ANALYZER_NOT_FOUND } from "./lib/codes.js"; /** * Indicates whether the given value represents an {@link Analyzer}. * * @param analyzer - A value that might be an Analyzer. */ export function isArangoAnalyzer(analyzer) { return Boolean(analyzer && analyzer.isArangoAnalyzer); } /** * Represents an Analyzer in a {@link database.Database}. */ export class Analyzer { _name; _db; /** * @internal */ constructor(db, name) { this._db = db; this._name = name; } /** * @internal * * Indicates that this object represents an ArangoDB Analyzer. */ get isArangoAnalyzer() { return true; } /** * Name of this Analyzer. * * See also {@link database.Database}. */ get name() { return this._name; } /** * Checks whether the Analyzer exists. * * @example * ```js * const db = new Database(); * const analyzer = db.analyzer("some-analyzer"); * const result = await analyzer.exists(); * // result indicates whether the Analyzer exists * ``` */ async exists() { try { await this.get(); return true; } catch (err) { if (isArangoError(err) && err.errorNum === ANALYZER_NOT_FOUND) { return false; } throw err; } } /** * Retrieves the Analyzer definition for the Analyzer. * * @example * ```js * const db = new Database(); * const analyzer = db.analyzer("some-analyzer"); * const definition = await analyzer.get(); * // definition contains the Analyzer definition * ``` */ get() { return this._db.request({ path: `/_api/analyzer/${encodeURIComponent(this._name)}`, }); } /** * Creates a new Analyzer with the given `options` and the instance's name. * * See also {@link database.Database#createAnalyzer}. * * @param options - Options for creating the Analyzer. * * @example * ```js * const db = new Database(); * const analyzer = db.analyzer("potatoes"); * await analyzer.create({ type: "identity" }); * // the identity Analyzer "potatoes" now exists * ``` */ create(options) { return this._db.request({ method: "POST", path: "/_api/analyzer", body: { name: this._name, ...options }, }); } /** * Deletes the Analyzer from the database. * * @param force - Whether the Analyzer should still be deleted even if it * is currently in use. * * @example * ```js * const db = new Database(); * const analyzer = db.analyzer("some-analyzer"); * await analyzer.drop(); * // the Analyzer "some-analyzer" no longer exists * ``` */ drop(force = false) { return this._db.request({ method: "DELETE", path: `/_api/analyzer/${encodeURIComponent(this._name)}`, search: { force }, }); } } //# sourceMappingURL=analyzer.js.map