Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

How to intercept & change set behavior? #180

Open
zaaack opened this issue Jun 3, 2017 · 5 comments
Open

How to intercept & change set behavior? #180

zaaack opened this issue Jun 3, 2017 · 5 comments
Labels

Comments

@zaaack
Copy link
Contributor

zaaack commented Jun 3, 2017

I want to intercept get & set action to make the field value different in using & store, here are my code:

import { ActionTypes, Model } from 'mongorito'

// const defaults = (val, defaults) => typeof val === 'undefined' ? defaults : val

export function mapFields(mappings) {
    const mapKeys = Object.keys(mappings)
    return () => store => next => action => {
        // action GET
        if (action.type === ActionTypes.GET) {
            let { fields } = store.getState()
            const { key } = action
            const { get: getter } = key ? mappings[key] : {}
            // current key has getter and not null
            if (getter) {
                const val = fields[key]
                return getter(val)
                // get all fields
            } else if (!key) {
                fields = { ...fields }
                return mapKeys.reduce((fields, key) => {
                    const val = fields[key]
                    fields[key] = mappings[key].get(val)
                    return fields
                }, fields)
            }
        // action set
        } else if (action.type === ActionTypes.SET) {
            const { fields } = action
            // set each field key with setter
            for (let key in fields) {
                const { set: setter } = mappings[key] || {}
                if (setter) {
                    const val = fields[key]
                    fields[key] = setter(val)
                }
            }
        }

        return next(action)
    }
}

class Users extends Model {

}

db.register(Users)

const Gender = {
    MALE: 1,
    FEMALE: 2,
    UNKNOWN: 3,
}

const GenderReverse = {
    1: 'MALE',
    2: 'FEMALE',
    3: 'UNKNOWN',
}

Users.use(mapFields({
    gender: {
        get: g => GenderReverse[g],
        set: g => Gender[g],
    },
}))

Then I reallize SET action is not only used by outside, but also inside when query from the db (in the constructor), so I couldn't tell which action is triggered by user input or query from the db.

@zaaack zaaack changed the title How to change intercept set behavior? [v3] How to change intercept set behavior? Jun 3, 2017
@zaaack zaaack changed the title [v3] How to change intercept set behavior? [v3] How to intercept & change set behavior? Jun 6, 2017
@vadimdemedes
Copy link
Owner

Could you reformat your code or post it elsewhere? It's hard to follow it, because the indentation is broken and there's no spacing.

@vadimdemedes vadimdemedes changed the title [v3] How to intercept & change set behavior? How to intercept & change set behavior? Jun 7, 2017
@zaaack
Copy link
Contributor Author

zaaack commented Jun 7, 2017

@vadimdemedes Sorry, added some annotations and a demo, hope it would be more clear.

@vadimdemedes
Copy link
Owner

Then I reallize SET action is not only used by outside, but also inside when query from the db (in the constructor), so I couldn't tell which action is triggered by user input or query from the db.

This is a good point, perhaps we'll need to introduce a specific action to dispatch on model initialization, like SEED or INITIAL_SET. @zaaack Any ideas for a better name?

@zaaack
Copy link
Contributor Author

zaaack commented Jun 26, 2017

@vadimdemedes I find out the feature I need is actual the virtual field feature in mongoose, and if I using another field name that doesn't exist in database, the problem solved.

@zaaack zaaack closed this as completed Jun 26, 2017
@zaaack zaaack reopened this Jun 26, 2017
@zaaack
Copy link
Contributor Author

zaaack commented Jun 26, 2017

But to solve the problem that there isn't a way to intercept query and persistence, I think we should let the models query from database using another action to initial, like INTERNAL_SET, not just in constructor, since this could still be called by user.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants