UNPKG

18.5 kBTypeScriptView Raw
1import { Application, Binding, BindingFromClassOptions, BindingScope, Component, Constructor, MixinTarget } from '@loopback/core';
2import { Class } from '../common-types';
3import { SchemaMigrationOptions } from '../datasource';
4import { Model } from '../model';
5import { juggler, Repository } from '../repositories';
6/**
7 * A mixin class for Application that creates a .repository()
8 * function to register a repository automatically. Also overrides
9 * component function to allow it to register repositories automatically.
10 *
11 * @example
12 * ```ts
13 * class MyApplication extends RepositoryMixin(Application) {}
14 * ```
15 *
16 * Please note: the members in the mixin function are documented in a dummy class
17 * called <a href="#RepositoryMixinDoc">RepositoryMixinDoc</a>
18 *
19 * @param superClass - Application class
20 * @returns A new class that extends the super class with repository related
21 * methods
22 *
23 * @typeParam T - Type of the application class as the target for the mixin
24 *
25 */
26export declare function RepositoryMixin<T extends MixinTarget<Application>>(superClass: T): {
27 new (...args: any[]): {
28 /**
29 * Add a repository to this application.
30 *
31 * @param repoClass - The repository to add.
32 * @param nameOrOptions - Name or options for the binding
33 *
34 * @example
35 * ```ts
36 *
37 * class NoteRepo {
38 * model: any;
39 *
40 * constructor() {
41 * const ds: juggler.DataSource = new juggler.DataSource({
42 * name: 'db',
43 * connector: 'memory',
44 * });
45 *
46 * this.model = ds.createModel(
47 * 'note',
48 * {title: 'string', content: 'string'},
49 * {}
50 * );
51 * }
52 * };
53 *
54 * app.repository(NoteRepo);
55 * ```
56 */
57 repository<R extends Repository<any>>(repoClass: Class<R>, nameOrOptions?: string | BindingFromClassOptions): Binding<R>;
58 /**
59 * Retrieve the repository instance from the given Repository class
60 *
61 * @param repo - The repository class to retrieve the instance of
62 */
63 getRepository<R_1 extends Repository<any>>(repo: Class<R_1>): Promise<R_1>;
64 /**
65 * Add the dataSource to this application.
66 *
67 * @param dataSource - The dataSource to add.
68 * @param nameOrOptions - The binding name or options of the datasource;
69 * defaults to dataSource.name
70 *
71 * @example
72 * ```ts
73 *
74 * const ds: juggler.DataSource = new juggler.DataSource({
75 * name: 'db',
76 * connector: 'memory',
77 * });
78 *
79 * app.dataSource(ds);
80 *
81 * // The datasource can be injected with
82 * constructor(@inject('datasources.db') dataSource: DataSourceType) {
83 *
84 * }
85 * ```
86 */
87 dataSource<D extends juggler.DataSource>(dataSource: D | Class<D>, nameOrOptions?: string | BindingFromClassOptions): Binding<D>;
88 /**
89 * Register a model class as a binding in the target context
90 * @param modelClass - Model class
91 */
92 model<M extends Class<unknown>>(modelClass: M): Binding<M>;
93 /**
94 * Add a component to this application. Also mounts
95 * all the components repositories.
96 *
97 * @param component - The component to add.
98 * @param nameOrOptions - Name or options for the binding.
99 *
100 * @example
101 * ```ts
102 *
103 * export class ProductComponent {
104 * controllers = [ProductController];
105 * repositories = [ProductRepo, UserRepo];
106 * providers = {
107 * [AUTHENTICATION_STRATEGY]: AuthStrategy,
108 * [AUTHORIZATION_ROLE]: Role,
109 * };
110 * };
111 *
112 * app.component(ProductComponent);
113 * ```
114 */
115 component<C extends Component = Component>(componentCtor: Constructor<C>, nameOrOptions?: string | BindingFromClassOptions): Binding<C>;
116 /**
117 * Get an instance of a component and mount all it's
118 * repositories. This function is intended to be used internally
119 * by `component()`.
120 *
121 * NOTE: Calling `mountComponentRepositories` with a component class
122 * constructor is deprecated. You should instantiate the component
123 * yourself and provide the component instance instead.
124 *
125 * @param componentInstanceOrClass - The component to mount repositories of
126 * @internal
127 */
128 mountComponentRepositories(componentInstanceOrClass: Class<unknown> | RepositoryComponent): void;
129 /**
130 * Bind all model classes provided by a component.
131 * @param component
132 * @internal
133 */
134 mountComponentModels(component: RepositoryComponent): void;
135 /**
136 * Update or recreate the database schema for all repositories.
137 *
138 * **WARNING**: By default, `migrateSchema()` will attempt to preserve data
139 * while updating the schema in your target database, but this is not
140 * guaranteed to be safe.
141 *
142 * Please check the documentation for your specific connector(s) for
143 * a detailed breakdown of behaviors for automigrate!
144 *
145 * @param options - Migration options, e.g. whether to update tables
146 * preserving data or rebuild everything from scratch.
147 */
148 migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
149 readonly options: import("@loopback/core").ApplicationConfig;
150 readonly state: string;
151 controller: <T_1>(controllerCtor: import("@loopback/core").ControllerClass<T_1>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_1>;
152 server: <T_2 extends import("@loopback/core").Server>(ctor: Constructor<T_2>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_2>;
153 servers: <T_3 extends import("@loopback/core").Server>(ctors: Constructor<T_3>[]) => Binding<any>[];
154 getServer: <T_4 extends import("@loopback/core").Server>(target: string | Constructor<T_4>) => Promise<T_4>;
155 init: () => Promise<void>;
156 onInit: (fn: () => import("@loopback/core").ValueOrPromise<void>) => Binding<import("@loopback/core").LifeCycleObserver>;
157 start: () => Promise<void>;
158 onStart: (fn: () => import("@loopback/core").ValueOrPromise<void>) => Binding<import("@loopback/core").LifeCycleObserver>;
159 stop: () => Promise<void>;
160 onStop: (fn: () => import("@loopback/core").ValueOrPromise<void>) => Binding<import("@loopback/core").LifeCycleObserver>;
161 setMetadata: (metadata: import("@loopback/core").ApplicationMetadata) => void;
162 lifeCycleObserver: <T_5 extends import("@loopback/core").LifeCycleObserver>(ctor: Constructor<T_5>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_5>;
163 service: <S>(cls: import("@loopback/core").ServiceOrProviderClass<S>, nameOrOptions?: string | import("@loopback/core").ServiceOptions | undefined) => Binding<S>;
164 interceptor: (interceptor: import("@loopback/core").Interceptor | Constructor<import("@loopback/core").Provider<import("@loopback/core").Interceptor>>, nameOrOptions?: string | import("@loopback/core").InterceptorBindingOptions | undefined) => Binding<import("@loopback/core").Interceptor>;
165 readonly name: string;
166 readonly subscriptionManager: import("@loopback/core").ContextSubscriptionManager;
167 scope: BindingScope;
168 readonly parent: import("@loopback/core").Context | undefined;
169 emitEvent: <T_6 extends import("@loopback/core").ContextEvent>(type: string, event: T_6) => void;
170 emitError: (err: unknown) => void;
171 bind: <ValueType = any>(key: import("@loopback/core").BindingAddress<ValueType>) => Binding<ValueType>;
172 add: (binding: Binding<unknown>) => Application;
173 configure: <ConfigValueType = any>(key?: import("@loopback/core").BindingAddress<unknown> | undefined) => Binding<ConfigValueType>;
174 getConfigAsValueOrPromise: <ConfigValueType_1>(key: import("@loopback/core").BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: import("@loopback/core").ResolutionOptions | undefined) => import("@loopback/core").ValueOrPromise<ConfigValueType_1 | undefined>;
175 getConfig: <ConfigValueType_2>(key: import("@loopback/core").BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: import("@loopback/core").ResolutionOptions | undefined) => Promise<ConfigValueType_2 | undefined>;
176 getConfigSync: <ConfigValueType_3>(key: import("@loopback/core").BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: import("@loopback/core").ResolutionOptions | undefined) => ConfigValueType_3 | undefined;
177 unbind: (key: import("@loopback/core").BindingAddress<unknown>) => boolean;
178 subscribe: (observer: import("@loopback/core").ContextEventObserver) => import("@loopback/core").Subscription;
179 unsubscribe: (observer: import("@loopback/core").ContextEventObserver) => boolean;
180 close: () => void;
181 isSubscribed: (observer: import("@loopback/core").ContextObserver) => boolean;
182 createView: <T_7 = unknown>(filter: import("@loopback/core").BindingFilter, comparator?: import("@loopback/core").BindingComparator | undefined, options?: Omit<import("@loopback/core").ResolutionOptions, "session"> | undefined) => import("@loopback/core").ContextView<T_7>;
183 contains: (key: import("@loopback/core").BindingAddress<unknown>) => boolean;
184 isBound: (key: import("@loopback/core").BindingAddress<unknown>) => boolean;
185 getOwnerContext: (keyOrBinding: import("@loopback/core").BindingAddress<unknown> | Readonly<Binding<unknown>>) => import("@loopback/core").Context | undefined;
186 getScopedContext: (scope: BindingScope.APPLICATION | BindingScope.SERVER | BindingScope.REQUEST) => import("@loopback/core").Context | undefined;
187 getResolutionContext: (binding: Readonly<Binding<unknown>>) => import("@loopback/core").Context | undefined;
188 isVisibleTo: (ctx: import("@loopback/core").Context) => boolean;
189 find: <ValueType_1 = any>(pattern?: string | RegExp | import("@loopback/core").BindingFilter | undefined) => Readonly<Binding<ValueType_1>>[];
190 findByTag: <ValueType_2 = any>(tagFilter: RegExp | import("@loopback/core").BindingTag) => Readonly<Binding<ValueType_2>>[];
191 get: {
192 <ValueType_3>(keyWithPath: import("@loopback/core").BindingAddress<ValueType_3>, session?: import("@loopback/core").ResolutionSession | undefined): Promise<ValueType_3>;
193 <ValueType_4>(keyWithPath: import("@loopback/core").BindingAddress<ValueType_4>, options: import("@loopback/core").ResolutionOptions): Promise<ValueType_4 | undefined>;
194 };
195 getSync: {
196 <ValueType_5>(keyWithPath: import("@loopback/core").BindingAddress<ValueType_5>, session?: import("@loopback/core").ResolutionSession | undefined): ValueType_5;
197 <ValueType_6>(keyWithPath: import("@loopback/core").BindingAddress<ValueType_6>, options?: import("@loopback/core").ResolutionOptions | undefined): ValueType_6 | undefined;
198 };
199 getBinding: {
200 <ValueType_7 = any>(key: import("@loopback/core").BindingAddress<ValueType_7>): Binding<ValueType_7>;
201 <ValueType_8>(key: import("@loopback/core").BindingAddress<ValueType_8>, options?: {
202 optional?: boolean | undefined;
203 } | undefined): Binding<ValueType_8> | undefined;
204 };
205 findOrCreateBinding: <T_8>(key: import("@loopback/core").BindingAddress<T_8>, policy?: import("@loopback/core").BindingCreationPolicy | undefined) => Binding<T_8>;
206 getValueOrPromise: <ValueType_9>(keyWithPath: import("@loopback/core").BindingAddress<ValueType_9>, optionsOrSession?: import("@loopback/core").ResolutionOptionsOrSession | undefined) => import("@loopback/core").ValueOrPromise<ValueType_9 | undefined>;
207 toJSON: () => import("@loopback/core").JSONObject;
208 inspect: (options?: import("@loopback/core").ContextInspectOptions | undefined) => import("@loopback/core").JSONObject;
209 on: {
210 (eventName: "bind" | "unbind", listener: import("@loopback/core").ContextEventListener): Application;
211 (event: string | symbol, listener: (...args: any[]) => void): Application;
212 };
213 once: {
214 (eventName: "bind" | "unbind", listener: import("@loopback/core").ContextEventListener): Application;
215 (event: string | symbol, listener: (...args: any[]) => void): Application;
216 };
217 addListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
218 removeListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
219 off: (event: string | symbol, listener: (...args: any[]) => void) => Application;
220 removeAllListeners: (event?: string | symbol | undefined) => Application;
221 setMaxListeners: (n: number) => Application;
222 getMaxListeners: () => number;
223 listeners: (event: string | symbol) => Function[];
224 rawListeners: (event: string | symbol) => Function[];
225 emit: (event: string | symbol, ...args: any[]) => boolean;
226 listenerCount: (event: string | symbol) => number;
227 prependListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
228 prependOnceListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
229 eventNames: () => (string | symbol)[];
230 };
231} & T;
232/**
233 * This interface describes additional Component properties
234 * allowing components to contribute Repository-related artifacts.
235 */
236export interface RepositoryComponent {
237 /**
238 * An optional list of Repository classes to bind for dependency injection
239 * via `app.repository()` API.
240 */
241 repositories?: Class<Repository<Model>>[];
242 /**
243 * An optional list of Model classes to bind for dependency injection
244 * via `app.model()` API.
245 */
246 models?: Class<Model>[];
247}
248/**
249 * Interface for an Application mixed in with RepositoryMixin
250 */
251export interface ApplicationWithRepositories extends Application {
252 repository<R extends Repository<any>>(repo: Class<R>, name?: string): Binding<R>;
253 getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R>;
254 dataSource<D extends juggler.DataSource>(dataSource: Class<D> | D, name?: string): Binding<D>;
255 model<M extends Class<unknown>>(modelClass: M): Binding<M>;
256 component(component: Class<unknown>, name?: string): Binding;
257 mountComponentRepositories(component: Class<unknown>): void;
258 migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
259}
260/**
261 * A dummy class created to generate the tsdoc for the members in repository
262 * mixin. Please don't use it.
263 *
264 * The members are implemented in function
265 * <a href="#RepositoryMixin">RepositoryMixin</a>
266 */
267export declare class RepositoryMixinDoc {
268 constructor(...args: any[]);
269 /**
270 * Add a repository to this application.
271 *
272 * @param repo - The repository to add.
273 *
274 * @example
275 * ```ts
276 *
277 * class NoteRepo {
278 * model: any;
279 *
280 * constructor() {
281 * const ds: juggler.DataSource = new juggler.DataSource({
282 * name: 'db',
283 * connector: 'memory',
284 * });
285 *
286 * this.model = ds.createModel(
287 * 'note',
288 * {title: 'string', content: 'string'},
289 * {}
290 * );
291 * }
292 * };
293 *
294 * app.repository(NoteRepo);
295 * ```
296 */
297 repository(repo: Class<Repository<any>>): Binding;
298 /**
299 * Retrieve the repository instance from the given Repository class
300 *
301 * @param repo - The repository class to retrieve the instance of
302 */
303 getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R>;
304 /**
305 * Add the dataSource to this application.
306 *
307 * @param dataSource - The dataSource to add.
308 * @param name - The binding name of the datasource; defaults to dataSource.name
309 *
310 * @example
311 * ```ts
312 *
313 * const ds: juggler.DataSource = new juggler.DataSource({
314 * name: 'db',
315 * connector: 'memory',
316 * });
317 *
318 * app.dataSource(ds);
319 *
320 * // The datasource can be injected with
321 * constructor(@inject('datasources.db') dataSource: DataSourceType) {
322 *
323 * }
324 * ```
325 */
326 dataSource(dataSource: Class<juggler.DataSource> | juggler.DataSource, name?: string): Binding;
327 /**
328 * Add a component to this application. Also mounts
329 * all the components repositories.
330 *
331 * @param component - The component to add.
332 *
333 * @example
334 * ```ts
335 *
336 * export class ProductComponent {
337 * controllers = [ProductController];
338 * repositories = [ProductRepo, UserRepo];
339 * providers = {
340 * [AUTHENTICATION_STRATEGY]: AuthStrategy,
341 * [AUTHORIZATION_ROLE]: Role,
342 * };
343 * };
344 *
345 * app.component(ProductComponent);
346 * ```
347 */
348 component(component: Class<{}>): Binding;
349 /**
350 * Get an instance of a component and mount all it's
351 * repositories. This function is intended to be used internally
352 * by component()
353 *
354 * @param component - The component to mount repositories of
355 */
356 mountComponentRepository(component: Class<{}>): void;
357 /**
358 * Update or recreate the database schema for all repositories.
359 *
360 * **WARNING**: By default, `migrateSchema()` will attempt to preserve data
361 * while updating the schema in your target database, but this is not
362 * guaranteed to be safe.
363 *
364 * Please check the documentation for your specific connector(s) for
365 * a detailed breakdown of behaviors for automigrate!
366 *
367 * @param options - Migration options, e.g. whether to update tables
368 * preserving data or rebuild everything from scratch.
369 */
370 migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
371}
372/**
373 * Create a binding for the given model class
374 * @param modelClass - Model class
375 */
376export declare function createModelClassBinding<M extends Class<unknown>>(modelClass: M): Binding<M>;
377
\No newline at end of file