database
BatchResult
Defines the result of a batch query.
Members
results QueryResult[]
The results of the individual statements.
Database
Represents a connection to a database record.
Members
raw object
constructor(recordName: string, address: string, options: RecordActionOptions, context: AuxGlobalContext): Database
The first parameter is a string and
The second parameter is a string and
The third parameter is a RecordActionOptions and
The fourth parameter is a AuxGlobalContext and
batch(statements: DatabaseStatement[], readonly: boolean): Promise<GenericResult>
Runs the given statements in a single transaction. Transactions can be used to group multiple statements together. If one statement fails, then none of the statements will have any effect.
The first parameter is a DatabaseStatement[] and
The second parameter is a boolean and whether the statements are read-only. If true, then the statements cannot modify data.
Examples
Run multiple select queries at onceconst results = await database.batch([
database.sql`SELECT * FROM items WHERE id = 'abc'`,
database.sql`SELECT * FROM items WHERE id = 'def'`,
database.sql`SELECT * FROM items WHERE id = 'ghi'`,
]);Insert multiple items at onceconst results = await database.batch([
database.sql`INSERT INTO items (name, value) VALUES ('Item 1', 100)`,
database.sql`INSERT INTO items (name, value) VALUES ('Item 2', 200)`,
database.sql`INSERT INTO items (name, value) VALUES ('Item 3', 300)`,
], false);execute(templates: TemplateStringsArray, ...params: unknown[]): Promise<GenericResult>
Runs the given SQL on the database and returns the result. This method supports read-write queries. This means that queries can be used to select, insert, update, and delete data.
Supports template string literals for parameterized queries.
Warning: To avoid SQL Injection attacks, always use template literals with expressions. Never use the
+operator to concatenate strings containing SQL code.The first parameter is a TemplateStringsArray and is the string templates.
Each other parameter is a unknown and are the parameters that should be used.
Examples
Insert a new item into a tableconst name = "New Item";
const value = 100;
const result = await database.execute`INSERT INTO items (name, value) VALUES (${name}, ${value})`;query(templates: TemplateStringsArray, ...params: unknown[]): Promise<GenericResult>
Runs the given readonly query against the database. This method requires queries to be read-only. This means that queries can only select data, they cannot insert, update, or delete data.
Supports template string literals for parameterized queries.
Warning: To avoid SQL Injection attacks, always use template literals with expressions. Never use the
+operator to concatenate strings containing SQL code.The first parameter is a TemplateStringsArray and is the string templates.
Each other parameter is a unknown and are the parameters that should be used.
Examples
Select all items from a tableconst result = await database.query`SELECT * FROM items`;Use a parameter in a queryconst itemId = 'abc';
const result = await database.query`SELECT * FROM items WHERE id = ${itemId}`;run(statement: DatabaseStatement, readonly: boolean): Promise<GenericResult>
Runs the given database statement.
The first parameter is a DatabaseStatement and is the statement to run.
The second parameter is a boolean and whether the statement is read-only. If true, then the statement cannot modify data.
sql(templates: TemplateStringsArray, ...params: unknown[]): DatabaseStatement
Constructs a database statement from the given template string literal.
Once constructed, the returned statement can be used with
run()orbatch().The first parameter is a TemplateStringsArray and is the string templates.
Each other parameter is a unknown and are the parameters to interpolate into the templates.
Examples
Create a database statement from a SQL queryconst statement = database.sql`SELECT * FROM items`;Use a parameter in a database statementconst itemId = 'abc';
const statement = database.sql`SELECT * FROM items WHERE id = ${itemId}`;statement(sql: string, ...params: unknown[]): DatabaseStatement
Creates a new database statement from the given SQL and parameters.
The first parameter is a string and is the SQL query string.
Each other parameter is a unknown and are the parameters to include in the query.
Examples
Get a database connection.const database = os.getDatabase('myRecord', 'myDatabase');RecordDatabaseRequest
Defines an interface that represents a request for
os.recordDatabase(request, options).Members