@awspilot/dynamodb

Issue Star Fork
query.js
query_index.js
query_filter.js
recursive_query.js
select_sql.js
// for hash key comparson operator is always eq() // for range key you can specify: le() , lt() , ge() , gt() , begins_with() , between(a,b) // you can pass an Array to .select( ['attr1', 'attr2'] ) // or multiple arguments .select( 'attr1' , 'attr2' ) DynamoDB .table('demo_table_hash_range') .select(['partitionKey','sortKey','nested_object.nested_attribute','array_mixed[1]','array_mixed[5].key']) .addSelect('created_at') .where('partitionKey').eq('foo.bar') .descending() .limit(2) .consistent_read() .query(function(err, data, raw ) { console.log( "LastEvaluatedKey = ",this.LastEvaluatedKey ) console.log( err, data ) console.log( "raw data ", raw ) });
// Query an Index // return all attributes including non-projected ( LSI only ) DynamoDB .table('cities') .index('country-index') .select( DynamoDB.ALL ) .where('country').eq('Canada') //.descending() .limit(10) //.consistent_read() .query(function( err, data, raw ) { console.log( "LastEvaluatedKey = ",this.LastEvaluatedKey ) console.log( err, data ) console.log( "raw data ", raw ) }); // NOTE: specifying non-projected fields in select() will: // * cost you extra reads on a LSI index // * not be returned on a GSI index
// Query filtering with .having() // .having() is alias of .filter() // A filter lets you apply conditions to the data after query // Only the items that meet your conditions are returned // All the conditions must evaluate to true ( conditions are ANDed together ) // You can not apply filter on HASH or RANGE key // Comparison operators: // The ones supported for RANGE key: // eq(), le() , lt() , ge() , gt() , begins_with() , between(a,b) // Plus: // ne(), defined(), undefined() // Unsupported yet ( for type SET ): // contains(), not_contains(), in() // // WARNING: defined() and undefined() are aliases for DynamoDB's NULL and NOT_NULL // they refer to the presence of an attribute and has nothing to do with it's value // so .having('attribute').null() differs from .having('attribute').eq( null ) // also .having('attribute').not_null() differs from .having('attribute').ne( null ) DynamoDB .table('messages') .where('to').eq('user1@test.com') .having('one_attribute').between(100,200) .filter('object.attribute').eq(true) .having('deleted').undefined() // or .null() .filter('last_login').defined() // or .not_null() .having('string').begins_with('substring') .filter('string').contains('substring') .having('string_set').contains('one') .filter('number').between(0,2) .having('attribute').in([3,4,'a']) .filter('fridge.shelf[1].cookies').not_contains('sugar') .query(function( err, data ) { console.log(err,data) })
// Query continue from last item // query a table until the end of results :) (function recursive_call( $lastKey ) { DynamoDB .table('cities') .index('country-index') .where('country').eq('Canada') .resume($lastKey) .limit(10) // 10 by 10 items for this demo .query(function( err, data ) { if (err) return console.log(err) console.log("fetched cities in Canada: ", data.length ) if (this.LastEvaluatedKey === null) { // reached end, do a callback() maybe console.log('---- reached end of Canada :) ---') return; } var $this = this; setTimeout(function() { recursive_call($this.LastEvaluatedKey); },100); }) })(null);
// for partition_key the comparison operator is always equal sign // for sort_key you can use =, <, <=, >, >=, BETWEEN x AND y, LIKE // SELECT stattement does not currently support HAVING ( FilterExpression ) DynamoDB.query(` SELECT unique_visitors, unique_pageviews FROM tbl_name USE INDEX idx_name WHERE partition_key = 'test' + '@' + 'test.com' AND sort_key LIKE 'contact@%' DESC LIMIT 10 CONSISTENT_READ `, function( err, data ) { console.log( err, data ) });