Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
Serverless Hero

Lee Gilmore leegilmorecode

:atom:
Serverless Hero
View GitHub Profile
@leegilmorecode
leegilmorecode / customer-account.ts
Created December 3, 2022 04:39
The customer account aggregate root entity
import * as customerAccountCreatedEvent from '@events/customer-account-created';
import * as customerAccountUpdatedEvent from '@events/customer-account-updated';
import * as customerAccountUpgradedEvent from '@events/customer-account-upgraded';
import {
CreateCustomerAccountProps,
NewCustomerAccountProps,
PaymentStatus,
SubscriptionType,
} from '@models/customer-account-types';
@leegilmorecode
leegilmorecode / domain-event.ts
Created December 3, 2022 04:35
Example of our domain event models and interfaces
export type DomainEvent = {
source: string;
eventName: string;
event: Record<string, any>;
eventDateTime: string;
eventVersion: string;
};
export interface ICreateDomainEvent {
source: string;
@leegilmorecode
leegilmorecode / aggregate.ts
Last active December 3, 2022 04:34
Example aggregate root abstract base entity
import { DomainEvent } from './domain-event';
import { Entity } from './entity';
// denotes visually that this entity is the aggregate root
// and stores the overall domain events for publishing
export abstract class AggregateRoot<T> extends Entity<T> {
// aggregates which implement this must create this method
// to consolidate events from the full aggrgate root and children
abstract retrieveDomainEvents(): DomainEvent[];
}
@leegilmorecode
leegilmorecode / database-adapter.ts
Created November 6, 2022 16:31
Example of our database adapter which talks to DynamoDB
import * as AWS from 'aws-sdk';
import { CustomerAccountProps } from '@models/types';
import { config } from '@config/config';
const dynamoDb = new AWS.DynamoDB.DocumentClient();
// this is the secondary adapter which creates the account from the db
// Note: you would typically use a module or package here to interact
// with the database technology - for example dynamoose
@leegilmorecode
leegilmorecode / create-customer-account.adapter.ts
Created November 6, 2022 16:30
Example of our primary adapter for Lambda
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { CreateCustomerAccountProps } from '@models/types';
import { ValidationError } from '@errors/validation-error';
import { createCustomerAccountUseCase } from '@use-cases/create-customer-account';
import { errorHandler } from '@packages/apigw-error-handler';
import { schema } from './create-customer-account.schema';
import { schemaValidator } from '@packages/schema-validator';
import { v4 as uuid } from 'uuid';
@leegilmorecode
leegilmorecode / create-customer-account.ts
Created November 6, 2022 15:15
Example of our create customer account use case
import {
CreateCustomerAccountProps,
CustomerAccountProps,
} from '@models/types';
import { eventName, eventSource } from '@events/customer-account-created';
import { CustomerAccount } from '@domain/customer-account';
import { createCustomerAccount } from '@repositories/create-customer-account-repository';
import { publishEvent } from '@repositories/publish-event-recipient';
@leegilmorecode
leegilmorecode / customer-account.ts
Created November 6, 2022 15:08
Example of our customer account entity
import {
CreateCustomerAccountProps,
NewCustomerAccountProps,
PaymentStatus,
SubscriptionType,
UnmarshalledCustomerAccount,
} from '@models/types';
import { Entity } from '@entity/entity';
import { PaymentInvalidError } from '@errors/payment-invalid-error';
@leegilmorecode
leegilmorecode / create-order.ts
Created October 28, 2022 09:42
Example of a bad lambda function handler not using Hexagonal Architectures
import * as AWS from 'aws-sdk'; // <-- service specific
// <-- lambda specific
import {
APIGatewayEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from 'aws-lambda';
import { OrderCreate } from '../../../../types';
@leegilmorecode
leegilmorecode / create-order.ts
Last active October 10, 2022 02:40
create-order.ts sample
// we get the order details from the event
const order: OrderCreated = JSON.parse(body);
// we generate an idempotency key
const idempotencyKey: string = uuidV5(
JSON.stringify(order),
namespaces.orders
);
// create the detail of the event
@leegilmorecode
leegilmorecode / validate.ts
Created October 9, 2022 09:08
validate.ts
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajvOptions = {
allErrors: true,
};
// as this is not a monorepo and an example repo only we are using a typescript path alias
// whereas this would typically be published to npm and reused in all domain service
export function validate(