UNPKG

18 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';
6import * as loopbackContext from '@loopback/core';
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 | undefined): 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 | undefined): 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 | undefined): 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: loopbackContext.ApplicationConfig;
151 readonly state: string;
152 controller: <T_1>(controllerCtor: loopbackContext.ControllerClass<T_1>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_1>;
153 server: <T_2 extends loopbackContext.Server>(ctor: Constructor<T_2>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_2>;
154 servers: <T_3 extends loopbackContext.Server>(ctors: Constructor<T_3>[]) => Binding<any>[];
155 getServer: <T_4 extends loopbackContext.Server>(target: string | Constructor<T_4>) => Promise<T_4>;
156 init: () => Promise<void>;
157 onInit: (fn: () => loopbackContext.ValueOrPromise<void>) => Binding<loopbackContext.LifeCycleObserver>;
158 start: () => Promise<void>;
159 onStart: (fn: () => loopbackContext.ValueOrPromise<void>) => Binding<loopbackContext.LifeCycleObserver>;
160 stop: () => Promise<void>;
161 onStop: (fn: () => loopbackContext.ValueOrPromise<void>) => Binding<loopbackContext.LifeCycleObserver>;
162 setMetadata: (metadata: loopbackContext.ApplicationMetadata) => void;
163 lifeCycleObserver: <T_5 extends loopbackContext.LifeCycleObserver>(ctor: Constructor<T_5>, nameOrOptions?: string | BindingFromClassOptions | undefined) => Binding<T_5>;
164 service: <S>(cls: loopbackContext.ServiceOrProviderClass<S>, nameOrOptions?: string | loopbackContext.ServiceOptions | undefined) => Binding<S>;
165 interceptor: (interceptor: loopbackContext.Interceptor | Constructor<loopbackContext.Provider<loopbackContext.Interceptor>>, nameOrOptions?: string | loopbackContext.InterceptorBindingOptions | undefined) => Binding<loopbackContext.Interceptor>;
166 readonly name: string;
167 readonly subscriptionManager: loopbackContext.ContextSubscriptionManager;
168 scope: BindingScope;
169 readonly parent: loopbackContext.Context | undefined;
170 emitEvent: <T_6 extends loopbackContext.ContextEvent>(type: string, event: T_6) => void;
171 emitError: (err: unknown) => void;
172 bind: <ValueType = any>(key: loopbackContext.BindingAddress<ValueType>) => Binding<ValueType>;
173 add: (binding: Binding<unknown>) => Application;
174 configure: <ConfigValueType = any>(key?: loopbackContext.BindingAddress<unknown> | undefined) => Binding<ConfigValueType>;
175 getConfigAsValueOrPromise: <ConfigValueType_1>(key: loopbackContext.BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: loopbackContext.ResolutionOptions | undefined) => loopbackContext.ValueOrPromise<ConfigValueType_1 | undefined>;
176 getConfig: <ConfigValueType_2>(key: loopbackContext.BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: loopbackContext.ResolutionOptions | undefined) => Promise<ConfigValueType_2 | undefined>;
177 getConfigSync: <ConfigValueType_3>(key: loopbackContext.BindingAddress<unknown>, propertyPath?: string | undefined, resolutionOptions?: loopbackContext.ResolutionOptions | undefined) => ConfigValueType_3 | undefined;
178 unbind: (key: loopbackContext.BindingAddress<unknown>) => boolean;
179 subscribe: (observer: loopbackContext.ContextEventObserver) => loopbackContext.Subscription;
180 unsubscribe: (observer: loopbackContext.ContextEventObserver) => boolean;
181 close: () => void;
182 isSubscribed: (observer: loopbackContext.ContextObserver) => boolean;
183 createView: <T_7 = unknown>(filter: loopbackContext.BindingFilter, comparator?: loopbackContext.BindingComparator | undefined, options?: Omit<loopbackContext.ResolutionOptions, "session"> | undefined) => loopbackContext.ContextView<T_7>;
184 contains: (key: loopbackContext.BindingAddress<unknown>) => boolean;
185 isBound: (key: loopbackContext.BindingAddress<unknown>) => boolean;
186 getOwnerContext: (keyOrBinding: loopbackContext.BindingAddress<unknown> | Readonly<Binding<unknown>>) => loopbackContext.Context | undefined;
187 getScopedContext: (scope: BindingScope.APPLICATION | BindingScope.SERVER | BindingScope.REQUEST) => loopbackContext.Context | undefined;
188 getResolutionContext: (binding: Readonly<Binding<unknown>>) => loopbackContext.Context | undefined;
189 isVisibleTo: (ctx: loopbackContext.Context) => boolean;
190 find: <ValueType_1 = any>(pattern?: string | RegExp | loopbackContext.BindingFilter | undefined) => Readonly<Binding<ValueType_1>>[];
191 findByTag: <ValueType_2 = any>(tagFilter: RegExp | loopbackContext.BindingTag) => Readonly<Binding<ValueType_2>>[];
192 get: {
193 <ValueType_3>(keyWithPath: loopbackContext.BindingAddress<ValueType_3>, session?: loopbackContext.ResolutionSession | undefined): Promise<ValueType_3>;
194 <ValueType_4>(keyWithPath: loopbackContext.BindingAddress<ValueType_4>, options: loopbackContext.ResolutionOptions): Promise<ValueType_4 | undefined>;
195 };
196 getSync: {
197 <ValueType_5>(keyWithPath: loopbackContext.BindingAddress<ValueType_5>, session?: loopbackContext.ResolutionSession | undefined): ValueType_5;
198 <ValueType_6>(keyWithPath: loopbackContext.BindingAddress<ValueType_6>, options?: loopbackContext.ResolutionOptions | undefined): ValueType_6 | undefined;
199 };
200 getBinding: {
201 <ValueType_7 = any>(key: loopbackContext.BindingAddress<ValueType_7>): Binding<ValueType_7>;
202 <ValueType_8>(key: loopbackContext.BindingAddress<ValueType_8>, options?: {
203 optional?: boolean | undefined;
204 } | undefined): Binding<ValueType_8> | undefined;
205 };
206 findOrCreateBinding: <T_8>(key: loopbackContext.BindingAddress<T_8>, policy?: loopbackContext.BindingCreationPolicy | undefined) => Binding<T_8>;
207 getValueOrPromise: <ValueType_9>(keyWithPath: loopbackContext.BindingAddress<ValueType_9>, optionsOrSession?: loopbackContext.ResolutionOptionsOrSession | undefined) => loopbackContext.ValueOrPromise<ValueType_9 | undefined>;
208 toJSON: () => loopbackContext.JSONObject;
209 inspect: (options?: loopbackContext.ContextInspectOptions | undefined) => loopbackContext.JSONObject;
210 on: {
211 (eventName: "bind" | "unbind", listener: loopbackContext.ContextEventListener): Application;
212 (event: string | symbol, listener: (...args: any[]) => void): Application;
213 };
214 once: {
215 (eventName: "bind" | "unbind", listener: loopbackContext.ContextEventListener): Application;
216 (event: string | symbol, listener: (...args: any[]) => void): Application;
217 };
218 addListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
219 removeListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
220 off: (event: string | symbol, listener: (...args: any[]) => void) => Application;
221 removeAllListeners: (event?: string | symbol | undefined) => Application;
222 setMaxListeners: (n: number) => Application;
223 getMaxListeners: () => number;
224 listeners: (event: string | symbol) => Function[];
225 rawListeners: (event: string | symbol) => Function[];
226 emit: (event: string | symbol, ...args: any[]) => boolean;
227 listenerCount: (type: string | symbol) => number;
228 prependListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
229 prependOnceListener: (event: string | symbol, listener: (...args: any[]) => void) => Application;
230 eventNames: () => (string | symbol)[];
231 };
232} & T;
233/**
234 * This interface describes additional Component properties
235 * allowing components to contribute Repository-related artifacts.
236 */
237export interface RepositoryComponent {
238 /**
239 * An optional list of Repository classes to bind for dependency injection
240 * via `app.repository()` API.
241 */
242 repositories?: Class<Repository<Model>>[];
243 /**
244 * An optional list of Model classes to bind for dependency injection
245 * via `app.model()` API.
246 */
247 models?: Class<Model>[];
248}
249/**
250 * Interface for an Application mixed in with RepositoryMixin
251 */
252export interface ApplicationWithRepositories extends Application {
253 repository<R extends Repository<any>>(repo: Class<R>, name?: string): Binding<R>;
254 getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R>;
255 dataSource<D extends juggler.DataSource>(dataSource: Class<D> | D, name?: string): Binding<D>;
256 model<M extends Class<unknown>>(modelClass: M): Binding<M>;
257 component(component: Class<unknown>, name?: string): Binding;
258 mountComponentRepositories(component: Class<unknown>): void;
259 migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
260}
261/**
262 * A dummy class created to generate the tsdoc for the members in repository
263 * mixin. Please don't use it.
264 *
265 * The members are implemented in function
266 * <a href="#RepositoryMixin">RepositoryMixin</a>
267 */
268export declare class RepositoryMixinDoc {
269 constructor(...args: any[]);
270 /**
271 * Add a repository to this application.
272 *
273 * @param repo - The repository to add.
274 *
275 * @example
276 * ```ts
277 *
278 * class NoteRepo {
279 * model: any;
280 *
281 * constructor() {
282 * const ds: juggler.DataSource = new juggler.DataSource({
283 * name: 'db',
284 * connector: 'memory',
285 * });
286 *
287 * this.model = ds.createModel(
288 * 'note',
289 * {title: 'string', content: 'string'},
290 * {}
291 * );
292 * }
293 * };
294 *
295 * app.repository(NoteRepo);
296 * ```
297 */
298 repository(repo: Class<Repository<any>>): Binding;
299 /**
300 * Retrieve the repository instance from the given Repository class
301 *
302 * @param repo - The repository class to retrieve the instance of
303 */
304 getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R>;
305 /**
306 * Add the dataSource to this application.
307 *
308 * @param dataSource - The dataSource to add.
309 * @param name - The binding name of the datasource; defaults to dataSource.name
310 *
311 * @example
312 * ```ts
313 *
314 * const ds: juggler.DataSource = new juggler.DataSource({
315 * name: 'db',
316 * connector: 'memory',
317 * });
318 *
319 * app.dataSource(ds);
320 *
321 * // The datasource can be injected with
322 * constructor(@inject('datasources.db') dataSource: DataSourceType) {
323 *
324 * }
325 * ```
326 */
327 dataSource(dataSource: Class<juggler.DataSource> | juggler.DataSource, name?: string): Binding;
328 /**
329 * Add a component to this application. Also mounts
330 * all the components repositories.
331 *
332 * @param component - The component to add.
333 *
334 * @example
335 * ```ts
336 *
337 * export class ProductComponent {
338 * controllers = [ProductController];
339 * repositories = [ProductRepo, UserRepo];
340 * providers = {
341 * [AUTHENTICATION_STRATEGY]: AuthStrategy,
342 * [AUTHORIZATION_ROLE]: Role,
343 * };
344 * };
345 *
346 * app.component(ProductComponent);
347 * ```
348 */
349 component(component: Class<{}>): Binding;
350 /**
351 * Get an instance of a component and mount all it's
352 * repositories. This function is intended to be used internally
353 * by component()
354 *
355 * @param component - The component to mount repositories of
356 */
357 mountComponentRepository(component: Class<{}>): void;
358 /**
359 * Update or recreate the database schema for all repositories.
360 *
361 * **WARNING**: By default, `migrateSchema()` will attempt to preserve data
362 * while updating the schema in your target database, but this is not
363 * guaranteed to be safe.
364 *
365 * Please check the documentation for your specific connector(s) for
366 * a detailed breakdown of behaviors for automigrate!
367 *
368 * @param options - Migration options, e.g. whether to update tables
369 * preserving data or rebuild everything from scratch.
370 */
371 migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
372}
373/**
374 * Create a binding for the given model class
375 * @param modelClass - Model class
376 */
377export declare function createModelClassBinding<M extends Class<unknown>>(modelClass: M): Binding<M>;
378
\No newline at end of file