* Add the dataSource to this application. * * @param dataSource - The dataSource to add. * @param nameOrOptions - The binding name or options of the datasource; * defaults to dataSource.name * * @example * ```ts * * const ds: juggler.DataSource = new jugg
(
dataSource: Class<D> | D,
nameOrOptions?: string | BindingFromClassOptions,
)
| 125 | * ``` |
| 126 | */ |
| 127 | dataSource<D extends juggler.DataSource>( |
| 128 | dataSource: Class<D> | D, |
| 129 | nameOrOptions?: string | BindingFromClassOptions, |
| 130 | ): Binding<D> { |
| 131 | const options = toOptions(nameOrOptions); |
| 132 | // We have an instance of |
| 133 | if (dataSource instanceof juggler.DataSource) { |
| 134 | const name = options.name || dataSource.name; |
| 135 | const namespace = options.namespace ?? RepositoryBindings.DATASOURCES; |
| 136 | const key = `${namespace}.${name}`; |
| 137 | return this.bind(key).to(dataSource).tag(RepositoryTags.DATASOURCE); |
| 138 | } else if (typeof dataSource === 'function') { |
| 139 | options.name = options.name || dataSource.dataSourceName; |
| 140 | const binding = createBindingFromClass(dataSource, { |
| 141 | namespace: RepositoryBindings.DATASOURCES, |
| 142 | type: RepositoryTags.DATASOURCE, |
| 143 | defaultScope: BindingScope.SINGLETON, |
| 144 | ...options, |
| 145 | }); |
| 146 | this.add(binding); |
| 147 | return binding; |
| 148 | } else { |
| 149 | throw new Error('not a valid DataSource.'); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Register a model class as a binding in the target context |