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

In memory inline projections - Add inMemoryDatabase #186

Merged
Merged
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
49 changes: 49 additions & 0 deletions src/packages/emmett/src/database/inMemoryDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { JSONParser } from '../serialization';

export interface DocumentsCollection<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUESTION: @stepaniukm, is it intentional that members in this file are not exposed in the main index file? I'm fine with not exposing them yet until it's ready; I'm just double-checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that is intentional, first I want to complete migration to something actually usable, tested and more stable, before exposing it publicly. One little thing I removed type DocumentHandler that will be a part of future effort.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stepaniukm, I'll pull in your changes and add the export. I think that it could be useful for folks to have even such in-memory db 👌

store: (id: string, obj: T) => void;
delete: (id: string) => void;
get: (id: string) => T | null;
}

export type DocumentHandler<T> =
| ((document: T | null) => T | null)
| ((document: T | null) => Promise<T | null>);

export interface Database {
collection: <T>(name: string) => DocumentsCollection<T>;
}

export const getInMemoryDatabase = (): Database => {
const storage = new Map<string, unknown>();

return {
collection: <T>(
collectionName: string,
_collectionOptions: {
errors?: { throwOnOperationFailures?: boolean } | undefined;
} = {},
): DocumentsCollection<T> => {
const toFullId = (id: string) => `${collectionName}-${id}`;

const collection = {
store: (id: string, obj: T): void => {
storage.set(toFullId(id), obj);
},
delete: (id: string): void => {
storage.delete(toFullId(id));
},
get: (id: string): T | null => {
const result = storage.get(toFullId(id));

return result
? // Clone to simulate getting new instance on loading
(JSONParser.parse(JSONParser.stringify(result)) as T)
: null;
},
};

return collection;
},
};
};
19 changes: 18 additions & 1 deletion src/packages/emmett/src/eventStore/inMemoryEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ import {
} from './eventStore';
import { assertExpectedVersionMatchesCurrent } from './expectedVersion';
import { StreamingCoordinator } from './subscriptions';
import type { ProjectionRegistration } from '../projections';

export const InMemoryEventStoreDefaultStreamVersion = 0n;

export type InMemoryEventStore =
EventStore<ReadEventMetadataWithGlobalPosition>;

export type InMemoryReadEventMetadata = ReadEventMetadataWithGlobalPosition;

export type InMemoryProjectionHandlerContext = {
eventStore: InMemoryEventStore;
};

export type InMemoryEventStoreOptions =
DefaultEventStoreOptions<InMemoryEventStore>;
DefaultEventStoreOptions<InMemoryEventStore> & {
projections?: ProjectionRegistration<
'inline',
InMemoryReadEventMetadata,
InMemoryProjectionHandlerContext
>[];
};

export type InMemoryReadEvent<EventType extends Event = Event> = ReadEvent<
EventType,
Expand All @@ -48,6 +61,10 @@ export const getInMemoryEventStore = (
.reduce((p, c) => p + c, 0);
};

const _inlineProjections = (eventStoreOptions?.projections ?? [])
.filter(({ type }) => type === 'inline')
.map(({ projection }) => projection);

return {
async aggregateStream<State, EventType extends Event>(
streamName: string,
Expand Down
Loading