New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add save
and revive
method
#62
Conversation
Codecov Report
@@ Coverage Diff @@
## master #62 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 36 36
Lines 838 901 +63
Branches 134 144 +10
=========================================
+ Hits 838 901 +63
Continue to review full report at Codecov.
|
The only obstacle here is the use of Investigating this further, the following is the best to my knowledge:
Custom set method bridges Vue 2 & 3 (inspired by vue-demi and vue-use, except it does not require a post-install fix): let Vue: any
try {
const m = require('vue')
// Webpack requires `.default`, Vite does not.
Vue = m.default || m
} catch (e) {
Vue = window.Vue
}
export function set<T>(array: T[], key: number, value: T): T
export function set<T>(object: Record<string, any>, key: string | number, value: T): T
export function set(target: any, key: any, value: any): any {
// Array does not require treatment, the following maintains reactivity in both versions.
if (isArray(target)) {
target.length = Math.max(target.length, key)
target.splice(key, 1, value)
return value
}
// Object does require treatment.
// Vue 2 requires `Vue.set` to maintain reactivity.
// Vue 3 simply reflects the object values.
assert(Vue && typeof Vue.version === 'string', [
'Vue was not detected. Please install Vue.'
])
const version = Vue.version.slice(0, 2)
assert(['2.', '3.'].includes(version), [
`Vue version ${Vue.version} is not supported.`
])
if (version === '2.') {
return Vue.set(target, key, value)
}
target[key] = value
return value
} |
Thinking about this more, it's becoming more evident the libraries migrating to Vue 3 are adding VCA as peer dependencies for Vue 2 support. So I think this might be the better option. |
Oh, didn't know vue-demi offers |
Co-authored-by: Kia King Ishii <kia.king.08@gmail.com>
ref #60
This PR adds 2 new method to the repository.
save
andrevive
.Type of PR:
Breaking changes:
Details
The
save
method will insert a given data, or update it if it already exists in the store. It will also normalize any relationships as well.The save method will return something we call
schema
, which is the original data passed into thesave
method, but with special__id
key attached.We may now pass this
schema
to the newrevive
method to retrieve the models.All of the relationship will be correctly resolved. Also, note that the order of the
posts
relationships are well preserved.Combining
save
andrevive
method, we can easily reuse the record order that external apis might had, while normalizing the data and saving it to the store.Todos