@orion-js/dogs 3.11.15 → 4.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/index.cjs +114048 -0
  2. package/dist/index.d.ts +748 -0
  3. package/dist/index.js +114022 -0
  4. package/package.json +23 -22
  5. package/LICENSE +0 -21
  6. package/lib/defineJob/index.d.ts +0 -2
  7. package/lib/defineJob/index.js +0 -12
  8. package/lib/events.test.d.ts +0 -1
  9. package/lib/events.test.js +0 -133
  10. package/lib/history.test.d.ts +0 -1
  11. package/lib/history.test.js +0 -140
  12. package/lib/index.d.ts +0 -12
  13. package/lib/index.js +0 -40
  14. package/lib/recurrent.test.d.ts +0 -1
  15. package/lib/recurrent.test.js +0 -47
  16. package/lib/repos/JobsHistoryRepo.d.ts +0 -7
  17. package/lib/repos/JobsHistoryRepo.js +0 -67
  18. package/lib/repos/JobsRepo.d.ts +0 -19
  19. package/lib/repos/JobsRepo.js +0 -158
  20. package/lib/service/index.d.ts +0 -10
  21. package/lib/service/index.js +0 -50
  22. package/lib/service/index.test.d.ts +0 -1
  23. package/lib/service/index.test.js +0 -51
  24. package/lib/services/EventsService.d.ts +0 -5
  25. package/lib/services/EventsService.js +0 -36
  26. package/lib/services/Executor.d.ts +0 -20
  27. package/lib/services/Executor.js +0 -195
  28. package/lib/services/WorkerService.d.ts +0 -16
  29. package/lib/services/WorkerService.js +0 -142
  30. package/lib/services/WorkerService.test.d.ts +0 -1
  31. package/lib/services/WorkerService.test.js +0 -10
  32. package/lib/services/getNextRunDate.d.ts +0 -9
  33. package/lib/services/getNextRunDate.js +0 -19
  34. package/lib/stale.test.d.ts +0 -1
  35. package/lib/stale.test.js +0 -108
  36. package/lib/tests/setup.d.ts +0 -1
  37. package/lib/tests/setup.js +0 -19
  38. package/lib/types/Events.d.ts +0 -21
  39. package/lib/types/Events.js +0 -2
  40. package/lib/types/HistoryRecord.d.ts +0 -21
  41. package/lib/types/HistoryRecord.js +0 -83
  42. package/lib/types/JobRecord.d.ts +0 -13
  43. package/lib/types/JobRecord.js +0 -59
  44. package/lib/types/JobsDefinition.d.ts +0 -61
  45. package/lib/types/JobsDefinition.js +0 -2
  46. package/lib/types/StartConfig.d.ts +0 -28
  47. package/lib/types/StartConfig.js +0 -2
  48. package/lib/types/Worker.d.ts +0 -38
  49. package/lib/types/Worker.js +0 -2
  50. package/lib/types/index.d.ts +0 -6
  51. package/lib/types/index.js +0 -22
@@ -0,0 +1,748 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ import * as MongoDB from 'mongodb';
4
+ import { Db, MongoClient } from 'mongodb';
5
+
6
+ export type Blackbox = {
7
+ [name: string]: any;
8
+ };
9
+ export type LogFunction = (message: string, metadata?: any) => void;
10
+ export interface OrionLogger {
11
+ debug: LogFunction;
12
+ info: LogFunction;
13
+ warn: LogFunction;
14
+ error: LogFunction;
15
+ addContext: (module: NodeJS.Module) => OrionLogger;
16
+ addMetadata: (metadata: any) => OrionLogger;
17
+ }
18
+ export interface JobToRun {
19
+ jobId: string;
20
+ executionId: string;
21
+ name: string;
22
+ type: "event" | "recurrent";
23
+ params: Blackbox;
24
+ tries: number;
25
+ lockTime: number;
26
+ priority: number;
27
+ uniqueIdentifier?: string;
28
+ }
29
+ export interface ExecutionContext {
30
+ record: JobToRun;
31
+ definition: JobDefinition;
32
+ tries: number;
33
+ logger: OrionLogger;
34
+ extendLockTime: (extraTime: number) => Promise<void>;
35
+ clearStaleTimeout: () => void;
36
+ }
37
+ export interface WorkerInstance {
38
+ running: boolean;
39
+ workerIndex: number;
40
+ stop: () => Promise<void>;
41
+ respawn: () => Promise<void>;
42
+ promise?: Promise<any>;
43
+ }
44
+ export interface WorkersInstance {
45
+ running: boolean;
46
+ workersCount: number;
47
+ workers: WorkerInstance[];
48
+ /**
49
+ * Stop all workers and wait for them to finish
50
+ */
51
+ stop: () => Promise<void>;
52
+ }
53
+ export interface JobRetryResultBase {
54
+ action: "retry" | "dismiss";
55
+ }
56
+ export type JobRetryResultRunIn = JobRetryResultBase & {
57
+ runIn: number;
58
+ };
59
+ export type JobRetryResultRunAt = JobRetryResultBase & {
60
+ runAt: Date;
61
+ };
62
+ export type JobRetryResult = JobRetryResultRunIn | JobRetryResultRunAt | JobRetryResultBase;
63
+ export interface BaseJobDefinition {
64
+ /**
65
+ * The function to execute when the job is executed.
66
+ */
67
+ resolve: (params: Blackbox, context: ExecutionContext) => Promise<Blackbox | void>;
68
+ /**
69
+ * Called if the job fails.
70
+ */
71
+ onError?: (error: Error, params: Blackbox, context: ExecutionContext) => Promise<JobRetryResult>;
72
+ /**
73
+ * Called if the job locktime is expired. The job will be executed again.
74
+ */
75
+ onStale?: (params: Blackbox, context: ExecutionContext) => Promise<void>;
76
+ /**
77
+ * Save the executions of the job time in milliseconds. Default is 1 week. Set to 0 to disable.
78
+ */
79
+ saveExecutionsFor?: number;
80
+ }
81
+ export interface RecurrentJobDefinition extends BaseJobDefinition {
82
+ /**
83
+ * Type of the job.
84
+ */
85
+ type: "recurrent";
86
+ /**
87
+ * A function executed after each execution that returns the date of the next run.
88
+ */
89
+ getNextRun?: () => Date;
90
+ /**
91
+ * Run every x milliseconds. This will be ignored if getNextRun is defined.
92
+ */
93
+ runEvery?: number;
94
+ /**
95
+ * The priority of the job. Higher is more priority. Default is 100.
96
+ */
97
+ priority?: number;
98
+ }
99
+ export interface EventJobDefinition extends BaseJobDefinition {
100
+ /**
101
+ * Type of the job.
102
+ */
103
+ type: "event";
104
+ }
105
+ export type JobDefinition = RecurrentJobDefinition | EventJobDefinition;
106
+ export type JobDefinitionWithName = JobDefinition & {
107
+ name: string;
108
+ };
109
+ export interface JobsDefinition {
110
+ [jobName: string]: JobDefinition;
111
+ }
112
+ export declare const defineJob: (options: JobDefinition) => JobDefinition;
113
+ export type LogLevels = "debug" | "info" | "warn" | "error" | "none";
114
+ export interface StartWorkersConfig {
115
+ /**
116
+ * Object map of the jobs that this workers will execute
117
+ */
118
+ jobs: JobsDefinition;
119
+ /**
120
+ * Time in milliseconds to wait between each look without results for a job
121
+ * to run at the database. Default is 3000.
122
+ */
123
+ pollInterval: number;
124
+ /**
125
+ * Time in milliseconds to wait too look for a job after a job execution. Default is 100.
126
+ */
127
+ cooldownPeriod: number;
128
+ /**
129
+ * Number of workers to start. Default is 1.
130
+ */
131
+ workersCount: number;
132
+ /**
133
+ * Time in milliseconds to lock a job for execution. Default is 30000 (30 seconds).
134
+ * If a job is locked for longer than this time, it will be considered as failed.
135
+ * This is to prevent a job from being executed multiple times at the same time.
136
+ * You can extend this time inside a job by calling extendLockTime from context.
137
+ */
138
+ lockTime: number;
139
+ }
140
+ export interface ScheduleJobOptionsBase {
141
+ name: string;
142
+ params?: Blackbox;
143
+ priority?: number;
144
+ uniqueIdentifier?: string;
145
+ }
146
+ export type ScheduleJobOptionsRunIn = ScheduleJobOptionsBase & {
147
+ runIn: number;
148
+ };
149
+ export type ScheduleJobOptionsRunAt = ScheduleJobOptionsBase & {
150
+ runAt: Date;
151
+ };
152
+ export type ScheduleJobOptions = ScheduleJobOptionsRunIn | ScheduleJobOptionsRunAt | ScheduleJobOptionsBase;
153
+ export interface ScheduleJobRecordOptions {
154
+ name: string;
155
+ params: Blackbox;
156
+ nextRunAt: Date;
157
+ priority: number;
158
+ uniqueIdentifier?: string;
159
+ }
160
+ // Generated by dts-bundle-generator v9.5.1
161
+ // Generated by dts-bundle-generator v9.5.1
162
+ // Generated by dts-bundle-generator v9.5.1
163
+ export interface StoredCacheData {
164
+ value: any;
165
+ expires?: Date;
166
+ }
167
+ export interface SetCacheOptions {
168
+ ttl?: number;
169
+ }
170
+ export interface GetCacheOptions {
171
+ ttl?: number;
172
+ fallback?(): Promise<any>;
173
+ }
174
+ export interface OrionCache {
175
+ /**
176
+ * Save data in the cache
177
+ */
178
+ set(key: string, value: any, options?: SetCacheOptions): Promise<void> | void;
179
+ /**
180
+ * Get data from the cache
181
+ */
182
+ get(key: string, options?: GetCacheOptions): Promise<StoredCacheData>;
183
+ /**
184
+ * Removes data from the cache
185
+ */
186
+ invalidate(key: string): Promise<void> | void;
187
+ }
188
+ type Blackbox$1 = {
189
+ [name: string]: any;
190
+ };
191
+ export type ModelResolverResolve = (item: any, params: any, viewer: any, info?: any) => Promise<any>;
192
+ export type GlobalCheckPermissions = (params: any, viewer: any, info?: any) => Promise<string | void>;
193
+ export type ModelCheckPermissions = (parent: any, params: any, viewer: any, info?: any) => Promise<string | void>;
194
+ export type GlobalGetCacheKey = (params: any, viewer: any, info: any) => Promise<any>;
195
+ export type ModelGetCacheKey = (parent: any, params: any, viewer: any, info: any) => Promise<any>;
196
+ export interface ExecuteOptions {
197
+ params: Blackbox$1;
198
+ viewer: any;
199
+ parent?: any;
200
+ info?: any;
201
+ options: ResolverOptions;
202
+ }
203
+ export type Parameters$1<T> = T extends (...args: infer P) => any ? P : never;
204
+ export type ReturnType$1<T> = T extends (...args: any) => infer R ? R : any;
205
+ export type ResolverParams<Resolve, IsModel> = IsModel extends undefined ? Parameters$1<Resolve>[0] : Parameters$1<Resolve>[1];
206
+ export interface ExecuteParams<Resolve = Function, IsModel = undefined> {
207
+ params?: ResolverParams<Resolve, IsModel>;
208
+ viewer?: any;
209
+ parent?: IsModel extends undefined ? undefined : Parameters$1<Resolve>[0];
210
+ info?: any;
211
+ }
212
+ export type Execute<Resolve = Function, IsModel = undefined> = (executeOptions: ExecuteParams<Resolve, IsModel>) => ReturnType$1<Resolve>;
213
+ export interface SharedResolverOptions {
214
+ resolverId?: string;
215
+ params?: any;
216
+ returns?: any;
217
+ mutation?: boolean;
218
+ private?: boolean;
219
+ checkPermission?: GlobalCheckPermissions | ModelCheckPermissions;
220
+ getCacheKey?: GlobalGetCacheKey | ModelGetCacheKey;
221
+ cache?: number;
222
+ cacheProvider?: OrionCache;
223
+ permissionsOptions?: any;
224
+ middlewares?: ResolverMiddleware[];
225
+ }
226
+ export interface ResolverOptions<Resolve = Function> extends SharedResolverOptions {
227
+ resolve: Resolve;
228
+ }
229
+ export type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never;
230
+ export interface Resolver<Resolve = Function, IsModel = undefined> extends SharedResolverOptions {
231
+ execute: Execute<Resolve, IsModel>;
232
+ resolve: Resolve;
233
+ modelResolve: IsModel extends undefined ? undefined : OmitFirstArg<Resolve>;
234
+ }
235
+ export type ModelResolver<Resolve = Function> = Resolver<Resolve, true>;
236
+ export type ResolverMiddleware = (executeOptions: ExecuteOptions, next: () => Promise<any>) => Promise<any>;
237
+ export interface FieldType {
238
+ name: string;
239
+ validate: ValidateFunction;
240
+ clean: CleanFunction;
241
+ meta?: any;
242
+ toGraphQLType?: (GraphQL: any) => any;
243
+ _isFieldType: boolean;
244
+ }
245
+ export type Constructor<T> = new (...args: any[]) => T;
246
+ export type FieldTypesList = "string" | "date" | "integer" | "number" | "ID" | "boolean" | "email" | "blackbox" | "any";
247
+ export type TypedModelOnSchema = Function;
248
+ export type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date>;
249
+ export type SchemaRecursiveNodeTypeExtras = {
250
+ _isFieldType?: boolean;
251
+ __clean?: CleanFunction;
252
+ __validate?: ValidateFunction;
253
+ __skipChildValidation?: (value: any, info: CurrentNodeInfo) => Promise<boolean>;
254
+ };
255
+ export interface Schema {
256
+ [key: string]: SchemaNode | Function;
257
+ }
258
+ export type SchemaRecursiveNodeType = Schema & SchemaRecursiveNodeTypeExtras;
259
+ export type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType | TypedModelOnSchema;
260
+ export type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
261
+ export type ValidateFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | void | Promise<object | string | void>;
262
+ export type CleanFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => any | Promise<any>;
263
+ export interface SchemaNode {
264
+ /**
265
+ * The type of the field. Used for type validations. Can also contain a subschema.
266
+ */
267
+ type: SchemaMetaFieldType;
268
+ /**
269
+ * Defaults to false
270
+ */
271
+ optional?: boolean;
272
+ allowedValues?: Array<any>;
273
+ defaultValue?: ((info: CurrentNodeInfo, ...args: any[]) => any | Promise<any>) | any;
274
+ /**
275
+ * Function that takes a value and returns an error message if there are any errors. Must return null or undefined otherwise.
276
+ */
277
+ validate?: ValidateFunction;
278
+ /**
279
+ * Function that preprocesses a value before it is set.
280
+ */
281
+ clean?: CleanFunction;
282
+ autoValue?: (value: any, info: CurrentNodeInfo, ...args: any[]) => any | Promise<any>;
283
+ /**
284
+ * The minimum value if it's a number, the minimum length if it's a string or array.
285
+ */
286
+ min?: number;
287
+ /**
288
+ * The maximum value if it's a number, the maximum length if it's a string or array.
289
+ */
290
+ max?: number;
291
+ /**
292
+ * Internal use only.
293
+ */
294
+ isBlackboxChild?: boolean;
295
+ /**
296
+ * @deprecated
297
+ */
298
+ custom?: ValidateFunction;
299
+ /**
300
+ * Used in GraphQL. If true, the field will be omitted from the schema.
301
+ */
302
+ private?: boolean;
303
+ /**
304
+ * Used in GraphQL. When in GraphQL, this resolver will replace the static field.
305
+ */
306
+ graphQLResolver?: (...args: any) => any;
307
+ /**
308
+ * Used in GraphQL. Sets the key of the field in the GraphQL schema. You must set this value when building your schema.
309
+ */
310
+ key?: string;
311
+ /**
312
+ * The name that would be displayed in a front-end form
313
+ */
314
+ label?: string;
315
+ /**
316
+ * The description that would be displayed in a front-end form
317
+ */
318
+ description?: string;
319
+ /**
320
+ * The placeholder that would be displayed in a front-end form
321
+ */
322
+ placeholder?: string;
323
+ /**
324
+ * The field type that would be used in a front-end form
325
+ */
326
+ fieldType?: string;
327
+ /**
328
+ * The field options that will be passed as props to the front-end field
329
+ */
330
+ fieldOptions?: any;
331
+ }
332
+ export interface CurrentNodeInfoOptions {
333
+ autoConvert?: boolean;
334
+ filter?: boolean;
335
+ trimStrings?: boolean;
336
+ removeEmptyStrings?: boolean;
337
+ forceDoc?: any;
338
+ omitRequired?: boolean;
339
+ }
340
+ export interface CurrentNodeInfo {
341
+ /**
342
+ * The global schema, prefaced by {type: {...}} to be compatible with subschemas
343
+ * Sometimes it's given without {type: {...}}. TODO: Normalize this.
344
+ */
345
+ schema?: SchemaNode | Schema;
346
+ /**
347
+ * The current node subschema
348
+ */
349
+ currentSchema?: Partial<SchemaNode>;
350
+ value: any;
351
+ doc?: any;
352
+ currentDoc?: any;
353
+ options?: CurrentNodeInfoOptions;
354
+ args?: any[];
355
+ type?: SchemaMetaFieldType;
356
+ keys?: string[];
357
+ addError?: (keys: string[], code: string | object) => void;
358
+ }
359
+ export interface ModelResolversMap {
360
+ [key: string]: ModelResolver<ModelResolverResolve>;
361
+ }
362
+ export interface CloneOptions {
363
+ name: string;
364
+ omitFields?: string[];
365
+ pickFields?: string[];
366
+ mapFields?: (field: any, key: string) => any;
367
+ extendSchema?: Schema;
368
+ extendResolvers?: ModelResolversMap;
369
+ }
370
+ export interface Model<TSchema = any> {
371
+ __isModel: boolean;
372
+ /**
373
+ * The name of the model, used for example for GraphQL
374
+ */
375
+ name: string;
376
+ /**
377
+ * Returns the schema of the model
378
+ */
379
+ getSchema: () => Schema & {
380
+ __model: Model;
381
+ };
382
+ /**
383
+ * Returns the schema without adding __model to the schema
384
+ */
385
+ getCleanSchema: () => Schema;
386
+ /**
387
+ * Returns the model resolvers
388
+ */
389
+ getResolvers: () => ModelResolversMap;
390
+ /**
391
+ * Adds the model resolvers to a item
392
+ */
393
+ initItem: (item: any) => any;
394
+ /**
395
+ * Validates an item using @orion-js/schema
396
+ */
397
+ validate: (item: any) => Promise<any>;
398
+ /**
399
+ * Cleans an item using @orion-js/schema
400
+ */
401
+ clean: (item: any) => Promise<TSchema>;
402
+ /**
403
+ * Cleans and validates an item using @orion-js/schema
404
+ */
405
+ cleanAndValidate: (item: any) => Promise<TSchema>;
406
+ /**
407
+ * Creates a new model using this one as a base
408
+ */
409
+ clone: (cloneOptions: CloneOptions) => Model;
410
+ /**
411
+ * The type of the model. Only use this in typescript
412
+ */
413
+ type: TSchema;
414
+ }
415
+ export interface FieldType$1 {
416
+ name: string;
417
+ validate: ValidateFunction$1;
418
+ clean: CleanFunction$1;
419
+ meta?: any;
420
+ toGraphQLType?: (GraphQL: any) => any;
421
+ _isFieldType: boolean;
422
+ }
423
+ export type Constructor$1<T> = new (...args: any[]) => T;
424
+ export type Blackbox$1 = {
425
+ [name: string]: any;
426
+ };
427
+ export type FieldTypesList$1 = "string" | "date" | "integer" | "number" | "ID" | "boolean" | "email" | "blackbox" | "any";
428
+ export type TypedModelOnSchema$1 = Function;
429
+ export type ConstructorsTypesList$1 = Constructor$1<String> | Constructor$1<Number> | Constructor$1<Boolean> | Constructor$1<Date>;
430
+ export type SchemaRecursiveNodeTypeExtras$1 = {
431
+ _isFieldType?: boolean;
432
+ __clean?: CleanFunction$1;
433
+ __validate?: ValidateFunction$1;
434
+ __skipChildValidation?: (value: any, info: CurrentNodeInfo$1) => Promise<boolean>;
435
+ };
436
+ export interface Schema$1 {
437
+ [key: string]: SchemaNode$1 | Function;
438
+ }
439
+ export type SchemaRecursiveNodeType$1 = Schema$1 & SchemaRecursiveNodeTypeExtras$1;
440
+ export type SchemaMetaFieldTypeSingle$1 = FieldTypesList$1 | ConstructorsTypesList$1 | SchemaRecursiveNodeType$1 | FieldType$1 | TypedModelOnSchema$1;
441
+ export type SchemaMetaFieldType$1 = SchemaMetaFieldTypeSingle$1 | SchemaMetaFieldTypeSingle$1[];
442
+ export type ValidateFunction$1 = (value: any, info?: Partial<CurrentNodeInfo$1>, ...args: any[]) => object | string | void | Promise<object | string | void>;
443
+ export type CleanFunction$1 = (value: any, info?: Partial<CurrentNodeInfo$1>, ...args: any[]) => any | Promise<any>;
444
+ export interface SchemaNode$1 {
445
+ /**
446
+ * The type of the field. Used for type validations. Can also contain a subschema.
447
+ */
448
+ type: SchemaMetaFieldType$1;
449
+ /**
450
+ * Defaults to false
451
+ */
452
+ optional?: boolean;
453
+ allowedValues?: Array<any>;
454
+ defaultValue?: ((info: CurrentNodeInfo$1, ...args: any[]) => any | Promise<any>) | any;
455
+ /**
456
+ * Function that takes a value and returns an error message if there are any errors. Must return null or undefined otherwise.
457
+ */
458
+ validate?: ValidateFunction$1;
459
+ /**
460
+ * Function that preprocesses a value before it is set.
461
+ */
462
+ clean?: CleanFunction$1;
463
+ autoValue?: (value: any, info: CurrentNodeInfo$1, ...args: any[]) => any | Promise<any>;
464
+ /**
465
+ * The minimum value if it's a number, the minimum length if it's a string or array.
466
+ */
467
+ min?: number;
468
+ /**
469
+ * The maximum value if it's a number, the maximum length if it's a string or array.
470
+ */
471
+ max?: number;
472
+ /**
473
+ * Internal use only.
474
+ */
475
+ isBlackboxChild?: boolean;
476
+ /**
477
+ * @deprecated
478
+ */
479
+ custom?: ValidateFunction$1;
480
+ /**
481
+ * Used in GraphQL. If true, the field will be omitted from the schema.
482
+ */
483
+ private?: boolean;
484
+ /**
485
+ * Used in GraphQL. When in GraphQL, this resolver will replace the static field.
486
+ */
487
+ graphQLResolver?: (...args: any) => any;
488
+ /**
489
+ * Used in GraphQL. Sets the key of the field in the GraphQL schema. You must set this value when building your schema.
490
+ */
491
+ key?: string;
492
+ /**
493
+ * The name that would be displayed in a front-end form
494
+ */
495
+ label?: string;
496
+ /**
497
+ * The description that would be displayed in a front-end form
498
+ */
499
+ description?: string;
500
+ /**
501
+ * The placeholder that would be displayed in a front-end form
502
+ */
503
+ placeholder?: string;
504
+ /**
505
+ * The field type that would be used in a front-end form
506
+ */
507
+ fieldType?: string;
508
+ /**
509
+ * The field options that will be passed as props to the front-end field
510
+ */
511
+ fieldOptions?: any;
512
+ }
513
+ export interface CurrentNodeInfoOptions$1 {
514
+ autoConvert?: boolean;
515
+ filter?: boolean;
516
+ trimStrings?: boolean;
517
+ removeEmptyStrings?: boolean;
518
+ forceDoc?: any;
519
+ omitRequired?: boolean;
520
+ }
521
+ export interface CurrentNodeInfo$1 {
522
+ /**
523
+ * The global schema, prefaced by {type: {...}} to be compatible with subschemas
524
+ * Sometimes it's given without {type: {...}}. TODO: Normalize this.
525
+ */
526
+ schema?: SchemaNode$1 | Schema$1;
527
+ /**
528
+ * The current node subschema
529
+ */
530
+ currentSchema?: Partial<SchemaNode$1>;
531
+ value: any;
532
+ doc?: any;
533
+ currentDoc?: any;
534
+ options?: CurrentNodeInfoOptions$1;
535
+ args?: any[];
536
+ type?: SchemaMetaFieldType$1;
537
+ keys?: string[];
538
+ addError?: (keys: string[], code: string | object) => void;
539
+ }
540
+ export interface OrionMongoClient {
541
+ client: MongoClient;
542
+ db: Db;
543
+ uri: string;
544
+ dbName: string;
545
+ connectionPromise: Promise<MongoClient>;
546
+ connectionName: string;
547
+ }
548
+ export type RemoveFunctions<T extends ModelClassBase> = Pick<T, {
549
+ [Key in keyof T]-?: T[Key] extends Function ? never : Key;
550
+ }[keyof T]> & {
551
+ _id: ModelClassBase["_id"];
552
+ };
553
+ export type ModelClassBase = {
554
+ _id: string;
555
+ } & Blackbox$1;
556
+ export type DocumentWithIdOptional<T extends ModelClassBase> = Omit<T, "_id"> & {
557
+ /**
558
+ * The ID of the document
559
+ */
560
+ _id?: T["_id"];
561
+ };
562
+ export type DocumentWithoutId<T> = Omit<T, "_id">;
563
+ export type ModelToDocumentType<ModelClass extends ModelClassBase> = RemoveFunctions<ModelClass>;
564
+ export type ModelToDocumentTypeWithoutId<ModelClass extends ModelClassBase> = DocumentWithoutId<ModelToDocumentType<ModelClass>>;
565
+ export type ModelToDocumentTypeWithIdOptional<ModelClass extends ModelClassBase> = DocumentWithIdOptional<ModelToDocumentType<ModelClass>>;
566
+ export type ModelToMongoSelector<ModelClass extends ModelClassBase> = MongoSelector<ModelToDocumentType<ModelClass>>;
567
+ export type ModelToUpdateFilter<ModelClass extends ModelClassBase> = MongoDB.UpdateFilter<ModelToDocumentTypeWithoutId<ModelClass>> | Partial<ModelToDocumentTypeWithoutId<ModelClass>>;
568
+ export interface CollectionIndex {
569
+ keys: MongoDB.IndexSpecification;
570
+ options?: MongoDB.CreateIndexesOptions;
571
+ }
572
+ declare namespace DataLoader {
573
+ interface LoadDataOptionsBase<ModelClass extends ModelClassBase> {
574
+ key: keyof ModelClass;
575
+ match?: MongoFilter<ModelClass>;
576
+ sort?: MongoDB.Sort;
577
+ project?: MongoDB.Document;
578
+ timeout?: number;
579
+ debug?: boolean;
580
+ }
581
+ export interface LoadDataOptions<ModelClass extends ModelClassBase> extends LoadDataOptionsBase<ModelClass> {
582
+ value?: any;
583
+ values?: Array<any>;
584
+ }
585
+ export interface LoadOneOptions<ModelClass extends ModelClassBase> extends LoadDataOptionsBase<ModelClass> {
586
+ value: any;
587
+ }
588
+ export type LoadData<ModelClass extends ModelClassBase> = (options: LoadDataOptions<ModelClass>) => Promise<Array<ModelClass>>;
589
+ export type LoadOne<ModelClass extends ModelClassBase> = (options: LoadOneOptions<ModelClass>) => Promise<ModelClass>;
590
+ export type LoadMany<ModelClass extends ModelClassBase> = (options: LoadDataOptions<ModelClass>) => Promise<Array<ModelClass>>;
591
+ export type LoadById<ModelClass extends ModelClassBase> = (id: ModelClass["_id"]) => Promise<ModelClass>;
592
+ export {};
593
+ }
594
+ export type MongoFilter<ModelClass extends ModelClassBase = ModelClassBase> = MongoDB.Filter<ModelClass> & ({
595
+ _id?: ModelClass["_id"];
596
+ } | {
597
+ _id?: {
598
+ $in: ModelClass["_id"][];
599
+ };
600
+ });
601
+ export type MongoSelector<ModelClass extends ModelClassBase = ModelClassBase> = ModelClass["_id"] | MongoFilter<ModelClass>;
602
+ export interface FindCursor<ModelClass> extends MongoDB.FindCursor {
603
+ toArray: () => Promise<Array<ModelClass>>;
604
+ }
605
+ export interface UpdateOptions {
606
+ clean?: boolean;
607
+ validate?: boolean;
608
+ mongoOptions?: MongoDB.UpdateOptions;
609
+ }
610
+ export interface FindOneAndUpdateUpdateOptions {
611
+ clean?: boolean;
612
+ validate?: boolean;
613
+ mongoOptions?: MongoDB.FindOneAndUpdateOptions;
614
+ }
615
+ export interface InsertOptions {
616
+ clean?: boolean;
617
+ validate?: boolean;
618
+ mongoOptions?: MongoDB.InsertOneOptions;
619
+ }
620
+ export type InitItem<ModelClass extends ModelClassBase> = (doc: any) => ModelClass;
621
+ export type FindOne<ModelClass extends ModelClassBase> = (selector?: ModelToMongoSelector<ModelClass>, options?: MongoDB.FindOptions) => Promise<ModelClass>;
622
+ export type Find<ModelClass extends ModelClassBase> = (selector?: ModelToMongoSelector<ModelClass>, options?: MongoDB.FindOptions) => FindCursor<ModelClass>;
623
+ export type FindOneAndUpdate<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: FindOneAndUpdateUpdateOptions) => Promise<ModelClass>;
624
+ export type UpdateAndFind<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: FindOneAndUpdateUpdateOptions) => Promise<ModelClass>;
625
+ export type UpdateItem<ModelClass extends ModelClassBase> = (item: ModelClass, modifier: ModelToUpdateFilter<ModelClass>, options?: FindOneAndUpdateUpdateOptions) => Promise<void>;
626
+ export type InsertOne<ModelClass extends ModelClassBase> = (doc: ModelToDocumentTypeWithIdOptional<ModelClass>, options?: InsertOptions) => Promise<ModelClass["_id"]>;
627
+ export type InsertMany<ModelClass extends ModelClassBase> = (doc: Array<ModelToDocumentTypeWithIdOptional<ModelClass>>, options?: InsertOptions) => Promise<Array<ModelClass["_id"]>>;
628
+ export type InsertAndFind<ModelClass extends ModelClassBase> = (doc: ModelToDocumentTypeWithIdOptional<ModelClass>, options?: InsertOptions) => Promise<ModelClass>;
629
+ export type DeleteMany<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
630
+ export type DeleteOne<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.DeleteOptions) => Promise<MongoDB.DeleteResult>;
631
+ export type UpdateOne<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
632
+ export type UpdateMany<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult | MongoDB.Document>;
633
+ export type Upsert<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, modifier: ModelToUpdateFilter<ModelClass>, options?: UpdateOptions) => Promise<MongoDB.UpdateResult>;
634
+ export type EstimatedDocumentCount<ModelClass extends ModelClassBase> = (options?: MongoDB.EstimatedDocumentCountOptions) => Promise<number>;
635
+ export type CountDocuments<ModelClass extends ModelClassBase> = (selector: ModelToMongoSelector<ModelClass>, options?: MongoDB.CountDocumentsOptions) => Promise<number>;
636
+ declare class Collection<ModelClass extends ModelClassBase = ModelClassBase> {
637
+ name: string;
638
+ connectionName?: string;
639
+ schema?: Schema$1;
640
+ /**
641
+ * @deprecated Use schema instead. If you use model, all items will be initialized with the model to add resolvers (which are also deprecated)
642
+ */
643
+ model?: Model;
644
+ indexes: Array<CollectionIndex>;
645
+ generateId: () => ModelClass["_id"];
646
+ getSchema: () => Schema$1;
647
+ db: MongoDB.Db;
648
+ client: OrionMongoClient;
649
+ rawCollection: MongoDB.Collection<ModelClass>;
650
+ initItem: InitItem<ModelClass>;
651
+ findOne: FindOne<ModelClass>;
652
+ find: Find<ModelClass>;
653
+ insertOne: InsertOne<ModelClass>;
654
+ insertMany: InsertMany<ModelClass>;
655
+ insertAndFind: InsertAndFind<ModelClass>;
656
+ deleteMany: DeleteMany<ModelClass>;
657
+ deleteOne: DeleteOne<ModelClass>;
658
+ updateOne: UpdateOne<ModelClass>;
659
+ updateMany: UpdateMany<ModelClass>;
660
+ upsert: Upsert<ModelClass>;
661
+ findOneAndUpdate: FindOneAndUpdate<ModelClass>;
662
+ /**
663
+ * Updates a document and returns the updated document with the changes
664
+ */
665
+ updateAndFind: UpdateAndFind<ModelClass>;
666
+ updateItem: UpdateItem<ModelClass>;
667
+ estimatedDocumentCount: EstimatedDocumentCount<ModelClass>;
668
+ countDocuments: CountDocuments<ModelClass>;
669
+ aggregate: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.AggregateOptions) => MongoDB.AggregationCursor<T>;
670
+ watch: <T = MongoDB.Document>(pipeline?: MongoDB.Document[], options?: MongoDB.ChangeStreamOptions) => MongoDB.ChangeStream<T>;
671
+ loadData: DataLoader.LoadData<ModelClass>;
672
+ loadOne: DataLoader.LoadOne<ModelClass>;
673
+ loadMany: DataLoader.LoadMany<ModelClass>;
674
+ loadById: DataLoader.LoadById<ModelClass>;
675
+ /**
676
+ * Use this function if you are using tests and you pass the
677
+ * env var DONT_CREATE_INDEXES_AUTOMATICALLY and you need to
678
+ * create the indexes for this collection
679
+ */
680
+ createIndexes: () => Promise<string[]>;
681
+ createIndexesPromise: Promise<string[]>;
682
+ connectionPromise: Promise<MongoDB.MongoClient>;
683
+ }
684
+ export declare class HistoryRecord {
685
+ _id: string;
686
+ jobId: string;
687
+ executionId: string;
688
+ jobName: string;
689
+ type: "recurrent" | "event";
690
+ priority: number;
691
+ tries: number;
692
+ uniqueIdentifier?: string;
693
+ startedAt: Date;
694
+ endedAt: Date;
695
+ duration: number;
696
+ expiresAt?: Date;
697
+ status: "success" | "error" | "stale";
698
+ errorMessage?: string;
699
+ params?: Blackbox;
700
+ result?: Blackbox;
701
+ }
702
+ declare class JobsHistoryRepo {
703
+ history: () => Collection<HistoryRecord>;
704
+ saveExecution(record: ModelToDocumentTypeWithoutId<HistoryRecord>): Promise<void>;
705
+ getExecutions(jobName: string, limit?: number, skip?: number): Promise<HistoryRecord[]>;
706
+ }
707
+ export declare class JobRecord {
708
+ _id: string;
709
+ jobName: string;
710
+ type: "recurrent" | "event";
711
+ priority: number;
712
+ uniqueIdentifier?: string;
713
+ nextRunAt: Date;
714
+ lastRunAt?: Date;
715
+ lockedUntil?: Date;
716
+ tries?: number;
717
+ params?: any;
718
+ }
719
+ declare class JobsRepo {
720
+ jobs: () => Collection<JobRecord>;
721
+ getJobAndLock(jobNames: string[], lockTime: number): Promise<JobToRun>;
722
+ setJobRecordPriority(jobId: string, priority: number): Promise<void>;
723
+ scheduleNextRun(options: {
724
+ jobId: string;
725
+ nextRunAt: Date;
726
+ addTries: boolean;
727
+ priority: number;
728
+ }): Promise<void>;
729
+ deleteEventJob(jobId: string): Promise<void>;
730
+ extendLockTime(jobId: string, extraTime: number): Promise<void>;
731
+ ensureJobRecord(job: JobDefinitionWithName): Promise<void>;
732
+ scheduleJob(options: ScheduleJobRecordOptions): Promise<void>;
733
+ }
734
+ export declare function Jobs(): ClassDecorator;
735
+ export interface JobsPropertyDescriptor extends Omit<PropertyDecorator, "value"> {
736
+ value?: JobDefinition["resolve"];
737
+ }
738
+ export declare function RecurrentJob(options: Omit<RecurrentJobDefinition, "resolve" | "type">): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
739
+ export declare function EventJob(options?: Omit<EventJobDefinition, "resolve" | "type">): (target: any, propertyKey: string, descriptor: JobsPropertyDescriptor) => void;
740
+ export declare function getServiceJobs(target: any): {
741
+ [key: string]: JobDefinition;
742
+ };
743
+ export declare const jobsHistoryRepo: JobsHistoryRepo;
744
+ export declare const jobsRepo: JobsRepo;
745
+ export declare const startWorkers: (config: Partial<StartWorkersConfig>) => WorkersInstance;
746
+ export declare const scheduleJob: (options: ScheduleJobOptions) => Promise<void>;
747
+
748
+ export {};