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(
|
async getPagedResults(
|
||||||
queryType: queryType,
|
queryType: queryType,
|
||||||
limit = 50,
|
limit = 50,
|
||||||
|
where = "",
|
||||||
|
bind: Array<any> = [],
|
||||||
) {
|
) {
|
||||||
|
const sqlwhere = where ? `AND (${where})` : "";
|
||||||
switch (queryType.type) {
|
switch (queryType.type) {
|
||||||
case "before": {
|
case "before": {
|
||||||
const [data] = await database.query(
|
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 ?`,
|
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||||
[queryType.id, 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 *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||||
[queryType.id, limit],
|
[queryType.id, ...bind, limit],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
case "around": {
|
case "around": {
|
||||||
const [data] = await database.query(
|
const [data] = await database.query(
|
||||||
` SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM (\n` +
|
` 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" +
|
"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`,
|
`) 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>;
|
) as unknown as data<T>;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
case null: {
|
case null: {
|
||||||
const [data] = await database.query(
|
const [data] = await database.query(
|
||||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE TRUE ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`,
|
||||||
[limit],
|
[...bind, limit],
|
||||||
) as unknown as data<T>;
|
) as unknown as data<T>;
|
||||||
return data;
|
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);
|
const query = getQuery(e);
|
||||||
|
|
||||||
let limit = defaultLimit;
|
let limit = defaultLimit;
|
||||||
|
@ -122,7 +131,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
||||||
|
|
||||||
const queryData = BaaPagination.getLocationParameterType(query);
|
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>>(
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async RESTrecordCount(e :H3Event) {
|
async RESTrecordCount(
|
||||||
|
e :H3Event,
|
||||||
|
where = "",
|
||||||
|
bind: Array<any> = [],
|
||||||
|
) {
|
||||||
|
const sqlwhere = where ? `WHERE ${where}` : "";
|
||||||
const [[data]] = await database.query(
|
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}>;
|
) as data<{count: number}>;
|
||||||
|
|
||||||
if (!data) throw createError("Database returned no rows");
|
if (!data) throw createError("Database returned no rows");
|
||||||
|
|
Loading…
Reference in a new issue