arangojs
Version:
The official ArangoDB JavaScript driver.
471 lines • 16.1 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collection = exports.CollectionStatus = exports.CollectionType = exports.collectionToString = exports.isArangoCollection = void 0;
/**
* ```ts
* import type {
* DocumentCollection,
* EdgeCollection,
* } from "arangojs/collections";
* ```
*
* The "collections" module provides collection related types and interfaces
* for TypeScript.
*
* @packageDocumentation
*/
const aql = __importStar(require("./aql.js"));
const documents = __importStar(require("./documents.js"));
const errors = __importStar(require("./errors.js"));
const indexes = __importStar(require("./indexes.js"));
const codes_js_1 = require("./lib/codes.js");
//#region ArangoCollection interface
/**
* Indicates whether the given value represents an {@link ArangoCollection}.
*
* @param collection - A value that might be a collection.
*/
function isArangoCollection(collection) {
return Boolean(collection && collection.isArangoCollection);
}
exports.isArangoCollection = isArangoCollection;
/**
* Coerces the given collection name or {@link ArangoCollection} object to
* a string representing the collection name.
*
* @param collection - Collection name or {@link ArangoCollection} object.
*/
function collectionToString(collection) {
if (isArangoCollection(collection)) {
return String(collection.name);
}
else
return String(collection);
}
exports.collectionToString = collectionToString;
//#endregion
//#region Shared types
/**
* Integer values indicating the collection type.
*/
var CollectionType;
(function (CollectionType) {
CollectionType[CollectionType["DOCUMENT_COLLECTION"] = 2] = "DOCUMENT_COLLECTION";
CollectionType[CollectionType["EDGE_COLLECTION"] = 3] = "EDGE_COLLECTION";
})(CollectionType || (exports.CollectionType = CollectionType = {}));
/**
* Integer values indicating the collection loading status.
*/
var CollectionStatus;
(function (CollectionStatus) {
CollectionStatus[CollectionStatus["NEWBORN"] = 1] = "NEWBORN";
CollectionStatus[CollectionStatus["UNLOADED"] = 2] = "UNLOADED";
CollectionStatus[CollectionStatus["LOADED"] = 3] = "LOADED";
CollectionStatus[CollectionStatus["UNLOADING"] = 4] = "UNLOADING";
CollectionStatus[CollectionStatus["DELETED"] = 5] = "DELETED";
CollectionStatus[CollectionStatus["LOADING"] = 6] = "LOADING";
})(CollectionStatus || (exports.CollectionStatus = CollectionStatus = {}));
//#endregion
//#region Collection class
/**
* @internal
*/
class Collection {
_name;
_db;
/**
* @internal
*/
constructor(db, name) {
this._name = name;
this._db = db;
}
get isArangoCollection() {
return true;
}
get database() {
return this._db;
}
get name() {
return this._name;
}
//#region Collection operations
get() {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}`,
});
}
async exists() {
try {
await this.get();
return true;
}
catch (err) {
if (errors.isArangoError(err) && err.errorNum === codes_js_1.COLLECTION_NOT_FOUND) {
return false;
}
throw err;
}
}
create(options = {}) {
const { waitForSyncReplication = undefined, enforceReplicationFactor = undefined, ...opts } = options;
if (opts.computedValues) {
opts.computedValues = opts.computedValues.map((computedValue) => {
if (aql.isAqlLiteral(computedValue.expression)) {
return {
...computedValue,
expression: computedValue.expression.toAQL(),
};
}
if (aql.isAqlQuery(computedValue.expression)) {
return {
...computedValue,
expression: computedValue.expression.query,
};
}
return computedValue;
});
}
const search = {};
if (typeof waitForSyncReplication === "boolean") {
search.waitForSyncReplication = waitForSyncReplication ? 1 : 0;
}
if (typeof enforceReplicationFactor === "boolean") {
search.enforceReplicationFactor = enforceReplicationFactor ? 1 : 0;
}
return this._db.request({
method: "POST",
pathname: "/_api/collection",
search,
body: {
...opts,
name: this._name,
},
});
}
properties(properties) {
if (!properties) {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/properties`,
});
}
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${encodeURIComponent(this._name)}/properties`,
body: properties,
});
}
count() {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/count`,
});
}
async recalculateCount() {
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${encodeURIComponent(this._name)}/recalculateCount`,
}, (res) => res.parsedBody.result);
}
figures(details = false) {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/figures`,
search: { details },
});
}
revision() {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/revision`,
});
}
checksum(options) {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/checksum`,
search: options,
});
}
shards(details) {
return this._db.request({
pathname: `/_api/collection/${encodeURIComponent(this._name)}/shards`,
search: { details },
});
}
async rename(newName) {
const result = await this._db.renameCollection(this._name, newName);
this._name = newName;
return result;
}
truncate(options) {
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${this._name}/truncate`,
search: options,
});
}
drop(options) {
return this._db.request({
method: "DELETE",
pathname: `/_api/collection/${encodeURIComponent(this._name)}`,
search: options,
});
}
compact() {
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${this._name}/compact`,
});
}
//#endregion
//#region Document operations
getResponsibleShard(document) {
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${encodeURIComponent(this._name)}/responsibleShard`,
body: document,
}, (res) => res.parsedBody.shardId);
}
documentId(selector) {
return documents._documentHandle(selector, this._name);
}
async documentExists(selector, options = {}) {
const { ifMatch = undefined, ifNoneMatch = undefined } = options;
const headers = {};
if (ifMatch)
headers["if-match"] = ifMatch;
if (ifNoneMatch)
headers["if-none-match"] = ifNoneMatch;
try {
return await this._db.request({
method: "HEAD",
pathname: `/_api/document/${encodeURI(documents._documentHandle(selector, this._name))}`,
headers,
}, (res) => {
if (ifNoneMatch && res.status === 304) {
throw new errors.HttpError(res);
}
return true;
});
}
catch (err) {
if (err.code === 404) {
return false;
}
throw err;
}
}
documents(selectors, options = {}) {
const { allowDirtyRead = undefined } = options;
return this._db.request({
method: "PUT",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
search: { onlyget: true },
allowDirtyRead,
body: selectors,
});
}
async document(selector, options = {}) {
if (typeof options === "boolean") {
options = { graceful: options };
}
const { allowDirtyRead = undefined, graceful = false, ifMatch = undefined, ifNoneMatch = undefined, } = options;
const headers = {};
if (ifMatch)
headers["if-match"] = ifMatch;
if (ifNoneMatch)
headers["if-none-match"] = ifNoneMatch;
const result = this._db.request({
pathname: `/_api/document/${encodeURI(documents._documentHandle(selector, this._name))}`,
headers,
allowDirtyRead,
}, (res) => {
if (ifNoneMatch && res.status === 304) {
throw new errors.HttpError(res);
}
return res.parsedBody;
});
if (!graceful)
return result;
try {
return await result;
}
catch (err) {
if (errors.isArangoError(err) && err.errorNum === codes_js_1.DOCUMENT_NOT_FOUND) {
return null;
}
throw err;
}
}
save(data, options) {
return this._db.request({
method: "POST",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
body: data,
search: options,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
saveAll(data, options) {
return this._db.request({
method: "POST",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
body: data,
search: options,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
replace(selector, newData, options = {}) {
const { ifMatch = undefined, ...opts } = options;
const headers = {};
if (ifMatch)
headers["if-match"] = ifMatch;
return this._db.request({
method: "PUT",
pathname: `/_api/document/${encodeURI(documents._documentHandle(selector, this._name))}`,
headers,
body: newData,
search: opts,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
replaceAll(newData, options) {
return this._db.request({
method: "PUT",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
body: newData,
search: options,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
update(selector, newData, options = {}) {
const { ifMatch = undefined, ...opts } = options;
const headers = {};
if (ifMatch)
headers["if-match"] = ifMatch;
return this._db.request({
method: "PATCH",
pathname: `/_api/document/${encodeURI(documents._documentHandle(selector, this._name))}`,
headers,
body: newData,
search: opts,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
updateAll(newData, options) {
return this._db.request({
method: "PATCH",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
body: newData,
search: options,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
remove(selector, options = {}) {
const { ifMatch = undefined, ...opts } = options;
const headers = {};
if (ifMatch)
headers["if-match"] = ifMatch;
return this._db.request({
method: "DELETE",
pathname: `/_api/document/${encodeURI(documents._documentHandle(selector, this._name))}`,
headers,
search: opts,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
removeAll(selectors, options) {
return this._db.request({
method: "DELETE",
pathname: `/_api/document/${encodeURIComponent(this._name)}`,
body: selectors,
search: options,
}, (res) => (options?.silent ? undefined : res.parsedBody));
}
import(data, options = {}) {
const search = { ...options, collection: this._name };
if (Array.isArray(data)) {
search.type = Array.isArray(data[0]) ? undefined : "documents";
const lines = data;
data = lines.map((line) => JSON.stringify(line)).join("\r\n") + "\r\n";
}
return this._db.request({
method: "POST",
pathname: "/_api/import",
body: data,
isBinary: true,
search,
});
}
//#endregion
//#region Edge operations
_edges(selector, options = {}, direction) {
const { allowDirtyRead = undefined } = options;
return this._db.request({
pathname: `/_api/edges/${encodeURIComponent(this._name)}`,
allowDirtyRead,
search: {
direction,
vertex: documents._documentHandle(selector, this._name, false),
},
});
}
edges(vertex, options) {
return this._edges(vertex, options);
}
inEdges(vertex, options) {
return this._edges(vertex, options, "in");
}
outEdges(vertex, options) {
return this._edges(vertex, options, "out");
}
//#endregion
//#region Index operations
async loadIndexes() {
return this._db.request({
method: "PUT",
pathname: `/_api/collection/${encodeURIComponent(this._name)}/loadIndexesIntoMemory`,
}, (res) => res.parsedBody.result);
}
indexes(options) {
return this._db.request({
pathname: "/_api/index",
search: { collection: this._name, ...options },
}, (res) => res.parsedBody.indexes);
}
index(selector) {
return this._db.request({
pathname: `/_api/index/${encodeURI(indexes._indexHandle(selector, this._name))}`,
});
}
ensureIndex(options) {
return this._db.request({
method: "POST",
pathname: "/_api/index",
body: options,
search: { collection: this._name },
});
}
dropIndex(selector) {
return this._db.request({
method: "DELETE",
pathname: `/_api/index/${encodeURI(indexes._indexHandle(selector, this._name))}`,
});
}
}
exports.Collection = Collection;
//#endregion
//# sourceMappingURL=collections.js.map