allow select modification
This commit is contained in:
parent
6d4cbbd1b2
commit
b14cf774ec
1 changed files with 10 additions and 8 deletions
|
@ -21,6 +21,7 @@ type queryType = {
|
|||
export default class BaaPagination<T extends {[k: string]: any}, keyType extends string = "id"> {
|
||||
readonly table: string;
|
||||
readonly key: keyType;
|
||||
readonly select: string;
|
||||
|
||||
/**
|
||||
* Gets queryType for a given query with a value
|
||||
|
@ -70,24 +71,24 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
switch (queryType.type) {
|
||||
case "before": {
|
||||
const [data] = await database.query(
|
||||
`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 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}\` > ? ${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 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 ${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` +
|
||||
"UNION ALL\n" +
|
||||
`(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 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>;
|
||||
|
@ -95,7 +96,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
}
|
||||
case null: {
|
||||
const [data] = await database.query(
|
||||
`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 LIMIT ?`,
|
||||
[...bind, limit],
|
||||
) as unknown as data<T>;
|
||||
return data;
|
||||
|
@ -167,7 +168,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
async RESTgetRecord(e: H3Event) {
|
||||
const key = e.context.params?.[this.key];
|
||||
const [data] = await database.query(
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` = ?`,
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` = ?`,
|
||||
[key],
|
||||
) as data<T>;
|
||||
|
||||
|
@ -206,7 +207,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
}
|
||||
|
||||
const [data] = await database.query(
|
||||
`SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` = ?`,
|
||||
`SELECT ${this.select}, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` = ?`,
|
||||
[key],
|
||||
) as data<T>;
|
||||
|
||||
|
@ -241,8 +242,9 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
|
|||
return data;
|
||||
}
|
||||
|
||||
constructor(table: string, key: keyType) {
|
||||
constructor(table: string, key: keyType, select = "*") {
|
||||
this.table = table;
|
||||
this.key = key;
|
||||
this.select = select;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue