Skip to content

Commit f8f6eff

Browse files
committed
Hydrate api implementation
1 parent 74b85c6 commit f8f6eff

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import useFind from './useFind'
1717
import useGet from './useGet'
1818

1919
import { FeathersVuexOptions } from './service-module/types'
20-
import { initAuth } from './utils'
20+
import { initAuth, hydrateApi } from './utils'
2121
import { FeathersVuex } from './vue-plugin/vue-plugin'
2222

2323
const defaultOptions: FeathersVuexOptions = {
@@ -69,6 +69,7 @@ export default function feathersVuex(feathers, options: FeathersVuexOptions) {
6969

7070
export {
7171
initAuth,
72+
hydrateApi,
7273
FeathersVuexFind,
7374
FeathersVuexGet,
7475
FeathersVuexFormWrapper,

src/service-module/make-base-model.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
238238
}
239239
}
240240

241+
/**
242+
* make the server side documents hydrated on client a FeathersVuexModel
243+
*/
244+
public static hydrateAll() {
245+
const { namespace, store } = this
246+
const state = store.state[namespace]
247+
const commit = store.commit
248+
// Replace each plain object with a model instance.
249+
Object.keys(state.keyedById).forEach(id => {
250+
const record = state.keyedById[id]
251+
console.log('hydrateAll')
252+
console.log(record)
253+
commit(`${namespace}/removeItem`, record)
254+
commit(`${namespace}/addItem`, record)
255+
})
256+
}
257+
241258
/**
242259
* clone the current record using the `createCopy` mutation
243260
*/

src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ export const initAuth = function initAuth(options) {
182182
return Promise.resolve(payload)
183183
}
184184

185+
/**
186+
* run de BaseModel hydration on client for each api
187+
*/
188+
export const hydrateApi = function hydrateApi({ api }) {
189+
Object.keys(api).forEach(modelName => {
190+
if (!['byServicePath', 'BaseModel'].includes(modelName)) {
191+
const Model = api[modelName]
192+
Model.hydrateAll()
193+
}
194+
})
195+
}
196+
185197
/**
186198
* Generate a new tempId and mark the record as a temp
187199
* @param state

0 commit comments

Comments
 (0)