@opra/mongodb 0.32.3 → 0.32.4

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.
@@ -1,8 +1,10 @@
1
1
  import mongodb from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
+ import { DTO, PartialDTO, PatchDTO } from '@opra/common';
3
4
  import * as OpraCommon from '@opra/common';
4
- import { PartialInput, PartialOutput } from '@opra/core';
5
+ import { FilterInput } from './adapter-utils/prepare-filter.js';
5
6
  import { MongoService } from './mongo-service.js';
7
+ import { AnyId } from './types.js';
6
8
  /**
7
9
  * A class that provides access to a MongoDB collection, with support for singleton document operations.
8
10
  * @class MongoSingletonService
@@ -10,13 +12,50 @@ import { MongoService } from './mongo-service.js';
10
12
  * @template T - The type of document stored in the collection
11
13
  */
12
14
  export declare class MongoSingletonService<T extends mongodb.Document> extends MongoService<T> {
13
- protected _id: any;
15
+ /**
16
+ * Represents a unique identifier for singleton record
17
+ * @type {AnyId} _id
18
+ */
19
+ _id: AnyId;
14
20
  /**
15
21
  * Represents the key for a collection.
16
22
  *
17
23
  * @type {string}
18
24
  */
19
25
  collectionKey: string;
26
+ /**
27
+ * Represents a common filter function for a MongoCollectionService.
28
+ *
29
+ * @type {FilterInput | Function}
30
+ */
31
+ $documentFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
32
+ /**
33
+ * Interceptor function for handling callback execution with provided arguments.
34
+ *
35
+ * @param {Function} callback - The callback function to be intercepted.
36
+ * @param {Object} args - The arguments object containing the following properties:
37
+ * @param {string} crud - The CRUD operation type.
38
+ * @param {string} method - The method name.
39
+ * @param {AnyId} documentId - The document ID (optional).
40
+ * @param {Object} input - The input object (optional).
41
+ * @param {Object} options - Additional options (optional).
42
+ * @param {Object} _this - The reference to the current object.
43
+ * @returns {Promise<any>} - The promise that resolves to the result of the callback execution.
44
+ */
45
+ $interceptor?: (callback: (...args: any[]) => any, args: {
46
+ crud: MongoService.CrudOp;
47
+ method: string;
48
+ documentId?: AnyId;
49
+ input?: Object;
50
+ options?: Record<string, any>;
51
+ }, _this: any) => Promise<any>;
52
+ /**
53
+ * Constructs a new instance
54
+ *
55
+ * @param {Type | string} dataType - The data type of the array elements.
56
+ * @param {MongoSingletonService.Options} [options] - The options for the array service.
57
+ * @constructor
58
+ */
20
59
  constructor(dataType: Type | string, options?: MongoSingletonService.Options);
21
60
  /**
22
61
  * Asserts the existence of a resource based on the given options.
@@ -24,16 +63,16 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
24
63
  * @returns {Promise<void>} A Promise that resolves when the resource exists.
25
64
  * @throws {ResourceNotFoundError} If the resource does not exist.
26
65
  */
27
- assert(): Promise<void>;
66
+ assert(options?: MongoSingletonService.ExistsOptions): Promise<void>;
28
67
  /**
29
68
  * Creates the document in the database.
30
69
  *
31
- * @param {PartialInput<T>} input - The partial input to create the document with.
70
+ * @param {DTO<T>} input - The partial input to create the document with.
32
71
  * @param {MongoSingletonService.CreateOptions} [options] - The options for creating the document.
33
- * @return {Promise<PartialOutput<T>>} A promise that resolves to the partial output of the created document.
72
+ * @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
34
73
  * @throws {Error} Throws an error if an unknown error occurs while creating the document.
35
74
  */
36
- create(input: PartialInput<T>, options?: MongoSingletonService.CreateOptions): Promise<PartialOutput<T>>;
75
+ create(input: DTO<T>, options?: MongoSingletonService.CreateOptions): Promise<PartialDTO<T>>;
37
76
  /**
38
77
  * Deletes a record from the database
39
78
  *
@@ -46,31 +85,49 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
46
85
  *
47
86
  * @return {Promise<boolean>} - A promise that resolves to a boolean value indicating if the document exists.
48
87
  */
49
- exists(): Promise<boolean>;
88
+ exists(options?: MongoSingletonService.ExistsOptions): Promise<boolean>;
50
89
  /**
51
90
  * Fetches the document if it exists. Returns undefined if not found.
52
91
  *
53
92
  * @param {MongoSingletonService.FindOptions<T>} [options] - The options for finding the document.
54
- * @returns {Promise<PartialOutput<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
93
+ * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
55
94
  */
56
- findOne(options?: MongoSingletonService.FindOptions<T>): Promise<PartialOutput<T> | undefined>;
95
+ findOne(options?: MongoSingletonService.FindOptions<T>): Promise<PartialDTO<T> | undefined>;
57
96
  /**
58
97
  * Fetches the document from the Mongo collection service. Throws error if not found.
59
98
  *
60
99
  * @param {MongoCollectionService.FindOneOptions} options - The options to customize the query.
61
- * @return {Promise<PartialOutput<T>>} - A promise that resolves to the fetched document.
100
+ * @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
62
101
  * @throws {ResourceNotFoundError} - If the document is not found in the collection.
63
102
  */
64
- get(options?: MongoSingletonService.FindOptions<T>): Promise<PartialOutput<T>>;
103
+ get(options?: MongoSingletonService.FindOptions<T>): Promise<PartialDTO<T>>;
65
104
  /**
66
105
  * Updates a document in the MongoDB collection.
67
106
  *
68
- * @param {PartialInput<T>} input - The partial input to update the document.
107
+ * @param {PatchDTO<T>} input - The partial input to update the document.
69
108
  * @param {MongoSingletonService.UpdateOptions<T>} [options] - The update options.
70
109
  *
71
- * @return {Promise<PartialOutput<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
110
+ * @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
72
111
  */
73
- update(input: PartialInput<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<PartialOutput<T> | undefined>;
112
+ updateOnly(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<number>;
113
+ /**
114
+ * Updates a document in the MongoDB collection.
115
+ *
116
+ * @param {PatchDTO<T>} input - The partial input to update the document.
117
+ * @param {MongoSingletonService.UpdateOptions<T>} [options] - The update options.
118
+ *
119
+ * @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
120
+ */
121
+ update(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
122
+ /**
123
+ * Retrieves the common filter used for querying the document.
124
+ * This method is mostly used for security issues like securing multi-tenant applications.
125
+ *
126
+ * @protected
127
+ * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
128
+ * that resolves to the common filter, or undefined if not available.
129
+ */
130
+ protected _getDocumentFilter(): FilterInput | Promise<FilterInput> | undefined;
74
131
  }
75
132
  /**
76
133
  *
@@ -84,8 +141,10 @@ export declare namespace MongoSingletonService {
84
141
  * @extends MongoService.Options
85
142
  */
86
143
  interface Options extends MongoService.Options {
87
- collectionKey?: string;
88
- _id?: any;
144
+ collectionKey?: MongoSingletonService<any>['collectionKey'];
145
+ _id?: MongoSingletonService<any>['_id'];
146
+ documentFilter?: MongoSingletonService<any>['$documentFilter'];
147
+ interceptor?: MongoSingletonService<any>['$interceptor'];
89
148
  }
90
149
  /**
91
150
  * Represents options for creating the document.
@@ -130,4 +189,11 @@ export declare namespace MongoSingletonService {
130
189
  include?: string[];
131
190
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
132
191
  }
192
+ /**
193
+ * Represents options for checking the document exists
194
+ *
195
+ * @interface
196
+ */
197
+ interface ExistsOptions extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
198
+ }
133
199
  }