Skip to content

Instantly share code, notes, and snippets.

Created October 6, 2017 09:51
Show Gist options
  • Save anonymous/c6367ee14c7c0eb7f42b34d7b76617e2 to your computer and use it in GitHub Desktop.
Save anonymous/c6367ee14c7c0eb7f42b34d7b76617e2 to your computer and use it in GitHub Desktop.
Example how to set custom mapper and default write policy
const Session = require('express-session')
const AerospikeStore = require('aerospike-session-store')(Session)
const Aerospike = require('aerospike')
const writePolicy = new Aerospike.WritePolicy({ key: Aerospike.policy.key.SEND })
const clientConfig = new Aerospike.Config({ policies: { write: writePolicy } })
const client = Aerospike.client(clientConfig)
client.connect().catch(error => { throw error })
const mapper = {
toRecord: session => {
return {
session: JSON.stringify(session),
mobileNumber: session.mobileNumber,
userId: session.userId
}
},
fromRecord: record => JSON.parse(record.session)
}
const store = new AerospikeStore({
client: client,
mapper: mapper
})
let sid = 12345
let session = {
name: 'jan',
mobileNumber: '+654321',
userId: 98765
}
store.set(sid, session, error => {
if (error) throw error
const key = new Aerospike.Key(store.as_namespace, store.as_set, sid)
store.client.get(key, (error, record) => {
if (error) throw error
console.info('Record:', record)
store.get(sid, (error, session) => {
if (error) throw error
console.info('Session:', session)
store.close()
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment