Interacts with a database, performing queries and issuing commands to manage records, types, and settings.

The fields necessary to connect to and interact with a specific database.

An instance of SpriteDatabase.

const db = new SpriteDatabase({
username: 'aUser',
password: 'aPassword',
address: 'http://localhost:2480',
databaseName: 'aDatabase'
});

type DocumentTypes = {
aDocument: {
aField: string
}
}

async function databaseExample() {
const client = db.documents<DocumentTypes>();
try {
await db.transaction(async (trx) => {
await db.createType('aDocument', trx);
trx.crud('sql', 'INSERT INTO aDocument CONTENT { "aField": "aValue" }');
});
const schema = await db.getSchema();
console.log(schema);
// [...]
} catch (error) {
console.error(error);
// handle error conditions
}
}

databaseExample();

Constructors

Methods

  • Executes a command on the target database. This method should only be used for non-transactional, non-idempotent statements such as CREATE, ALTER, or DROP.

    CRUD operations must be part of a transaction; otherwise, changes will not persist. Use the SpriteTransaction.crud() method for this purpose.

    If you need to execute idempotent commands, see SpriteDatabase.query().

    Type Parameters

    • T = unknown

    Parameters

    • language: ArcadeSupportedQueryLanguages

      The language in which the command is written (e.g. SQL).

    • command: string

      The command to execute in the given language.

    • Optionalparameters: Record<string, unknown>

    Returns Promise<T>

    The result property of the command response from the server, typically an Array.

    This package includes type definitions to help you issue commands with typed return values.

    db.command<CreateDocumentType>(
    'sql',
    'CREATE DOCUMENT TYPE aType'
    );

    Error if the command cannot be executed.

    SpriteDatabase.query()
    SpriteDatabase.transaction()

    const db = new SpriteDatabase({
    username: 'aUser',
    password: 'aPassword',
    address: 'http://localhost:2480',
    databaseName: 'aDatabase'
    });

    async function spriteCommandExample() {
    try {
    const result = await db.command<CreateDocumentType>(
    'sql',
    'CREATE DOCUMENT TYPE aType',
    );
    console.log(result);
    // [ { operation: 'create document type', typeName: 'aType' } ]
    return result;
    } catch (error) {
    // handle error conditions
    console.error(error);
    }
    }

    spriteCommandExample();
  • Create a new database on the server.

    Returns Promise<SpriteDatabase>

    true if the database was created.

    Error if the database could not be created.

    async function createDatabaseExample() {
    try {
    const exists = await db.exits();
    if (!exits) {
    const created = await db.create();
    console.log(created);
    // true
    }
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    }

    createDatabaseExample();
  • Check to see if this database exists on the server (i.e. the database was created).

    Returns Promise<boolean>

    true if the database exists.

    Error if the database does not exist.

    async function databaseExistsExample() {
    try {
    const exists = await db.exists();
    console.log(exists);
    // true
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    }

    databaseExistsExample();
  • Returns information about query execution planning of a specific statement, without executing the statement itself.

    Parameters

    • sql: string

      The SQL command to explain.

    Returns Promise<ArcadeSqlExplanation>

    The explanation of the command.

    const db = new SpriteDatabase({
    username: 'aUser',
    password: 'aPassword',
    address: 'http://localhost:2480',
    databaseName: 'aDatabase'
    });

    async function spriteExplainExample() {
    try {
    const explanation = await db.explain("SELECT FROM schema:types");
    console.log(explanation);
    // {
    // executionPlan: {
    // type: 'QueryExecutionPlan',
    // javaType: 'com.arcadedb.query.sql.executor.SelectExecutionPlan',
    // cost: -1,
    // prettyPrint: '+ FETCH DATABASE METADATA TYPES',
    // steps: [ [Object] ]
    // },
    // executionPlanAsString: '+ FETCH DATABASE METADATA TYPES'
    // }
    return explanation;
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    };

    spriteExplainExample();
  • Return the current schema.

    Returns Promise<ArcadeGetSchemaResponse>

    An array of objects describing the schema.

    async function getSchemaExample() {
    try {
    const schema = await db.getSchema();
    console.log(schema);
    // [...]
    return schema;
    } catch (error) {
    console.log(error);
    // handle error conditions
    }
    }

    getSchemaExample();
  • Creates and returns a new SpriteTransaction. Operations requiring the transaction should be executed using the crud() method on the returned object. The transaction can be committed using the commit() method, and rolled-back by invoking rollback().

    Parameters

    • OptionalisolationLevel: ArcadeTransactionIsolationLevel

      The isolation level of the transaction.

    Returns Promise<SpriteTransaction>

    void

    SpriteTransaction

    const db = new SpriteDatabase({
    username: 'aUser',
    password: 'aPassword',
    address: 'http://localhost:2480',
    databaseName: 'aSpriteDatabase'
    });

    type DocumentType = {
    aProperty: string
    }

    async function transactionExample() {
    try {
    await db.command<CreatDocumentType>(
    'sql',
    'CREATE document TYPE aType',
    );

    const trx = await db.newTransaction();
    await trx.crud<InsertDocument<DocumentType>(
    'sql',
    'INSERT INTO aType SET aProperty = :aProperty',
    { aProperty: 'aValue' }
    );
    await trx.commit();
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    };

    transactionExample();
  • Executes a query against the target database. This method only executes idempotent statements (that cannot change the database), namely SELECT and MATCH.

    The execution of non-idempotent commands will throw an IllegalArgumentException exception.

    If you are trying to execute non-idempotent commands, see the SpriteDatabase.command method.

    Type Parameters

    • T = unknown

    Parameters

    • language: ArcadeSupportedQueryLanguages

      The language of the query.

    • command: string

      The command to execute in the given language.

    • Optionalparameters: ArcadeQueryParameters

    Returns Promise<T[]>

    The result property of the query response from the server, this is an Array containing the result set of the query.

    This library includes type definitions to assist in writing queries with typed return values. For example: ArcadeDocument, ArcadeEdge, etc. You can use these like so:

    const result = await db.query<ArcadeDocument<DocumentType>>(
    'sql',
    'SELECT * FROM aType WHERE aProperty == "aValue"'
    );

    SpriteDatabase.command()

    const db = new SpriteDatabase({
    username: 'aUser',
    password: 'aPassword',
    address: 'http://localhost:2480',
    databaseName: 'aDatabase'
    });

    type DocumentType = {
    aProperty: string
    }

    async function spriteQueryExample() {
    try {
    const result = await db.query<ArcadeDocument<DocumentType>>(
    'sql',
    'SELECT * FROM DocumentType WHERE aProperty == "aValue"'
    );
    console.log(result);
    // [{
    // '@rid': '#0:0',
    // '@cat': 'd',
    // '@type': 'DocumentType',
    // aProperty: 'aValue'
    // }];
    return result
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    };

    spriteQueryExample();
  • Set the credentials that the database client should use when interacting with the ArcadeDB server.

    Parameters

    • username: string

      The username to authenticate with.

    • password: string

      The password to authenticate with.

    Returns void

    true if the credentials were set.

    Error if the credentials could not be set.

  • Creates a new transaction and passes it as an argument to a callback which represents the transaction scope. The transaction is committed when the callback resolves. The transaction can be rolled back by invoking SpriteTransaction.rollback() within the callback.

    Type Parameters

    • T

    Parameters

    • callback: ((trx: SpriteTransaction) => Promise<T>)

      The callback to execute within the transaction scope.

    • OptionalisolationLevel: ArcadeTransactionIsolationLevel

      The isolation level of the transaction.

    Returns Promise<[boolean, T]>

    void

    SpriteDatabase.newTransaction()
    SpriteTransaction.commit()

    const db = new SpriteDatabase({
    username: 'aUser',
    password: 'aPassword',
    address: 'http://localhost:2480',
    databaseName: 'aSpriteDatabase'
    });

    type DocumentType = {
    aProperty: string
    }

    async function transactionExample() {
    try {
    await db.command<CreatDocumentType>(
    'sql',
    'CREATE document TYPE aType',
    );
    await db.transaction(async (trx) => {
    trx.crud<InsertDocument<DocumentType>(
    'sql',
    'INSERT INTO aType SET aProperty = :aProperty',
    { aProperty: 'aValue' }
    );
    });
    } catch (error) {
    console.error(error);
    // handle error conditions
    }
    };

    transactionExample();