Skip to content
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

Before change event #116

Closed
bkniffler opened this issue Jan 30, 2019 · 1 comment
Closed

Before change event #116

bkniffler opened this issue Jan 30, 2019 · 1 comment

Comments

@bkniffler
Copy link
Contributor

bkniffler commented Jan 30, 2019

Is your feature request related to a problem? Please describe.
The change will not allow me to modify a row. Say I want to create a plugin to update updatedOn field each time a row is updated, how can I manage this?

Describe the solution you'd like
Maybe a "beforeChange" hook? Or make the change hook allow to return Promise or call a callback with an updated item. Or just call the change hook before actual writing to storage so that mutation on event.result will be persisted.

@only-cliches
Copy link
Owner

only-cliches commented Jan 30, 2019

This is thoroughly implemented with filters, documented in the plugin docs here.

A quick example to implement what you're asking for:

nSQL().connect({
    tables: [...],
    plugins: [
        {
            name: "My Plugin",
            version: 1.0,
            dependencies: {},
            filters: [
                {
                    name: "adapterWrite",
                    priority: 1000,
                    call: (inputArgs: {res: {table: string, pk: any, row: any, complete: () => void, error: () => void}, query: InanoSQLQuery}, complete, cancel) => {
                        // passthrough
                        complete(inputArgs)
                    }
                }
            ]
        }
    ]
})

This filter is called before rows are written into the database from upsert queries, works for new rows and rows being updated.

Hope that makes sense!

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

No branches or pull requests

2 participants