rethinkdb / horizon

Horizon is a realtime, open-source backend for JavaScript apps.
MIT License
6.78k stars 349 forks source link

Plugins #373

Open bopjesvla opened 8 years ago

bopjesvla commented 8 years ago

Reading through the discussion on document validation, it seems to me that if everything is implemented as a fixed API, horizon will only be used by people with specific use cases.

I don't think document validation should be part of the core, for the simple reason that no one solution will suit every app. You might be thinking about storing the schemas in the database. What if I want to validate login information before the web socket is opened? The schemas might be read from JSON files. What if I want to use YAML? You might pick ajv as the json-schema implementation because it's fast. What if I want to use jsen because it's three times as small? What if my app doesn't need any fancy APIs whatsoever, and I'm just using horizon for the realtime feeds?


This is what I imagine a document validation plugin would look like:

let validate = require("json-something").validate
module.exports = function(hz, opts) {
    let schemas = hz("schemas").fetch();
    let validate = (table, doc) => validate(schemas[table], doc)
    hz.on("insert", validate)
    hz.on("update", validate)
}

An auth plugin:

module.exports = function(hz, opts) {
    let users = hz("users");
    if (!users) {
        // initialize
    }
    hz.on("connect", credentials => users.findOne({user: credentials.user, password: credentials.password}))
}

Plugin configuration would be done in config.toml and/or a configuration module like webpack's config.js.

deontologician commented 8 years ago

345 is also related to freeform customization of the server.

Ultimately, we have the fallback of allowing the user complete control by importing horizon as a library. I think if you're interested in the different between size and speed of json-schema parsing libraries, then you're in the realm of writing your own custom backend. The default backend should focus on working for 80% of use cases and allowing the user to take over once it isn't sufficient

bopjesvla commented 8 years ago

I think if you're interested in the different between size and speed of json-schema parsing libraries, then you're in the realm of writing your own custom backend.

That particular comment was about document validation on the client. And yeah, it wasn't the best example. The first and the last one are more important.

A slightly more concrete proposal that depends on #345:

I'll expand on why I think auth and validation should be implemented as plugins later, but I'd like to hear your thoughts on this first.

bopjesvla commented 8 years ago

Disorganised notes:

bopjesvla commented 8 years ago

Two more things:

danielmewes commented 8 years ago

Linking the work-in-progress RFC for Horizon plugins: https://github.com/rethinkdb/horizon/pull/588