add groupBy
This commit is contained in:
parent
775cc2e1db
commit
7a9e451739
1 changed files with 18 additions and 7 deletions
|
@ -22,6 +22,11 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
readonly table: string;
|
||||
readonly key: keyType;
|
||||
readonly select: string;
|
||||
readonly groupBy: string;
|
||||
|
||||
private get sqlGroupBy() {
|
||||
return this.groupBy !== "" ? `GROUP BY ${this.groupBy}` : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets queryType for a given query with a value
|
||||
|
@ -71,14 +76,14 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
switch (queryType.type) {
|
||||
case "before": {
|
||||
const [data] = await database.query(
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC ${this.sqlGroupBy} LIMIT ?`,
|
||||
[queryType.id, ...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
}
|
||||
case "after": {
|
||||
const [data] = await database.query(
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ${sqlwhere} ORDER BY \`${this.key}\` DESC ${this.sqlGroupBy} LIMIT ?`,
|
||||
[queryType.id, ...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
|
@ -86,9 +91,9 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
case "around": {
|
||||
const [data] = await database.query(
|
||||
` SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM (\n` +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ${sqlwhere} ORDER BY \`${this.key}\` ASC LIMIT ?)\n` +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ${sqlwhere} ORDER BY \`${this.key}\` ${this.sqlGroupBy} ASC LIMIT ?)\n` +
|
||||
"UNION ALL\n" +
|
||||
`(SELECT ${this.select} FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?)\n` +
|
||||
`(SELECT ${this.select} FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC ${this.sqlGroupBy} LIMIT ?)\n` +
|
||||
`) as \`x\` ORDER BY \`${this.key}\` DESC`,
|
||||
[queryType.id, ...bind, Math.ceil(limit / 2), queryType.id, ...bind, Math.floor(limit / 2)],
|
||||
) as unknown as data<T>;
|
||||
|
@ -96,7 +101,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
}
|
||||
case null: {
|
||||
const [data] = await database.query(
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE TRUE ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE TRUE ${sqlwhere} ORDER BY \`${this.key}\` DESC ${this.sqlGroupBy} LIMIT ?`,
|
||||
[...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
|
@ -234,7 +239,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
) {
|
||||
const sqlwhere = where !== "" ? `WHERE ${where}` : "";
|
||||
const [[data]] = await database.query(
|
||||
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere}`,
|
||||
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere} ${this.sqlGroupBy}`,
|
||||
bind,
|
||||
) as data<{count: number}>;
|
||||
|
||||
|
@ -242,9 +247,15 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
return data;
|
||||
}
|
||||
|
||||
constructor(table: string, key: keyType, select = "*") {
|
||||
constructor(
|
||||
table: string,
|
||||
key: keyType,
|
||||
select = "*",
|
||||
groupBy = "",
|
||||
) {
|
||||
this.table = table;
|
||||
this.key = key;
|
||||
this.select = select;
|
||||
this.groupBy = groupBy;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue