Skip to content

Commit

Permalink
Set up server
Browse files Browse the repository at this point in the history
  • Loading branch information
codeliner committed May 5, 2023
1 parent a30f76e commit 6bbebcf
Show file tree
Hide file tree
Showing 26 changed files with 392 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ testem.log
# System Files
.DS_Store
Thumbs.db

# Filesystem storage
data/*
!data/.gitkeep
Empty file added data/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions packages/be/environments/environment.current.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const envFile = process.env['NODE_ENV'] ? `.${process.env['NODE_ENV']}` : '';

export const { env } = require(`./environment${envFile}.ts`);

27 changes: 27 additions & 0 deletions packages/be/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Environment} from "./environment.schema";

export const env: Environment = {
production: true,
keycloak: {
baseUrl: 'http://localhost:8080',
realm: 'app'
},
//@TODO: Inject via env
postgres: {
host: 'localhost',
port: 5432,
database: 'app',
user: 'dev',
password: 'dev',
max: 200
},
eventStore: {
adapter: "postgres"
},
documentStore: {
adapter: "postgres"
},
authentication: {
disabled: false
}
};
16 changes: 16 additions & 0 deletions packages/be/environments/environment.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {PoolConfig} from "pg";

export interface Environment {
production: boolean;
postgres: PoolConfig;
keycloak: {baseUrl: string, realm: string};
eventStore: {
adapter: "postgres" | "memory" | "filesystem"
},
documentStore: {
adapter: "postgres" | "memory" | "filesystem"
},
authentication?: {
disabled: boolean;
}
}
29 changes: 29 additions & 0 deletions packages/be/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This file can be replaced during build by using the `fileReplacements` array.
// When building for production, this file is replaced with `environment.prod.ts`.

import {Environment} from "./environment.schema";

export const env: Environment = {
production: false,
keycloak: {
baseUrl: 'http://localhost:8080',
realm: 'app'
},
postgres: {
host: 'localhost',
port: 5433,
database: 'test',
user: 'dev',
password: 'dev',
max: 200
},
eventStore: {
adapter: "filesystem"
},
documentStore: {
adapter: "filesystem"
},
authentication: {
disabled: true
}
};
13 changes: 13 additions & 0 deletions packages/be/src/infrastructure/configuredDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {DB} from "@event-engine/infrastructure/Postgres/DB";
import {Pool} from "pg";
import {env} from "@server/env";

let db: DB;

export const getConfiguredDB = (): DB => {
if(!db) {
db = new DB(new Pool(env.postgres))
}

return db;
}
26 changes: 26 additions & 0 deletions packages/be/src/infrastructure/configuredDocumentStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {DocumentStore} from "@event-engine/infrastructure/DocumentStore";
import {PostgresDocumentStore} from "@event-engine/infrastructure/DocumentStore/PostgresDocumentStore";
import {getConfiguredDB} from "@server/infrastructure/configuredDB";
import {env} from "@server/env";
import {InMemoryDocumentStore} from "@event-engine/infrastructure/DocumentStore/InMemoryDocumentStore";

let store: DocumentStore;

export const PERSISTENT_COLLECTION_FILE = process.cwd() + '/../../../data/persistent-collections.json';

export const getConfiguredDocumentStore = (): DocumentStore => {
if(!store) {
switch (env.documentStore.adapter) {
case "postgres":
store = new PostgresDocumentStore(getConfiguredDB());
break;
case "filesystem":
store = new InMemoryDocumentStore(PERSISTENT_COLLECTION_FILE);
break;
default:
store = new InMemoryDocumentStore();
}
}

return store;
}
36 changes: 36 additions & 0 deletions packages/be/src/infrastructure/configuredEventStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {EventStore} from "@event-engine/infrastructure/EventStore";
import {PostgresEventStore} from "@event-engine/infrastructure/EventStore/PostgresEventStore";
import {getConfiguredDB} from "@server/infrastructure/configuredDB";
import {env} from "@server/env";
import {InMemoryEventStore} from "@event-engine/infrastructure/EventStore/InMemoryEventStore";
import {InMemoryStreamListenerQueue} from "@event-engine/infrastructure/Queue/InMemoryStreamListenerQueue";
import {EventDispatcher} from "@event-engine/infrastructure/EventDispatcher";

export const WRITE_MODEL_STREAM = 'write_model_stream';
export const PUBLIC_STREAM = 'public_stream';

export const PERSISTENT_STREAMS_FILE = process.cwd() + '/../../../data/persistent-streams.json';

let es: EventStore;

export const getConfiguredEventStore = (): EventStore => {
if(!es) {
switch (env.eventStore.adapter) {
case "postgres":
es = new PostgresEventStore(getConfiguredDB());
break;
case "filesystem":
es = new InMemoryEventStore(PERSISTENT_STREAMS_FILE);
break;
default:
es = new InMemoryEventStore();
}

// Avoid circular deps in listeners
const streamListener = new InMemoryStreamListenerQueue(es, PUBLIC_STREAM);

streamListener.startProcessing();
}

return es;
}
51 changes: 51 additions & 0 deletions packages/be/src/infrastructure/configuredMultiModelStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {MultiModelStore} from "@event-engine/infrastructure/MultiModelStore";
import {getConfiguredEventStore} from "@server/infrastructure/configuredEventStore";
import {getConfiguredDocumentStore} from "@server/infrastructure/configuredDocumentStore";
import {PostgresDocumentStore} from "@event-engine/infrastructure/DocumentStore/PostgresDocumentStore";
import {getConfiguredDB} from "@server/infrastructure/configuredDB";
import {PostgresMultiModelStore} from "@event-engine/infrastructure/MultiModelStore/PostgresMultiModelStore";
import {PostgresEventStore} from "@event-engine/infrastructure/EventStore/PostgresEventStore";
import {env} from "@server/env";
import {InMemoryEventStore} from "@event-engine/infrastructure/EventStore/InMemoryEventStore";
import {InMemoryDocumentStore} from "@event-engine/infrastructure/DocumentStore/InMemoryDocumentStore";
import {InMemoryMultiModelStore} from "@event-engine/infrastructure/MultiModelStore/InMemoryMultiModelStore";

let store: MultiModelStore;

export const getConfiguredMultiModelStore = () => {
if(!store) {
const es = getConfiguredEventStore();
const ds = getConfiguredDocumentStore();

if(env.eventStore.adapter === "postgres" && env.documentStore.adapter === "postgres") {
if(!(es instanceof PostgresEventStore)) {
throw new Error("Postgres MultiModelStore requires an instance of PostgresEventStore, but another EventStore is given.");
}

if(!(ds instanceof PostgresDocumentStore)) {
throw new Error("Postgres MultiModelStore requires an instance of PostgresDocumentStore, but another DocumentStore is given.");
}

store = new PostgresMultiModelStore(
getConfiguredDB(),
es,
ds
)
} else {
if(!(es instanceof InMemoryEventStore)) {
throw new Error("InMemory MultiModelStore requires an instance of InMemoryEventStore, but another EventStore is given.");
}

if(!(ds instanceof InMemoryDocumentStore)) {
throw new Error("InMemory MultiModelStore requires an instance of InMemoryDocumentStore, but another DocumentStore is given.");
}

store = new InMemoryMultiModelStore(
es,
ds
)
}
}

return store;
}
9 changes: 9 additions & 0 deletions packages/infrastructure/src/lib/ProophBoard/Description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Description {
_pbBoardId: string;
_pbCardId: string;
_pbCreatedBy: string;
_pbCreatedAt: string;
_pbLastUpdatedBy: string;
_pbLastUpdatedAt: string;
_pbVersion: number;
}
17 changes: 17 additions & 0 deletions packages/messaging/src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ import {cloneSchema, resolveRefs} from "@event-engine/messaging/resolve-refs";
import {DeepReadonly} from "json-schema-to-ts/lib/types/type-utils/readonly";
import {addInstanceNameToError} from "@event-engine/messaging/add-instance-name-to-error";

export interface CommandDescription {
name: string;
aggregateCommand: boolean;
}

export interface AggregateCommandDescription extends CommandDescription{
newAggregate: boolean;
aggregateName: string;
aggregateIdentifier: string;
}

export interface CommandRuntimeInfo {
desc: CommandDescription | AggregateCommandDescription;
factory: ReturnType<typeof makeCommand>,
schema: DeepReadonly<JSONSchema7>,
}

export type Command<P extends Payload = any, M extends Meta = any> = Message<
P,
M
Expand Down
10 changes: 10 additions & 0 deletions packages/messaging/src/lib/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import {cloneSchema, resolveRefs} from "@event-engine/messaging/resolve-refs";
import {DeepReadonly} from "json-schema-to-ts/lib/types/type-utils/readonly";
import {addInstanceNameToError} from "@event-engine/messaging/add-instance-name-to-error";

export interface EventDescription {
aggregateEvent: boolean;
}

export interface AggregateEventDescription extends EventDescription {
aggregateName: string;
aggregateIdentifier: string;
aggregateState: string;
}

export type EventVisibility = "public" | "service" | "archive";

export type EventMeta = Meta & {visibility: EventVisibility, version: string}
Expand Down
14 changes: 14 additions & 0 deletions packages/messaging/src/lib/value-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import {cloneSchema, resolveRefs} from "@event-engine/messaging/resolve-refs";
import {DeepReadonly} from "json-schema-to-ts/lib/types/type-utils/readonly";
import {addInstanceNameToError} from "@event-engine/messaging/add-instance-name-to-error";

export interface ValueObjectDescription {
name: string;
isList: boolean;
hasIdentifier: boolean;
}

export interface StateDescription extends ValueObjectDescription {
identifier: string;
}

export interface StateListDescription extends ValueObjectDescription{
itemIdentifier: string;
}

export const makeValueObject = <T>(
name: string,
schema: JSONSchema7,
Expand Down
18 changes: 18 additions & 0 deletions packages/shared/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# shared

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test shared` to execute the unit tests via [Jest](https://jestjs.io).
11 changes: 11 additions & 0 deletions packages/shared/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'shared',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/shared',
};
30 changes: 30 additions & 0 deletions packages/shared/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "shared",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/shared/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/shared/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/shared/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
Empty file added packages/shared/src/index.ts
Empty file.
9 changes: 9 additions & 0 deletions packages/shared/src/lib/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {CommandRuntimeInfo} from "@event-engine/messaging/command";

import {fleetManagementAddCarToFleetRuntimeInfo} from "@app/shared/commands/fleet-management/add-car-to-fleet";

type CommandRegistry = {[name: string]: CommandRuntimeInfo};

export const commands: CommandRegistry = {
"FleetManagement.AddCarToFleet": fleetManagementAddCarToFleetRuntimeInfo,
}
9 changes: 9 additions & 0 deletions packages/shared/src/lib/types/definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {CarListSchema} from "@app/shared/types/fleet-management/car/car-list.schema";
import {CarSchema} from "@app/shared/types/fleet-management/car/car.schema";

const definitions = {
'/definitions/fleet-management/car/car-list': CarListSchema,
'/definitions/fleet-management/car/car': CarSchema,
};

export default definitions;
7 changes: 7 additions & 0 deletions packages/shared/src/lib/types/references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {CarListSchema as FleetManagementCarListSchema} from "@app/shared/types/fleet-management/car/car-list.schema";
import {CarSchema as FleetManagementCarSchema} from "@app/shared/types/fleet-management/car/car.schema";

export type references = [
typeof FleetManagementCarListSchema,
typeof FleetManagementCarSchema,
];
Loading

0 comments on commit 6bbebcf

Please sign in to comment.