Skip to content

Commit

Permalink
feat(pouch-link): Allow to inject a Pouch adapter in CozyPouchLink
Browse files Browse the repository at this point in the history
In previous commit we added a `platform` option into the PouchLink
constructor in order to allow injecting a custom local storage API from
a react-native project

We also want to inject a custom PouchDB adapter as a react-native
project would use a different PouchDB implementation

Like for the local storage API, if no injection is given, then
`pouchdb-browser` adapter will be used by default
  • Loading branch information
Ldoppea committed May 28, 2024
1 parent 0f878eb commit c3ffd2e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/cozy-pouch-link/src/PouchManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PouchDB from 'pouchdb-browser'
import fromPairs from 'lodash/fromPairs'
import forEach from 'lodash/forEach'
import get from 'lodash/get'
Expand Down Expand Up @@ -41,16 +40,21 @@ class PouchManager {
this.storage = new PouchLocalStorage(
options.platform?.storage || platformWeb.storage
)
this.PouchDB = options.platform?.pouchAdapter || platformWeb.pouchAdapter
}

async init() {
const pouchPlugins = get(this.options, 'pouch.plugins', [])
const pouchOptions = get(this.options, 'pouch.options', {})

forEach(pouchPlugins, plugin => this.PouchDB.plugin(plugin))
this.pouches = fromPairs(
this.doctypes.map(doctype => [
doctype,
new PouchDB(getDatabaseName(this.options.prefix, doctype), pouchOptions)
new this.PouchDB(
getDatabaseName(this.options.prefix, doctype),
pouchOptions
)
])
)
this.syncedDoctypes = await this.storage.getPersistedSyncedDoctypes()
Expand Down
5 changes: 4 additions & 1 deletion packages/cozy-pouch-link/src/platformWeb.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import PouchDB from 'pouchdb-browser'

const storage = {
getItem: async key => {
return window.localStorage.getItem(key)
Expand All @@ -11,5 +13,6 @@ const storage = {
}

export const platformWeb = {
storage
storage,
pouchAdapter: PouchDB
}
1 change: 1 addition & 0 deletions packages/cozy-pouch-link/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/**
* @typedef {object} PouchLinkPlatform
* @property {LocalStorage} storage Methods to access local storage
* @property {any} pouchAdapter PouchDB class (can be pouchdb-core or pouchdb-browser)
*/

export default {}

0 comments on commit c3ffd2e

Please sign in to comment.