tywalch/electrodb

Partition key without composite

Closed this issue · 5 comments

I am trying to create a simple index to get a collection of all records (this will be used with pagination). I don't want to use scan as it is expensive with large tables.

My index looks like that:

all: {
      index: 'gsi2pk-gsi2sk-index',
      pk: {
        field: 'gsi2pk',
        template: '$users'
      },
      sk: {
        field: 'gsi2sk',
        composite: ['id'],
        template: '${id}'
      },
    },

then I just want to call

db.entities.users.query.all().go({ limit: 100 });

but it looks like that is not possible.

I use typescript so:

  • composite is required on pk and it can't be an empty array
  • if I have a composite key then I need to use it inside the template
  • I tried to disable TS errors but then I am required to pass an object when I call .all()
db.entities.users.query.all({ anything: null }).go({ limit: 100 });

that works but requires that dummy object { anything: null } to be passed

the only way I got that working was to add a pseudo attribute

all: {
      type: 'string',
      hidden: true,
      required: false,
      default: '',
    }

and setup my index as

all: {
      index: 'gsi2pk-gsi2sk-index',
      pk: {
        field: 'gsi2pk',
        composite: ['all'],
        template: '$users${all}'
      },
      sk: {
        field: 'gsi2sk',
        composite: ['id'],
        template: '${id}'
      },
    }

and call

db.entities.users.query.all({ all: '' }).go({ limit: 100 });

that works as expected but there is too much boilerplate code and I still need to pass that object to .all() method

is there any other way to achieve that?

I merged and published 1.8.2, it should address your need here

Whoops, replied to the wrong issue. This should be mixed once 1.8.3 is merged

Found here: #123

Merged

thanks @tywalch, that works great the only thing is the type Query, it requires composite: CompositeAttributes so I need to call

db.entities.users.query.all({}).go({ limit: 100 });

if we could change that to composite?: CompositeAttributes or make it optional only when there are no composite fields then I don't have to pass that empty object and just call .all()

db.entities.users.query.all().go({ limit: 100 });