Skip to content

Commit

Permalink
Expose batch input to users of queryDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed May 21, 2024
1 parent c4fb670 commit e751897
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/init-dependencies/event-store/commit-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export const commitEvent =
event_type: type,
payload: JSON.stringify(payload),
}),
row =>
queryDatabase(
'INSERT INTO events (id, resource_id, resource_type, event_type, payload) VALUES ($id, $resource_id, $resource_type, $event_type, $payload); ',
row
),
args =>
queryDatabase([
{
sql: 'INSERT INTO events (id, resource_id, resource_type, event_type, payload) VALUES ($id, $resource_id, $resource_type, $event_type, $payload); ',
args,
},
]),
TE.map(() => ({
status: StatusCodes.CREATED,
message: 'Persisted a new event',
Expand Down
10 changes: 6 additions & 4 deletions src/init-dependencies/event-store/ensure-event-table-exists.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {QueryEventsDatabase} from './query-events-database';

export const ensureEventTableExists = (queryDatabase: QueryEventsDatabase) =>
queryDatabase(
`
queryDatabase([
{
sql: `
CREATE TABLE IF NOT EXISTS events (
id TEXT,
resource_version number,
Expand All @@ -12,5 +13,6 @@ export const ensureEventTableExists = (queryDatabase: QueryEventsDatabase) =>
payload TEXT
);
`,
[]
);
args: {},
},
]);
2 changes: 1 addition & 1 deletion src/init-dependencies/event-store/get-all-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getAllEvents =
(queryDatabase: QueryEventsDatabase): Dependencies['getAllEvents'] =>
() =>
pipe(
queryDatabase('SELECT * FROM events;', []),
queryDatabase([{sql: 'SELECT * FROM events;', args: {}}]),
TE.chainEitherK(
flow(
EventsFromDb.decode,
Expand Down
10 changes: 2 additions & 8 deletions src/init-dependencies/event-store/init-query-events-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ import {pipe} from 'fp-ts/lib/function';

export const initQueryEventsDatabase = (): QueryEventsDatabase => {
const client = libsqlClient.createClient({url: ':memory:'});
return (query: string, args: libsqlClient.InArgs) =>
return statements =>
pipe(
TE.tryCatch(
() =>
client.batch([
{
sql: query,
args: args,
},
]),
() => client.batch(statements),
failureWithStatus('DB query failed', StatusCodes.INTERNAL_SERVER_ERROR)
),
TE.map(results => results[0])
Expand Down
5 changes: 2 additions & 3 deletions src/init-dependencies/event-store/query-events-database.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as TE from 'fp-ts/TaskEither';
import {FailureWithStatus} from '../../types/failureWithStatus';
import {InArgs} from '@libsql/client/.';
import {InStatement} from '@libsql/client/.';

export type QueryEventsDatabase = (
query: string,
args: InArgs
statements: InStatement[]
) => TE.TaskEither<FailureWithStatus, unknown>;

0 comments on commit e751897

Please sign in to comment.