UNPKG

dbaas

Version:

Database as a service. Expose db to REST and GraphQL.

72 lines 2.85 kB
import { InvalidConfigException, } from "directus"; import createItemsService from "./createItemsService.js"; // import getUserAccountability from "./getUserAccountability.js" /** Services instance created per request or filter/action context */ class ServicesBase { /** Global extension context */ #extensionContext; /** The the context or request this services instance made for */ #requestOrHookContext; /** The ItemsService options */ #options; /** The cached ItemsService scoped by request or hook context */ #itemsServices = {}; constructor(extensionContext, requestOrHookContext, options) { this.#extensionContext = extensionContext; this.#requestOrHookContext = requestOrHookContext; this.#options = options; } get extensionContext() { return this.#extensionContext; } get requestOrHookContext() { return this.#requestOrHookContext; } get options() { return this.#options; } loginAsAdmin() { if (Object.keys(this.#itemsServices).length > 0) { throw new InvalidConfigException("You cannot login after any ItemsService has been initialized"); } // const accountability = await getUserAccountability( // this.#requestOrHookContext, // userId, // ) // if (!accountability) { // throw new InvalidCredentialsException("User not found") // } this.#options = { ...this.#options, accountability: { // satisfy TypeScript role: null, ...this.#options?.accountability, // For now we can't login as any user/role to keep activity record. // The accountability.permissions must be set based on the user/role // There is an issue importing getPermissions or even fetch using knex directly. admin: true, }, }; } /** Get and cache the ItemsService scoped by request or hook context */ getItemsService(collection) { let itemsService = this.#itemsServices[collection]; if (!itemsService) { const reqOrHookCtx = this.requestOrHookContext; const options = this.options; // This check is unnecessary in JS but just to satisfy TS // against choosing the createItemsService overloads if ("url" in reqOrHookCtx) { itemsService = createItemsService(collection, reqOrHookCtx, options); } else { itemsService = createItemsService(collection, reqOrHookCtx, options); } this.#itemsServices[collection] = itemsService; } return itemsService; } } export default ServicesBase; //# sourceMappingURL=ServicesBase.js.map