Created
July 9, 2017 12:29
-
-
Save fl0w/18ce22044e62a06ed32bd35529baaf38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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