session-mg

1.0.4 • Public • Published

session-mg icon NPM

Documentation

Description

session-mg is a simple session/credentials manager that helps you to store key/value in a JSON database (based on SJDB), who can expire with time, session-mg also provide an events API. I use it to temporaly store user info that i took from my SJDB database, reducing heavy file manipulation, that mean reducing CPU usage and so make my apps faster, moreover basic sessions usage ! It could be same for you ;)

SessionManager public class Three

There is one class, SessionManager, who take as argument, the session-mg folder path (where sessions and logs will be stored).

expiration
    .active( true || false@default ): En/Disable sessions expiration
    .time( int >= 1 && < 2147483647 || 1800000@default ): Set default time before session expiration
events
	.fire( eventName ): Fire event
add( sessionKey, value ): Add a new session
get( sessionKey ): Get a session by session key
append( sessionKey, key, value ): Append a key/value to a session by his sessionKey
update( sessionKey, newValue ): Update the value of a session
delete( sessionKey ): Delete a session by his session key
CLI( identifier ): Start SJDB Query CLI

Events

added -> Fired each time that a session is created.
getted -> Fired each time that a session is getted.
appended -> Fired each time that a sessions is appended.
updated -> Fired each time that a session is updated.
deleted -> Fired each time that a session is deleted.
expired -> Fired each time that a session expired.

Tutorials

Instantiating session-mg

Like there is just one class, no need to make two vars, one for require and the other for new class, at least that you want two session managers.

const sessions = new ( require('session-mg') ).SessionManager('./session')

Adding and getting session

Know that we instantiated session-mg, let's add sessions !

(...)
sessions.add('mySess', { name: 'Alexandre', pass: 'mg' })
console.log( 'My session:', sessions.get('mySess') )

If sessions doesn't exist, will return undefined. That simple right ? :)

Make session expire

By default every sessions expire after 1 800 000 ms (30 mins), but you can specify expiration time as 3th argument

(...)
sessions.add('shortSess', 'Can be string too!', 12000)
// Will expire in 12 000ms

Updating, appending and deleting session

Moreover adding and getting, you can do simple operations on sessions.

(...)
session.create('changingSess', 010101) // Can be int
sessions.update('changingSess', { now: `I'm object!`, later: 'A bigger object! ;o' })
sessions.append('changingSess', 'ImNewKey', { desc: 'And new object!' })
sessions.delete('changingSess') // No more session ! :(

Setting and handling events

To help you build awesomes projects, you can use the session-mg events API !

First, like Documentation -> Events says it, there is actualy 6 events that you can handle

(...)
sessions.on('added', () => { console.log('A new session !') })
sessions.add('sess', 12)
// output: 'A new session !'
// And you can do the same thing with the 5 other events

Well it's great but.. that kind of pretty limited.. But of course, with session-mg we can do more complex handlings !

(...)
session.on('added', (addedSess) => { console.log('Session named', deletedSess.name, 'was added!' } )
// events API return targeted session

session.add('sess', { name: 'Joe', pass: 0000 } )

// output: 'Session named Joe was added!'

Pretty easy no ? ;)

Expiration settings

Well, there is only 2 settings, active and time, that can be reached by sessions.expiration., where active define if sessions expire, can be false or true (by default), and time set time before session expiration if actived (by default 1 800 000 ms).

Credits

Made by

Alexandre Daubricourt, Paris, France.

mail: alexr.daubricourt@gmail.com Twitter: @AlexDaubricourt Github: Akronae npm: akrone

Icon

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

LICENSE

GPL-3.O

Package Sidebar

Install

npm i session-mg

Weekly Downloads

1

Version

1.0.4

License

GPL-3.0

Last publish

Collaborators

  • akrone