Skip to content

Commit

Permalink
Store the resource type at the resource level
Browse files Browse the repository at this point in the history
Refs #1973
  • Loading branch information
thewilkybarkid committed Oct 10, 2024
1 parent af6ba47 commit 0b3ee7e
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/LibsqlEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl

yield* sql`
CREATE TABLE IF NOT EXISTS resources (
id TEXT NOT NULL PRIMARY KEY
id TEXT NOT NULL PRIMARY KEY,
type TEXT NOT NULL
)
`

yield* sql`
CREATE TABLE IF NOT EXISTS events (
event_id TEXT NOT NULL PRIMARY KEY,
resource_type TEXT NOT NULL,
resource_id TEXT NOT NULL,
resource_version number,
event_type TEXT NOT NULL,
event_timestamp TEXT NOT NULL,
payload TEXT NOT NULL,
FOREIGN KEY(resource_id) REFERENCES resources(id)
UNIQUE (resource_type, resource_id, resource_version)
UNIQUE (resource_id, resource_version)
)
`

Expand All @@ -35,7 +35,6 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
sql`
SELECT
event_id,
resource_type,
resource_id,
resource_version,
event_type,
Expand Down Expand Up @@ -66,7 +65,6 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
sql`
SELECT
event_id,
resource_type,
resource_id,
resource_version,
event_type,
Expand All @@ -75,8 +73,7 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
FROM
events
WHERE
resource_type = 'Feedback'
AND resource_id = ${resourceId}
resource_id = ${resourceId}
ORDER BY
resource_version ASC
`,
Expand Down Expand Up @@ -109,17 +106,20 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
Effect.gen(function* () {
const encoded = yield* Schema.encode(ResourcesTable)({
id: resourceId,
type: 'Feedback',
})

if (lastKnownVersion === 0) {
const results = yield* pipe(
sql`
INSERT INTO
resources (
id
id,
type
)
SELECT
${encoded.id}
${encoded.id},
${encoded.type}
WHERE
NOT EXISTS (
SELECT
Expand All @@ -142,11 +142,13 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl

const rows = yield* sql`
SELECT
id
id,
type
FROM
resources
WHERE
id = ${resourceId}
AND type = 'Feedback'
`

if (rows.length !== 1) {
Expand All @@ -162,7 +164,6 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl

const encoded = yield* Schema.encode(EventsTable)({
eventId,
resourceType: 'Feedback',
resourceId,
resourceVersion: newResourceVersion,
eventTimestamp,
Expand All @@ -174,7 +175,6 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
INSERT INTO
events (
event_id,
resource_type,
resource_id,
resource_version,
event_type,
Expand All @@ -183,7 +183,6 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
)
SELECT
${encoded.event_id},
${encoded.resource_type},
${encoded.resource_id},
${encoded.resource_version},
${encoded.event_type},
Expand All @@ -196,8 +195,7 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl
FROM
events
WHERE
resource_type = ${encoded.resource_type}
AND resource_id = ${encoded.resource_id}
resource_id = ${encoded.resource_id}
AND resource_version >= ${encoded.resource_version}
)
`.raw,
Expand Down Expand Up @@ -233,12 +231,12 @@ export const make: Effect.Effect<EventStore.EventStore, SqlError.SqlError, SqlCl

const ResourcesTable = Schema.Struct({
id: Uuid.UuidSchema,
type: Schema.String,
})

const EventsTable = Schema.transformOrFail(
Schema.Struct({
eventId: Schema.propertySignature(Uuid.UuidSchema).pipe(Schema.fromKey('event_id')),
resourceType: Schema.propertySignature(Schema.String).pipe(Schema.fromKey('resource_type')),
resourceId: Schema.propertySignature(Uuid.UuidSchema).pipe(Schema.fromKey('resource_id')),
resourceVersion: Schema.propertySignature(Schema.Number).pipe(Schema.fromKey('resource_version')),
eventTimestamp: Schema.propertySignature(Schema.DateTimeUtc).pipe(Schema.fromKey('event_timestamp')),
Expand All @@ -248,7 +246,6 @@ const EventsTable = Schema.transformOrFail(
Schema.typeSchema(
Schema.Struct({
eventId: Uuid.UuidSchema,
resourceType: Schema.String,
resourceId: Uuid.UuidSchema,
resourceVersion: Schema.Number,
eventTimestamp: Schema.DateTimeUtc,
Expand Down

0 comments on commit 0b3ee7e

Please sign in to comment.