Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save some of the data in separate bin? #7

Closed
umangkedia opened this issue Oct 4, 2017 · 4 comments
Closed

How to save some of the data in separate bin? #7

umangkedia opened this issue Oct 4, 2017 · 4 comments
Milestone

Comments

@umangkedia
Copy link

I was seeing the code and at the time of saving the session, it stringifies the object and saves it in one bin. I want to save some of the data in separate bin so that I can create a secondary index on them? Any way to do that?

@jhecking
Copy link
Member

jhecking commented Oct 5, 2017

At the moment, that's not possible. But it wouldn't be too difficult to extend the concept of the customizable serializer and provide hooks to customize how the session object is mapped to/from a record in Aerospike. I.e. we would need a new interface that provides two methods:

(session) => record
(record) => session

PR would be welcome.

jhecking added a commit that referenced this issue Oct 6, 2017
Resolves #7: Support custom mappings of session object to database record.
@jhecking
Copy link
Member

jhecking commented Oct 6, 2017

@umangkedia, please take a look at PR #10. I've added a new, customizable data mapper to replace the existing serializer. With the new data mapper interface, it's possible to map the session object to Aerospike record bins in any way you want.

jhecking added a commit that referenced this issue Oct 6, 2017
Resolves #7: Support custom mappings of session object to database record.
jhecking added a commit that referenced this issue Oct 6, 2017
Resolves #7: Support custom mappings of session object to a database record.
@umangkedia
Copy link
Author

@jhecking This should work fine. I have forked the repo for now as I needed few more changes like option to save the PK so that it is returned back while querying secondary index.

@jhecking jhecking added this to the v0.4.0 milestone Oct 6, 2017
@jhecking
Copy link
Member

jhecking commented Oct 6, 2017

@umangkedia, sure, let me know if you make some changes to your fork that you think should be merged back into the official copy.

To save the record key on the server, you can set the client's default write policy like this:

const writePolicy = new Aerospike.WritePolicy({ key: Aerospike.policy.key.SEND })
const clientConfig = new Aerospike.Config({ policies: { write: writePolicy } })
const client = Aerospike.client(clientConfig)

And you can now supply a custom mapper to add extra bins to the record in the DB:

const mapper = {
  toRecord: session => {
    return {
      session: JSON.stringify(session),
      mobileNumber: session.mobileNumber,
      userId: session.userId
    }
  },
  fromRecord: record => JSON.parse(record.session)
}

Here is a full, working example: https://gist.github.com/anonymous/c6367ee14c7c0eb7f42b34d7b76617e2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants