@tmlmobilidade/go-interfaces-godb 20260907.2103.49 → 20260908.220.27
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.
|
@@ -10,6 +10,7 @@ import { findById } from './methods/find-by-id.js';
|
|
|
10
10
|
import { findMany } from './methods/find-many.js';
|
|
11
11
|
import { findOne } from './methods/find-one.js';
|
|
12
12
|
import { getCollection } from './methods/get-collection.js';
|
|
13
|
+
import { insertManyUnsafe } from './methods/insert-many-unsafe.js';
|
|
13
14
|
import { insertMany } from './methods/insert-many.js';
|
|
14
15
|
import { insertOneUnsafe } from './methods/insert-one-unsafe.js';
|
|
15
16
|
import { insertOne } from './methods/insert-one.js';
|
|
@@ -45,6 +46,7 @@ export function createGoDbCollection({ collectionName, database, indexDescriptio
|
|
|
45
46
|
getCollection: () => getCollection(context),
|
|
46
47
|
// getCollectionName: () => getCollectionName(context),
|
|
47
48
|
insertMany: (docs, options) => insertMany(context, docs, options),
|
|
49
|
+
insertManyUnsafe: (docs, options) => insertManyUnsafe(context, docs, options),
|
|
48
50
|
insertOne: (doc, clientSession) => insertOne(context, doc, clientSession),
|
|
49
51
|
insertOneUnsafe: (doc, clientSession) => insertOneUnsafe(context, doc, clientSession),
|
|
50
52
|
// isLocked: filter => isLocked(context, filter),
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Document } from '@tmlmobilidade/go-clients-mongo';
|
|
2
|
+
import { type GoDbCollectionContext } from '../types/godb-collection-context.type.js';
|
|
3
|
+
import { type MinimalOptions } from '../types/minimal-options.type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Inserts a single document into the collection.
|
|
6
|
+
* @param doc The document to insert.
|
|
7
|
+
* @param options The options for the insert operation.
|
|
8
|
+
* @returns A promise that resolves to the result of the insert operation.
|
|
9
|
+
*/
|
|
10
|
+
export declare function insertManyUnsafe<T extends Document>(context: GoDbCollectionContext<T>, docs: T[], options?: MinimalOptions): Promise<T[]>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { HTTP_STATUS, HttpException } from '@tmlmobilidade/consts';
|
|
3
|
+
import { generateRandomString } from '@tmlmobilidade/strings';
|
|
4
|
+
/**
|
|
5
|
+
* Inserts a single document into the collection.
|
|
6
|
+
* @param doc The document to insert.
|
|
7
|
+
* @param options The options for the insert operation.
|
|
8
|
+
* @returns A promise that resolves to the result of the insert operation.
|
|
9
|
+
*/
|
|
10
|
+
export async function insertManyUnsafe(context, docs, options) {
|
|
11
|
+
// Setup a copy of the document to be inserted with defaults
|
|
12
|
+
const insertableDocuments = [];
|
|
13
|
+
for (const doc of docs) {
|
|
14
|
+
let parsedDocument = {
|
|
15
|
+
...doc,
|
|
16
|
+
// created_at: doc.created_at || Dates.now('utc').unix_milliseconds,
|
|
17
|
+
// created_by: doc.created_by || 'system',
|
|
18
|
+
// updated_at: doc.updated_at || Dates.now('utc').unix_milliseconds,
|
|
19
|
+
// updated_by: doc.updated_by || 'system',
|
|
20
|
+
};
|
|
21
|
+
// Validate the document against the create schema if unsafe is false
|
|
22
|
+
try {
|
|
23
|
+
if (!context.schema)
|
|
24
|
+
throw new Error('No schema defined for insert operation. This is either an internal interface error or you should pass unsafe=true to the insert operation.');
|
|
25
|
+
parsedDocument = context.schema.parse(parsedDocument);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
throw new HttpException(HTTP_STATUS.BAD_REQUEST, error.message, { cause: error });
|
|
29
|
+
}
|
|
30
|
+
// Preserve a caller-provided _id (create schemas omit it, so parse strips it),
|
|
31
|
+
// otherwise generate a unique one.
|
|
32
|
+
if (doc._id) {
|
|
33
|
+
parsedDocument._id = doc._id;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
parsedDocument._id = generateRandomString({ length: 5 });
|
|
37
|
+
while (await context.collection.findOne({ _id: { $eq: parsedDocument._id } })) {
|
|
38
|
+
parsedDocument._id = generateRandomString({ length: 5 });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
insertableDocuments.push(parsedDocument);
|
|
42
|
+
}
|
|
43
|
+
// Attempt to insert the document into the collection
|
|
44
|
+
const result = await context.collection.insertMany(insertableDocuments, { session: options?.session });
|
|
45
|
+
// Check if the insert operation was acknowledged
|
|
46
|
+
if (!result.acknowledged)
|
|
47
|
+
throw new HttpException(HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Failed to insert document', result);
|
|
48
|
+
// Otherwise, fetch and return the inserted document
|
|
49
|
+
const insertedDocs = await context.collection.find({ _id: { $in: Object.values(result.insertedIds) } }, { session: options?.session }).toArray();
|
|
50
|
+
if (!insertedDocs)
|
|
51
|
+
throw new HttpException(HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Failed to find inserted document', result);
|
|
52
|
+
return insertedDocs;
|
|
53
|
+
}
|
|
@@ -19,6 +19,7 @@ export interface GoDbCollection<T extends Document> {
|
|
|
19
19
|
findOne(filter: Filter<T>, options?: MinimalOptions): Promise<null | T>;
|
|
20
20
|
getCollection(): Promise<Collection<T>>;
|
|
21
21
|
insertMany(docs: InsertableDocument<T>[], options?: MinimalOptions): Promise<T[]>;
|
|
22
|
+
insertManyUnsafe(docs: T[], options?: MinimalOptions): Promise<T[]>;
|
|
22
23
|
insertOne(doc: InsertableDocument<T>, options?: MinimalOptions): Promise<T>;
|
|
23
24
|
insertOneUnsafe(doc: T, options?: MinimalOptions): Promise<T>;
|
|
24
25
|
/**
|