From 74f2c29b5a70e7a8b0a33c02942acc037fb5d2e3 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Mon, 30 Apr 2018 14:49:32 -0400 Subject: [PATCH] fix: remove static dep on 'definitions' and 'serverless-yml' --- src/db-utils.ts | 30 +++++++++++++++++++++--------- src/lambda-utils.ts | 7 ++++++- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/db-utils.ts b/src/db-utils.ts index 6ec76140b..c6910fed1 100644 --- a/src/db-utils.ts +++ b/src/db-utils.ts @@ -20,7 +20,8 @@ import { waitImmediate, timeMethods, traverse, - pluck + pluck, + defineGetter } from './utils' import { prettify, alphabetical, format } from './string-utils' @@ -60,7 +61,6 @@ type BatchWorker = (batch:Batch) => Promise type ItemWorker = (item:any) => Promise const alwaysTrue = (...any) => true -const DEFINITIONS = require('./definitions') const MAX_BATCH_SIZE = 25 const CONSISTENT_READ_EVERYTHING = true const TABLE_BUCKET_REGEX = /-bucket-\d+$/ @@ -98,12 +98,22 @@ const renderDefinitions = ({ definitions, stackName }) => { } function createDBUtils ({ aws, logger, env }) { - const definitions = renderDefinitions({ - definitions: DEFINITIONS, - stackName: env.STACK_NAME - }) + const getDefinitions = (() => { + let definitions + return () => { + if (!definitions) { + definitions = renderDefinitions({ + definitions: require('./definitions'), + stackName: env.STACK_NAME + }) + } - const getDefinition = tableName => { + return definitions + } + })(); + + const getCachedDefinition = tableName => { + const definitions = getDefinitions() const logicalId = Object.keys(definitions).find(logicalId => { return definitions[logicalId].Properties.TableName === tableName }) @@ -129,6 +139,7 @@ function createDBUtils ({ aws, logger, env }) { let tableBuckets const getTableBuckets = () => { + const definitions = getDefinitions() if (!tableBuckets) { tableBuckets = Object.keys(definitions) .filter(logicalId => { @@ -208,7 +219,7 @@ function createDBUtils ({ aws, logger, env }) { tableAPI.client = aws.docClient tableAPI.rawClient = aws.dynamodb tableAPI.name = TableName - tableAPI.definition = getDefinition(TableName) + defineGetter(tableAPI, 'definition', () => getCachedDefinition(TableName)) tableAPI.batchProcess = ({ params={}, ...opts }) => { return batchProcess({ params: { ...params, TableName }, @@ -341,6 +352,7 @@ function createDBUtils ({ aws, logger, env }) { } const getTableDefinition = async (TableName:string) => { + const definitions = getDefinitions() if (definitions[TableName]) return definitions[TableName] const { Table } = await aws.dynamodb.describeTable({ TableName }).promise() @@ -544,7 +556,7 @@ function createDBUtils ({ aws, logger, env }) { getModelMap, getTableDefinition, get definitions() { - return definitions + return getDefinitions() } } diff --git a/src/lambda-utils.ts b/src/lambda-utils.ts index 2e9a1e249..1bb08cda9 100644 --- a/src/lambda-utils.ts +++ b/src/lambda-utils.ts @@ -13,7 +13,6 @@ import { unitToMillis } from './constants' -import serverlessYml from './cli/serverless-yml' import PRICING from './lambda-pricing' const defaultConcurrency = 1 @@ -112,7 +111,12 @@ export default class LambdaUtils { return this.aws.lambda.getFunctionConfiguration({ FunctionName }).promise() } + private get serverlessYml() { + return require('./cli/serverless-yml') + } + private requireLambdaByName = (shortName:string) => { + const { serverlessYml } = this const { functions } = serverlessYml const handlerExportPath = functions[shortName].handler const lastDotIdx = handlerExportPath.lastIndexOf('.') @@ -126,6 +130,7 @@ export default class LambdaUtils { private invokeLocal = async (params:AWS.Lambda.InvocationRequest) :Promise => { + const { serverlessYml } = this const { FunctionName, InvocationType, Payload } = params this.logger.debug(`invoking ${params.FunctionName} inside ${this.env.FUNCTION_NAME}`) const shortName = this.env.getStackResourceShortName(FunctionName)