Skip to content

Commit

Permalink
feat(lib): register and inject objection models (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
KwabenBerko authored May 23, 2020
1 parent 3bc832b commit da8822f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Module } from "@nestjs/common";
import knex from "knex";
import { knexSnakeCaseMappers } from "objection";
import { ConfigModule, ConfigService } from "../config";
import { User } from "./user";
import { BaseModel } from "./base";

@Module({
Expand All @@ -56,6 +57,9 @@ import { BaseModel } from "./base";
},
},
}),

//Register your objection models so it can be provided when needed.
ObjectionModule.forFeature([User]),
],
exports: [ObjectionModule],
})
Expand All @@ -70,6 +74,7 @@ import { Module } from "@nestjs/common";
import knex from "knex";
import { knexSnakeCaseMappers } from "objection";
import { ConfigModule, ConfigService } from "../config";
import { User } from "./user";
import { BaseModel } from "./base";

@Module({
Expand All @@ -90,6 +95,8 @@ import { BaseModel } from "./base";
};
},
}),
//Register your objection models so it can be provided when needed.
ObjectionModule.forFeature([User]),
],
exports: [ObjectionModule],
})
Expand Down Expand Up @@ -128,3 +135,19 @@ export class PrimaryDatabaseHealthIndicator extends HealthIndicator {
}
}
```

### Injecting an objection model

```ts
import { Injectable, Inject } from "@nestjs/common";
import { User } from "./user";

@Injectable()
export class UserService {
constructor(@Inject(User) private readonly userModel: typeof User) {}

async getUsers(): Promise<User[]> {
return await this.userModel.query();
}
}
```
18 changes: 17 additions & 1 deletion lib/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable new-cap */
import { DynamicModule, Logger, Module } from "@nestjs/common";
import { DynamicModule, Logger, Module, Provider } from "@nestjs/common";
import { Model } from "objection";
import { ObjectionCoreModule } from "./core";
import {
ObjectionModuleAsyncOptions,
Expand Down Expand Up @@ -42,4 +43,19 @@ export class ObjectionModule {
exports: [ObjectionCoreModule],
};
}

public static forFeature(models: typeof Model[]): DynamicModule {
const modelProviders: Provider[] = models.map((model) => {
return {
useValue: model,
provide: model.name,
};
});

return {
module: ObjectionModule,
providers: modelProviders,
exports: modelProviders,
};
}
}
19 changes: 19 additions & 0 deletions tests/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { KNEX_CONNECTION, OBJECTION_BASE_MODEL } from "@/constants";
import { ObjectionModule } from "@/module";
import { Test, TestingModule } from "@nestjs/testing";
import knex from "knex";
import { Model } from "objection";

describe("ObjectionModule", () => {
let testingModule: TestingModule;
Expand Down Expand Up @@ -64,4 +65,22 @@ describe("ObjectionModule", () => {
expect(model).toBeDefined();
});
});

describe("#forFeature", () => {
beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [
ObjectionModule.register({
config,
}),
ObjectionModule.forFeature([Model]),
],
}).compile();
});

test("provides a model", () => {
const model = testingModule.get(Model);
expect(model).toBeDefined();
});
});
});

0 comments on commit da8822f

Please sign in to comment.