@tragedy-labs/sprite
Version:
A TypeScript driver for ArcadeDB
125 lines • 5.07 kB
JavaScript
import { Routes } from './routes.js';
import { EXPLAIN, SELECT_SCHEMA } from './commands/index.js';
import { Rest } from '../rest/Rest.js';
import { DatabaseSession } from '../session/DatabaseSession.js';
export var Dialect;
(function (Dialect) {
Dialect["SQL"] = "sql";
Dialect["SQLSCRIPT"] = "sqlscript";
Dialect["GRAPHQL"] = "graphql";
Dialect["CYPHER"] = "cypher";
Dialect["GREMLIN"] = "gremlin";
Dialect["MONGO"] = "mongo";
})(Dialect || (Dialect = {}));
/**
* Static methods for common database operations.
*/
class Database {
/**
* Set the credentials that the database client should use
* when interacting with the ArcadeDB server.
* @param username The username to authenticate with.
* @param password The password to authenticate with.
* @returns `true` if the credentials were set.
* @throws `Error` if the credentials could not be set.
*/
static createSession = (params) => {
// TODO: I don't think this even has an error case
// it probably should throw an error if something
// goes wrong.
return new DatabaseSession(params);
};
/**
* Static method to execute a command on the database.s
* @param session The {@link DatabaseSession `DatabaseSession`} to execute the command on.
* @param language The language of the command.
* @param command The command to execute.
* @param transaction The transaction to execute the command in.
* @returns The result of the command.
*/
static command = async (session, language, command, parameters) => {
try {
return await Rest.postJson(Routes.COMMAND, {
language,
command,
params: parameters
}, session);
}
catch (error) {
throw new Error(`Could not perform command on database: ${session.databaseName}`, { cause: error });
}
};
/**
* Static method to query the database.
* @param session The {@link DatabaseSession `DatabaseSession`} to execute the query on.
* @param language The language of the query.
* @param command The query to execute.
* @param parameters
* @returns The result-set of the query.
*/
static query = async (session, language, command, parameters) => Rest.postJson(Routes.QUERY, { language, command, params: parameters }, session);
/**
* Static method to get the schema of the database.
* @param session The {@link DatabaseSession `DatabaseSession`} to target for the schema.
* @returns The schema of the database.
*/
static getSchema = async (session) => {
try {
return await this.query(session, Dialect.SQL, SELECT_SCHEMA);
}
catch (error) {
throw new Error(`Could not retreive schema from database: ${session.databaseName}`, {
cause: error
});
}
};
/**
* Static method to explain a query.
* @param session The {@link DatabaseSession `DatabaseSession`} to target for the explanation.
* @param sql The SQL query to explain.
* @returns The explanation of the query.
*/
static explain = async (session, sql) => {
try {
const result = await this.query(session, Dialect.SQL, EXPLAIN(sql));
return result[0];
}
catch (error) {
throw new Error(`Could not retreive explanation for ${sql}.`, {
cause: error
});
}
};
/**
* Check to see if the database exists.
* @param session The session to use to check for the database.
* @param databaseName The name of the database to check for existence.
* @returns `true` if database exists, `false` if not
* @throws `Error` if the existence of the database could not be verified.
*/
static exists = async (session, databaseName) => {
try {
const response = await Rest.get(Routes.EXISTS, session);
switch (response.status) {
case 200: {
const { result } = await response.json();
if (typeof result === 'boolean') {
return result;
}
else {
throw new Error(`Recieved an unexpected result from the server, expected boolean, recieved: [${result}] which has a type of: ${typeof result}.`);
}
}
case 400:
throw new Error(`No database name was passed to the server. Recieved: [${databaseName}].`);
default:
throw new Error(`Received an unexpected status code from the server when attempting to check if database "${databaseName}" exists.`);
}
}
catch (error) {
throw new Error(`Encountered an error when checking to see if database "${databaseName}" exists`, { cause: error });
}
};
}
export { Database };
//# sourceMappingURL=Database.js.map