var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var arango_exports = {}; __export(arango_exports, { CollectionType: () => import_collection.CollectionType, IfCollectionDoesNotExistOnGet: () => IfCollectionDoesNotExistOnGet, IfCollectionExistsOnCreate: () => IfCollectionExistsOnCreate, IfDbDoesNotExistOnGet: () => IfDbDoesNotExistOnGet, IfDbExistsOnCreate: () => IfDbExistsOnCreate, canConnectToDbServer: () => canConnectToDbServer, canNotConnectToDbServer: () => canNotConnectToDbServer, collectionDocCount: () => collectionDocCount, collectionDoesNotExist: () => collectionDoesNotExist, collectionExists: () => collectionExists, createCollection: () => createCollection, createDb: () => createDb, createDocCollection: () => createDocCollection, createEdgeCollection: () => createEdgeCollection, dbDoesNotExist: () => dbDoesNotExist, dbExists: () => dbExists, dbIsConnected: () => dbIsConnected, dbIsNotConnected: () => dbIsNotConnected, documentDoesNotExist: () => documentDoesNotExist, documentDoesNotExistById: () => documentDoesNotExistById, documentExists: () => documentExists, documentExistsById: () => documentExistsById, dropAllDatabases: () => dropAllDatabases, dropCollection: () => dropCollection, dropDb: () => dropDb, getCollection: () => getCollection, getCollectionType: () => getCollectionType, getDb: () => getDb, getDocCollection: () => getDocCollection, getEdgeCollection: () => getEdgeCollection, getSysDb: () => getSysDb, isNotSysDb: () => isNotSysDb, isSysDb: () => isSysDb, nonSystemDbsExists: () => nonSystemDbsExists }); module.exports = __toCommonJS(arango_exports); // utils/ArangoUtilsTypes.ts var import_collection = require("arangojs/collection"); var IfDbExistsOnCreate; (function(IfDbExistsOnCreate2) { IfDbExistsOnCreate2["ThrowError"] = "throw-error"; IfDbExistsOnCreate2["Overwrite"] = "overwrite"; IfDbExistsOnCreate2["ReturnExisting"] = "return-existing"; })(IfDbExistsOnCreate || (IfDbExistsOnCreate = {})); var IfDbDoesNotExistOnGet; (function(IfDbDoesNotExistOnGet2) { IfDbDoesNotExistOnGet2["ThrowError"] = "throw-error"; IfDbDoesNotExistOnGet2["Create"] = "create"; })(IfDbDoesNotExistOnGet || (IfDbDoesNotExistOnGet = {})); var IfCollectionExistsOnCreate; (function(IfCollectionExistsOnCreate2) { IfCollectionExistsOnCreate2["ThrowError"] = "throw-error"; IfCollectionExistsOnCreate2["Overwrite"] = "overwrite"; IfCollectionExistsOnCreate2["ReturnExisting"] = "return-existing"; })(IfCollectionExistsOnCreate || (IfCollectionExistsOnCreate = {})); var IfCollectionDoesNotExistOnGet; (function(IfCollectionDoesNotExistOnGet2) { IfCollectionDoesNotExistOnGet2["ThrowError"] = "throw-error"; IfCollectionDoesNotExistOnGet2["Create"] = "create"; })(IfCollectionDoesNotExistOnGet || (IfCollectionDoesNotExistOnGet = {})); // utils/arangoUtils.ts var import_ramda = require("ramda"); var import_arangojs = require("arangojs"); var import_utils = require("@stcland/utils"); var import_errors = require("@stcland/errors"); var canConnectToDbServer = /* @__PURE__ */ __name(async (hostConfig) => { const sysDb = await getSysDb(hostConfig); return dbIsConnected(sysDb); }, "canConnectToDbServer"); var canNotConnectToDbServer = (0, import_utils.asyncComplement)(canConnectToDbServer); var getSysDb = /* @__PURE__ */ __name(async (hostConfig, opts) => { const { url = "", username = "", password = "" } = hostConfig || {}; const sysDb = new import_arangojs.Database({ url, auth: { username, password } }); if (opts?.checkConnection) { if (await dbIsNotConnected(sysDb)) throw new Error("Sys database connection failed"); } return sysDb; }, "getSysDb"); var dbIsConnected = /* @__PURE__ */ __name(async (db) => { try { await db.get(); return true; } catch (error) { return false; } }, "dbIsConnected"); var dbIsNotConnected = (0, import_utils.asyncComplement)(dbIsConnected); var isSysDb = /* @__PURE__ */ __name((db) => db.name === "_system", "isSysDb"); var isNotSysDb = (0, import_ramda.complement)(isSysDb); var dbExists = /* @__PURE__ */ __name(async (dbOrArangoHostConfig, dbName) => { const db = await getDbFromVarious(dbOrArangoHostConfig); if (isNotSysDb(db)) return db.exists(); const existingDbs = await db.listDatabases(); return existingDbs.includes(dbName); }, "dbExists"); var dbDoesNotExist = (0, import_utils.asyncComplement)(dbExists); var createDb = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig, dbName, dbUsers, ifDbExists = IfDbExistsOnCreate.ThrowError) => { const sysDb = await getDbFromVarious(sysDbOrArangoHostConfig); if (isNotSysDb(sysDb)) throw new Error("createArangoDb(): non system DB provided: " + sysDb.name); let requestedDbExists = await dbExists(sysDb, dbName); if (requestedDbExists && ifDbExists === IfDbExistsOnCreate.ThrowError) throw new Error(`Attempting to create arango database '${dbName}', but it already exists`); if (requestedDbExists && ifDbExists === IfDbExistsOnCreate.Overwrite) { await sysDb.dropDatabase(dbName); requestedDbExists = false; } if (!requestedDbExists) await sysDb.createDatabase(dbName, { users: dbUsers }); return sysDb.database(dbName); }, "createDb"); var getDb = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig, dbName, ifDbDoesNotExist = IfDbDoesNotExistOnGet.ThrowError, dbUsers) => { const sysDb = await getDbFromVarious(sysDbOrArangoHostConfig); if (isNotSysDb(sysDb)) throw new Error("getArangoDb(): non system DB provided: " + sysDb.name); const requestedDbExists = await dbExists(sysDb, dbName); if (!requestedDbExists && ifDbDoesNotExist === IfDbDoesNotExistOnGet.ThrowError) throw new Error(`Attempting to fetch database '${dbName}', but it does not exit`); if (!requestedDbExists) await sysDb.createDatabase(dbName, { users: dbUsers }); return sysDb.database(dbName); }, "getDb"); var dropDb = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig, dbName) => { const sysDb = await getDbFromVarious(sysDbOrArangoHostConfig); if (isNotSysDb(sysDb)) throw new Error("deleteArangoDb(): non system DB provided: " + sysDb.name); if (await dbDoesNotExist(sysDb, dbName)) return false; try { await sysDb.dropDatabase(dbName); } catch (error) { console.warn(`deleteArangoDb(): Error deleting database '${dbName}': `, error); return false; } return true; }, "dropDb"); var dropAllDatabases = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig) => { let success = true; const sysDb = await getDbFromVarious(sysDbOrArangoHostConfig); if (isNotSysDb(sysDb)) throw new Error("deleteAllArangoDatabases(): non system DB provided: " + sysDb.name); const existingDbs = await sysDb.listDatabases(); for (const dbName of existingDbs) { if (dbName.startsWith("_")) continue; try { await sysDb.dropDatabase(dbName); } catch (error) { console.warn(`deleteAllArangoDatabases(): Error deleting database '${dbName}': `, error); success = false; } } return success; }, "dropAllDatabases"); var nonSystemDbsExists = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig) => { const sysDb = await getDbFromVarious(sysDbOrArangoHostConfig); if (isNotSysDb(sysDb)) throw new Error("nonSystemDbsExists(): non system DB provided: " + sysDb.name); const existingDbs = await sysDb.listDatabases(); return existingDbs.some((dbName) => !dbName.startsWith("_")); }, "nonSystemDbsExists"); var documentExists = /* @__PURE__ */ __name(async (db, collectionName, documentKey) => { try { const collection = db.collection(collectionName); const exists = await collection.documentExists(documentKey); return exists; } catch (error) { return false; } }, "documentExists"); var documentDoesNotExist = (0, import_utils.asyncComplement)(documentExists); var documentExistsById = /* @__PURE__ */ __name(async (db, documentId) => { const [collectionName, documentKey] = documentId.split("/"); return documentExists(db, collectionName, documentKey); }, "documentExistsById"); var documentDoesNotExistById = (0, import_utils.asyncComplement)(documentExistsById); var collectionExists = /* @__PURE__ */ __name((db, collectionName) => db.collection(collectionName).exists(), "collectionExists"); var collectionDoesNotExist = (0, import_utils.asyncComplement)(collectionExists); var createCollection = /* @__PURE__ */ __name(async (db, collectionName, opts) => { const { ifExists = IfCollectionExistsOnCreate.ThrowError, type = import_collection.CollectionType.EDGE_COLLECTION } = opts || {}; const collection = db.collection(collectionName); let collectionExists2 = await collection.exists(); if (collectionExists2 && ifExists === IfCollectionExistsOnCreate.ThrowError) throw new Error(`DB ${db.name}: Attempting to create collection '${collectionName}', but it already exists`); if (collectionExists2 && ifExists === IfCollectionExistsOnCreate.Overwrite) { await collection.drop(); collectionExists2 = false; } if (!collectionExists2) await collection.create({ type }); return collection; }, "createCollection"); var collectionTypeToString = /* @__PURE__ */ __name((type) => type === import_collection.CollectionType.DOCUMENT_COLLECTION ? "document" : "edge", "collectionTypeToString"); var getCollection = /* @__PURE__ */ __name(async (db, collectionName, ifCollectionDoesNotExist = IfCollectionDoesNotExistOnGet.ThrowError, collectionType) => { const collection = db.collection(collectionName); const collectionExists2 = await collection.exists(); const existingCollectionType = collectionExists2 ? (await collection.properties()).type : null; (0, import_errors.throwIf)(!collectionExists2 && ifCollectionDoesNotExist === IfCollectionDoesNotExistOnGet.ThrowError, `getCollection() DB ${db.name}: Attempting to fetch collection '${collectionName}', but it does not exist`); (0, import_errors.throwIf)(!collectionExists2 && ifCollectionDoesNotExist === IfCollectionDoesNotExistOnGet.Create && !collectionType, `getCollection() DB ${db.name}: Need to create '${collectionName}', but collection type not provided`); (0, import_errors.throwIf)(collectionExists2 && collectionType && collectionType !== existingCollectionType, `getCollection() DB ${db.name}: fetching collection '${collectionName}' Collection exists, but not of requested type: ${collectionTypeToString(collectionType)} collection`); if (!collectionExists2) await collection.create({ type: collectionType }); return collection; }, "getCollection"); var getDocCollection = /* @__PURE__ */ __name(async (db, collectionName, ifCollectionDoesNotExist = IfCollectionDoesNotExistOnGet.ThrowError) => getCollection(db, collectionName, ifCollectionDoesNotExist, import_collection.CollectionType.DOCUMENT_COLLECTION), "getDocCollection"); var getEdgeCollection = /* @__PURE__ */ __name(async (db, collectionName, ifCollectionDoesNotExist = IfCollectionDoesNotExistOnGet.ThrowError) => getCollection(db, collectionName, ifCollectionDoesNotExist, import_collection.CollectionType.EDGE_COLLECTION), "getEdgeCollection"); var getCollectionType = /* @__PURE__ */ __name(async (collectionOrDb, collectionNameOrUndefined) => { const collection = collectionNameOrUndefined ? collectionOrDb.collection(collectionNameOrUndefined) : collectionOrDb; const type = (await collection.properties()).type; return type; }, "getCollectionType"); var dropCollection = /* @__PURE__ */ __name(async (db, collectionName) => { const collection = db.collection(collectionName); if (await collection.exists()) { await collection.drop(); return true; } console.warn(`DB ${db.name}: Attempting to drop collection '${collectionName}', but it does not exist`); return false; }, "dropCollection"); var collectionDocCount = /* @__PURE__ */ __name(async (collection) => { const c = await collection.count(); return c.count; }, "collectionDocCount"); var createDocCollection = /* @__PURE__ */ __name(async (db, collectionName, ifExists = IfCollectionExistsOnCreate.ThrowError) => { const opts = { ifExists, type: import_collection.CollectionType.DOCUMENT_COLLECTION }; return createCollection(db, collectionName, opts); }, "createDocCollection"); var createEdgeCollection = /* @__PURE__ */ __name(async (db, collectionName, ifExists = IfCollectionExistsOnCreate.ThrowError) => { const opts = { ifExists, type: import_collection.CollectionType.EDGE_COLLECTION }; return createCollection(db, collectionName, opts); }, "createEdgeCollection"); var isHostConfig = /* @__PURE__ */ __name((arg) => { return arg.url && typeof arg.url === "string"; }, "isHostConfig"); var getDbFromVarious = /* @__PURE__ */ __name(async (sysDbOrArangoHostConfig) => isHostConfig(sysDbOrArangoHostConfig) ? await getSysDb(sysDbOrArangoHostConfig) : sysDbOrArangoHostConfig, "getDbFromVarious"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CollectionType, IfCollectionDoesNotExistOnGet, IfCollectionExistsOnCreate, IfDbDoesNotExistOnGet, IfDbExistsOnCreate, canConnectToDbServer, canNotConnectToDbServer, collectionDocCount, collectionDoesNotExist, collectionExists, createCollection, createDb, createDocCollection, createEdgeCollection, dbDoesNotExist, dbExists, dbIsConnected, dbIsNotConnected, documentDoesNotExist, documentDoesNotExistById, documentExists, documentExistsById, dropAllDatabases, dropCollection, dropDb, getCollection, getCollectionType, getDb, getDocCollection, getEdgeCollection, getSysDb, isNotSysDb, isSysDb, nonSystemDbsExists });