orbitinghail / sqlsync

SQLSync is a collaborative offline-first wrapper around SQLite. It is designed to synchronize web application state between users, devices, and the edge.
https://sqlsync.dev
Apache License 2.0
2.19k stars 28 forks source link

Introduce Reducer Trait #40

Closed matthewgapp closed 6 months ago

matthewgapp commented 6 months ago

Resolves https://github.com/orbitinghail/sqlsync/issues/39

Decided to have a go at implementing the reducer trait so that custom server dead letters and such can be inserted based on mutation content and the user's specific business logic. Seems to be working fine in my server but haven't tested exhaustively.

Then users can use it like so

struct CustomReducer {
    wasm_reducer: sqlsync::reducer::WasmReducer,
}

impl sqlsync::reducer::Reducer for CustomReducer {
    fn apply(
        &mut self,
        tx: &mut sqlsync::sqlite::Transaction,
        mutation: &[u8],
    ) -> sqlsync::reducer::Result<()> {
        // custom logic goes here
        /*
        match self.customInner.validate(mutation) => { 
            Err(e) => { 
                tx.execute("INSERT INTO errors (error) VALUES (?)", params![e.to_string()])?;
            }
        }
         */

        // can use SQLSync WasmReducer or just use the tx handle yourself?
        self.wasm_reducer.apply(tx, mutation)
    }
}

Most of the lines are changed because of auto formatting.

carlsverre commented 6 months ago

I think if you rebase this on main it should prompt me to run tests on your PRs.

carlsverre commented 6 months ago

I'm going to clean this PR up and merge it now - just pinging incase you are also doing so.

matthewgapp commented 6 months ago

I'm going to clean this PR up and merge it now - just pinging incase you are also doing so.

Thanks! Sorry for the lint issues. My LSP was falling over yesterday.