Skip to content

Commit

Permalink
fix(db): Fix find on all shards
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Nov 25, 2019
1 parent 6e07e71 commit 4cd22f6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/framework/services/DatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export class DatabaseService {
table: TABLE,
where: string,
values: O[],
selector: (obj: O) => number | string = o => o as any
selector: (obj: O) => number | string = o => o as any,
dataSelector: (obj: O) => any = o => o
): Promise<T[]> {
const map: Map<Pool, Map<string, O[]>> = new Map();
for (const value of values) {
Expand All @@ -135,9 +136,9 @@ export class DatabaseService {
if (poolData) {
const shardData = poolData.get(id);
if (shardData) {
shardData.push(value);
shardData.push(dataSelector(value));
} else {
poolData.set(id, [value]);
poolData.set(id, [dataSelector(value)]);
}
} else {
const shardData = new Map<string, O[]>();
Expand All @@ -149,10 +150,10 @@ export class DatabaseService {
const promises: Promise<RowDataPacket[]>[] = [];
for (const [pool, poolData] of map.entries()) {
const queries: string[] = [];
let poolValues: O[] = [];
const poolValues: O[][] = [];
for (const [db, vals] of poolData.entries()) {
queries.push(`SELECT ${table}.* FROM ${db}.${table} WHERE ${where}`);
poolValues = poolValues.concat(vals);
poolValues.push(vals);
}
const query = queries.join(' UNION ');
promises.push(pool.query<RowDataPacket[]>(query, poolValues).then(([rs]) => rs));
Expand Down Expand Up @@ -826,6 +827,7 @@ export class DatabaseService {
return this.findOne<ScheduledAction>(guildId, TABLE.scheduledActions, '`id` = ?', [id]);
}
public async getScheduledActionsForGuilds(guildIds: string[]) {
// TODO: Fix
return this.findManyOnAllShards<ScheduledAction>(TABLE.scheduledActions, '`guildId` IN (?)', guildIds);
}
public async saveScheduledAction(action: Partial<ScheduledAction>) {
Expand Down

0 comments on commit 4cd22f6

Please sign in to comment.