You can start a local backend by the following command:
# start DynamoDB local
docker-compose up -d
# start express
npm run local
We use DynamoDB Local to mock DynamoDB locally. To use a DynamoDB table deployed on AWS, you can modify the environment variables defined in app.local.ts
; set the TABLE_NAME
and AWS_REGION
environment variables to the actual table name and region, and remove the DYNAMODB_ENDPOINT
.
You can additonally set JOB_QUEUE_NAME
to the SQS queue name you deployed to AWS, if you want to test job enqueue feature locally.
When you add an endpoint, please follow the below steps:
- Add service directory (when necessary)
- implement method in
controller.ts
for the service - Add request / response type definition to
types.ts
- Add route to
router.ts
- If you added a new service, add the route to app according to it is public API or not. (
apps
directory)
Note that there are endpoints that requires authentication and not (a.k.a. public endpoints). These endpoints are separated by Express apps; Add your service to apps/authenticated.ts for endpoints that requires authentication, or apps/public.ts for ones that is public API. Additionally, always add your service to apps/loacl.ts to allow local testing.
When you add another asynchronous job, please follow the below steps:
- Add a job logic in jobs directory in a similar way as
jobs/sample-job.ts
. You must define at least a job handler (likesampleJob
function) and a job event type (likeSampleJobEvent
.) You must define a unique stirng asjobType
for your new job. - Modify
JobEvent
type incommon/jobs.ts
to include a new event type you added. You can use a union type, likeSampleJobEvent | SomeNewEvent
. - Modify
handler-job.ts
to call the new job handler function for the newjobType
like the below code.
switch (event.jobType) {
case 'sample':
await sampleJob(event.payload);
// Add this case
case 'someNewJobType':
await someNewJob(event.payload);
}
You can then run your new job using runJob
function in common/jobs.ts
.
You can also execute the async jobs in a scheduled manner. To configure scheduled rules, please edit cron-jobs.ts in CDK templates.
To add a scheduled job, you have to call addRule
function in the CronJobs
construct.
When calling the function, you can specify a job type, a cron schedule, and any event payload if required for the job.
// private addRule(jobType: string, schedule: Schedule, payload?: any)
this.addRule('SampleJob', Schedule.cron({ minute: '0', hour: '0', day: '1' }, {}));