Comparing version 1.0.0 to 1.0.2
@@ -11,28 +11,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const assertHasValidDynamoDBFieldNames_1 = require("./assertHasValidDynamoDBFieldNames"); | ||
const findAll_1 = require("./findAll"); | ||
exports.find = ({ client, tableName }) => (fields) => __awaiter(this, void 0, void 0, function* () { | ||
assertHasValidDynamoDBFieldNames_1.assertHasValidDynamoDBFieldNames(fields); | ||
if ('id' in fields) { | ||
throw new Error('Use read rather than find if you know the id'); | ||
} | ||
const fieldNames = Object.keys(fields); | ||
const filterExpression = fieldNames.map(key => `${key} = :${key}`) | ||
.join(' and '); | ||
const expressionAtributeValues = fieldNames.reduce((obj, key) => { | ||
obj[`:${key}`] = fields[key]; | ||
return obj; | ||
}, {}); | ||
const response = yield client.scan({ | ||
TableName: tableName, | ||
FilterExpression: filterExpression, | ||
ExpressionAttributeValues: expressionAtributeValues | ||
}) | ||
.promise(); | ||
if (response.Items.length === 0) { | ||
const items = yield findAll_1.__findAll({ client, tableName })(fields); | ||
if (items.length === 0) { | ||
throw new Error('No value found'); | ||
} | ||
if (response.Items.length > 1) { | ||
if (items.length > 1) { | ||
throw new Error('Multiple values found'); | ||
} | ||
return response.Items[0]; | ||
return items[0]; | ||
}); |
@@ -43,2 +43,5 @@ import { DynamoDB } from 'aws-sdk'; | ||
update: (item: T & IHasId & IHasVersion) => Promise<T & IHasVersion & IHasMetadata>; | ||
__findAll: (fields: { | ||
[key: string]: string | number | boolean; | ||
}) => Promise<(T & IHasVersion & IHasMetadata)[]>; | ||
find: (fields: { | ||
@@ -45,0 +48,0 @@ [key: string]: string | number | boolean; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const find_1 = require("./find"); | ||
const findAll_1 = require("./findAll"); | ||
const truncate_1 = require("./truncate"); | ||
@@ -21,2 +22,3 @@ exports.default = ({ endpoint = process.env.AWS_DYNAMODB_ENDPOINT, region = process.env.AWS_REGION, tableName }) => { | ||
update: update_1.update({ client, tableName }), | ||
__findAll: findAll_1.__findAll({ client, tableName }), | ||
find: find_1.find({ client, tableName }), | ||
@@ -23,0 +25,0 @@ truncate: truncate_1.truncate({ client, tableName }) |
{ | ||
"name": "cruft-ddb", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "A simple optimistic-locking abstraction over Dynamo DB", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,41 +0,17 @@ | ||
import { assertHasValidDynamoDBFieldNames } from './assertHasValidDynamoDBFieldNames'; | ||
import { IConfiguration, IHasVersion, IHasMetadata } from './index'; | ||
import { __findAll } from './findAll'; | ||
export const find = <T>({ client, tableName }: IConfiguration) => | ||
async (fields: { [key: string]: string | number | boolean }): Promise<T & IHasVersion & IHasMetadata> => { | ||
assertHasValidDynamoDBFieldNames(fields); | ||
const items = await __findAll<T>({ client, tableName })(fields); | ||
if ('id' in fields) { | ||
throw new Error('Use read rather than find if you know the id'); | ||
} | ||
const fieldNames = Object.keys(fields); | ||
const filterExpression = fieldNames.map(key => `${key} = :${key}`) | ||
.join(' and '); | ||
const expressionAtributeValues = fieldNames.reduce( | ||
(obj, key) => { | ||
obj[`:${key}`] = fields[key]; | ||
return obj; | ||
}, | ||
{} | ||
); | ||
const response = await client.scan({ | ||
TableName: tableName, | ||
FilterExpression: filterExpression, | ||
ExpressionAttributeValues: expressionAtributeValues | ||
}) | ||
.promise(); | ||
if (response.Items.length === 0) { | ||
if (items.length === 0) { | ||
throw new Error('No value found'); | ||
} | ||
if (response.Items.length > 1) { | ||
if (items.length > 1) { | ||
throw new Error('Multiple values found'); | ||
} | ||
return <T & IHasVersion & IHasMetadata>response.Items[0]; | ||
return items[0]; | ||
}; |
@@ -6,2 +6,3 @@ import { DynamoDB } from 'aws-sdk'; | ||
import { find } from './find'; | ||
import { __findAll } from './findAll'; | ||
import { truncate } from './truncate'; | ||
@@ -61,2 +62,3 @@ | ||
update: update<T>({ client, tableName }), | ||
__findAll: __findAll<T>({ client, tableName }), | ||
find: find<T>({ client, tableName }), | ||
@@ -63,0 +65,0 @@ truncate: truncate({ client, tableName }) |
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
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
127863
32
740