Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete to EventRepository #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/common/src/interfaces/event-repository.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export abstract class EventRepository {
*/
abstract find(filter: Filter): Promise<Event[]> | Event[];

/**
* If filter is present, events that match the filter are deleted.
* If no filter is present, all events are deleted.
*
* @param filter Query filter
*/
abstract delete(filter?: Filter): Promise<number>;

/**
* This method doesn't need to be implemented. It's just a helper method for
* finding one event. And it will call `find` method internally.
Expand Down
124 changes: 82 additions & 42 deletions packages/event-repository-sqlite/src/event-repository-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import * as BetterSqlite3 from 'better-sqlite3';
import { readFileSync, readdirSync } from 'fs';
import * as path from 'path';

interface FilterSqlClauses {
innerJoinClauses: string[];
whereClauses: string[];
whereValues: (string | number)[];
}

export class EventRepositorySqlite extends EventRepository {
private db: BetterSqlite3.Database;

Expand Down Expand Up @@ -108,7 +114,7 @@ export class EventRepositorySqlite extends EventRepository {
}

async find(filter: Filter): Promise<Event[]> {
const { ids, authors, kinds, since, until, limit } = filter;
const { limit } = filter;

if (limit === 0) return [];

Expand All @@ -117,47 +123,7 @@ export class EventRepositorySqlite extends EventRepository {
return this.findFromGenericTags(filter, genericTags);
}

const innerJoinClauses: string[] = [];
const whereClauses: string[] = [];
const whereValues: (string | number)[] = [];

if (genericTags.length) {
genericTags.forEach((genericTags, index) => {
const alias = `g${index + 1}`;
innerJoinClauses.push(
`INNER JOIN generic_tags ${alias} ON ${alias}.event_id = e.id`,
);
whereClauses.push(
`${alias}.tag IN (${genericTags.map(() => '?').join(',')})`,
);
whereValues.push(...genericTags);
});
}

if (ids?.length) {
whereClauses.push(`id IN (${ids.map(() => '?').join(',')})`);
whereValues.push(...ids);
}

if (authors?.length) {
whereClauses.push(`author IN (${authors.map(() => '?').join(',')})`);
whereValues.push(...authors);
}

if (kinds?.length) {
whereClauses.push(`kind IN (${kinds.map(() => '?').join(',')})`);
whereValues.push(...kinds);
}

if (since) {
whereClauses.push(`created_at >= ?`);
whereValues.push(since);
}

if (until) {
whereClauses.push(`created_at <= ?`);
whereValues.push(until);
}
const { innerJoinClauses, whereClauses, whereValues } = this.sqlClausesFrom(filter);

const whereClause =
whereClauses.length > 0 ? `WHERE ${whereClauses.join(' AND ')}` : '';
Expand All @@ -172,6 +138,27 @@ export class EventRepositorySqlite extends EventRepository {
return rows.map(this.toEvent);
}

async delete(filter?: Filter): Promise<number> {
let whereClauses: string[] = [];
let whereValues: (string | number)[] = [];

if (filter) {
({ whereClauses, whereValues } = this.sqlClausesFrom(filter));
}

const whereClause =
whereClauses.length > 0 ? `WHERE ${whereClauses.join(' AND ')}` : '';
const deleteResult = this.db
.prepare(
`
DELETE FROM events ${whereClause};
`,
)
.run(whereValues);

return deleteResult.changes;
}

private async findFromGenericTags(
filter: Filter,
genericTags: string[][],
Expand Down Expand Up @@ -330,4 +317,57 @@ export class EventRepositorySqlite extends EventRepository {
executedMigrations: migrationsToRun,
};
}

private sqlClausesFrom(filter: Filter): FilterSqlClauses {
const innerJoinClauses: string[] = [];
const whereClauses: string[] = [];
const whereValues: (string | number)[] = [];

const { ids, authors, kinds, since, until } = filter;
const genericTags = this.extractGenericTagsFrom(filter);

if (genericTags.length) {
genericTags.forEach((genericTags, index) => {
const alias = `g${index + 1}`;
innerJoinClauses.push(
`INNER JOIN generic_tags ${alias} ON ${alias}.event_id = e.id`,
);
whereClauses.push(
`${alias}.tag IN (${genericTags.map(() => '?').join(',')})`,
);
whereValues.push(...genericTags);
});
}

if (ids?.length) {
whereClauses.push(`id IN (${ids.map(() => '?').join(',')})`);
whereValues.push(...ids);
}

if (authors?.length) {
whereClauses.push(`author IN (${authors.map(() => '?').join(',')})`);
whereValues.push(...authors);
}

if (kinds?.length) {
whereClauses.push(`kind IN (${kinds.map(() => '?').join(',')})`);
whereValues.push(...kinds);
}

if (since) {
whereClauses.push(`created_at >= ?`);
whereValues.push(since);
}

if (until) {
whereClauses.push(`created_at <= ?`);
whereValues.push(until);
}

return {
innerJoinClauses,
whereClauses,
whereValues,
};
}
}