Skip to content
Merged
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
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.3",
"rxjs": "^6.3.3",
"semantic-release": "^16.0.0-beta.9",
"semantic-release": "^16.0.0-beta.16",
"sort-package-json": "^1.16.0",
"ts-jest": "^23.10.4",
"ts-loader": "^5.3.2",
Expand All @@ -78,7 +78,7 @@
},
"peerDependencies": {
"aws-sdk": "^2.382.0",
"lodash": "^4.17.10",
"lodash": "^4.17.11",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.3.3",
"uuid": "^3.3.2"
Expand Down
16 changes: 10 additions & 6 deletions src/dynamo/expression/condition-expression-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { curry, forEach, isPlainObject } from 'lodash'
import { curry, isPlainObject } from 'lodash'
import { Metadata } from '../../decorator/metadata/metadata'
import { PropertyMetadata } from '../../decorator/metadata/property-metadata.model'
import { toDbOne, typeOf } from '../../mapper'
Expand Down Expand Up @@ -58,12 +58,15 @@ export function deepFilter(obj: any, filterFn: (value: any) => boolean): any {
} else if (isPlainObject(obj)) {
const returnObj: Record<string, any> = {}

forEach(obj, (value: any, key: string) => {
const item = deepFilter(value, filterFn)
if (item !== null) {
returnObj[key] = item
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key]
const item = deepFilter(value, filterFn)
if (item !== null) {
returnObj[key] = item
}
}
})
}

return Object.keys(returnObj).length ? returnObj : null
} else {
Expand Down Expand Up @@ -331,6 +334,7 @@ function validateArity(operator: ConditionOperator, values?: any[]) {
export const ERR_VALUES_BETWEEN_TYPE =
'both values for operator BETWEEN must have the same type, got ${value1} and ${value2}'
export const ERR_VALUES_IN = 'the provided value for IN operator must be an array'

// tslint:enable:no-invalid-template-strings

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const BRACED_INDEX_REGEX = /\[(\d+)]/g
/**
* Creates a unique attribute value placeholder name to use in the expression
*
* @returns {string} The unique attribute value placeholder name in respect to the given existing value names (no duplicates)
* @returns {string} The unique attribute value placeholder name in respect to the given existing value names (no duplicates allowed)
*/
export function uniqueAttributeValueName(key: string, existingValueNames?: string[]): string {
key = key.replace(/\./g, '__').replace(BRACED_INDEX_REGEX, attributeNameReplacer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapKeys } from 'lodash'
import { Metadata } from '../../../decorator/metadata/metadata'
import { Attribute } from '../../../mapper'
import { uniqueAttributeValueName } from '../functions/unique-attribute-value-name.function'
import { ConditionExpressionDefinitionFunction } from '../type/condition-expression-definition-function'
import { Expression } from '../type/expression.type'
Expand All @@ -19,19 +19,35 @@ export function mergeConditions(
conditionDefinitionFns.forEach(conditionDefinitionFn => {
// we can reuse the same for multiple conditions
const condition = conditionDefinitionFn(expressionAttributeValues, metadata)
Object.assign(mergedCondition.attributeNames, condition.attributeNames)
mergedCondition.attributeNames = {...mergedCondition.attributeNames, ...condition.attributeNames}

// we need to make sure the value variable name is unique
const attributeValues = mapKeys(condition.attributeValues, (value, key) => {
/*
* we need to make sure the value variable name is unique, this wont' work so the second :name must be renamed
* {
* ":name" : { S: "the name" },
* ":name" : { S: "other name" }
* }
* |
* |
* ▽
* {
* ":name" : { S: "the name" },
* ":name_2" : { S: "other name" }
* }
*
*/
const attributeValues: Record<string, Attribute> = {}
Object.keys(condition.attributeValues).forEach(key => {
const unique = uniqueAttributeValueName(key.replace(':', ''), Object.keys(mergedCondition.attributeValues))
if (key !== unique) {
// rename of the attributeName is required in condition
condition.statement = condition.statement.replace(key, unique)
}

return unique
attributeValues[unique] = condition.attributeValues[key]
})

Object.assign(mergedCondition.attributeValues, attributeValues)
mergedCondition.attributeValues = {...mergedCondition.attributeValues, ...attributeValues }
statements.push(condition.statement)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
import { forEach } from 'lodash'
import { Metadata } from '../../decorator/metadata/index'
import { Attributes } from '../../mapper/index'
import { SortedUpdateExpressions } from '../request/update/update.request'
import { addUpdateExpression } from './param-util'
import { Expression, UpdateExpressionDefinitionFunction } from './type/index'
import { Expression, UpdateExpression, UpdateExpressionDefinitionFunction } from './type/index'
import { UpdateActionKeyword } from './type/update-action-keyword.type'

export function prepareAndAddUpdateExpressions(
metadata: Metadata<any>,
params: DynamoDB.UpdateItemInput | DynamoDB.Update,
updateDefFns: UpdateExpressionDefinitionFunction[],
) {
if (updateDefFns && updateDefFns.length) {
const sortedByActionKeyWord: SortedUpdateExpressions = updateDefFns
const sortedByActionKeyWord: Map<UpdateActionKeyword, UpdateExpression[]> = updateDefFns
.map(updateDefFn => {
return updateDefFn(<any>params.ExpressionAttributeNames, metadata)
})
.reduce(
(result, expr) => {
if (!result[expr.type]) {
result[expr.type] = []
.reduce((result, expr) => {
const actionKeyword = expr.type
if (!result.has(actionKeyword)) {
result.set(actionKeyword, [])
}

result[expr.type].push(expr)
result.get(actionKeyword).push(expr)
return result
},
<SortedUpdateExpressions>{},
new Map(),
)

const actionStatements: string[] = []
let attributeValues: Attributes = {}
let attributeNames: Record<string, string> = {}

forEach(sortedByActionKeyWord, (value, key) => {
for (const [actionKeyword, updateExpressions] of sortedByActionKeyWord) {
const statements: string[] = []
if (value && value.length) {
value.forEach(updateExpression => {
if (updateExpressions && updateExpressions.length) {
updateExpressions.forEach(updateExpression => {
statements.push(updateExpression.statement)
attributeValues = { ...attributeValues, ...updateExpression.attributeValues }
attributeNames = { ...attributeNames, ...updateExpression.attributeNames }
})
actionStatements.push(`${key} ${statements.join(', ')}`)
actionStatements.push(`${actionKeyword} ${statements.join(', ')}`)
}
})
}

const expression: Expression = {
statement: actionStatements.join(' '),
Expand Down
3 changes: 1 addition & 2 deletions src/dynamo/request/get/get.request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
import { values as objValues } from 'lodash'
import { Observable } from 'rxjs'
import { map, tap } from 'rxjs/operators'
import { createLogger, Logger } from '../../../logger/logger'
Expand Down Expand Up @@ -28,7 +27,7 @@ export class GetRequest<T> extends StandardRequest<T, DynamoDB.GetItemInput, Get
// tslint:disable-next-line:no-unnecessary-callback-wrapper
const resolved = attributesToGet.map(a => resolveAttributeNames(a))
this.params.ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ')
objValues(resolved).forEach(r => {
Object.values(resolved).forEach(r => {
this.params.ExpressionAttributeNames = { ...this.params.ExpressionAttributeNames, ...r.attributeNames }
})
return this
Expand Down
5 changes: 1 addition & 4 deletions src/dynamo/request/update/update.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import { ModelConstructor } from '../../../model'
import { DynamoRx } from '../../dynamo-rx'
import { prepareAndAddUpdateExpressions } from '../../expression/prepare-and-add-update-expressions.function'
import { addUpdate } from '../../expression/request-expression-builder'
import { RequestUpdateFunction, UpdateExpression, UpdateExpressionDefinitionFunction } from '../../expression/type'
import { UpdateActionKeyword } from '../../expression/type/update-action-keyword.type'
import { RequestUpdateFunction, UpdateExpressionDefinitionFunction } from '../../expression/type'
import { WriteRequest } from '../write.request'

export type SortedUpdateExpressions = Record<UpdateActionKeyword, UpdateExpression[]>

export class UpdateRequest<T> extends WriteRequest<T, DynamoDB.UpdateItemInput, UpdateRequest<T>> {
private readonly logger: Logger

Expand Down
2 changes: 1 addition & 1 deletion src/mapper/for-type/number.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNumber } from 'lodash'
import { NumberAttribute } from '../type/attribute.type'
import { isNumber } from 'lodash'
import { MapperForType } from './base.mapper'

function numberFromDb(attributeValue: NumberAttribute): number {
Expand Down
2 changes: 1 addition & 1 deletion tools/tslint/test/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash'
import * as moment from 'moment'
import * as DynamoDB from 'aws-sdk'
import { Config } from 'aws-sdk'
import { Key } from 'aws-sdk/clients/dynamodb'
Expand Down