@tmlmobilidade/go-interfaces-pcgi-file-manager 20260720.1149.38 → 20260720.1248.5
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.
- package/dist/databases/file-management.d.ts +9 -0
- package/dist/databases/{location-management.js → file-management.js} +4 -8
- package/dist/index.d.ts +5 -4
- package/dist/index.js +8 -12
- package/dist/interface.template.d.ts +5 -60
- package/dist/interface.template.js +3 -155
- package/package.json +2 -1
- package/dist/databases/location-management.d.ts +0 -8
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MongoInterfaceTemplate } from '../interface.template.js';
|
|
2
|
+
import { type MongoClient } from '@tmlmobilidade/go-clients-mongo';
|
|
3
|
+
import { type PcgiTransactionEntity } from '@tmlmobilidade/go-types-apex';
|
|
4
|
+
export declare class FileManagementDatabase {
|
|
5
|
+
readonly transactionEntity: MongoInterfaceTemplate<PcgiTransactionEntity>;
|
|
6
|
+
private readonly database;
|
|
7
|
+
private readonly databaseName;
|
|
8
|
+
constructor(instance: MongoClient);
|
|
9
|
+
}
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
/* * */
|
|
2
2
|
import { MongoInterfaceTemplate } from '../interface.template.js';
|
|
3
|
-
import z from 'zod';
|
|
4
3
|
/* * */
|
|
5
|
-
export class
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
// Collections
|
|
9
|
-
locationEntity;
|
|
4
|
+
export class FileManagementDatabase {
|
|
10
5
|
//
|
|
6
|
+
transactionEntity;
|
|
11
7
|
database;
|
|
12
|
-
databaseName = '
|
|
8
|
+
databaseName = 'FileManagement';
|
|
13
9
|
constructor(instance) {
|
|
14
10
|
// Create the database instance
|
|
15
11
|
this.database = instance.db(this.databaseName);
|
|
16
12
|
// Create collection interfaces
|
|
17
|
-
this.
|
|
13
|
+
this.transactionEntity = new MongoInterfaceTemplate('transactionEntity', this.database);
|
|
18
14
|
}
|
|
19
15
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FileManagementDatabase } from './databases/file-management.js';
|
|
2
2
|
declare class PCGIFileManagerClass {
|
|
3
3
|
private static _instance;
|
|
4
|
-
readonly
|
|
4
|
+
readonly fileManagement: FileManagementDatabase;
|
|
5
|
+
private constructor();
|
|
5
6
|
/**
|
|
6
7
|
* Establishes a connection to the Mongo database and initializes the collection.
|
|
7
|
-
* @
|
|
8
|
+
* @param options Optional Mongo client connection options.
|
|
9
|
+
* @throws Error if the environment variable for the database URI is missing or if the connection fails.
|
|
8
10
|
*/
|
|
9
11
|
static getInstance(): Promise<PCGIFileManagerClass>;
|
|
10
|
-
private constructor();
|
|
11
12
|
}
|
|
12
13
|
export declare const pcgiFileManager: PCGIFileManagerClass;
|
|
13
14
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
/* * */
|
|
2
2
|
import { MongoDatabaseClient } from '@tmlmobilidade/go-clients-mongo';
|
|
3
3
|
import { asyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
-
import {
|
|
4
|
+
import { FileManagementDatabase } from './databases/file-management.js';
|
|
5
5
|
/* * */
|
|
6
6
|
class PCGIFileManagerClass {
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
7
|
//
|
|
10
8
|
static _instance;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
fileManagement;
|
|
10
|
+
constructor(mongoClient) {
|
|
11
|
+
this.fileManagement = new FileManagementDatabase(mongoClient);
|
|
12
|
+
}
|
|
14
13
|
/**
|
|
15
14
|
* Establishes a connection to the Mongo database and initializes the collection.
|
|
16
|
-
* @
|
|
15
|
+
* @param options Optional Mongo client connection options.
|
|
16
|
+
* @throws Error if the environment variable for the database URI is missing or if the connection fails.
|
|
17
17
|
*/
|
|
18
18
|
static async getInstance() {
|
|
19
19
|
if (!PCGIFileManagerClass._instance) {
|
|
@@ -22,10 +22,6 @@ class PCGIFileManagerClass {
|
|
|
22
22
|
}
|
|
23
23
|
return PCGIFileManagerClass._instance;
|
|
24
24
|
}
|
|
25
|
-
//
|
|
26
|
-
// Constructor
|
|
27
|
-
constructor(mongoClient) {
|
|
28
|
-
this.locationManagement = new LocationManagementDatabase(mongoClient);
|
|
29
|
-
}
|
|
30
25
|
}
|
|
26
|
+
/* * */
|
|
31
27
|
export const pcgiFileManager = asyncSingletonProxy(PCGIFileManagerClass);
|
|
@@ -1,19 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare class MongoInterfaceTemplate<T extends Document, TCreate> {
|
|
4
|
-
private readonly collectionName;
|
|
5
|
-
private readonly createSchema;
|
|
6
|
-
private readonly database;
|
|
7
|
-
private readonly indexDescription;
|
|
1
|
+
import { type Collection, type Db, type Document, type Filter, type FindOptions, type WithId } from '@tmlmobilidade/go-clients-mongo';
|
|
2
|
+
export declare class MongoInterfaceTemplate<T extends Document> {
|
|
8
3
|
private readonly collection;
|
|
9
|
-
private initPromise;
|
|
10
4
|
/**
|
|
11
|
-
* @param collectionName
|
|
12
|
-
* @param database
|
|
13
|
-
* @param createSchema - The schema to use for creating documents.
|
|
14
|
-
* @param indexDescription - The index description to use for the collection.
|
|
5
|
+
* @param collectionName The name of the collection to create the interface for.
|
|
6
|
+
* @param database The database to create the interface for.
|
|
15
7
|
*/
|
|
16
|
-
constructor(collectionName: string, database: Db
|
|
8
|
+
constructor(collectionName: string, database: Db);
|
|
17
9
|
/**
|
|
18
10
|
* Counts documents matching the filter criteria.
|
|
19
11
|
* @param filter The filter criteria to match documents.
|
|
@@ -49,51 +41,4 @@ export declare class MongoInterfaceTemplate<T extends Document, TCreate> {
|
|
|
49
41
|
* @warning Use with caution: direct access to the collection allows for executing arbitrary queries.
|
|
50
42
|
*/
|
|
51
43
|
getCollection(): Promise<Collection<T>>;
|
|
52
|
-
/**
|
|
53
|
-
* This method allows for performing multiple write operations
|
|
54
|
-
* in a single request, which can improve performance.
|
|
55
|
-
* The operations can include inserts, updates, deletes, and replacements.
|
|
56
|
-
* @param operations An array of bulk write operations to execute on the collection.
|
|
57
|
-
* @param options The options for the bulk write operation.
|
|
58
|
-
* @returns A promise that resolves to the result of the bulk write operation.
|
|
59
|
-
* @warning This method does not perform schema validation on the operations.
|
|
60
|
-
* It is the responsibility of the caller to ensure that the operations are valid and conform to the expected schemas.
|
|
61
|
-
*/
|
|
62
|
-
bulkWrite(operations: AnyBulkWriteOperation<T>[], options?: BulkWriteOptions): Promise<BulkWriteResult>;
|
|
63
|
-
/**
|
|
64
|
-
* Inserts multiple documents into the collection after validating them against the create schema.
|
|
65
|
-
* @param data An array of documents to insert, conforming to the TCreate type.
|
|
66
|
-
* @param options Optional insert options to configure the behavior of the insert operation.
|
|
67
|
-
* @returns A promise that resolves to the result of the insertMany operation.
|
|
68
|
-
*/
|
|
69
|
-
insertMany(data: TCreate[], options?: BulkWriteOptions): Promise<import("mongodb").InsertManyResult<T>>;
|
|
70
|
-
insertOne(data: TCreate, options?: InsertOneOptions): Promise<import("mongodb").InsertOneResult<T>>;
|
|
71
|
-
/**
|
|
72
|
-
* Initializes the MongoDB client and ensures that the specified database and collection exist.
|
|
73
|
-
* This method should be called before performing any operations on the database or collection.
|
|
74
|
-
* It handles the asynchronous setup process and logs any errors that occur during initialization.
|
|
75
|
-
* @throws Will throw an error if the client initialization or database/collection setup fails.
|
|
76
|
-
* @returns A promise that resolves when the initialization process is complete.
|
|
77
|
-
*/
|
|
78
|
-
protected init(): Promise<void>;
|
|
79
|
-
private ensureInitialized;
|
|
80
|
-
/**
|
|
81
|
-
* Optional override for custom setup logic:
|
|
82
|
-
* indexes, materialized views, constraints, etc.
|
|
83
|
-
*/
|
|
84
|
-
protected postInit(): Promise<void>;
|
|
85
|
-
/**
|
|
86
|
-
* Ensures that the specified collection exists in the MongoDB database,
|
|
87
|
-
* creating it if it does not already exist.
|
|
88
|
-
* @returns A promise that resolves when the collection is ensured to exist.
|
|
89
|
-
*/
|
|
90
|
-
private createCollectionIfNotExists;
|
|
91
|
-
/**
|
|
92
|
-
* Ensures that the specified indexes exist in MongoDB, creating them if they do not already exist.
|
|
93
|
-
* This method performs input validation to prevent unsafe operations and logs the outcome of the operation.
|
|
94
|
-
* It constructs the necessary index creation queries based on the provided index descriptions and executes them using the client.
|
|
95
|
-
* @throws Will throw an error if any of the inputs are unsafe or if the index creation query fails.
|
|
96
|
-
* @returns A promise that resolves when the indexes are ensured to exist.
|
|
97
|
-
*/
|
|
98
|
-
private syncIndexes;
|
|
99
44
|
}
|
|
@@ -1,32 +1,14 @@
|
|
|
1
1
|
/* * */
|
|
2
|
-
import { isSameIndex, prepareMongoIndexOptions } from '@tmlmobilidade/go-clients-mongo';
|
|
3
|
-
import { Logger } from '@tmlmobilidade/logger';
|
|
4
2
|
/* * */
|
|
5
3
|
export class MongoInterfaceTemplate {
|
|
6
|
-
//
|
|
7
|
-
collectionName;
|
|
8
|
-
createSchema;
|
|
9
|
-
database;
|
|
10
|
-
indexDescription;
|
|
11
4
|
//
|
|
12
5
|
collection;
|
|
13
|
-
initPromise = null;
|
|
14
6
|
/**
|
|
15
|
-
* @param collectionName
|
|
16
|
-
* @param database
|
|
17
|
-
* @param createSchema - The schema to use for creating documents.
|
|
18
|
-
* @param indexDescription - The index description to use for the collection.
|
|
7
|
+
* @param collectionName The name of the collection to create the interface for.
|
|
8
|
+
* @param database The database to create the interface for.
|
|
19
9
|
*/
|
|
20
|
-
constructor(collectionName, database
|
|
21
|
-
this.collectionName = collectionName;
|
|
10
|
+
constructor(collectionName, database) {
|
|
22
11
|
this.collection = database.collection(collectionName);
|
|
23
|
-
this.database = database;
|
|
24
|
-
this.createSchema = createSchema;
|
|
25
|
-
this.indexDescription = indexDescription;
|
|
26
|
-
this.initPromise = this.init().catch((error) => {
|
|
27
|
-
Logger.error({ error, message: `MONGODB [${this.collectionName}]: Error @ constructor(): ${error.message}` });
|
|
28
|
-
throw error;
|
|
29
|
-
});
|
|
30
12
|
}
|
|
31
13
|
/**
|
|
32
14
|
* Counts documents matching the filter criteria.
|
|
@@ -34,7 +16,6 @@ export class MongoInterfaceTemplate {
|
|
|
34
16
|
* @returns A promise that resolves to the count of matching documents.
|
|
35
17
|
*/
|
|
36
18
|
async count(filter) {
|
|
37
|
-
await this.ensureInitialized();
|
|
38
19
|
return await this.collection.countDocuments(filter);
|
|
39
20
|
}
|
|
40
21
|
/**
|
|
@@ -44,7 +25,6 @@ export class MongoInterfaceTemplate {
|
|
|
44
25
|
* @returns A promise that resolves to an array of distinct values for the given key.
|
|
45
26
|
*/
|
|
46
27
|
async distinct(key, filter) {
|
|
47
|
-
await this.ensureInitialized();
|
|
48
28
|
return this.collection.distinct(key, filter);
|
|
49
29
|
}
|
|
50
30
|
/**
|
|
@@ -55,7 +35,6 @@ export class MongoInterfaceTemplate {
|
|
|
55
35
|
* @returns A promise that resolves to an array of matching documents.
|
|
56
36
|
*/
|
|
57
37
|
async findMany(filter, options) {
|
|
58
|
-
await this.ensureInitialized();
|
|
59
38
|
return await this.collection.find(filter, options).toArray();
|
|
60
39
|
}
|
|
61
40
|
/**
|
|
@@ -65,7 +44,6 @@ export class MongoInterfaceTemplate {
|
|
|
65
44
|
* @returns A promise that resolves to the matching document or null if not found.
|
|
66
45
|
*/
|
|
67
46
|
async findOne(filter, options) {
|
|
68
|
-
await this.ensureInitialized();
|
|
69
47
|
return await this.collection.findOne(filter, options);
|
|
70
48
|
}
|
|
71
49
|
/**
|
|
@@ -75,136 +53,6 @@ export class MongoInterfaceTemplate {
|
|
|
75
53
|
* @warning Use with caution: direct access to the collection allows for executing arbitrary queries.
|
|
76
54
|
*/
|
|
77
55
|
async getCollection() {
|
|
78
|
-
await this.ensureInitialized();
|
|
79
56
|
return this.collection;
|
|
80
57
|
}
|
|
81
|
-
/**
|
|
82
|
-
* This method allows for performing multiple write operations
|
|
83
|
-
* in a single request, which can improve performance.
|
|
84
|
-
* The operations can include inserts, updates, deletes, and replacements.
|
|
85
|
-
* @param operations An array of bulk write operations to execute on the collection.
|
|
86
|
-
* @param options The options for the bulk write operation.
|
|
87
|
-
* @returns A promise that resolves to the result of the bulk write operation.
|
|
88
|
-
* @warning This method does not perform schema validation on the operations.
|
|
89
|
-
* It is the responsibility of the caller to ensure that the operations are valid and conform to the expected schemas.
|
|
90
|
-
*/
|
|
91
|
-
async bulkWrite(operations, options) {
|
|
92
|
-
await this.ensureInitialized();
|
|
93
|
-
return await this.collection.bulkWrite(operations, options);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Inserts multiple documents into the collection after validating them against the create schema.
|
|
97
|
-
* @param data An array of documents to insert, conforming to the TCreate type.
|
|
98
|
-
* @param options Optional insert options to configure the behavior of the insert operation.
|
|
99
|
-
* @returns A promise that resolves to the result of the insertMany operation.
|
|
100
|
-
*/
|
|
101
|
-
async insertMany(data, options) {
|
|
102
|
-
await this.ensureInitialized();
|
|
103
|
-
// If no create schema is defined, throw an error.
|
|
104
|
-
if (!this.createSchema)
|
|
105
|
-
throw new Error(`No schema defined for insert operation for ${this.collectionName} collection.`);
|
|
106
|
-
// Validate each document against the create schema
|
|
107
|
-
const parsedDocuments = data.map((doc) => {
|
|
108
|
-
const parseResult = this.createSchema.safeParse(doc);
|
|
109
|
-
if (!parseResult.success)
|
|
110
|
-
throw new Error(`Document validation failed: ${parseResult.error.message}`);
|
|
111
|
-
return parseResult.data;
|
|
112
|
-
});
|
|
113
|
-
// Attempt to insert the documents into the collection
|
|
114
|
-
return await this.collection.insertMany(parsedDocuments, options);
|
|
115
|
-
}
|
|
116
|
-
async insertOne(data, options) {
|
|
117
|
-
await this.ensureInitialized();
|
|
118
|
-
// If no create schema is defined, throw an error.
|
|
119
|
-
if (!this.createSchema)
|
|
120
|
-
throw new Error(`No schema defined for insert operation for ${this.collectionName} collection.`);
|
|
121
|
-
// Validate the document against the create schema
|
|
122
|
-
const parseResult = this.createSchema.safeParse(data);
|
|
123
|
-
if (!parseResult.success)
|
|
124
|
-
throw new Error(`Document validation failed: ${parseResult.error.message}`);
|
|
125
|
-
// Attempt to insert the document into the collection
|
|
126
|
-
return await this.collection.insertOne(parseResult.data, options);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Initializes the MongoDB client and ensures that the specified database and collection exist.
|
|
130
|
-
* This method should be called before performing any operations on the database or collection.
|
|
131
|
-
* It handles the asynchronous setup process and logs any errors that occur during initialization.
|
|
132
|
-
* @throws Will throw an error if the client initialization or database/collection setup fails.
|
|
133
|
-
* @returns A promise that resolves when the initialization process is complete.
|
|
134
|
-
*/
|
|
135
|
-
async init() {
|
|
136
|
-
// Ensure the collection exists and its indexes are in sync
|
|
137
|
-
// with the provided index description.
|
|
138
|
-
await this.createCollectionIfNotExists();
|
|
139
|
-
await this.syncIndexes();
|
|
140
|
-
// Call postInit for any additional setup logic defined in subclasses
|
|
141
|
-
await this.postInit();
|
|
142
|
-
}
|
|
143
|
-
async ensureInitialized() {
|
|
144
|
-
if (!this.initPromise)
|
|
145
|
-
this.initPromise = this.init();
|
|
146
|
-
await this.initPromise;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Optional override for custom setup logic:
|
|
150
|
-
* indexes, materialized views, constraints, etc.
|
|
151
|
-
*/
|
|
152
|
-
async postInit() {
|
|
153
|
-
// no-op by default
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Ensures that the specified collection exists in the MongoDB database,
|
|
157
|
-
* creating it if it does not already exist.
|
|
158
|
-
* @returns A promise that resolves when the collection is ensured to exist.
|
|
159
|
-
*/
|
|
160
|
-
async createCollectionIfNotExists() {
|
|
161
|
-
const collections = await this.database.listCollections({ name: this.collectionName }).toArray();
|
|
162
|
-
if (collections.length)
|
|
163
|
-
return;
|
|
164
|
-
await this.database.createCollection(this.collectionName);
|
|
165
|
-
Logger.info({ message: `MONGODB [${this.collectionName}]: Collection created.` });
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Ensures that the specified indexes exist in MongoDB, creating them if they do not already exist.
|
|
169
|
-
* This method performs input validation to prevent unsafe operations and logs the outcome of the operation.
|
|
170
|
-
* It constructs the necessary index creation queries based on the provided index descriptions and executes them using the client.
|
|
171
|
-
* @throws Will throw an error if any of the inputs are unsafe or if the index creation query fails.
|
|
172
|
-
* @returns A promise that resolves when the indexes are ensured to exist.
|
|
173
|
-
*/
|
|
174
|
-
async syncIndexes() {
|
|
175
|
-
try {
|
|
176
|
-
if (this.indexDescription === false) {
|
|
177
|
-
Logger.info({ message: `MONGODB [${this.collectionName}]: Skipping index synchronization.` });
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
// Start index synchronization process
|
|
181
|
-
Logger.info({ message: `MONGODB [${this.collectionName}]: Synchronizing indexes...` });
|
|
182
|
-
// Normalize already applied and new indexes
|
|
183
|
-
// and filter the default _id index.
|
|
184
|
-
const existingIndexes = await this.collection.indexes();
|
|
185
|
-
const filteredExisting = existingIndexes.filter(idx => JSON.stringify(idx.key) !== JSON.stringify({ _id: 1 }));
|
|
186
|
-
// Setup desired indexes based on indexDescription
|
|
187
|
-
const indexesToCreate = [];
|
|
188
|
-
// Find indexes to create
|
|
189
|
-
for (const desiredIdx of this.indexDescription) {
|
|
190
|
-
// For the list of desired indexes,
|
|
191
|
-
// check if they are present in the existing indexes.
|
|
192
|
-
const found = filteredExisting.some(existingIdx => isSameIndex(existingIdx, desiredIdx));
|
|
193
|
-
// If not, mark them for creation.
|
|
194
|
-
if (!found)
|
|
195
|
-
indexesToCreate.push(desiredIdx);
|
|
196
|
-
}
|
|
197
|
-
// Create indexes
|
|
198
|
-
for (const idx of indexesToCreate) {
|
|
199
|
-
Logger.info({ message: `MONGODB [${this.collectionName}]: Creating index on keys ${JSON.stringify(idx.key)} with options ${JSON.stringify(prepareMongoIndexOptions(idx))}.` });
|
|
200
|
-
await this.collection.createIndex(idx.key, prepareMongoIndexOptions(idx));
|
|
201
|
-
Logger.success(`MONGODB [${this.collectionName}]: Created index on keys ${JSON.stringify(idx.key)}.`);
|
|
202
|
-
}
|
|
203
|
-
Logger.success(`MONGODB [${this.collectionName}]: Indexes synchronized.`);
|
|
204
|
-
}
|
|
205
|
-
catch (error) {
|
|
206
|
-
Logger.error({ error, message: `MONGODB [${this.collectionName}]: Error @ syncIndexes(): ${error.message}` });
|
|
207
|
-
throw error;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
58
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmlmobilidade/go-interfaces-pcgi-file-manager",
|
|
3
|
-
"version": "20260720.
|
|
3
|
+
"version": "20260720.1248.5",
|
|
4
4
|
"author": {
|
|
5
5
|
"email": "iso@tmlmobilidade.pt",
|
|
6
6
|
"name": "TML-ISO"
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@tmlmobilidade/go-clients-mongo": "*",
|
|
40
|
+
"@tmlmobilidade/go-types-apex": "*",
|
|
40
41
|
"@tmlmobilidade/logger": "*",
|
|
41
42
|
"@tmlmobilidade/utils": "*",
|
|
42
43
|
"luxon": "3.7.2",
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { MongoClient } from '@tmlmobilidade/go-clients-mongo';
|
|
2
|
-
import { MongoInterfaceTemplate } from '../interface.template.js';
|
|
3
|
-
export declare class LocationManagementDatabase {
|
|
4
|
-
readonly locationEntity: MongoInterfaceTemplate<any, any>;
|
|
5
|
-
private readonly database;
|
|
6
|
-
private readonly databaseName;
|
|
7
|
-
constructor(instance: MongoClient);
|
|
8
|
-
}
|