pouchdb / couchdb身份验证:为注册用户创建私有数据库

时间:2017-04-18 12:00:22

标签: javascript couchdb pouchdb

我使用带有PouchDB-authentication插件的PouchDB来注册用户。我可以使用以下代码注册用户:

username='potato' 
password='potato chips crisis'
const dummyDB=new window.PouchDB('http://192.168.145.19:5984/dummy',
    {skipSetup:true})
dummyDB.signup(username, password).then(res=>{
        //user is signed up
        //now, how to create a database for the user who just signed up?
    }).catch(err=>{
        window.console.log('Error: ', err)
    })

上面的代码对用户没有任何问题,但它没有创建与用户和虚拟数据库is just a dummy one相关联的任何数据库。

现在我想知道如何创建与我刚注册的用户相关联的数据库,以便只有该用户可以读/写它们。

更新:

看起来要走的路是这样的:

const username='potato'
const password='potato chips crisis'

let realDBRemote
let realDBLocal

const dummyDB=new window.PouchDB('http://192.168.145.19:5984/dummy',
    {skipSetup:true})

dummyDB.signup(username, password).then(res=>{
        /**
         * user is signed up,
         * so now, we might create real databases (rather than dummy onee):
         */
        realDBRemote=new window.PouchDB('http://192.168.145.19:5984/'+
            username,{skipSetup:false})
        realDBLocal=new window.PouchDB(username)
        /**
         * and now you're able to do stuff to real databases like login:
         */
        return realDBRemote.login(username, password /*,ajaxOpts*/)
    }).then(res=>{
        /**
         * We might be able to sync remote/local real databases too:
         */
        realDBLocal.sync(realDBRemote,{live:true,retry:true})
            .on('error',err=>{console.log('Error:', err)})
    }).catch(err=>{
        window.console.log('Error: ', err)
    })

我不确定上述方法是最佳做法,但我的应用看起来很不错。

1 个答案:

答案 0 :(得分:1)

要创建数据库,只需在没有提供的new PouchDB选项的情况下调用skip_setup 。这在PouchDB API docs

中有记录
var db = new PouchDB('http://192.168.145.19:5984/realDatabaseName');