Reputation: 11
i am working on node js using dynamoDB with dynamoose. For example, let us assume we have a table Employees in which there is two attributes Branch and Domain. I have a given branch and domain, now i want to get all the employees under either the given branch or the give domain. Can anyone please give an example for above case?
Upvotes: 0
Views: 6773
Reputation: 39186
Here is the code to query and scan.
The code connects to DynamoDB local instance.
Employee Schema used:-
Branch - Hash Key
No sort key in the table
Domain - is the attribute
Code:-
var dynamoose = require('dynamoose');
dynamoose.AWS.config.update({
accessKeyId : 'AKID',
secretAccessKey : 'SECRET',
region : 'us-east-1'
});
dynamoose.local();
var Schema = dynamoose.Schema;
var Table = dynamoose.Table;
var Employee = dynamoose.model('employee', { branch: String, domain: String });
Employee.get('UK').then(function (data) {
console.log('Get :' + JSON.stringify(data));
});
Employee.query('branch').eq('UK').exec(function (err, data) {
console.log('Query :' + JSON.stringify(data));
});
Employee.scan('domain').eq('Banking').exec(function (err, data) {
console.log('Scan :' + JSON.stringify(data));
});
Explanation:-
Employee.get(..) - Get the data by hash key
Employee.query (..) - Get the data by hash key along with other attributes as needed
Employee.scan (..) - Get the data based on non-key attributes
Upvotes: 3