diff --git a/src/backend/src/CoreModule.js b/src/backend/src/CoreModule.js index 0cc820b6..9328d032 100644 --- a/src/backend/src/CoreModule.js +++ b/src/backend/src/CoreModule.js @@ -64,6 +64,7 @@ const install = async ({ services, app, useapi, modapi }) => { def('core.APIError', require('./api/APIError')); def('core', require('./services/auth/Actor'), { assign: true }); + def('core.config', config); }); // === LIBRARIES === diff --git a/src/backend/src/Extension.js b/src/backend/src/Extension.js index 9f8672fa..6b2d4ade 100644 --- a/src/backend/src/Extension.js +++ b/src/backend/src/Extension.js @@ -20,6 +20,21 @@ class Extension extends AdvancedBase { this.ensure_service_(); } + example () { + console.log('Example method called by an extension.'); + } + + get db () { + const db = this.service.values.get('db'); + if ( ! db ) { + throw new Error( + 'extension tried to access database before it was ' + + 'initialized' + ); + } + return db; + } + get (path, handler, options) { // this extension will have a default service this.ensure_service_(); diff --git a/src/backend/src/ExtensionService.js b/src/backend/src/ExtensionService.js index 346d5abd..7ea96d12 100644 --- a/src/backend/src/ExtensionService.js +++ b/src/backend/src/ExtensionService.js @@ -2,6 +2,8 @@ const { AdvancedBase } = require("@heyputer/putility"); const BaseService = require("./services/BaseService"); const { Endpoint } = require("./util/expressutil"); const configurable_auth = require("./middleware/configurable_auth"); +const { Context } = require("./util/context"); +const { DB_READ, DB_WRITE } = require("./services/database/consts"); class ExtensionServiceState extends AdvancedBase { constructor (...a) { @@ -10,6 +12,9 @@ class ExtensionServiceState extends AdvancedBase { this.extension = a[0].extension; this.endpoints_ = []; + + // Values shared between the `extension` global and its service + this.values = new Context(); } register_route_handler_ (path, handler, options = {}) { // handler and options may be flipped @@ -51,6 +56,10 @@ class ExtensionService extends BaseService { async _init (args) { this.state = args.state; + // Create database access object for extension + const db = this.services.get('database').get(DB_WRITE, 'extension'); + this.state.values.set('db', db); + // Propagate all events not from extensions to `core.` const svc_event = this.services.get('event'); svc_event.on_all((key, data, meta = {}) => {