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()
.
The language in which the command is written (e.g. SQL).
The command to execute in the given language.
Optional
parameters: Record<string, unknown>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.
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();
Returns information about query execution planning of a specific statement, without executing the statement itself.
The SQL command to explain.
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.
An array of objects describing the schema.
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()
.
Optional
isolationLevel: ArcadeTransactionIsolationLevelThe isolation level of the transaction.
void
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.
The language of the query.
The command to execute in the given language.
Optional
parameters: ArcadeQueryParametersThe 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"'
);
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();
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.
The callback to execute within the transaction scope.
Optional
isolationLevel: ArcadeTransactionIsolationLevelThe isolation level of the transaction.
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();
Interacts with a database, performing queries and issuing commands to manage records, types, and settings.
Param: parameters
The fields necessary to connect to and interact with a specific database.
Returns
An instance of SpriteDatabase.
Example