MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / dataSource

Method dataSource

packages/repository/src/mixins/repository.mixin.ts:127–151  ·  view source on GitHub ↗

* 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,
    )

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

createBindingFromClassFunction · 0.85
tagMethod · 0.80
toMethod · 0.80
toOptionsFunction · 0.70
addMethod · 0.65
bindMethod · 0.45

Tested by

no test coverage detected