Skip to content

Commit 101697e

Browse files
authored
Multiple GSI Support (#42)
* increment version * index option now supports multiple indexes
1 parent 0cbaa43 commit 101697e

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ HighScore: {
159159
```
160160

161161
#### Fields with multiple indexes
162-
A field can be both the primary and part of a GSI index. Participating in multiple GSI indexes is not currently supported.
162+
A field can be both the primary and part of a GSI index. Participating in multiple GSI indexes is supported as of v0.12.5.
163163

164164
```
165165
GameTitle: {
@@ -169,7 +169,7 @@ GameTitle: {
169169
}
170170
```
171171

172-
__Not Supported Yet__:
172+
Multiple GSIs:
173173
```
174174
GameTitle: {
175175
type: 'string',
@@ -178,6 +178,15 @@ GameTitle: {
178178
}
179179
```
180180

181+
Multiple GSIs and a secondary index:
182+
```
183+
GameTitle: {
184+
type: 'string',
185+
primaryKey: 'hash'
186+
index: ['secondary', GameTitleIndex-hash'. 'SomeOtherIndex-hash']
187+
}
188+
```
189+
181190
### Sorting By Indexes
182191
Sorting does not look like how it looks with the normal sails database adapters. You can not sort by an arbitrary field, you must sort by a range field in an index. The index is automatically inferred by what you are querying and you can specify a direction to sort the range fields of the used index. Using the GSI defined above, this will query for descending highscores of Super Mario World:
183192
```

index.js

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,29 @@ module.exports = (function () {
187187

188188
index = attributes.index;
189189

190-
indexParts = adapter._parseIndex(index, columnName);
191-
indexName = indexParts[0];
192-
indexType = indexParts[1];
190+
if (_.isArray(index)){
191+
index.forEach((oneIndex) => {
192+
indexParts = adapter._parseIndex(oneIndex, columnName);
193+
indexName = indexParts[0];
194+
indexType = indexParts[1];
195+
196+
if (typeof indices[indexName] === 'undefined') {
197+
indices[indexName] = {};
198+
}
199+
200+
indices[indexName][indexType] = columnName;
201+
});
202+
}else{
203+
indexParts = adapter._parseIndex(index, columnName);
204+
indexName = indexParts[0];
205+
indexType = indexParts[1];
193206

194-
if (typeof indices[indexName] === 'undefined') {
195-
indices[indexName] = {};
196-
}
207+
if (typeof indices[indexName] === 'undefined') {
208+
indices[indexName] = {};
209+
}
197210

198-
indices[indexName][indexType] = columnName;
211+
indices[indexName][indexType] = columnName;
212+
}
199213

200214
}
201215

@@ -549,6 +563,8 @@ module.exports = (function () {
549563
scanning = false;
550564

551565
if (indexing) {
566+
// console.log("USING INDEX")
567+
// console.log(indexing);
552568
query = model.query(options.where[hash])
553569
delete options.where[hash];
554570

@@ -758,6 +774,14 @@ module.exports = (function () {
758774
var indexInfo;
759775
var indexName;
760776
var indexType;
777+
778+
if (!(_.isArray(fields))){
779+
fields = Object.keys(fields);
780+
}
781+
782+
// console.log("FIELDS")
783+
// console.log(fields);
784+
761785
for (var i = 0; i < fields.length; i++) {
762786

763787
fieldName = fields[i];
@@ -778,8 +802,25 @@ module.exports = (function () {
778802

779803
// using secondary or GSIs
780804
if (column.index){
805+
// console.log("COLUMN.INDEX")
806+
// console.log(column.index)
781807
if (_.isArray(column.index)){
782-
throw new Error(`No support yet for multiple non-primary indexes, ${fieldName} = ${column.index}`);
808+
column.index.forEach((oneIndex) => {
809+
if (oneIndex === 'secondary'){
810+
secondaryRange = fieldName;
811+
}else{
812+
indexInfo = adapter._parseIndex(oneIndex, fieldName);
813+
indexName = indexInfo[0];
814+
indexType = indexInfo[1];
815+
816+
if (typeof indices[indexName] === 'undefined') {
817+
indices[indexName] = {};
818+
}
819+
820+
indices[indexName][indexType] = fieldName;
821+
}
822+
});
823+
// throw new Error(`No support yet for multiple non-primary indexes, ${fieldName} = ${column.index}`);
783824
}else if (column.index === 'secondary'){
784825
secondaryRange = fieldName;
785826
}else{
@@ -797,6 +838,9 @@ module.exports = (function () {
797838

798839
}
799840

841+
// console.log("INDICES")
842+
// console.log(indices)
843+
800844
// set global secondary hash info
801845
var indicesHashed;
802846
var indicesRanged;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sails-dynamodb",
3-
"version": "0.12.3",
3+
"version": "0.12.5",
44
"description": "Amazon DynamoDB adapter for Sails / Waterline",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)