Skip to content

Commit

Permalink
feat(pkg): expose model instance
Browse files Browse the repository at this point in the history
  • Loading branch information
acellam committed Aug 11, 2024
1 parent 9b026f1 commit b257388
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ const task = await TaskModel.create({
const task = await TaskModel.findOne({ id: 'cbdabs-29232323-msasd'});
```

5. You can also use the underlying ORM model and instance methods

```typescript
// create task
const filter = { };
const countTasks = await TaskModel.model.countDocuments(filter);

```

## 🫶 Projects using this package
See the projects using this package in action.
- [Fractal Js](https://github.com/fractalerp/fractal-js)
Expand Down
2 changes: 0 additions & 2 deletions active_record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { ActiveRecordError } from "./lib/active_record_error";
export class ActiveRecord<T> implements ActiveRecordInterface<T> {
private nosqlActiveRecord!: NoSqlActiveRecord<T>;
private relationalActiveRecord!: RelationalActiveRecord<T>;

// TODO read database type from memory
private databaseType = DatabaseType.NOSQL;

constructor(modelName: string, schema: Record<string, SchemaProperty>, databaseType = DatabaseType.NOSQL) {
Expand Down
4 changes: 2 additions & 2 deletions nosql/nosql_active_record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { NoSQLActiveRecordInterface } from "./nosql_active_record_interface";
import DatabaseManagementSystem from "./../database/dms";

export class NoSqlActiveRecord<T> implements NoSQLActiveRecordInterface<T> {
private model: Model<T>;
public model: Model<T>;
private dbms: DatabaseManagementSystem = new DatabaseManagementSystem();


Expand All @@ -22,7 +22,7 @@ export class NoSqlActiveRecord<T> implements NoSQLActiveRecordInterface<T> {
this.model =
process.env.NODE_ENV !== "test"
? this.dbms.nosqlDB.database.model<T>(modelName, new Schema(mongoSchema as any))
: model<T>(modelName, new Schema(mongoSchema as any)) // allow for
: model<T>(modelName, new Schema(mongoSchema as any)) // allow for test
}

async find(query: any): Promise<T[]> {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fractalerp/active-record-js",
"appName": "Active Record Js",
"version": "1.0.14",
"version": "1.0.16",
"description": "Responsible for representing business data",
"author": "Acellam Guy <[email protected]>",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion relational/relational_active_record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RelationalActiveRecordInterface } from "./relational_active_record_inte

export class RelationalActiveRecord<T> implements RelationalActiveRecordInterface<T> {
private sequelize!: Sequelize;
private model: typeof Model;
public model: typeof Model;

constructor(modelName: string, schema: Record<string, SchemaProperty>) {
// TODO get global sequilize instance from memory
Expand Down

0 comments on commit b257388

Please sign in to comment.