Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async getTranslations(req: express.Request): Promise {
let locale = server.extractFromReq(req, 'locale')
locale = locale.slice(0, 2) // We support base locales for now, like en, it, etc
return new Translation().fetch(locale)
}
}
export function mockModelDbOperations(operations: { [key: string]: Function }) {
const log = new Log('Query')
const toJSON = obj => JSON.stringify(obj) // Formatting aid
const _query = Model.db.query
const _count = Model.db.count
operations = {
query: async function(query, args) {
const rows = await _query.call(this, query, args)
log.info(`Executing:\n${query} ${toJSON(args)} => ${rows.length} rows`)
return rows
},
count: async function(tableName, conditions) {
const count = await _count.call(this, tableName, conditions)
log.info(
`Counting: ${toJSON(conditions)} on ${tableName} => ${toJSON(count)}`
)
return count
},
insert: (tableName, row) => {
export function mockModelDbOperations(operations: { [key: string]: Function }) {
const log = new Log('Query')
const toJSON = obj => JSON.stringify(obj) // Formatting aid
const _query = Model.db.query
const _count = Model.db.count
operations = {
query: async function(query, args) {
const rows = await _query.call(this, query, args)
log.info(`Executing:\n${query} ${toJSON(args)} => ${rows.length} rows`)
return rows
},
count: async function(tableName, conditions) {
const count = await _count.call(this, tableName, conditions)
log.info(
`Counting: ${toJSON(conditions)} on ${tableName} => ${toJSON(count)}`
)
return count
},
insert: (tableName, row) => {
log.info(`Inserting: ${toJSON(row)} on ${tableName}`)
async getPoll(req: express.Request) {
const id = server.extractFromReq(req, 'id')
const poll = await Poll.findByIdWithAssociations(id)
return utils.omit(poll, blacklist.poll)
}
}
export async function initializeDatabase() {
const shouldContinue = await cli.confirm(
`Careful! this will DROP 'projects' and 'district_entries,
upsert 'account_balances' and a district token.
Do you wish to continue?`
)
if (!shouldContinue) return process.exit()
log.info('Connecting database')
await db.connect()
log.info('Initializing state')
await dropDumps()
log.info('Restoring district_entries')
execSync(runpsql('../dumps/districts.20180105.sql'))
log.info('Restoring projects')
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}`)
}