-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
51 lines (42 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module.exports = accountApi
var EventEmitter = require('events').EventEmitter
var account = require('./lib/account')
var addSession = require('./lib/sessions/add')
var findSession = require('./lib/sessions/find')
var removeSession = require('./lib/sessions/remove')
var addAccount = require('./lib/accounts/add')
var findAccount = require('./lib/accounts/find')
var findAllAccounts = require('./lib/accounts/find-all')
var updateAccount = require('./lib/accounts/update')
var removeAccount = require('./lib/accounts/remove')
var accountsOn = require('./lib/accounts/on')
var cache = require('./lib/utils/cache')
var startListeningToAccountChanges = require('./lib/utils/start-listening-to-account-changes')
function accountApi (options) {
options.PouchDB.plugin(require('pouchdb-users'))
var accountsEmitter = new EventEmitter()
var db = new options.PouchDB(options.usersDb || '_users')
var state = {
db: db,
secret: options.secret,
accountsEmitter: accountsEmitter,
cache: cache(db)
}
accountsEmitter.once('newListener', startListeningToAccountChanges.bind(null, state))
return {
sessions: {
add: addSession.bind(null, state),
find: findSession.bind(null, state),
remove: removeSession.bind(null, state)
},
accounts: {
add: addAccount.bind(null, state),
find: findAccount.bind(null, state),
findAll: findAllAccounts.bind(null, state),
remove: removeAccount.bind(null, state),
update: updateAccount.bind(null, state),
on: accountsOn.bind(null, state)
},
account: account.bind(null, state)
}
}