allow select modification

This commit is contained in:
Wroclaw 2023-05-11 10:51:41 +02:00
parent 6d4cbbd1b2
commit b14cf774ec

View file

@ -21,6 +21,7 @@ type queryType = {
export default class BaaPagination<T extends {[k: string]: any}, keyType extends string = "id"> { export default class BaaPagination<T extends {[k: string]: any}, keyType extends string = "id"> {
readonly table: string; readonly table: string;
readonly key: keyType; readonly key: keyType;
readonly select: string;
/** /**
* Gets queryType for a given query with a value * 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) { 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}\` < ? ${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], [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}\` > ? ${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], [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 ${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}\` ASC LIMIT ?)\n` +
"UNION ALL\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`, `) 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>;
@ -95,7 +96,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 *, 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], [...bind, limit],
) as unknown as data<T>; ) as unknown as data<T>;
return data; return data;
@ -167,7 +168,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
async RESTgetRecord(e: H3Event) { async RESTgetRecord(e: H3Event) {
const key = e.context.params?.[this.key]; const key = e.context.params?.[this.key];
const [data] = await database.query( 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], [key],
) as data<T>; ) as data<T>;
@ -206,7 +207,7 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
} }
const [data] = await database.query( 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], [key],
) as data<T>; ) as data<T>;
@ -241,8 +242,9 @@ export default class BaaPagination<T extends {[k: string]: any}, keyType extends
return data; return data;
} }
constructor(table: string, key: keyType) { constructor(table: string, key: keyType, select = "*") {
this.table = table; this.table = table;
this.key = key; this.key = key;
this.select = select;
} }
} }