Compare commits

...

12 Commits

Author SHA1 Message Date
90cfc4644d 5.8.3
Some checks failed
Default (tags) / security (push) Successful in 39s
Default (tags) / test (push) Successful in 3m3s
Default (tags) / release (push) Failing after 54s
Default (tags) / metadata (push) Successful in 1m4s
2025-04-17 11:21:35 +00:00
0be279e5f5 fix(readme): Improve readme documentation on data models and connection management 2025-04-17 11:21:35 +00:00
9755522bba 5.8.2
Some checks failed
Default (tags) / security (push) Successful in 38s
Default (tags) / test (push) Successful in 2m58s
Default (tags) / release (push) Failing after 51s
Default (tags) / metadata (push) Successful in 58s
2025-04-14 18:13:10 +00:00
de8736e99e fix(classes.doc.ts): Ensure collection initialization before creating a cursor in getCursorExtended 2025-04-14 18:13:10 +00:00
c430627a21 5.8.1
Some checks failed
Default (tags) / security (push) Successful in 32s
Default (tags) / test (push) Successful in 3m0s
Default (tags) / release (push) Failing after 50s
Default (tags) / metadata (push) Successful in 1m2s
2025-04-14 18:06:29 +00:00
0bfebaf5b9 fix(cursor, doc): Add explicit return types and casts to SmartdataDbCursor methods and update getCursorExtended signature in SmartDataDbDoc. 2025-04-14 18:06:29 +00:00
4733982d03 5.8.0
Some checks failed
Default (tags) / security (push) Successful in 32s
Default (tags) / test (push) Successful in 3m3s
Default (tags) / release (push) Failing after 52s
Default (tags) / metadata (push) Successful in 59s
2025-04-14 17:58:54 +00:00
368dc27607 feat(cursor): Add toArray method to SmartdataDbCursor to convert raw MongoDB documents into initialized class instances 2025-04-14 17:58:54 +00:00
938b25c925 5.7.0
Some checks failed
Default (tags) / security (push) Successful in 39s
Default (tags) / test (push) Successful in 3m2s
Default (tags) / release (push) Failing after 51s
Default (tags) / metadata (push) Successful in 59s
2025-04-14 17:49:07 +00:00
ab251858ba feat(SmartDataDbDoc): Add extended cursor method getCursorExtended for flexible cursor modifications 2025-04-14 17:49:07 +00:00
24371ccf78 5.6.0
Some checks failed
Default (tags) / security (push) Successful in 36s
Default (tags) / test (push) Successful in 3m1s
Default (tags) / release (push) Failing after 51s
Default (tags) / metadata (push) Successful in 58s
2025-04-07 16:47:16 +00:00
ed1eecbab8 feat(indexing): Add support for regular index creation in documents and collections 2025-04-07 16:47:16 +00:00
7 changed files with 142 additions and 16 deletions

View File

@ -1,5 +1,46 @@
# Changelog # Changelog
## 2025-04-17 - 5.8.3 - fix(readme)
Improve readme documentation on data models and connection management
- Clarify that data models use @Collection, @unI, @svDb, @index, and @searchable decorators
- Document that ObjectId and Buffer fields are stored as BSON types natively without extra decorators
- Update connection management section to use 'db.close()' instead of 'db.disconnect()'
- Revise license section to reference the MIT License without including additional legal details
## 2025-04-14 - 5.8.2 - fix(classes.doc.ts)
Ensure collection initialization before creating a cursor in getCursorExtended
- Added 'await collection.init()' to guarantee that the MongoDB collection is initialized before using the cursor
- Prevents potential runtime errors when accessing collection.mongoDbCollection
## 2025-04-14 - 5.8.1 - fix(cursor, doc)
Add explicit return types and casts to SmartdataDbCursor methods and update getCursorExtended signature in SmartDataDbDoc.
- Specify Promise<T> as return type for next() in SmartdataDbCursor and cast return value to T.
- Specify Promise<T[]> as return type for toArray() in SmartdataDbCursor and cast return value to T[].
- Update getCursorExtended to return Promise<SmartdataDbCursor<T>> for clearer type safety.
## 2025-04-14 - 5.8.0 - feat(cursor)
Add toArray method to SmartdataDbCursor to convert raw MongoDB documents into initialized class instances
- Introduced asynchronous toArray method in SmartdataDbCursor which retrieves all documents from the MongoDB cursor
- Maps each native document to a SmartDataDbDoc instance using createInstanceFromMongoDbNativeDoc for consistent API usage
## 2025-04-14 - 5.7.0 - feat(SmartDataDbDoc)
Add extended cursor method getCursorExtended for flexible cursor modifications
- Introduces getCursorExtended in classes.doc.ts to allow modifier functions for MongoDB cursors
- Wraps the modified cursor with SmartdataDbCursor for improved API consistency
- Enhances querying capabilities by enabling customized cursor transformations
## 2025-04-07 - 5.6.0 - feat(indexing)
Add support for regular index creation in documents and collections
- Implement new index decorator in classes.doc.ts to mark properties with regular indexing options
- Update SmartdataCollection to create regular indexes if defined on a document during insert
- Enhance document structure to store and utilize regular index configurations
## 2025-04-06 - 5.5.1 - fix(ci & formatting) ## 2025-04-06 - 5.5.1 - fix(ci & formatting)
Minor fixes: update CI workflow image and npmci package references, adjust package.json and readme URLs, and apply consistent code formatting. Minor fixes: update CI workflow image and npmci package references, adjust package.json and readme URLs, and apply consistent code formatting.

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartdata", "name": "@push.rocks/smartdata",
"version": "5.5.1", "version": "5.8.3",
"private": false, "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.", "description": "An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -66,7 +66,7 @@ await db.init();
### Defining Data Models ### Defining Data Models
Data models in `@push.rocks/smartdata` are classes that represent collections and documents in your MongoDB database. Use decorators such as `@Collection`, `@unI`, and `@svDb` to define your data models. Data models in `@push.rocks/smartdata` are classes that represent collections and documents in your MongoDB database. Use decorators such as `@Collection`, `@unI`, `@svDb`, `@index`, and `@searchable` to define your data models. Fields of type `ObjectId` or `Buffer` decorated with `@svDb()` will be stored as BSON ObjectId and Binary, respectively; no separate `@oid()` or `@bin()` decorators are required.
```typescript ```typescript
import { import {
@ -74,8 +74,6 @@ import {
Collection, Collection,
unI, unI,
svDb, svDb,
oid,
bin,
index, index,
searchable, searchable,
} from '@push.rocks/smartdata'; } from '@push.rocks/smartdata';
@ -96,12 +94,10 @@ class User extends SmartDataDbDoc<User, User> {
public email: string; // Mark 'email' to be saved in DB public email: string; // Mark 'email' to be saved in DB
@svDb() @svDb()
@oid() // Automatically handle as ObjectId type public organizationId: ObjectId; // Stored as BSON ObjectId
public organizationId: ObjectId; // Will be automatically converted to/from ObjectId
@svDb() @svDb()
@bin() // Automatically handle as Binary data public profilePicture: Buffer; // Stored as BSON Binary
public profilePicture: Buffer; // Will be automatically converted to/from Binary
@svDb({ @svDb({
serialize: (data) => JSON.stringify(data), // Custom serialization serialize: (data) => JSON.stringify(data), // Custom serialization
@ -541,7 +537,7 @@ class Order extends SmartDataDbDoc<Order, Order> {
### Connection Management ### Connection Management
- Always call `db.init()` before using any database features - Always call `db.init()` before using any database features
- Use `db.disconnect()` when shutting down your application - Use `db.close()` when shutting down your application
- Set appropriate connection pool sizes based on your application's needs - Set appropriate connection pool sizes based on your application's needs
### Document Design ### Document Design
@ -590,7 +586,7 @@ Please make sure to update tests as appropriate and follow our coding standards.
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. This repository is licensed under the MIT License. For details, see [MIT License](https://opensource.org/licenses/MIT).
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartdata', name: '@push.rocks/smartdata',
version: '5.5.1', version: '5.8.3',
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.' 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

@ -1,7 +1,7 @@
import * as plugins from './plugins.js'; import * as plugins from './plugins.js';
import { SmartdataDb } from './classes.db.js'; import { SmartdataDb } from './classes.db.js';
import { SmartdataDbCursor } from './classes.cursor.js'; import { SmartdataDbCursor } from './classes.cursor.js';
import { SmartDataDbDoc } from './classes.doc.js'; import { SmartDataDbDoc, type IIndexOptions } from './classes.doc.js';
import { SmartdataDbWatcher } from './classes.watcher.js'; import { SmartdataDbWatcher } from './classes.watcher.js';
import { CollectionFactory } from './classes.collectionfactory.js'; import { CollectionFactory } from './classes.collectionfactory.js';
@ -127,6 +127,7 @@ export class SmartdataCollection<T> {
public collectionName: string; public collectionName: string;
public smartdataDb: SmartdataDb; public smartdataDb: SmartdataDb;
public uniqueIndexes: string[] = []; public uniqueIndexes: string[] = [];
public regularIndexes: Array<{field: string, options: IIndexOptions}> = [];
constructor(classNameArg: string, smartDataDbArg: SmartdataDb) { constructor(classNameArg: string, smartDataDbArg: SmartdataDb) {
// tell the collection where it belongs // tell the collection where it belongs
@ -170,6 +171,24 @@ export class SmartdataCollection<T> {
} }
} }
/**
* creates regular indexes for the collection
*/
public createRegularIndexes(indexesArg: Array<{field: string, options: IIndexOptions}> = []) {
for (const indexDef of indexesArg) {
// Check if we've already created this index
const indexKey = indexDef.field;
if (!this.regularIndexes.some(i => i.field === indexKey)) {
this.mongoDbCollection.createIndex(
{ [indexDef.field]: 1 }, // Simple single-field index
indexDef.options
);
// Track that we've created this index
this.regularIndexes.push(indexDef);
}
}
}
/** /**
* adds a validation function that all newly inserted and updated objects have to pass * adds a validation function that all newly inserted and updated objects have to pass
*/ */
@ -238,6 +257,12 @@ export class SmartdataCollection<T> {
await this.init(); await this.init();
await this.checkDoc(dbDocArg); await this.checkDoc(dbDocArg);
this.markUniqueIndexes(dbDocArg.uniqueIndexes); this.markUniqueIndexes(dbDocArg.uniqueIndexes);
// Create regular indexes if available
if (dbDocArg.regularIndexes && dbDocArg.regularIndexes.length > 0) {
this.createRegularIndexes(dbDocArg.regularIndexes);
}
const saveableObject = await dbDocArg.createSavableObject(); const saveableObject = await dbDocArg.createSavableObject();
const result = await this.mongoDbCollection.insertOne(saveableObject); const result = await this.mongoDbCollection.insertOne(saveableObject);
return result; return result;
@ -295,4 +320,4 @@ export class SmartdataCollection<T> {
} }
return done.promise; return done.promise;
} }
} }

View File

@ -15,14 +15,14 @@ export class SmartdataDbCursor<T = any> {
this.smartdataDbDoc = dbDocArg; this.smartdataDbDoc = dbDocArg;
} }
public async next(closeAtEnd = true) { public async next(closeAtEnd = true): Promise<T> {
const result = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc( const result = this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(
await this.mongodbCursor.next(), await this.mongodbCursor.next(),
); );
if (!result && closeAtEnd) { if (!result && closeAtEnd) {
await this.close(); await this.close();
} }
return result; return result as T;
} }
public async forEach(forEachFuncArg: (itemArg: T) => Promise<any>, closeCursorAtEnd = true) { public async forEach(forEachFuncArg: (itemArg: T) => Promise<any>, closeCursorAtEnd = true) {
@ -40,6 +40,11 @@ export class SmartdataDbCursor<T = any> {
} }
} }
public async toArray(): Promise<T[]> {
const result = await this.mongodbCursor.toArray();
return result.map((itemArg) => this.smartdataDbDoc.createInstanceFromMongoDbNativeDoc(itemArg)) as T[];
}
public async close() { public async close() {
await this.mongodbCursor.close(); await this.mongodbCursor.close();
} }

View File

@ -83,6 +83,46 @@ export function unI() {
}; };
} }
/**
* Options for MongoDB indexes
*/
export interface IIndexOptions {
background?: boolean;
unique?: boolean;
sparse?: boolean;
expireAfterSeconds?: number;
[key: string]: any;
}
/**
* index - decorator to mark a field for regular indexing
*/
export function index(options?: IIndexOptions) {
return (target: SmartDataDbDoc<unknown, unknown>, key: string) => {
console.log(`called index() on >${target.constructor.name}.${key}<`);
// Initialize regular indexes array if it doesn't exist
if (!target.regularIndexes) {
target.regularIndexes = [];
}
// Add this field to regularIndexes with its options
target.regularIndexes.push({
field: key,
options: options || {}
});
// Also ensure it's marked as saveable
if (!target.saveableProperties) {
target.saveableProperties = [];
}
if (!target.saveableProperties.includes(key)) {
target.saveableProperties.push(key);
}
};
}
export const convertFilterForMongoDb = (filterArg: { [key: string]: any }) => { export const convertFilterForMongoDb = (filterArg: { [key: string]: any }) => {
// Special case: detect MongoDB operators and pass them through directly // Special case: detect MongoDB operators and pass them through directly
const topLevelOperators = ['$and', '$or', '$nor', '$not', '$text', '$where', '$regex']; const topLevelOperators = ['$and', '$or', '$nor', '$not', '$text', '$where', '$regex'];
@ -209,6 +249,20 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
return cursor; return cursor;
} }
public static async getCursorExtended<T>(
this: plugins.tsclass.typeFest.Class<T>,
filterArg: plugins.tsclass.typeFest.PartialDeep<T>,
modifierFunction = (cursorArg: plugins.mongodb.FindCursor<plugins.mongodb.WithId<plugins.mongodb.BSON.Document>>) => cursorArg,
): Promise<SmartdataDbCursor<T>> {
const collection: SmartdataCollection<T> = (this as any).collection;
await collection.init();
let cursor: plugins.mongodb.FindCursor<any> = collection.mongoDbCollection.find(
convertFilterForMongoDb(filterArg),
);
cursor = modifierFunction(cursor);
return new SmartdataDbCursor<T>(cursor, this as any as typeof SmartDataDbDoc);
}
/** /**
* watch the collection * watch the collection
* @param this * @param this
@ -401,6 +455,11 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
*/ */
public uniqueIndexes: string[]; public uniqueIndexes: string[];
/**
* regular indexes with their options
*/
public regularIndexes: Array<{field: string, options: IIndexOptions}> = [];
/** /**
* an array of saveable properties of a specific doc * an array of saveable properties of a specific doc
*/ */
@ -503,4 +562,4 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
} }
return identifiableObject; return identifiableObject;
} }
} }