Hi, how to give pass Table name to all routes. I...
# help
s
Hi, how to give pass Table name to all routes. In older version we do like this
const api = new Api(stack, "api", {
customDomain: "<http://api.easy-cloud.in|api.easy-cloud.in>",
defaultFunctionProps: {
// Pass in the table name to our API
environment: {
tableName: table.dynamodbTable.tableName,
},
},
routes: {
"POST /addsubscriber": "functions/addSubscriber.handler",
},
});
How to do it in v1.x.x?
a
Copy code
const api = new Api(stack, "api", {
      defaults: {
         authorizer: "iam",
         function: {
            environment: {
               ...tableNames,
            },
         },
      },
replace ...tableNames with your tableName: bla config line
I'm passing multiple tables, that's why my code looks different
the trick is that defaultFunctionProps is now defaults: { function: {
s
@Adrian Schweizer Thanks, I refactored my code to this
defaults: {
function: {
environment: {
tableNames: "ec.subscriptions",
},
},
},
Is this correct? But it still throws an error.
@Adrian Schweizer Thankyou, i made it work. I just want to clarify is tableNames: is an array object .
a
no, it's actually a map in my case
it's constructed like this:
Copy code
const tables = {
      organisations: new Table(stack, "Organisations", {
         fields: {
            organisationId: "string",
         },
         primaryIndex: { partitionKey: "organisationId" },
      }),
      map_user_organisation: new Table(stack, "Map_User_Organisation", {
         fields: {
            userId: "string",
            organisationId: "string",
         },
         primaryIndex: { partitionKey: "userId", sortKey: "organisationId" },
         globalIndexes: {
            organisationIndex: { partitionKey: "organisationId", sortKey: "userId" },
         },
      }),
      events: new Table(stack, "Events", {
         fields: {
            eventId: "string",
            organisationId: "string",
         },
         primaryIndex: { partitionKey: "eventId" },
         globalIndexes: {
            organisationIndex: { partitionKey: "organisationId", sortKey: "eventId" },
         },
      }),
      participations: new Table(stack, "Participations", {
         fields: {
            participationId: "string",
            eventId: "string",
         },
         primaryIndex: { partitionKey: "participationId" },
         globalIndexes: {
            eventIndex: { partitionKey: "eventId", sortKey: "participationId" },
         },
      }),
   };

   // Add table names to process environment for lambda functions
   const tableNames = Object.keys(tables).reduce((tableNames, key) => ({
      ...tableNames,
      ["TABLE_NAME_" + key.toUpperCase()]: tables[key].tableName,
   }), {});