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 table: string;
|
||||||
readonly key: keyType;
|
readonly key: keyType;
|
||||||
readonly select: string;
|
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
|
* 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) {
|
switch (queryType.type) {
|
||||||
case "before": {
|
case "before": {
|
||||||
const [data] = await database.query(
|
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],
|
[queryType.id, ...bind, limit],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
case "after": {
|
case "after": {
|
||||||
const [data] = await database.query(
|
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],
|
[queryType.id, ...bind, limit],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
return data;
|
return data;
|
||||||
|
@ -86,9 +91,9 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
||||||
case "around": {
|
case "around": {
|
||||||
const [data] = await database.query(
|
const [data] = await database.query(
|
||||||
` SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM (\n` +
|
` 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" +
|
"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`,
|
`) as \`x\` ORDER BY \`${this.key}\` DESC`,
|
||||||
[queryType.id, ...bind, Math.ceil(limit / 2), queryType.id, ...bind, Math.floor(limit / 2)],
|
[queryType.id, ...bind, Math.ceil(limit / 2), queryType.id, ...bind, Math.floor(limit / 2)],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
|
@ -96,7 +101,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
||||||
}
|
}
|
||||||
case null: {
|
case null: {
|
||||||
const [data] = await database.query(
|
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],
|
[...bind, limit],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
return data;
|
return data;
|
||||||
|
@ -234,7 +239,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
||||||
) {
|
) {
|
||||||
const sqlwhere = where !== "" ? `WHERE ${where}` : "";
|
const sqlwhere = where !== "" ? `WHERE ${where}` : "";
|
||||||
const [[data]] = await database.query(
|
const [[data]] = await database.query(
|
||||||
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere}`,
|
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere} ${this.sqlGroupBy}`,
|
||||||
bind,
|
bind,
|
||||||
) as data<{count: number}>;
|
) as data<{count: number}>;
|
||||||
|
|
||||||
|
@ -242,9 +247,15 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(table: string, key: keyType, select = "*") {
|
constructor(
|
||||||
|
table: string,
|
||||||
|
key: keyType,
|
||||||
|
select = "*",
|
||||||
|
groupBy = "",
|
||||||
|
) {
|
||||||
this.table = table;
|
this.table = table;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.select = select;
|
this.select = select;
|
||||||
|
this.groupBy = groupBy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue