How to use the decentraland-server.raw function in decentraland-server

To help you get started, we’ve selected a few decentraland-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github decentraland / agora / src / lib / Model.queries.ts View on Github external
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),
            '[]'
        )`
  }
})
github decentraland / agora / src / Vote / Vote.model.ts View on Github external
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}`)
  }
github decentraland / agora / src / Poll / Poll.queries.ts View on Github external
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`
})
github decentraland / agora / src / Vote / Vote.model.ts View on Github external
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}`)
  }
github decentraland / agora / src / Vote / Vote.model.ts View on Github external
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}`)
  }
github decentraland / agora / src / Vote / Vote.model.ts View on Github external
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}`)
  }
github decentraland / agora / src / lib / Model.queries.ts View on Github external
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),
            '[]'
        )`
  }
})
github decentraland / agora / src / Poll / Poll.queries.ts View on Github external
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`
})
github decentraland / agora / src / lib / Model.queries.ts View on Github external
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),
            '[]'
        )`
  }
})
github decentraland / agora / src / Vote / Vote.model.ts View on Github external
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}`)
  }
}