hapipal / schwifty

A model layer for hapi integrating Objection ORM
MIT License
73 stars 10 forks source link

Model/knex namespacing and sandboxing #84

Closed devinivy closed 4 years ago

devinivy commented 4 years ago

See also https://github.com/hapipal/schmervice/pull/10 where similar features are implemented for schmervice.

Namespaces are an extension to the ability to pass true to server.models(true) in order to access all models (from the perspective of the root server), and similarly for server.knex(true). Now you may also pass a plugin name as a string server.models('my-plugin') (and server.knex('my-plugin')) in order to access models/knex from the perspective of that plugin. In other words, calling server.models() inside my-plugin is the same as calling server.models('my-plugin') from any plugin; identically calling server.models() on the root server is the same as calling server.models(true) from any plugin.

Sandboxing is a mechanism to opt-out of models being available to all ancestor plugins/namespaces of the plugin that registered the model, and same goes with knex instances for child plugins/namespaces. This essentially is the schwifty-y way of having models and knex instances that are "private" within the plugin that registered them.

These features work together to enable better plugin encapsulation and greater testability, with the ability to not only access, but also mock-out models on a per-plugin basis.