$query
Updated on Aug 29, 2025 5 minutes to readThe Query Plugin provides methods to create query objects, raw SQL commands, and expressions. It also provides constants for sorting directions.
Properties
Property | Description |
---|---|
SORT_ASC | Sort direction: ascending. |
SORT_DESC | Sort direction: descending. |
Methods
Method | Description |
---|---|
create | Creates a new Query object. |
expr | Creates a new Expression object. |
raw | Creates a new Command object representing raw SQL. |
Methods Details
create()
• Type
() => Query
• Details
Creates a new Query object.
• Example
// Select a single record of the current user by ID
E8App.$query.create()
.select('*')
.from('stbUsers')
.where({ 'stbUsers.id': E8App.$currentUser.getId() })
.one()
.then((record) => {
console.log(record) // Returns one user object
});
// Select username and email of all users except the current one (alias mapping)
E8App.$query.create()
.select([
{ username: 'stbUsers.username' }, // Alias: return as "username"
{ email: 'stbUsers.email' }, // Alias: return as "email"
])
.from('stbUsers')
.where(['not', { 'stbUsers.id': E8App.$currentUser.getId() }])
.all()
.then((record) => {
console.log(record) // Returns an array of user objects
});
// Select username and email without aliases (keys match column names)
E8App.$query.create()
.select(['username', 'email'])
.from('stbUsers')
.where(['not', { 'stbUsers.id': E8App.$currentUser.getId() }])
.all()
.then((record) => {
console.log(record) // Array of user objects with username and email
});
expr()
• Type
(expression: string, params?: object) => Expression
• Details
Expects a string representing an expression and optional parameters object.
Returns a new Expression object.
• Example
// Select username, email, and a computed field "isCurrent" (boolean)
E8App.$query.create()
.select([
{ username: 'stbUsers.username' },
{ email: 'stbUsers.email' },
{ isCurrent: E8App.$query.expr('IF(stbUsers.id = {{ id }}, TRUE, FALSE)', { id: E8App.$currentUser.getId() }) }
])
.from('stbUsers')
.all()
.then((record) => {
console.log(record) // Array of user objects with computed "isCurrent"
});
raw()
• Type
(sql: string, params?: object) => Command
• Details
Expects a raw SQL string and optional parameters object.
Returns a Command object for executing raw SQL.
• Example
// Run a raw SQL query with parameter substitution
E8App.$query.raw(
`
SELECT *
FROM stbUsers
WHERE stbUsers.id = {{ id }}
`,
{ id: E8App.$currentUser.getId() }
)
.one()
.then((record) => {
console.log(record) // Single user object from raw SQL
});