Skip to content

Instantly share code, notes, and snippets.

@fl0w
Created July 9, 2017 12:29
Show Gist options
  • Save fl0w/18ce22044e62a06ed32bd35529baaf38 to your computer and use it in GitHub Desktop.
Save fl0w/18ce22044e62a06ed32bd35529baaf38 to your computer and use it in GitHub Desktop.
class BaseQueryBuilder extends objection.QueryBuilder {
distinctOn (columns, tableStar = `${this.modelClass().tableName}.*`) {
columns = asArray(columns)
const identifiers = columns.map(() => '??').join(', ')
columns = columns.map(column => {
return column.split('.').length === 1
? `${this.modelClass().tableName}.${column}`
: column
})
const distinctOn = raw(`DISTINCT ON (${identifiers}) ??`, [
...columns,
tableStar
])
return this.select(distinctOn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment