UNPKG

@decaf-ts/core

Version:

Core persistence module for the decaf framework

50 lines 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateInjectableNameForRepository = generateInjectableNameForRepository; const db_decorators_1 = require("@decaf-ts/db-decorators"); const decorator_validation_1 = require("@decaf-ts/decorator-validation"); const constants_1 = require("./../persistence/constants.cjs"); const decorator_validation_2 = require("@decaf-ts/decorator-validation"); const decoration_1 = require("@decaf-ts/decoration"); /** * @description Generates a unique injectable name for a repository. * @summary Creates a standardized injectable token for repositories using the adapter flavour and model table name. * This helps the DI system register and resolve repository instances consistently across adapters. * @template T The model type that extends Model. * @param {Constructor<T> | T} model The model constructor or instance from which to derive the table name. * @param {string} [flavour] Optional adapter flavour/alias. If omitted, it is read from model metadata. * @return {string} A namespaced injectable token for the repository (e.g., "db:repo:ram:users"). * @throws {InternalError} If the flavour cannot be determined from arguments or metadata. * @function generateInjectableNameForRepository * @mermaid * sequenceDiagram * participant C as Caller * participant U as generateInjectableNameForRepository * participant R as Reflect Metadata * participant A as Adapter * participant S as String Formatter * C->>U: call(model, flavour?) * alt flavour provided * U-->>U: use provided flavour * else flavour not provided * U->>A: ADAPTER * U->>R: getMetadata(key, model|model.ctor) * alt metadata present * R-->>U: flavour * else missing * U-->>C: throw InternalError * end * end * U->>S: sf(INJECTABLE, flavour, Model.tableName(model)) * S-->>C: token string * @memberOf module:core */ function generateInjectableNameForRepository(model, flavour) { if (!flavour) { flavour = decoration_1.Decoration["flavourResolver"](model instanceof decorator_validation_2.Model ? model.constructor : model); if (!flavour || flavour === decoration_1.DefaultFlavour) throw new db_decorators_1.InternalError(`Could not retrieve flavour from model ${model instanceof decorator_validation_2.Model ? model.constructor.name : model.name}`); } return (0, decorator_validation_1.sf)(constants_1.PersistenceKeys.INJECTABLE, flavour, decorator_validation_2.Model.tableName(model)); } //# sourceMappingURL=utils.js.map