Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
jsonAgg: (tableName: string, params: JSONAggParams = {}): SQLStatement => {
const { columnName, filterColumn, orderColumn } = Object.assign(
{ columnName: '*', filterColumn: 'id', orderColumn: 'id' },
params
)
const column = raw(`${tableName}.${columnName}`)
const filter = raw(`${tableName}.${filterColumn}`)
const order = raw(`${tableName}.${orderColumn}`)
return SQL`COALESCE(
json_agg(${column} ORDER BY ${order}) FILTER (WHERE ${filter} IS NOT NULL),
'[]'
)`
}
})
static async findCastVoteById(id: string) {
return this.query(SQL`
SELECT v.*,
row_to_json(p.*) as poll,
row_to_json(o.*) as option,
row_to_json(t.*) as token
FROM ${raw(this.tableName)} v
JOIN ${raw(Poll.tableName)} p ON p.id = v.poll_id
JOIN ${raw(Option.tableName)} o ON o.id = v.option_id
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
WHERE v.id = ${id}`)
}
findWithAssociations: (whereStatement: SQLStatement = SQL``): SQLStatement =>
SQL`
SELECT p.*,
row_to_json(t.*) as token,
(SELECT ${ModelQueries.jsonAgg('v', {
orderColumn: 'timestamp DESC'
})} AS votes FROM ${raw(Vote.tableName)} v WHERE v.poll_id = p.id),
(SELECT ${ModelQueries.jsonAgg('o', {
orderColumn: 'value ASC'
})} AS options FROM ${raw(Option.tableName)} o WHERE o.poll_id = p.id)
FROM polls p
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
${whereStatement}
GROUP BY p.id, t.address`
})
static async findCastVoteById(id: string) {
return this.query(SQL`
SELECT v.*,
row_to_json(p.*) as poll,
row_to_json(o.*) as option,
row_to_json(t.*) as token
FROM ${raw(this.tableName)} v
JOIN ${raw(Poll.tableName)} p ON p.id = v.poll_id
JOIN ${raw(Option.tableName)} o ON o.id = v.option_id
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
WHERE v.id = ${id}`)
}
static async findCastVoteById(id: string) {
return this.query(SQL`
SELECT v.*,
row_to_json(p.*) as poll,
row_to_json(o.*) as option,
row_to_json(t.*) as token
FROM ${raw(this.tableName)} v
JOIN ${raw(Poll.tableName)} p ON p.id = v.poll_id
JOIN ${raw(Option.tableName)} o ON o.id = v.option_id
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
WHERE v.id = ${id}`)
}
static async findCastVoteById(id: string) {
return this.query(SQL`
SELECT v.*,
row_to_json(p.*) as poll,
row_to_json(o.*) as option,
row_to_json(t.*) as token
FROM ${raw(this.tableName)} v
JOIN ${raw(Poll.tableName)} p ON p.id = v.poll_id
JOIN ${raw(Option.tableName)} o ON o.id = v.option_id
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
WHERE v.id = ${id}`)
}
jsonAgg: (tableName: string, params: JSONAggParams = {}): SQLStatement => {
const { columnName, filterColumn, orderColumn } = Object.assign(
{ columnName: '*', filterColumn: 'id', orderColumn: 'id' },
params
)
const column = raw(`${tableName}.${columnName}`)
const filter = raw(`${tableName}.${filterColumn}`)
const order = raw(`${tableName}.${orderColumn}`)
return SQL`COALESCE(
json_agg(${column} ORDER BY ${order}) FILTER (WHERE ${filter} IS NOT NULL),
'[]'
)`
}
})
findWithAssociations: (whereStatement: SQLStatement = SQL``): SQLStatement =>
SQL`
SELECT p.*,
row_to_json(t.*) as token,
(SELECT ${ModelQueries.jsonAgg('v', {
orderColumn: 'timestamp DESC'
})} AS votes FROM ${raw(Vote.tableName)} v WHERE v.poll_id = p.id),
(SELECT ${ModelQueries.jsonAgg('o', {
orderColumn: 'value ASC'
})} AS options FROM ${raw(Option.tableName)} o WHERE o.poll_id = p.id)
FROM polls p
JOIN ${raw(Token.tableName)} t ON t.address = p.token_address
${whereStatement}
GROUP BY p.id, t.address`
})
jsonAgg: (tableName: string, params: JSONAggParams = {}): SQLStatement => {
const { columnName, filterColumn, orderColumn } = Object.assign(
{ columnName: '*', filterColumn: 'id', orderColumn: 'id' },
params
)
const column = raw(`${tableName}.${columnName}`)
const filter = raw(`${tableName}.${filterColumn}`)
const order = raw(`${tableName}.${orderColumn}`)
return SQL`COALESCE(
json_agg(${column} ORDER BY ${order}) FILTER (WHERE ${filter} IS NOT NULL),
'[]'
)`
}
})
static async updateBalance(
account: AccountBalanceAttributes,
balance: number | string
) {
return this.query(SQL`
UPDATE ${raw(this.tableName)} v
SET account_balance = ${balance}
FROM ${raw(Poll.tableName)} p
${PollQueries.whereStatusAndType({
status: 'active'
})}
AND v.account_address = ${account.address}
AND v.poll_id = p.id
AND p.token_address = ${account.token_address}`)
}
}