我有一个表:digital_asset,它的分区键是:id。它有一个全局辅助索引:ean索引,分区键为:ean。我希望能够使用dynstyjs使用辅助索引查询表。使用aws-sdk,我会这样做:
return new Promise(function (resolve, reject) {
var params = {
TableName: "digital_asset",
IndexName: "ean-index",
KeyConditionExpression: "ean = :ean",
ExpressionAttributeValues: {
":ean": ean
},
ProjectionExpression: "id, ean"
};
docClient.query(params, function(err, data) {
if (err)
console.error(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
if (!err) {
resolve(data.Items.map(function(item){return item.id}));
} else {
reject(err);
}
});
});
使用王朝要好得多,例如,以下是按主要索引搜索的查询:
return new Promise(function (resolve) {
return digital_asset.find(asset_id).then(function (item) {
resolve(item.ean);
});
});
这是我尝试过的,但得到了一个错误:
return new Promise(function (resolve, reject) {
return digital_asset.findAll({"ean-index":ean}).then(function (item) {
resolve(item.id);
}).catch(reject);
});
错误:
InvalidParameterType: Expected params.KeyConditions['id'].AttributeValueList[0].S to be a string
at ParamValidator.fail (node_modules/aws-sdk/lib/param_validator.js:50:37)
at ParamValidator.validateType (node_modules/aws-sdk/lib/param_validator.js:218:10)
at ParamValidator.validateString (node_modules/aws-sdk/lib/param_validator.js:150:14)
at ParamValidator.validateScalar (node_modules/aws-sdk/lib/param_validator.js:130:21)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:94:21)
at ParamValidator.validateStructure (node_modules/aws-sdk/lib/param_validator.js:75:14)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:88:21)
at ParamValidator.validateList (node_modules/aws-sdk/lib/param_validator.js:103:14)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:90:21)
at ParamValidator.validateStructure (node_modules/aws-sdk/lib/param_validator.js:75:14)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:88:21)
at ParamValidator.validateMap (node_modules/aws-sdk/lib/param_validator.js:117:14)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:92:21)
at ParamValidator.validateStructure (node_modules/aws-sdk/lib/param_validator.js:75:14)
at ParamValidator.validateMember (node_modules/aws-sdk/lib/param_validator.js:88:21)
at ParamValidator.validate (node_modules/aws-sdk/lib/param_validator.js:34:10)
at Request.VALIDATE_PARAMETERS (node_modules/aws-sdk/lib/event_listeners.js:89:42)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at node_modules/aws-sdk/lib/event_listeners.js:75:9
at finish (node_modules/aws-sdk/lib/config.js:307:7)
at node_modules/aws-sdk/lib/config.js:323:9
at Credentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:317:24)
at Config.getCredentials (node_modules/aws-sdk/lib/config.js:337:9)
at Request.VALIDATE_CREDENTIALS (node_modules/aws-sdk/lib/event_listeners.js:70:26)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:661:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at Request.runTo (node_modules/aws-sdk/lib/request.js:394:15)
at Request.send (node_modules/aws-sdk/lib/request.js:358:10)
at makeRequest (node_modules/aws-sdk/lib/service.js:180:27)
at svc.(anonymous function) [as query] (node_modules/aws-sdk/lib/service.js:437:23)
at tryCatcher (node_modules/dynasty/node_modules/bluebird/js/main/util.js:26:23)
at ret [as queryAsync] (eval at <anonymous> (node_modules/dynasty/node_modules/bluebird/js/main/promisify.js:163:12), <anonymous>:13:39)
at Table.module.exports.queryByHashKey (node_modules/dynasty/lib/lib/aws-translators.js:142:31)
at tryCatcher (node_modules/dynasty/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (node_modules/dynasty/node_modules/bluebird/js/main/promise.js:507:31)
at Promise._settlePromiseAt (node_modules/dynasty/node_modules/bluebird/js/main/promise.js:581:18)
at Promise._settlePromises (node_modules/dynasty/node_modules/bluebird/js/main/promise.js:697:14)
at Async._drainQueue (node_modules/dynasty/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (node_modules/dynasty/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues (node_modules/dynasty/node_modules/bluebird/js/main/async.js:15:14)
我也在使用Dynasty的全局二级索引。查看他们的源代码(特别是135行附近的aws-translators.coffee v1.0)。看起来您可能不得不执行类似.query({ indexName: "ean-index" })
的操作。