I have a Lambda node.js function like so:
function storeUser(email, username, password, salt, fn) {
// Bytesize
var len = 128;
crypto.randomBytes(len, function(err, token) {
if (err) return fn(err);
token = token.toString('hex');
dynamodb.putItem({
TableName: "Users",
Item: {
email: {
S: email
},
username: {
S: username
},
passwordHash: {
S: password
},
passwordSalt: {
S: salt
},
verified: {
BOOL: false
},
verifyToken: {
S: token
}
},
ConditionExpression: 'attribute_not_exists (email)'
}, function(err, data) {
if (err) return fn(err);
else fn(null, token);
});
});
}
The event handler is like so:
exports.handler = function(event, context) {
var email = event.email;
var username = event.username;
var clearPassword = event.password;
computeHash(clearPassword, function(err, salt, hash) {
if (err) {
context.fail('Error in hash: ' + err);
} else {
storeUser(email, username, hash, salt, function(err, token) {
if (err) {
if (err.code == 'ConditionalCheckFailedException') {
// userId already found
context.succeed({
created: false
});
} else {
context.fail('Error in storeUser: ' + err);
}
} else {
context.succeed({
created: true
});
}
});
}
});
}
I want to make the username and email unique. How do I ensure that in the write to the db and if one (or both) is not unique, get an error back saying which is not unique?
This is a quick and dirty way to do it. It's not extraordinarily efficient but it gets the job done:
const // You want const because no required packages ever change at runtime
AWS = require('aws-sdk'),
ddb = new AWS.DynamoDB();
exports.handler = function(event, context) {
// QUERY USERNAME FIRST
var table = 'tableName';
var paramsUNCheck = {
TableName : table,
ProjectionExpression : 'hash',
KeyConditionExpression : 'hash = :v_hash AND range = :v_rng',
ExpressionAttributeValues : {
':v_hash' : { S : 'hash' },
':v_rng' : { S : event.username }
}
}
ddb.query (paramsUNCheck, function(err, data) {
if (err) {
context.fail (JSON.stringify(err, null, 2))
} else {
if (data.Count === 0) {
var paramsEMCheck = {
TableName : table,
ProjectionExpression : 'hash',
KeyConditionExpression : 'hash = :v_hash',
FilterExpression : 'email = :v_eml',
ExpressionAttributeValues : {
':v_hash' : { S : 'hash' },
':v_eml' : { S : event.email }
}
}
ddb.query (paramsEMCheck, function(err, data) {
if (err) {
context.fail (JSON.stringify(err, null, 2))
} else {
if (data.Count === 0) {
// REGISTER IF IT PASSES ALL CHECKS
var paramsReg = {
TableName : table,
Item : {
'hash' : { S : 'hash' },
'username' : { S : event.username },
'password' : { S : event.password },
'email' : { S : event.email }
}
}
ddb.putItem (paramsReg, function (err, data) {
if (err) context.fail (JSON.stringify(err, null, 2));
else
context.succeed ({ done: 'yes' });
});
} else {
context.succeed ('Email already used, please try again.')
}
}
});
} else {
context.succeed ('Username taken, please try again.')
}
}
})
};
Now, the reason why I say this is a dirty way of doing it is because you do two queries.
With that said, running reads is significantly cheaper than running writes. The reason for this madness:
One read capacity unit = one strongly consistent read per second, or two eventually consistent reads per second, for items up 4 KB in size.
One write capacity unit = one write per second, for items up to 1 KB in size.
From here
Basically, it's way cheaper to run two read requests then one write request. PER PROVISIONAL THROUGHPUT the cost is ~$0.09/ read throughput and ~$0.48/ write throughput.
But wait, don't you have to do a write anyway? Well, yes you do, but it's a write that should always succeed. What if you have a user constantly hitting that lambda? You'll want a read on the front line of defense and not a write. Basically I'm as cheap as possible. I'll crank read throughput all day long if it means not 'uping' the write (within reason).
The other way to do it is Conditional Writes and you're going to want to check begins_with usernameofemail#gmail.com all the way up to the end - basically forcing a full string check, so disregard the "begins with".
Related
I am creating a skill where I want to call alexa to read differnt items at certain dates and times.
I currently have my table set up as follows:
Date|Time|State
Date is setup as my primary key with time as my sort key. I have setup date and time as slot values in ASK, and I can see that these values are being passed through. I have also made sure the format of my date and time is correct within dynamodb.
My issue is when I call alexa and ask for the state at a certain date and time, I can't get alexa to respond with the state corresponding with that date and time.
Can anyone help me with this? Or tell me where i'm going wrong, I will insert my code below.
const awsSDK = require('aws-sdk');
const updatedincident = 'updatedincident';
const docClient = new awsSDK.DynamoDB.DocumentClient();
var AWSregion = 'us-east-1'; // us-east-1
var AWS = require('aws-sdk');
var dbClient = new AWS.DynamoDB.DocumentClient();
AWS.config.update({
region: "'us-east-1'"
});
let GetMachineStateIntent = (context, callback, dateSlot, timeSlot) => {
var params = {
TableName: "updatedincident",
KeyConditionExpression: 'date = :dVal and time < :tVal',
ExpressionAttributeValues: {
':dVal': dateSlot,
':tVal': timeSlot
},
ScanIndexForward: false // gets values in reverse order by time
};
dbClient.query(params, function (err, data) {
if (err) {
// failed to read from table for some reason..
console.log('failed to load data item:\n' + JSON.stringify(err, null, 2));
// let skill tell the user that it couldn't find the data
sendResponse(context, callback, {
output: "the data could not be loaded from your database",
endSession: false
});
} else {
let dataItem = data.Items[0];
console.log('loaded data item:\n' + JSON.stringify(dataItem, null, 2));
// assuming the item has an attribute called "state"..
sendResponse(context, callback, {
output: dataItem.state,
endSession: false
});
}
});
};
function sendResponse(context, callback, responseOptions) {
if(typeof callback === 'undefined') {
context.succeed(buildResponse(responseOptions));
} else {
callback(null, buildResponse(responseOptions));
}
}
function buildResponse(options) {
var alexaResponse = {
version: "1.0",
response: {
outputSpeech: {
"type": "SSML",
"ssml": `<speak><prosody rate="slow">${options.output}</prosody></speak>`
},
shouldEndSession: options.endSession
}
};
if (options.repromptText) {
alexaResponse.response.reprompt = {
outputSpeech: {
"type": "SSML",
"ssml": `<speak><prosody rate="slow">${options.reprompt}</prosody></speak>`
}
};
}
return alexaResponse;
}
exports.handler = (event, context, callback) => {
try {
var request = event.request;
if (request.type === "LaunchRequest") {
sendResponse(context, callback, {
output: "welcome to my skill, I can tell you about the status of machines at different times. what data are you looking for?",
endSession: false
});
} else if (request.type === "IntentRequest") {
if (request.intent.name === "GetMachineStateIntent") {
var dateSlot = request.intent.slots.Date != null
? request.intent.slots.Date. value : null;
var timeSlot = request.intent.slots.Time != null
? request.intent.slots.Time.value : null;
// pass the slot values to the GetMachineStateIntent function
GetMachineStateIntent(context, callback, dateSlot, timeSlot);
} else if (request.intent.name === "AMAZON.StopIntent" || request.intent.name === "AMAZON.CancelIntent") {
sendResponse(context, callback, {
output: "ok. good bye!",
endSession: true
});
}
else if (request.intent.name === "AMAZON.HelpIntent") {
sendResponse(context, callback, {
output: "you can ask me about incidents that have happened or states of machines in the past",
reprompt: "what can I help you with?",
endSession: false
});
}
else {
sendResponse(context, callback, {
output: "I don't know that one! please try again!",
endSession: false
});
}
}
else if (request.type === "SessionEndedRequest") {
sendResponse(context, callback, ""); // no response needed
}
else {
// an unexpected request type received.. just say I don't know..
sendResponse(context, callback, {
output: "I don't know that one! please try again!",
endSession: false
});
}
} catch (e) {
// handle the error by logging it and sending back an failure
console.log('Unexpected error occurred in the skill handler!', e);
if(typeof callback === 'undefined') {
context.fail("Unexpected error");
} else {
callback("Unexpected error");
}
}
};
The response i currently get with my above code is
'the data could not be loaded from your database'
Cloud watch also tells me this
2018-05-16T09:29:06.635Z 93d4b6e6-58eb-11e8-b686-597d65771e90 failed to load data item:
{
"message": "Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: date",
"code": "ValidationException",
"time": "2018-05-16T09:29:06.633Z",
"requestId": "EQPQTAGO4QKH9SM5GSOA9O3DDFVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 35.56027710686527
}
In the GetMachineStateIntent function, try changing the params structure like this:
var params = {
TableName: "updatedincident",
KeyConditionExpression: '#d = :dVal and #t < :tVal',
ExpressionAttributeValues: {
':dVal': dateSlot,
':tVal': timeSlot
},
ExpressionAttributeNames: {
'#d': 'date',
'#t': 'time'
},
ScanIndexForward: false // gets values in reverse order by time
};
It looks like the word date is a reserved keyword so it can't be used directly in an expression such as date = :dVal which is why you must give it an attribute name alias (#d) which maps back to the actual attribute name (date).
In DynamoDB, you have to use two keys i.e Primary key and Primary sort key. The query searches on the basis of these two keys for the requested value.
Try my code:
'FindName': function() {
var tableName = "CVRaman";
var userName = "Prateek";
var userId = "kapoor";
const dynamodbParams = {
TableName: tableName,
Key: {
userId: userId,
userName: userName,
},
ProjectionExpression: 'userName', //Projection Expression is used to select only specific columns which we want to get
};
console.log("Attempting to find name", dynamodbParams);
dynamoDb.get(dynamodbParams).promise()
.then(data => {
console.log('name found', dynamodbParams);
console.log(data.Item.userName);
var a = data.Item.userName;
console.log(a);
this.emit(':ask', 'Name as ' + a);
})
.catch(err => {
console.log(err);
this.emit(':tell', 'we have a problem');
});
},
Using Mongodb, Nodejs, Async.js and Express.js
I am trying to update multiple documents, where each document has its own update, at the same time. I want to wait for all documents to update so that I can notify user that all documents have updated.
The issue I am having now is that my callback function is not firing, or if it is, then nothing is happening. Here is my progress:
db.client.collection('page').find({page_id: page_id}).toArray(function(page_err, document_page) {
if(page_err) {
throw page_err;
} else if(document_page === '' || document_page === undefined || document_page === null) {
throw page_err;
} else {
var count = 0;
async.each(data, function iteratee(i, callback) {
var item_id = (i.item_id === '') ? new ObjectId() : new ObjectId(i.item_id);
var query = {item_id: item_id};
var update = {
_id : new ObjectId(),
page_id : page_id,
section_id : null,
item_id : item_id,
created : new Date().toISOString(),
item_type : "dish",
item: {
title: i.title,
description: i.description,
price: i.price,
star: false,
double_star: false
},
last_modified: new Date().toISOString()
};
var options = { upsert: true };
db.client.collection('item').updateOne(query, {$set: update}, options, function(item_err, results) {
if(item_err) {
res.sendStatus(500);
} else if(results === '' || results === undefined || results === null) {
res.sendStatus(400);
} else {
++count;
if(count === data.length) {
callback();
return;
}
}
});
}, function() {
console.log('sending 200 status');
res.sendStatus(200);
});
}
});
When I run the code I do enter the if statement where I call callback(). I have been stuck on this for a few hours and I cannot get it to work. If you need more info, I'd be happy to provide it. For simplicity's sake I removed many console.logs to avoid clutter as well.
All iterations need to fire the callback otherwise it will hang indefinitely. callback must be called in every iteration. Always.
If you encounter an error, you need to call callback(error). The problem you'll have is that async.each schedules all iterations beforehand so iteratee will fire data.length times regardless of whether an error is encountered half way through execution or not. If you need to run them in series you can use async.eachSeries which will take more time but gives you better control and no need to rollback.
So code wise it looks like this:
db.client.collection('page').find({page_id: page_id}).toArray(function(page_err, document_page) {
if(page_err) {
throw page_err;
} else if(document_page === '' || document_page === undefined || document_page === null) {
throw page_err;
} else {
async.each(data, function iteratee(i, callback) {
var item_id = (i.item_id === '') ? new ObjectId() : new ObjectId(i.item_id);
var query = {item_id: item_id};
var update = {
_id : new ObjectId(),
page_id : page_id,
section_id : null,
item_id : item_id,
created : new Date().toISOString(),
item_type : "dish",
item: {
title: i.title,
description: i.description,
price: i.price,
star: false,
double_star: false
},
last_modified: new Date().toISOString()
};
var options = { upsert: true };
db.client.collection('item').updateOne(query, {$set: update}, options, function(item_err, results) {
if(item_err) {
callback(500);
} else if(results === '' || results === undefined || results === null) {
callback(400)
} else {
callback();
}
});
}, function(err) {
// Passing the status code only for the example.
// `err` should be an object with more metadata probably
if(err) {
res.sendStatus(err);
return;
}
console.log('sending 200 status');
res.sendStatus(200);
});
}
});
I am new to dynamoDB and node.js I have written a code where it will make a query to the database (dynamodb) and look for an element which is entered by the user in the database. I am able to verify that but when the user tries with some other number which is not present in the database I am getting a null value.
My table name is "DevReferenceNumber" and only one column which is primary key "referencenumber".
'use strict';
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({ region : 'us-east-1'});
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
};
}
exports.handler = (event, context, callback) => {
try{
console.log(`event.bot.name=${event.bot.name}`);
if(event.bot.name != 'getCustomerReferenceNumber'){
callback('Invalid Bot Name');
}
dispatch(event, (response) => {
callback(null, response)
});
}catch(err){
callback("Error is occured while querying");
}
};
function dispatch(intentRequest, callback){
console.log(`dispatch UserID => ${intentRequest.userId}, intentName => ${intentRequest.currentIntent.name}`);
const intentName = intentRequest.currentIntent.name;
if(intentName === "checkReferenceNumber"){
return referenceNumber(intentRequest, callback);
}
}
function referenceNumber(intentRequest, callback){
const enteredReferenceNumber = intentRequest.currentIntent.slots.ReferenceNumber;
const sessionAttributes = intentRequest.sessionAttributes;
console.log("User has entered reference number is --> " + enteredReferenceNumber);
var params = {
TableName : "DevReferenceNumber",
KeyConditionExpression: "#refer = :ref",
ProjectionExpression : "#refer",
ExpressionAttributeNames: {
"#refer" : "referencenumber"
},
ExpressionAttributeValues:{
":ref": parseInt(enteredReferenceNumber)
}
};
docClient.query(params, function(err, data){
if(err){
callback(close(sessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content : 'Developer reference number is not matched with data from database'}));
}
else {
data.Items.forEach(function (item){
console.log("User matched data is ==> " + item.referencenumber);
callback(close(sessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content : 'Developer reference number is matched with data from database'}));
});
}
});
}
It is obvious that you will get a null record when you don't have a matching record. If you don't want null from node callback then you can do a custom logic to do a null check and return data according to the way you want.
I've a program that does the below.
Look into a DynamoDB table.
Get the data from the table.
Save the variables in session
After the process, print the values in console.
My code is as below.
intentHandlers['GetMYBusinessInfo'] = function (request, session, response, slots) {
console.log('entered ext bloxk');
if (!session.attributes.userName) {
console.log('eneterd the user entered the block');
var userName = 'jim';
isUserRegistered(userName.toLowerCase(), function (res, err) {
if (err) {
response.fail(err);
console.log(err);
}
else if (!res) {
response.shouldEndSession = true;
}
else {
console.log(res);
var countRes = JSON.stringify(res.Count);
var unpUserRegion = JSON.stringify(res.Items[0].Region);
var unpUserCity = JSON.stringify(res.Items[0].State);
var userRegion = JSON.parse(unpUserRegion);
var userCity = JSON.parse(unpUserCity);
session.attributes.city = userCity;
session.attributes.region = userRegion;
console.log("parsed " + countRes + "\t region is " + userRegion);
session.attributes.userName = true;
}
});
}
console.log(`session values after authentication are user city is ${session.attributes.city}`);
}
The method to check if the value is in DynamoDb or not.
function isUserRegistered(userName, callback) {
var params = {
TableName: "usersTable",
FilterExpression: "#nme = :nme",
ExpressionAttributeNames: {
"#nme": "Name",
},
ExpressionAttributeValues: {
":nme": userName
}
};
var count = 0;
docClient.scan(params, function (err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
callback(false, err);
} else {
console.log("Scan succeeded." + data.Items.length);
if (data.Items.length === 0) {
callback(false);
}
else {
data.Items.forEach(function (itemData) {
console.log("Item :", ++count, JSON.stringify(itemData));
});
callback(data);
}
}
});
}
when I run this, the output that I get is:
session values after authentication are user city is undefined
Scan succeeded.1
Item : 1
{
"ID": "3",
"State": "wisconsin",
"Region": "midwest",
"Name": "jim"
}
{ Items: [ { ID: '3', State: 'wisconsin', Region: 'midwest', Name: 'jim' } ],
Count: 1,
ScannedCount: 1 }
parsed 1 region is midwest
Here I know that Node js being Non-blockable process, the above output is correct, but I want to get the value of city printed in session values after authentication are user city is {HereTheCityComes} instead of session values after authentication are user city is undefined.
I'm sure that placing the console.log(session values after authentication are user city is ${session.attributes.city}); in the last else block(place where the data is returned).
But I need this type of functionality(Get data as shown in my current scenario), as there is some other things to be done after checking if the user is available in database.
please let me know where am I going wrong and how can I fix this.
You can't synchronously expect async result.
What you can do here is solve your problem with promises.
Here is a solution:
intentHandlers['GetMYBusinessInfo'] = function(request, session, response, slots) {
console.log('entered ext bloxk');
var userPromise = Promise.resolve();
if (!session.attributes.userName) {
console.log('eneterd the user entered the block');
var userName = 'jim';
userPromise = new Promise(function (resolve, reject) {
isUserRegistered(userName.toLowerCase(), function (res, err) {
if (err) {
response.fail(err);
reject(err);
}
var countRes = JSON.stringify(res.Count);
var unpUserRegion = JSON.stringify(res.Items[0].Region);
var unpUserCity = JSON.stringify(res.Items[0].State);
var userRegion = JSON.parse(unpUserRegion);
var userCity = JSON.parse(unpUserCity);
session.attributes.city = userCity;
session.attributes.region = userRegion;
console.log("parsed " + countRes + "\t region is " + userRegion);
resolve(res);
});
});
}
userPromise.then(function () {
console.log(`session values after authentication are user city is ${session.attributes.city}`);
});
}
If you are not using ES6, then just install bluebird and use var Promise = require('bluebird')
I want to have exactly auto-increment field like relational or objective databases, so i need an integer _id field with automatically set field value, value should be one more last record _id value like this:
data:
{_id:1,name"foo"}
{_id:2,name"bar"}
remove last record:
{_id:1,name"foo"}
add new record:
{_id:1,name"foo"}
{_id:3,name"newbar"}
I added a function to my datastore and calculate maximum of _id and plus 1 max(_id)+1 and set as field value, but there is problem here:
When we use auto-increment field in relational databases, it works like i said and after you remove last record it reserved a deleted record number and new inserted records continue increment but in my way its says the _id of removed record for new record.
My code is:
var Datastore = require('nedb'),
localDb = new Datastore({
filename: __dirname + '/dbFilePath.db',
autoload: true
});
localDb.getMax = function(fieldName, onFind){
db.find({}).sort({_id:-1}).limit(1).exec(function (err, docs) {onFind && onFind(err, docs['_id']);});
return localDb;
}
localDb.insertAutoId = function(data, onAdd){
var newIndex = 0;
localDb.getMax(function (err, maxValue) {
newIndex = maxValue+1;
if(!data["_id"])
data["_id"] = newIndex;
localDb.insert(data, function (err, newDoc) {
onAdd && onAdd(err, newDoc);
});
});
return localDb;
}
An improved answer for nedb would be:
db.getAutoincrementId = function (cb) {
this.update(
{ _id: '__autoid__' },
{ $inc: { seq: 1 } },
{ upsert: true, returnUpdatedDocs: true },
function (err, affected, autoid) {
cb && cb(err, autoid.seq);
}
);
return this;
};
Which is equivalent to the mongodb way:
db.getAutoincrementId = function (cb) {
this.findAndModify({
query: { _id: '__autoid__' },
update: { $inc: { seq: 1 } },
new: true
}
function (err, autoid) {
cb && cb(err, autoid.seq);
}
);
return this;
};
You can store the last value of the index in the database. Something like this:
var Datastore = require('nedb');
var db = new Datastore({
filename: __dirname + '/dbFilePath.db',
autoload: true
});
// Initialize the initial index value
// (if it already exists in the database, it is not overwritten)
db.insert({_id: '__autoid__', value: -1});
db.getAutoId = function(onFind) {
db.findOne( { _id: '__autoid__' }, function(err, doc) {
if (err) {
onFind && onFind(err)
} else {
// Update and returns the index value
db.update({ _id: '__autoid__'}, { $set: {value: ++doc.value} }, {},
function(err, count) {
onFind && onFind(err, doc.value);
});
}
});
return db;
}
I do not know if it will be useful for you anymore I use a database to store the next ids, inspired in the mysql system. Who always reserves the next id.
So I created a function that verifies if there is an id to the db, if it does not, it add with the value "1", and when it updates it looks for and if it exists and it performs the sequence.
This gave me full control over my ids.
The schema would be:
{
name: nameDb,
nextId: itemID
}
If you want you can create functions for updating documents, versioning, etc.
example:
db.autoincrement = new Datastore({filename: 'data/autoincrement.db', autoload: true});
function getUniqueId(nameDb, cb) {
db.autoincrement.findOne({name: nameDb}, function (err, doc) {
if (err) {
throw err;
} else {
if (doc) {
const itemID = doc.nextId + 1;
db.autoincrement.update({name: nameDb}, {
name: nameDb,
nextId: itemID
}, {}, function (err, numReplaced) {
db.autoincrement.persistence.compactDatafile();
if (err) {
throw err;
} else {
// console.log(numReplaced);
}
cb(doc.nextId);
});
} else {
const data = {
name: nameDb,
nextId: 2
};
db.autoincrement.insert(data, function (err, newDoc) {
if (err) {
throw err;
} else {
// console.log(newDoc);
}
cb(1);
});
}
}
});
}
insert new document example:
function insert(req, cb) {
getUniqueId("testdb", function (uniqueId) {
data.itemId = uniqueId;
db.testdb.insert(data, function (err, newDoc) {
if (err) {
cb({error: '1', message: 'error#2'});
throw err;
}
cb({error: '0', message: 'Item add'});
});
});
}