Constructor
new MongoDbNoteRepository(url, dbName) → {MongoDbNoteRepository}
Create a new MongoDbNoteRepository
Parameters:
Name | Type | Description |
---|---|---|
url |
string | MongoDB connection URL (with or without authentication) |
dbName |
string | Database name to use for storing notes |
- Source:
Returns:
New MongoDbNoteRepository instance
Example
const repository = new MongoDbNoteRepository('mongodb://localhost:27017', 'notes_db');
await repository.init();
const notes = await repository.findAll();
Extends
Classes
Methods
(async) countDeleted() → {Promise.<number>}
Count the number of deleted notes in recycle bin
- Overrides:
- Source:
Throws:
-
When database query fails or MongoDB is unreachable
- Type
- Error
Returns:
Promise resolving to the count of deleted notes
- Type
- Promise.<number>
Example
const count = await repository.countDeleted();
console.log(`Recycle bin contains ${count} notes`);
(async) create(note) → {Promise.<Note>}
Create a new note in the database
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
note |
Object | The note data to create Properties
|
- Overrides:
- Source:
Throws:
-
When note creation fails due to validation errors or database issues
- Type
- Error
Returns:
Promise resolving to the created Note object with assigned ID
- Type
- Promise.<Note>
Example
const newNote = await repository.create({
title: 'My New Note',
content: 'This is the content of my note'
});
console.log(`Created note with ID: ${newNote.id}`);
(async) emptyRecycleBin() → {Promise.<number>}
Empty the recycle bin by permanently deleting all deleted notes
- Overrides:
- Source:
Throws:
-
When operation fails due to database issues
- Type
- Error
Returns:
Promise resolving to the number of notes permanently deleted
- Type
- Promise.<number>
Example
const deletedCount = await repository.emptyRecycleBin();
console.log(`Permanently deleted ${deletedCount} notes from recycle bin`);
(async) findAll() → {Promise.<Array.<Note>>}
Find all active notes (not deleted)
- Overrides:
- Source:
Throws:
-
When database query fails or MongoDB is unreachable
- Type
- Error
Returns:
Promise resolving to an array of active Note objects
- Type
- Promise.<Array.<Note>>
Example
const activeNotes = await repository.findAll();
console.log(`Found ${activeNotes.length} active notes`);
(async) findAllIncludingDeleted() → {Promise.<Array.<Note>>}
Find all notes regardless of deletion status
- Overrides:
- Source:
Throws:
-
When database query fails or MongoDB is unreachable
- Type
- Error
Returns:
Promise resolving to an array of all Note objects
- Type
- Promise.<Array.<Note>>
(async) findById(id) → {Promise.<(Note|null)>}
Find a note by its unique identifier
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The unique ID of the note to retrieve (MongoDB ObjectId) |
- Overrides:
- Source:
Throws:
-
When database query fails (except for invalid ObjectId format)
- Type
- Error
Returns:
Promise resolving to a Note object or null if not found
- Type
- Promise.<(Note|null)>
Example
const note = await repository.findById('507f1f77bcf86cd799439011');
if (note) {
console.log(`Found note: ${note.title}`);
} else {
console.log('Note not found');
}
(async) findDeleted() → {Promise.<Array.<Note>>}
Find all deleted notes (in recycle bin)
- Overrides:
- Source:
Throws:
-
When database query fails or MongoDB is unreachable
- Type
- Error
Returns:
Promise resolving to an array of deleted Note objects
- Type
- Promise.<Array.<Note>>
Example
const deletedNotes = await repository.findDeleted();
console.log(`Found ${deletedNotes.length} notes in recycle bin`);
(async) init() → {Promise.<void>}
Initialize the repository by connecting to MongoDB and setting up the schema. This method must be called before using any other repository methods.
- Source:
Throws:
-
-
When MongoDB is unreachable or connection fails
- Type
- Error
-
-
-
When schema creation fails
- Type
- Error
-
Returns:
- Type
- Promise.<void>
Example
const repository = new MongoDbNoteRepository(url, dbName);
await repository.init(); // Connects to MongoDB and creates schema
(async) moveToRecycleBin(id) → {Promise.<boolean>}
Move a note to recycle bin (soft delete)
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The ID of the note to move to recycle bin (MongoDB ObjectId) |
- Overrides:
- Source:
Throws:
-
When operation fails due to database issues
- Type
- Error
Returns:
Promise resolving to true if moved to recycle bin, false if not found
- Type
- Promise.<boolean>
Example
const moved = await repository.moveToRecycleBin('507f1f77bcf86cd799439011');
if (moved) {
console.log('Note moved to recycle bin successfully');
} else {
console.log('Note not found');
}
(async) permanentDelete(id) → {Promise.<boolean>}
Permanently delete a note from the database
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The ID of the note to permanently delete (MongoDB ObjectId) |
- Overrides:
- Source:
Throws:
-
When deletion fails due to database issues
- Type
- Error
Returns:
Promise resolving to true if deleted, false if not found
- Type
- Promise.<boolean>
Example
const deleted = await repository.permanentDelete('507f1f77bcf86cd799439011');
if (deleted) {
console.log('Note permanently deleted');
} else {
console.log('Note not found');
}
(async) restore(id) → {Promise.<boolean>}
Restore a note from recycle bin
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The ID of the note to restore (MongoDB ObjectId) |
- Overrides:
- Source:
Throws:
-
When operation fails due to database issues
- Type
- Error
Returns:
Promise resolving to true if restored, false if not found
- Type
- Promise.<boolean>
Example
const restored = await repository.restore('507f1f77bcf86cd799439011');
if (restored) {
console.log('Note restored successfully');
} else {
console.log('Note not found');
}
(async) restoreAll() → {Promise.<number>}
Restore all notes from recycle bin
- Overrides:
- Source:
Throws:
-
When operation fails due to database issues
- Type
- Error
Returns:
Promise resolving to the number of notes restored
- Type
- Promise.<number>
Example
const restoredCount = await repository.restoreAll();
console.log(`Restored ${restoredCount} notes from recycle bin`);
(async) update(id, note) → {Promise.<(Note|null)>}
Update an existing note in the database
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
id |
string | The ID of the note to update (MongoDB ObjectId) |
|||||||||
note |
Object | The updated note data Properties
|
- Overrides:
- Source:
Throws:
-
When update fails due to validation errors or database issues
- Type
- Error
Returns:
Promise resolving to the updated Note object or null if not found
- Type
- Promise.<(Note|null)>
Example
const updatedNote = await repository.update('507f1f77bcf86cd799439011', {
title: 'Updated Title',
content: 'Updated content'
});
if (updatedNote) {
console.log('Note updated successfully');
} else {
console.log('Note not found');
}