1- import { GenericContainer } from ' testcontainers' ;
1+ import { GenericContainer } from " testcontainers" ;
22import {
33 CreateTableCommand ,
44 DeleteTableCommand ,
55 DynamoDBClient ,
6- UpdateTimeToLiveCommand
7- } from ' @aws-sdk/client-dynamodb' ;
8- import { DynamoDBDocumentClient } from ' @aws-sdk/lib-dynamodb' ;
9- import { DatastoreConfig } from ' ../config' ;
6+ UpdateTimeToLiveCommand ,
7+ } from " @aws-sdk/client-dynamodb" ;
8+ import { DynamoDBDocumentClient } from " @aws-sdk/lib-dynamodb" ;
9+ import { DatastoreConfig } from " ../config" ;
1010
1111export async function setupDynamoDBContainer ( ) {
12- const container = await new GenericContainer ( ' amazon/dynamodb-local' )
12+ const container = await new GenericContainer ( " amazon/dynamodb-local" )
1313 . withExposedPorts ( 8000 )
1414 . start ( ) ;
1515
1616 const endpoint = `http://${ container . getHost ( ) } :${ container . getMappedPort ( 8000 ) } ` ;
1717
1818 const ddbClient = new DynamoDBClient ( {
19- region : ' us-west-2' ,
19+ region : " us-west-2" ,
2020 endpoint,
2121 credentials : {
22- accessKeyId : ' fakeMyKeyId' ,
23- secretAccessKey : ' fakeSecretAccessKey'
24- }
22+ accessKeyId : " fakeMyKeyId" ,
23+ secretAccessKey : " fakeSecretAccessKey" ,
24+ } ,
2525 } ) ;
2626
2727 const docClient = DynamoDBDocumentClient . from ( ddbClient ) ;
2828
29- const config : DatastoreConfig = {
30- region : ' us-west-2' ,
29+ const config : DatastoreConfig = {
30+ region : " us-west-2" ,
3131 endpoint,
32- lettersTableName : ' letters' ,
33- miTableName : ' management-info' ,
34- suppliersTableName : ' suppliers' ,
32+ lettersTableName : " letters" ,
33+ miTableName : " management-info" ,
34+ suppliersTableName : " suppliers" ,
3535 lettersTtlHours : 1 ,
36- miTtlHours : 1
36+ miTtlHours : 1 ,
3737 } ;
3838
3939 return {
4040 container,
4141 ddbClient,
4242 docClient,
4343 endpoint,
44- config
44+ config,
4545 } ;
4646}
4747
4848export type DBContext = Awaited < ReturnType < typeof setupDynamoDBContainer > > ;
4949
5050const createLetterTableCommand = new CreateTableCommand ( {
51- TableName : ' letters' ,
52- BillingMode : ' PAY_PER_REQUEST' ,
53- KeySchema : [
54- { AttributeName : ' supplierId' , KeyType : ' HASH' } , // Partition key
55- { AttributeName : 'id' , KeyType : ' RANGE' } // Sort key
56- ] ,
57- GlobalSecondaryIndexes : [
58- {
59- IndexName : ' supplierStatus-index' ,
60- KeySchema : [
61- { AttributeName : ' supplierStatus' , KeyType : ' HASH' } , // Partition key for GSI
62- { AttributeName : ' supplierStatusSk' , KeyType : ' RANGE' } // Sort key for GSI
63- ] ,
64- Projection : {
65- ProjectionType : ' ALL'
66- }
67- }
68- ] ,
69- AttributeDefinitions : [
70- { AttributeName : ' supplierId' , AttributeType : 'S' } ,
71- { AttributeName : 'id' , AttributeType : 'S' } ,
72- { AttributeName : ' supplierStatus' , AttributeType : 'S' } ,
73- { AttributeName : ' supplierStatusSk' , AttributeType : 'S' } ,
74- ]
75- } ) ;
51+ TableName : " letters" ,
52+ BillingMode : " PAY_PER_REQUEST" ,
53+ KeySchema : [
54+ { AttributeName : " supplierId" , KeyType : " HASH" } , // Partition key
55+ { AttributeName : "id" , KeyType : " RANGE" } , // Sort key
56+ ] ,
57+ GlobalSecondaryIndexes : [
58+ {
59+ IndexName : " supplierStatus-index" ,
60+ KeySchema : [
61+ { AttributeName : " supplierStatus" , KeyType : " HASH" } , // Partition key for GSI
62+ { AttributeName : " supplierStatusSk" , KeyType : " RANGE" } , // Sort key for GSI
63+ ] ,
64+ Projection : {
65+ ProjectionType : " ALL" ,
66+ } ,
67+ } ,
68+ ] ,
69+ AttributeDefinitions : [
70+ { AttributeName : " supplierId" , AttributeType : "S" } ,
71+ { AttributeName : "id" , AttributeType : "S" } ,
72+ { AttributeName : " supplierStatus" , AttributeType : "S" } ,
73+ { AttributeName : " supplierStatusSk" , AttributeType : "S" } ,
74+ ] ,
75+ } ) ;
7676
7777const updateTimeToLiveCommand = new UpdateTimeToLiveCommand ( {
78- TableName : ' letters' ,
79- TimeToLiveSpecification : {
80- AttributeName : ' ttl' ,
81- Enabled : true
82- }
83- } ) ;
78+ TableName : " letters" ,
79+ TimeToLiveSpecification : {
80+ AttributeName : " ttl" ,
81+ Enabled : true ,
82+ } ,
83+ } ) ;
8484
8585const createMITableCommand = new CreateTableCommand ( {
86- TableName : ' management-info' ,
87- BillingMode : ' PAY_PER_REQUEST' ,
88- KeySchema : [
89- { AttributeName : ' supplierId' , KeyType : ' HASH' } , // Partition key
90- { AttributeName : 'id' , KeyType : ' RANGE' } // Sort key
91- ] ,
92- AttributeDefinitions : [
93- { AttributeName : ' supplierId' , AttributeType : 'S' } ,
94- { AttributeName : 'id' , AttributeType : 'S' } ,
95- ]
96- } ) ;
86+ TableName : " management-info" ,
87+ BillingMode : " PAY_PER_REQUEST" ,
88+ KeySchema : [
89+ { AttributeName : " supplierId" , KeyType : " HASH" } , // Partition key
90+ { AttributeName : "id" , KeyType : " RANGE" } , // Sort key
91+ ] ,
92+ AttributeDefinitions : [
93+ { AttributeName : " supplierId" , AttributeType : "S" } ,
94+ { AttributeName : "id" , AttributeType : "S" } ,
95+ ] ,
96+ } ) ;
9797
9898const createSupplierTableCommand = new CreateTableCommand ( {
99- TableName : 'suppliers' ,
100- BillingMode : 'PAY_PER_REQUEST' ,
101- KeySchema : [
102- { AttributeName : 'id' , KeyType : 'HASH' } // Partition key
103- ] ,
104- GlobalSecondaryIndexes : [
105- {
106- IndexName : 'supplier-apim-index' ,
107- KeySchema : [
108- { AttributeName : 'apimId' , KeyType : 'HASH' } // Partition key for GSI
109- ] ,
110- Projection : {
111- ProjectionType : 'ALL'
112- }
113- }
114- ] ,
115- AttributeDefinitions : [
116- { AttributeName : 'id' , AttributeType : 'S' } ,
117- { AttributeName : 'apimId' , AttributeType : 'S' }
118- ]
119- } ) ;
120-
99+ TableName : "suppliers" ,
100+ BillingMode : "PAY_PER_REQUEST" ,
101+ KeySchema : [
102+ { AttributeName : "id" , KeyType : "HASH" } , // Partition key
103+ ] ,
104+ GlobalSecondaryIndexes : [
105+ {
106+ IndexName : "supplier-apim-index" ,
107+ KeySchema : [
108+ { AttributeName : "apimId" , KeyType : "HASH" } , // Partition key for GSI
109+ ] ,
110+ Projection : {
111+ ProjectionType : "ALL" ,
112+ } ,
113+ } ,
114+ ] ,
115+ AttributeDefinitions : [
116+ { AttributeName : "id" , AttributeType : "S" } ,
117+ { AttributeName : "apimId" , AttributeType : "S" } ,
118+ ] ,
119+ } ) ;
121120
122121export async function createTables ( context : DBContext ) {
123122 const { ddbClient } = context ;
@@ -129,19 +128,24 @@ export async function createTables(context: DBContext) {
129128 await ddbClient . send ( createSupplierTableCommand ) ;
130129}
131130
132-
133131export async function deleteTables ( context : DBContext ) {
134132 const { ddbClient } = context ;
135133
136- await ddbClient . send ( new DeleteTableCommand ( {
137- TableName : 'letters'
138- } ) ) ;
139-
140- await ddbClient . send ( new DeleteTableCommand ( {
141- TableName : 'management-info'
142- } ) ) ;
143-
144- await ddbClient . send ( new DeleteTableCommand ( {
145- TableName : 'suppliers'
146- } ) ) ;
134+ await ddbClient . send (
135+ new DeleteTableCommand ( {
136+ TableName : "letters" ,
137+ } ) ,
138+ ) ;
139+
140+ await ddbClient . send (
141+ new DeleteTableCommand ( {
142+ TableName : "management-info" ,
143+ } ) ,
144+ ) ;
145+
146+ await ddbClient . send (
147+ new DeleteTableCommand ( {
148+ TableName : "suppliers" ,
149+ } ) ,
150+ ) ;
147151}
0 commit comments