UNPKG

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