Skip to content

Commit

Permalink
馃憣 Possible fix for #16 and #13
Browse files Browse the repository at this point in the history
  • Loading branch information
TemaSM committed Aug 28, 2018
1 parent 406fc50 commit 7283441
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const localSession = new LocalSession({
database: 'example_db.json',
// Name of session property object in Telegraf Context (default: 'session')
property: 'session',
// Type of lowdb storage (default: 'storagefileAsync')
storage: LocalSession.storagefileAsync,
// Type of lowdb storage (default: 'storageFileSync')
storage: LocalSession.storageFileAsync,
// Format of storage/database (default: JSON.stringify / JSON.parse)
format: {
serialize: (obj) => JSON.stringify(obj, null, 2), // null & 2 for pretty-formatted JSON
Expand All @@ -86,6 +86,13 @@ const localSession = new LocalSession({
state: { messages: [] }
})

// Wait for database async initialization finished (storageFileAsync or your own asynchronous storage adapter)
localSession.DB.then(DB => {
// Database now initialized, so now you can retrieve anything you want from it
console.log('Current LocalSession DB:', DB.value())
// console.log(DB.get('sessions').getById('1:1').value())
})

// Telegraf will use `telegraf-session-local` configured above middleware with overrided `property` name
Bot.use(localSession.middleware(property))

Expand Down
11 changes: 9 additions & 2 deletions examples/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const localSession = new LocalSession({
database: 'example_db.json',
// Name of session property object in Telegraf Context (default: 'session')
property: 'session',
// Type of lowdb storage (default: 'storagefileAsync')
storage: LocalSession.storagefileAsync,
// Type of lowdb storage (default: 'storageFileSync')
storage: LocalSession.storageFileAsync,
// Format of storage/database (default: JSON.stringify / JSON.parse)
format: {
serialize: (obj) => JSON.stringify(obj, null, 2), // null & 2 for pretty-formatted JSON
Expand All @@ -23,6 +23,13 @@ const localSession = new LocalSession({
state: { messages: [] }
})

// Wait for database async initialization finished (storageFileAsync or your own asynchronous storage adapter)
localSession.DB.then(DB => {
// Database now initialized, so now you can retrieve anything you want from it
console.log('Current LocalSession DB:', DB.value())
// console.log(DB.get('sessions').getById('1:1').value())
})

// Telegraf will use `telegraf-session-local` configured above middleware with overrided `property` name
Bot.use(localSession.middleware(property))

Expand Down
5 changes: 4 additions & 1 deletion lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class LocalSession {
if (isPromise(DbInstance)) {
debug('DbInstance is Promise like')
// TODO: Split it from constructor, because this code will produce glitches if async initiating may take too long time
DbInstance.then((DB) => {
this.DB = DbInstance
this.DB.then((DB) => {
debug('DbInstance Promise resolved')
this.DB = DB
_initDB.call(this)
})
Expand Down Expand Up @@ -273,6 +275,7 @@ function _initDB () {
// If database is empty, fill it with empty Array of sessions and optionally with initial state
this.DB.defaults(Object.assign({ sessions: [] }, this.options.state)).write()
debug('Initiating finished')
return true
}

// Credits to `is-promise` package
Expand Down
30 changes: 28 additions & 2 deletions tests/storageFileAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const
debug = require('debug')('telegraf:session-local:test'),
options = { database: 'test_async_db.json', storage: LocalSession.storageFileAsync }

let bot = {}
let localSession = new LocalSession(options)

// Wait for database async initialization finished
before((done) => {
localSession.DB.then(DB => done())
})

describe('Telegraf Session local : storageFileAsync', () => {
let bot = {}
let localSession = new LocalSession(options)

it('storageFileAsync: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
Expand Down Expand Up @@ -71,4 +77,24 @@ describe('Telegraf Session local : storageFileAsync', () => {
})
bot.handleUpdate({ message: { chat: { id: 1 }, from: { id: 1 }, text: 'hey' } })
})

it('storageFileAsync: Should work properly with deprecated stoarge name - storagefileAsync', (done) => {
let _options = Object.assign({ storage: LocalSession.storagefileAsync }, options)
let _localSession = new LocalSession(_options)
// Wait for database async initialization finished
_localSession.DB.then(DB => {
// console.log(DB.get('sessions').getById('1:1').value())
const key = '1:1' // ChatID:FromID
let session = _localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
_localSession.saveSession(key, session).then(_session => {
debug('Saved session %O', _session)
should.exist(_session)
_session.data.should.be.deepEqual({ foo: 42 })
done()
})
})
})
})
21 changes: 19 additions & 2 deletions tests/storageFileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const
debug = require('debug')('telegraf:session-local:test'),
options = { database: 'test_sync_db.json', storage: LocalSession.storageFileSync }

let bot = {}
let localSession = new LocalSession(options)

describe('Telegraf Session local : storageFileSync', () => {
let bot = {}
let localSession = new LocalSession(options)

it('storageFileSync: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
Expand Down Expand Up @@ -71,4 +72,20 @@ describe('Telegraf Session local : storageFileSync', () => {
})
bot.handleUpdate({ message: { chat: { id: 1 }, from: { id: 1 }, text: 'hey' } })
})

it('storageFileSync: Should work properly with deprecated stoarge name - storagefileSync', async (done) => {
let _options = Object.assign({ storage: LocalSession.storagefileSync }, options)
let _localSession = new LocalSession(_options)
const key = '1:1' // ChatID:FromID
let session = _localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
_localSession.saveSession(key, session).then(_session => {
debug('Saved session %O', _session)
should.exist(_session)
_session.data.should.be.deepEqual({ foo: 42 })
done()
})
})
})

0 comments on commit 7283441

Please sign in to comment.