Compare commits

..

2 Commits

Author SHA1 Message Date
97d9302e71 v7.1.6
Some checks failed
Default (tags) / security (push) Failing after 0s
Default (tags) / test (push) Failing after 0s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-04-05 04:23:55 +00:00
fa60f625e9 fix(collection): improve duplicate key error reporting on insert 2026-04-05 04:23:55 +00:00
4 changed files with 25 additions and 4 deletions

View File

@@ -1,5 +1,12 @@
# Changelog
## 2026-04-05 - 7.1.6 - fix(collection)
improve duplicate key error reporting on insert
- Wrap insertOne() in error handling to detect MongoDB duplicate key conflicts
- Log a clearer message with the collection name and identifiable object when unique indexes are involved
- Guide callers to use getInstance() or save() on a db-retrieved instance when a duplicate already exists
## 2026-04-05 - 7.1.5 - fix(collection)
ensure unique indexes are marked before upsert operations

View File

@@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartdata",
"version": "7.1.5",
"version": "7.1.6",
"private": false,
"description": "An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.",
"exports": {

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartdata',
version: '7.1.5',
version: '7.1.6',
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
}

View File

@@ -447,8 +447,22 @@ export class SmartdataCollection<T> {
}
const saveableObject = await dbDocArg.createSavableObject() as any;
const result = await this.mongoDbCollection.insertOne(saveableObject, { session: opts?.session });
return result;
try {
const result = await this.mongoDbCollection.insertOne(saveableObject, { session: opts?.session });
return result;
} catch (err: any) {
const isDuplicateKey = err?.code === 11000 || err?.codeName === 'DuplicateKey';
if (isDuplicateKey && dbDocArg.uniqueIndexes && dbDocArg.uniqueIndexes.length > 0) {
const identifiableObject = await dbDocArg.createIdentifiableObject();
logger.log(
'error',
`Duplicate key conflict in "${this.collectionName}" on insert. ` +
`A document with ${JSON.stringify(identifiableObject)} already exists. ` +
`Use getInstance() to retrieve the existing document, or update it via save() on a db-retrieved instance.`
);
}
throw err;
}
}
/**