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

feat: airline example #103

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions examples/airline/infra/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"app": "ts-node ./src/app.ts",
"watch": "."
}
36 changes: 36 additions & 0 deletions examples/airline/infra/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@example/airline-infra",
"version": "0.0.0",
"private": true,
"scripts": {
"test": "cdk synth",
"synth": "cdk synth",
"cdk": "cdk",
"deploy": "cdk deploy --require-approval=never",
"hotswap": "cdk deploy --hotswap --require-approval=never",
"eventual": "eventual"
},
"dependencies": {
"@example/airline": "workspace:^"
},
"devDependencies": {
"aws-cdk-lib": "2.50.0",
"constructs": "10.1.154",
"@eventual/aws-cdk": "workspace:^",
"@eventual/cli": "workspace:^",
"@types/jest": "^29",
"@types/node": "^16",
"aws-cdk": "2.50.0",
"esbuild": "^0.15.14",
"jest": "^29",
"ts-jest": "^29",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
}
}
}
17 changes: 17 additions & 0 deletions examples/airline/infra/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { App } from "aws-cdk-lib";
import { OperationsStack } from "./operations";
import { ReservationsStack } from "./reservations";

const app = new App();

const stages = ["dev", "prod"] as const;

for (const stage of stages) {
new OperationsStack(app, `Operations-${stage}`, {
stage,
});

new ReservationsStack(app, `Reservations-${stage}`, {
stage,
});
}
34 changes: 34 additions & 0 deletions examples/airline/infra/src/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Service } from "@eventual/aws-cdk";
import { aws_dynamodb, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";

export interface OperationsStackProps extends StackProps {
stage: "dev" | "prod";
}

export class OperationsStack extends Stack {
readonly service: Service;
readonly table: aws_dynamodb.Table;

constructor(scope: Construct, id: string, props: OperationsStackProps) {
super(scope, id, props);

this.table = new aws_dynamodb.Table(this, "Table", {
partitionKey: {
name: "pk",
type: aws_dynamodb.AttributeType.STRING,
},
billingMode: aws_dynamodb.BillingMode.PAY_PER_REQUEST,
});

this.service = new Service(this, "Service", {
name: `operations-${props.stage}`,
entry: require.resolve("@example/airline/lib/operations/service.js"),
environment: {
TABLE_NAME: this.table.tableArn,
},
});

this.table.grantReadWriteData(this.service);
}
}
34 changes: 34 additions & 0 deletions examples/airline/infra/src/reservations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Service } from "@eventual/aws-cdk";
import { aws_dynamodb, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";

export interface ReservationsStackProps extends StackProps {
stage: "dev" | "prod";
}

export class ReservationsStack extends Stack {
readonly service: Service;
readonly table: aws_dynamodb.Table;

constructor(scope: Construct, id: string, props: ReservationsStackProps) {
super(scope, id, props);

this.table = new aws_dynamodb.Table(this, "Table", {
partitionKey: {
name: "pk",
type: aws_dynamodb.AttributeType.STRING,
},
billingMode: aws_dynamodb.BillingMode.PAY_PER_REQUEST,
});

this.service = new Service(this, "Service", {
name: `reservations-${props.stage}`,
entry: require.resolve("@example/airline/lib/reservations/service.js"),
environment: {
TABLE_NAME: this.table.tableArn,
},
});

this.table.grantReadWriteData(this.service);
}
}
16 changes: 16 additions & 0 deletions examples/airline/infra/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig-base.cjs",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"typeRoots": ["./node_modules/@types"],
"allowJs": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["lib", "node_modules"],
"references": [
{ "path": "../../../packages/@eventual/aws-cdk" },
{ "path": "../" }
]
}
32 changes: 32 additions & 0 deletions examples/airline/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@example/airline",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"synth": "cd infra && pnpm synth",
"test": "pnpm synth"
},
"dependencies": {
"@eventual/core": "workspace:^",
"@aws-sdk/client-dynamodb": "^3.229.0",
"@aws-sdk/lib-dynamodb": "^3.229.0",
"ms": "^2.1.3"
},
"devDependencies": {
"@eventual/cli": "workspace:^",
"@types/jest": "^29",
"@types/node": "^16",
"@types/ms": "^0.7.31",
"jest": "^29",
"ts-jest": "^29",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
}
}
}
6 changes: 6 additions & 0 deletions examples/airline/src/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";

export const dynamo = DynamoDBDocumentClient.from(new DynamoDBClient({}));

export const tableName = process.env.TABLE_NAME!;
58 changes: 58 additions & 0 deletions examples/airline/src/operations/add-flight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { PutCommand } from "@aws-sdk/lib-dynamodb";
import { activity, api, event } from "@eventual/core";
import { dynamo, tableName } from "../dynamodb.js";
import { FlightAdded, FlightEventType } from "./flight-event.js";

interface AddFlightRequest {
flightId: string;
flightNo: string;
origin: string;
destination: string;
aircraftType: string;
tailNo: string;
departureTime: string;
arrivalTime: string;
}

interface AddFlightResponse {
flightId: string;
}

export const flightAdded = event<FlightAdded>(FlightEventType.FlightAdded);

api.post("/flights", async (request) => {
const payload: AddFlightRequest = await request.json();

await addFlight(payload);

await flightAdded.publishEvents({
type: FlightEventType.FlightAdded,
...payload,
});

return new Response(
JSON.stringify({
flightId: payload.flightId,
} satisfies AddFlightResponse),
{
// request is accepted and is being processed
status: 202,
headers: {
"Content-Type": "application/json",
},
}
);
});

const addFlight = activity("addFlight", async (request: AddFlightRequest) => {
await dynamo.send(
new PutCommand({
TableName: tableName,
Item: {
pk: request.flightId,
...request,
},
ConditionExpression: "attribute_not_exists(pk)",
})
);
});
67 changes: 67 additions & 0 deletions examples/airline/src/operations/cancel-flight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { UpdateCommand } from "@aws-sdk/lib-dynamodb";
import { dynamo, tableName } from "../dynamodb.js";
import { FlightCancelled, FlightEventType } from "./flight-event.js";
import { CancelledFlightRecord, FlightStatus } from "./flight-record.js";
import { activity, api, event } from "@eventual/core";

interface CancelFlightRequest {
flightId: string;
day: string;
route: string;
origin: string;
destination: string;
cancelledAt: string;
}

export const flightCancelled = event<FlightCancelled>(
FlightEventType.FlightCancelled
);

api.post("/flight/:flightId/cancellation", async (request) => {
const flightId = request.params?.flightId;
if (!flightId) {
return new Response("Missing Flight ID", {
status: 400,
});
}
const payload: CancelFlightRequest = await request.json();

const flight = await cancelFlight(payload);

await flightCancelled.publishEvents({
type: FlightEventType.FlightCancelled,
cancelledAt: flight.cancelledAt,
day: flight.day,
destination: flight.destination,
flightNo: flight.flightNo,
origin: flight.origin,
route: flight.route,
});

return new Response(JSON.stringify(flight));
});

const cancelFlight = activity(
"cancelFlight",
async (request: CancelFlightRequest): Promise<CancelledFlightRecord> => {
const result = await dynamo.send(
new UpdateCommand({
TableName: tableName,
Key: {
pk: request.flightId,
},
UpdateExpression: `SET #status = :status`,
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": FlightStatus.Cancelled,
":scheduled": FlightStatus.Scheduled,
},
ConditionExpression: `attribute_exists(pk) AND #status = :scheduled`,
})
);

return result.Attributes as CancelledFlightRecord;
}
);
27 changes: 27 additions & 0 deletions examples/airline/src/operations/flight-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export enum FlightEventType {
FlightAdded = "FlightAdded",
FlightCancelled = "FlightCancelled",
}

export type FlightEvent = FlightAdded | FlightCancelled;

export interface FlightAdded {
type: FlightEventType.FlightAdded;
flightNo: string;
origin: string;
destination: string;
aircraftType: string;
tailNo: string;
departureTime: string;
arrivalTime: string;
}

export interface FlightCancelled {
type: FlightEventType.FlightCancelled;
flightNo: string;
day: string;
route: string;
origin: string;
destination: string;
cancelledAt: string;
}
23 changes: 23 additions & 0 deletions examples/airline/src/operations/flight-record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export enum FlightStatus {
Cancelled = "Cancelled",
Landed = "Landed",
Scheduled = "Scheduled",
}

export interface FlightRecord {
pk: string;
status: FlightStatus;

flightId: string;
flightNo: string;
day: string;
route: string;
origin: string;
destination: string;
cancelledAt?: string;
}

export interface CancelledFlightRecord extends FlightRecord {
status: FlightStatus.Cancelled;
cancelledAt: string;
}
2 changes: 2 additions & 0 deletions examples/airline/src/operations/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./add-flight.js";
export * from "./cancel-flight.js";
Loading