@xapp/dynamo-service
Advanced tools
Comparing version 0.0.37 to 0.0.38
@@ -43,3 +43,3 @@ import { DynamoDB } from "aws-sdk"; | ||
constructor(db: ConstructorDB); | ||
put(table: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
put(table: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap, condition?: ConditionExpression): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
update<T>(table: string, key: DynamoDB.DocumentClient.Key, update: UpdateBody<T>): Promise<void>; | ||
@@ -46,0 +46,0 @@ update<T>(table: string, key: DynamoDB.DocumentClient.Key, update: UpdateBody<T>, condition: ConditionExpression): Promise<void>; |
@@ -9,7 +9,4 @@ "use strict"; | ||
} | ||
put(table, obj) { | ||
const params = { | ||
TableName: table, | ||
Item: obj | ||
}; | ||
put(table, obj, condition = {}) { | ||
const params = Object.assign({ TableName: table, Item: obj }, condition); | ||
return this.db.put(params).promise(); | ||
@@ -16,0 +13,0 @@ } |
@@ -20,2 +20,4 @@ import { ConditionExpression, DynamoService, QueryParams, QueryResult, ScanParams, ScanResult, UpdateBody, UpdateReturnType } from "./DynamoService"; | ||
readonly tableSchema: TableSchema; | ||
private readonly primaryKey; | ||
private readonly sortKey; | ||
private readonly requiredKeys; | ||
@@ -27,3 +29,3 @@ private readonly constantKeys; | ||
constructor(tableName: string, db: DynamoService, tableSchema: TableSchema, props?: TableServiceProps); | ||
put(obj: T): Promise<T>; | ||
put(obj: T, condition?: ConditionExpression): Promise<T>; | ||
update(key: Partial<T>, obj: UpdateBody<T>): Promise<void>; | ||
@@ -30,0 +32,0 @@ update(key: Partial<T>, obj: UpdateBody<T>, conditionExpression: ConditionExpression): Promise<void>; |
@@ -5,2 +5,3 @@ "use strict"; | ||
exports.DynamoService = DynamoService_1.DynamoService; | ||
const DynamoQueryBuilder_1 = require("../dynamo-query-builder/DynamoQueryBuilder"); | ||
const Object_1 = require("../utils/Object"); | ||
@@ -16,11 +17,11 @@ class TableService { | ||
this.props = props; | ||
let primaryKeys = 0; | ||
let sortKeys = 0; | ||
let primaryKeys = []; | ||
let sortKeys = []; | ||
for (let key in tableSchema) { | ||
const v = tableSchema[key]; | ||
if (v.primary) { | ||
++primaryKeys; | ||
primaryKeys.push(key); | ||
} | ||
if (v.sort) { | ||
++sortKeys; | ||
sortKeys.push(key); | ||
} | ||
@@ -35,16 +36,22 @@ if (v.required) { | ||
} | ||
if (primaryKeys === 0) { | ||
if (primaryKeys.length === 0) { | ||
throw new Error("Table " + tableName + " must include a primary key."); | ||
} | ||
if (primaryKeys > 1) { | ||
if (primaryKeys.length > 1) { | ||
throw new Error("Table " + tableName + " must only have one primary key."); | ||
} | ||
if (sortKeys > 1) { | ||
if (sortKeys.length > 1) { | ||
throw new Error("Table " + tableName + " can not have more than one sort key."); | ||
} | ||
this.primaryKey = primaryKeys[0]; | ||
this.sortKey = sortKeys[0]; | ||
} | ||
put(obj) { | ||
put(obj, condition) { | ||
Object_1.throwIfDoesNotContain(obj, this.requiredKeys); | ||
const putObj = (this.props.trimUnknown) ? Object_1.subset(obj, this.knownKeys) : obj; | ||
return this.db.put(this.tableName, putObj).then(() => { return putObj; }); | ||
const primaryExistsQuery = (this.sortKey) ? | ||
DynamoQueryBuilder_1.withCondition(this.primaryKey).doesNotExist.and(this.sortKey).doesNotExist : | ||
DynamoQueryBuilder_1.withCondition(this.primaryKey).doesNotExist; | ||
return this.db.put(this.tableName, putObj, primaryExistsQuery.and(condition).query()) | ||
.then(() => { return putObj; }); | ||
} | ||
@@ -51,0 +58,0 @@ update(key, obj, conditionExpression, returnType) { |
{ | ||
"name": "@xapp/dynamo-service", | ||
"version": "0.0.37", | ||
"version": "0.0.38", | ||
"description": "A dynamo help class which will help maintain data integrity.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
119692
3240