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

Added SQLite event store the ability to create connection #178

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ lib
*/.output
e2e/esmCompatibility/.output
src/e2e/esmCompatibility/.output
**/0x
**/0x

**/*.db
272 changes: 157 additions & 115 deletions src/packages/emmett-sqlite/src/eventStore/SQLiteEventStore.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,122 +5,168 @@ import {
assertThrowsAsync,
ExpectedVersionConflictError,
} from '@event-driven-io/emmett';
import fs from 'fs';
import { afterEach, beforeEach, describe, it } from 'node:test';
import sqlite3 from 'sqlite3';
import path from 'path';
import { fileURLToPath } from 'url';
import { v4 as uuid } from 'uuid';
import { sqliteConnection, type SQLiteConnection } from '../sqliteConnection';
import { InMemorySQLiteDatabase, sqliteConnection } from '../sqliteConnection';
import {
type DiscountApplied,
type PricedProductItem,
type ProductItemAdded,
type ShoppingCartEvent,
} from '../testing/shoppingCart.domain';
import { createEventStoreSchema } from './schema';
import { getSQLiteEventStore } from './SQLiteEventStore';

void describe('EventStoreDBEventStore', () => {
let db: SQLiteConnection;
let conn: sqlite3.Database;

beforeEach(() => {
conn = new sqlite3.Database(':memory:');
db = sqliteConnection(conn);
});
import {
getSQLiteEventStore,
type SQLiteEventStoreOptions,
} from './SQLiteEventStore';

void describe('SQLiteEventStore', () => {
const testDatabasePath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'..',
'testing',
);
const fileName = path.resolve(testDatabasePath, 'test.db');

afterEach(() => {
conn.close();
if (!fs.existsSync(fileName)) {
return;
}
fs.unlinkSync(fileName);
});

void it('should append events', async () => {
await createEventStoreSchema(db);
const eventStore = getSQLiteEventStore(db);

const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
void describe('With manual Schema Creation', () => {
const config: SQLiteEventStoreOptions = {
schema: {
autoMigration: 'None',
},
fileName,
};
const discount = 10;
const shoppingCartId = `shopping_cart-${uuid()}`;

const result = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
);

const result2 = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
{ expectedStreamVersion: result.nextExpectedStreamVersion },
);

await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'DiscountApplied',
data: { percent: discount, couponId: uuid() },
},
],
{ expectedStreamVersion: result2.nextExpectedStreamVersion },
);
beforeEach(() => createEventStoreSchema(sqliteConnection({ fileName })));

const { events } = await eventStore.readStream(shoppingCartId);
void it('should append events', async () => {
const eventStore = getSQLiteEventStore(config);

assertIsNotNull(events);
assertEqual(3, events.length);
});
const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
};
const discount = 10;
const shoppingCartId = `shopping_cart-${uuid()}`;

void it('should aggregate stream', async () => {
await createEventStoreSchema(db);
const eventStore = getSQLiteEventStore(db);
const result = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
);

const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
};
const discount = 10;
const shoppingCartId = `shopping_cart-${uuid()}`;
const result2 = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
{ expectedStreamVersion: result.nextExpectedStreamVersion },
);

const result = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
);

const result2 = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
{ expectedStreamVersion: result.nextExpectedStreamVersion },
);

await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'DiscountApplied',
data: { percent: discount, couponId: uuid() },
},
],
{ expectedStreamVersion: result2.nextExpectedStreamVersion },
);
await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'DiscountApplied',
data: { percent: discount, couponId: uuid() },
},
],
{ expectedStreamVersion: result2.nextExpectedStreamVersion },
);

const aggregation = await eventStore.aggregateStream(shoppingCartId, {
evolve,
initialState: () => null,
const { events } = await eventStore.readStream(shoppingCartId);

assertIsNotNull(events);
assertEqual(3, events.length);
});

assertDeepEqual(
{ totalAmount: 54, productItemsCount: 20 },
aggregation.state,
);
void it('should aggregate stream', async () => {
const eventStore = getSQLiteEventStore(config);

const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
};
const discount = 10;
const shoppingCartId = `shopping_cart-${uuid()}`;

const result = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
);

const result2 = await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[{ type: 'ProductItemAdded', data: { productItem } }],
{ expectedStreamVersion: result.nextExpectedStreamVersion },
);

await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'DiscountApplied',
data: { percent: discount, couponId: uuid() },
},
],
{ expectedStreamVersion: result2.nextExpectedStreamVersion },
);

const aggregation = await eventStore.aggregateStream(shoppingCartId, {
evolve,
initialState: () => null,
});

assertDeepEqual(
{ totalAmount: 54, productItemsCount: 20 },
aggregation.state,
);
});

void it('should throw an error if concurrency check has failed when appending stream', async () => {
const eventStore = getSQLiteEventStore(config);

const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
price: 3,
};

const shoppingCartId = `shopping_cart-${uuid()}`;

await assertThrowsAsync<ExpectedVersionConflictError<bigint>>(
async () => {
await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'ProductItemAdded',
data: { productItem },
},
],
{
expectedStreamVersion: 5n,
},
);
},
);
});
});

void it('should automatically create schema', async () => {
const eventStore = getSQLiteEventStore(db, {
const eventStore = getSQLiteEventStore({
schema: {
autoMigration: 'CreateOrUpdate',
},
fileName,
});

const productItem: PricedProductItem = {
Expand All @@ -141,13 +187,13 @@ void describe('EventStoreDBEventStore', () => {
assertEqual(1, events.length);
});

void it('should not overwrite event store if it exists', async () => {
const eventStore = getSQLiteEventStore(db, {
void it('should create the sqlite connection in memory, and not close the connection', async () => {
const eventStore = getSQLiteEventStore({
schema: {
autoMigration: 'CreateOrUpdate',
},
fileName: InMemorySQLiteDatabase,
});

const productItem: PricedProductItem = {
productId: '123',
quantity: 10,
Expand All @@ -164,23 +210,14 @@ void describe('EventStoreDBEventStore', () => {

assertIsNotNull(events);
assertEqual(1, events.length);

const sameEventStore = getSQLiteEventStore(db, {
schema: {
autoMigration: 'CreateOrUpdate',
},
});

const stream = await sameEventStore.readStream(shoppingCartId);
assertIsNotNull(stream.events);
assertEqual(1, stream.events.length);
});

void it('should throw an error if concurrency check has failed when appending stream', async () => {
const eventStore = getSQLiteEventStore(db, {
void it('should not overwrite event store if it exists', async () => {
const eventStore = getSQLiteEventStore({
schema: {
autoMigration: 'CreateOrUpdate',
},
fileName,
});

const productItem: PricedProductItem = {
Expand All @@ -191,20 +228,25 @@ void describe('EventStoreDBEventStore', () => {

const shoppingCartId = `shopping_cart-${uuid()}`;

await assertThrowsAsync<ExpectedVersionConflictError<bigint>>(async () => {
await eventStore.appendToStream<ShoppingCartEvent>(
shoppingCartId,
[
{
type: 'ProductItemAdded',
data: { productItem },
},
],
{
expectedStreamVersion: 5n,
},
);
await eventStore.appendToStream<ShoppingCartEvent>(shoppingCartId, [
{ type: 'ProductItemAdded', data: { productItem } },
]);

const { events } = await eventStore.readStream(shoppingCartId);

assertIsNotNull(events);
assertEqual(1, events.length);
const sameEventStore = getSQLiteEventStore({
schema: {
autoMigration: 'CreateOrUpdate',
},
fileName,
});

const stream = await sameEventStore.readStream(shoppingCartId);

assertIsNotNull(stream.events);
assertEqual(1, stream.events.length);
});
});

Expand Down
Loading