forked from Wroclaw/WorkshopTasker
add where arguments
This commit is contained in:
parent
28f0d7992e
commit
7c2ca8bbe4
1 changed files with 28 additions and 13 deletions
|
@ -63,37 +63,40 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
async getPagedResults(
|
||||
queryType: queryType,
|
||||
limit = 50,
|
||||
where = "",
|
||||
bind: Array<any> = [],
|
||||
) {
|
||||
const sqlwhere = where ? `AND (${where})` : "";
|
||||
switch (queryType.type) {
|
||||
case "before": {
|
||||
const [data] = await database.query(
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[queryType.id, limit],
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[queryType.id, ...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
}
|
||||
case "after": {
|
||||
const [data] = await database.query(
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[queryType.id, limit],
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[queryType.id, ...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
}
|
||||
case "around": {
|
||||
const [data] = await database.query(
|
||||
` SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM (\n` +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ORDER BY \`${this.key}\` ASC LIMIT ?)\n` +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ${sqlwhere} ORDER BY \`${this.key}\` ASC LIMIT ?)\n` +
|
||||
"UNION ALL\n" +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` < ? ORDER BY \`${this.key}\` DESC LIMIT ?)\n` +
|
||||
`(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?)\n` +
|
||||
`) as \`x\` ORDER BY \`${this.key}\` DESC`,
|
||||
[queryType.id, Math.ceil(limit / 2), queryType.id, Math.floor(limit / 2)],
|
||||
[queryType.id, ...bind, Math.ceil(limit / 2), queryType.id, ...bind, Math.floor(limit / 2)],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
}
|
||||
case null: {
|
||||
const [data] = await database.query(
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[limit],
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE TRUE ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||
[...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
}
|
||||
|
@ -102,7 +105,13 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
}
|
||||
}
|
||||
|
||||
RESTget(e: H3Event, defaultLimit = 50, limitLimit = 200) {
|
||||
RESTget(
|
||||
e: H3Event,
|
||||
defaultLimit = 50,
|
||||
limitLimit = 200,
|
||||
where = "",
|
||||
bind: Array<any> = [],
|
||||
) {
|
||||
const query = getQuery(e);
|
||||
|
||||
let limit = defaultLimit;
|
||||
|
@ -122,7 +131,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
|
||||
const queryData = BaaPagination.getLocationParameterType(query);
|
||||
|
||||
return this.getPagedResults(queryData, limit);
|
||||
return this.getPagedResults(queryData, limit, where, bind);
|
||||
}
|
||||
|
||||
async RESTpost<K extends keyof Omit<T, keyType>>(
|
||||
|
@ -217,9 +226,15 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
return null;
|
||||
}
|
||||
|
||||
async RESTrecordCount(e :H3Event) {
|
||||
async RESTrecordCount(
|
||||
e :H3Event,
|
||||
where = "",
|
||||
bind: Array<any> = [],
|
||||
) {
|
||||
const sqlwhere = where ? `WHERE ${where}` : "";
|
||||
const [[data]] = await database.query(
|
||||
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\``,
|
||||
`SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere}`,
|
||||
bind,
|
||||
) as data<{count: number}>;
|
||||
|
||||
if (!data) throw createError("Database returned no rows");
|
||||
|
|
Loading…
Reference in a new issue