| 'use strict'; |
| |
| import { Map } from 'pouchdb-collections'; |
| |
| var cachedDatabases = new Map(); |
| |
| // openDatabase passed in through opts (e.g. for node-websql) |
| function openDatabaseWithOpts(opts) { |
| return opts.websql(opts.name, opts.version, opts.description, opts.size); |
| } |
| |
| function openDBSafely(opts) { |
| try { |
| return { |
| db: openDatabaseWithOpts(opts) |
| }; |
| } catch (err) { |
| return { |
| error: err |
| }; |
| } |
| } |
| |
| function openDB(opts) { |
| var cachedResult = cachedDatabases.get(opts.name); |
| if (!cachedResult) { |
| cachedResult = openDBSafely(opts); |
| cachedDatabases.set(opts.name, cachedResult); |
| } |
| return cachedResult; |
| } |
| |
| export default openDB; |