Skip to content

Commit

Permalink
refactor(*): separate core out to match other nestjs/* repos
Browse files Browse the repository at this point in the history
Signed-off-by: Will Soto <willsoto@users.noreply.github.com>
  • Loading branch information
willsoto committed Feb 4, 2019
1 parent 5003674 commit 9e2bcf9
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 123 deletions.
8 changes: 8 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module.exports = {
plugins: [
[
"@babel/plugin-proposal-decorators",
{
legacy: true
}
]
],
presets: [
"@babel/preset-typescript",
[
Expand Down
8 changes: 4 additions & 4 deletions lib/__tests__/module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test, TestingModule } from "@nestjs/testing";
import knex from "knex";
import { Model } from "objection";
import { ObjectionCoreModule } from "../module";
import { ObjectionModule } from "../module";

describe("ObjectionCoreModule", () => {
describe("ObjectionModule", () => {
let testingModule: TestingModule;
const config: knex.Config = {
client: "sqlite3",
Expand All @@ -17,7 +17,7 @@ describe("ObjectionCoreModule", () => {
beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [
ObjectionCoreModule.forRoot({
ObjectionModule.forRoot({
config
})
]
Expand All @@ -41,7 +41,7 @@ describe("ObjectionCoreModule", () => {
beforeEach(async () => {
testingModule = await Test.createTestingModule({
imports: [
ObjectionCoreModule.forRootAsync({
ObjectionModule.forRootAsync({
useFactory() {
return {
config
Expand Down
132 changes: 132 additions & 0 deletions lib/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { DynamicModule, Provider } from "@nestjs/common";
import knex from "knex";
import { Model } from "objection";
import {
KNEX_CONNECTION,
OBJECTION_BASE_MODEL,
OBJECTION_MODULE_OPTIONS
} from "./constants";
import {
Connection,
ObjectionModuleAsyncOptions,
ObjectionModuleOptions,
ObjectionModuleOptionsFactory
} from "./interfaces";

export class ObjectionCoreModule {
public static forRoot(options: ObjectionModuleOptions = {}): DynamicModule {
const knexConfig = options.config || {};
const Base = options.Model || Model;
const connection = knex(knexConfig);

Base.knex(connection);

const objectionModuleOptions: Provider = {
provide: OBJECTION_MODULE_OPTIONS,
useValue: options
};

const objectionBaseModelProvider: Provider = {
provide: OBJECTION_BASE_MODEL,
useValue: Base
};

const knexConnectionProvider: Provider = {
provide: KNEX_CONNECTION,
useValue: connection
};

return {
module: ObjectionCoreModule,
providers: [
objectionModuleOptions,
objectionBaseModelProvider,
knexConnectionProvider
],
exports: [objectionBaseModelProvider, knexConnectionProvider]
};
}

public static forRootAsync(
options: ObjectionModuleAsyncOptions = {}
): DynamicModule {
const knexConnectionProvider: Provider = {
provide: KNEX_CONNECTION,
inject: [OBJECTION_MODULE_OPTIONS],
useFactory(objectionModuleOptions: ObjectionModuleOptions) {
const config = objectionModuleOptions.config || {};

return knex(config);
}
};

const objectionBaseModelProvider: Provider = {
provide: OBJECTION_BASE_MODEL,
inject: [KNEX_CONNECTION, OBJECTION_MODULE_OPTIONS],
useFactory(
connection: Connection,
objectionModuleOptions: ObjectionModuleOptions
) {
const Base = objectionModuleOptions.Model || Model;

Base.knex(connection);

return Base;
}
};
const asyncProviders = this.createAsyncProviders(options);

return {
module: ObjectionCoreModule,
imports: options.imports,
providers: [
...asyncProviders,
knexConnectionProvider,
objectionBaseModelProvider
],
exports: [objectionBaseModelProvider, knexConnectionProvider]
};
}

private static createAsyncProviders(
options: ObjectionModuleAsyncOptions
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}

if (!options.useClass) {
throw new Error("Invalid configuration");
}

return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass
}
];
}

private static createAsyncOptionsProvider(
options: ObjectionModuleAsyncOptions
): Provider {
if (options.useFactory) {
return {
provide: OBJECTION_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || []
};
}

return {
provide: OBJECTION_MODULE_OPTIONS,
async useFactory(optionsFactory: ObjectionModuleOptionsFactory) {
const opts = await optionsFactory.createObjectionModuleOptions();

return opts;
},
inject: [options.useClass || options.useExisting]
};
}
}
128 changes: 11 additions & 117 deletions lib/module.ts
Original file line number Diff line number Diff line change
@@ -1,132 +1,26 @@
import { DynamicModule, Provider } from "@nestjs/common";
import knex from "knex";
import { Model } from "objection";
import { DynamicModule, Module } from "@nestjs/common";
import { ObjectionCoreModule } from "./core";
import {
KNEX_CONNECTION,
OBJECTION_BASE_MODEL,
OBJECTION_MODULE_OPTIONS
} from "./constants";
import {
Connection,
ObjectionModuleAsyncOptions,
ObjectionModuleOptions,
ObjectionModuleOptionsFactory
ObjectionModuleOptions
} from "./interfaces";

export class ObjectionCoreModule {
public static forRoot(options: ObjectionModuleOptions): DynamicModule {
const knexConfig = options.config || {};
const Base = options.Model || Model;
const connection = knex(knexConfig);

Base.knex(connection);

const objectionModuleOptions: Provider = {
provide: OBJECTION_MODULE_OPTIONS,
useValue: options
};

const objectionBaseModelProvider: Provider = {
provide: OBJECTION_BASE_MODEL,
useValue: Base
};

const knexConnectionProvider: Provider = {
provide: KNEX_CONNECTION,
useValue: connection
};

// eslint-disable-next-line new-cap
@Module({})
export class ObjectionModule {
public static forRoot(options?: ObjectionModuleOptions): DynamicModule {
return {
module: ObjectionCoreModule,
providers: [
objectionModuleOptions,
objectionBaseModelProvider,
knexConnectionProvider
],
exports: [objectionBaseModelProvider, knexConnectionProvider]
module: ObjectionModule,
imports: [ObjectionCoreModule.forRoot(options)]
};
}

public static forRootAsync(
options: ObjectionModuleAsyncOptions
): DynamicModule {
const knexConnectionProvider: Provider = {
provide: KNEX_CONNECTION,
inject: [OBJECTION_MODULE_OPTIONS],
useFactory(objectionModuleOptions: ObjectionModuleOptions) {
const config = objectionModuleOptions.config || {};

return knex(config);
}
};

const objectionBaseModelProvider: Provider = {
provide: OBJECTION_BASE_MODEL,
inject: [KNEX_CONNECTION, OBJECTION_MODULE_OPTIONS],
useFactory(
connection: Connection,
objectionModuleOptions: ObjectionModuleOptions
) {
const Base = objectionModuleOptions.Model || Model;

Base.knex(connection);

return Base;
}
};
const asyncProviders = this.createAsyncProviders(options);

return {
module: ObjectionCoreModule,
imports: options.imports,
providers: [
...asyncProviders,
knexConnectionProvider,
objectionBaseModelProvider
],
exports: [objectionBaseModelProvider, knexConnectionProvider]
};
}

private static createAsyncProviders(
options: ObjectionModuleAsyncOptions
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}

if (!options.useClass) {
throw new Error("Invalid configuration");
}

return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass
}
];
}

private static createAsyncOptionsProvider(
options: ObjectionModuleAsyncOptions
): Provider {
if (options.useFactory) {
return {
provide: OBJECTION_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || []
};
}

return {
provide: OBJECTION_MODULE_OPTIONS,
async useFactory(optionsFactory: ObjectionModuleOptionsFactory) {
const opts = await optionsFactory.createObjectionModuleOptions();

return opts;
},
inject: [options.useClass || options.useExisting]
module: ObjectionModule,
imports: [ObjectionCoreModule.forRootAsync(options)]
};
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"author": {
"email": "will.soto9+github@gmail.com",
"email": "willsoto@users.noreply.github.com",
"name": "Will Soto",
"url": "https://github.com/willsoto"
},
"description": "Objection module for NestJS",
"devDependencies": {
"@babel/core": "7.2.2",
"@babel/plugin-proposal-decorators": "7.3.0",
"@babel/preset-env": "7.3.1",
"@babel/preset-typescript": "7.1.0",
"@commitlint/cli": "7.5.0",
Expand Down Expand Up @@ -72,5 +73,6 @@
"typings": "tsc --project tsconfig.json --declaration --declarationDir typings --emitDeclarationOnly",
"test": "jest"
},
"typings": "./typings/index.d.ts",
"version": "0.2.1"
}
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@
"@babel/traverse" "^7.1.0"
"@babel/types" "^7.0.0"

"@babel/helper-create-class-features-plugin@^7.3.0":
version "7.3.0"
resolved "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.3.0.tgz#2b01a81b3adc2b1287f9ee193688ef8dc71e718f"
integrity sha512-DUsQNS2CGLZZ7I3W3fvh0YpPDd6BuWJlDl+qmZZpABZHza2ErE3LxtEzLJFHFC1ZwtlAXvHhbFYbtM5o5B0WBw==
dependencies:
"@babel/helper-function-name" "^7.1.0"
"@babel/helper-member-expression-to-functions" "^7.0.0"
"@babel/helper-optimise-call-expression" "^7.0.0"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/helper-replace-supers" "^7.2.3"

"@babel/helper-define-map@^7.1.0":
version "7.1.0"
resolved "https://registry.npmjs.org/@babel/helper-define-map/-/helper-define-map-7.1.0.tgz#3b74caec329b3c80c116290887c0dd9ae468c20c"
Expand Down Expand Up @@ -194,7 +205,7 @@
"@babel/traverse" "^7.1.0"
"@babel/types" "^7.0.0"

"@babel/helper-replace-supers@^7.1.0":
"@babel/helper-replace-supers@^7.1.0", "@babel/helper-replace-supers@^7.2.3":
version "7.2.3"
resolved "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.2.3.tgz#19970020cf22677d62b3a689561dbd9644d8c5e5"
integrity sha512-GyieIznGUfPXPWu0yLS6U55Mz67AZD9cUk0BfirOWlPrXlBcan9Gz+vHGz+cPfuoweZSnPzPIm67VtQM0OWZbA==
Expand Down Expand Up @@ -277,6 +288,15 @@
"@babel/helper-remap-async-to-generator" "^7.1.0"
"@babel/plugin-syntax-async-generators" "^7.2.0"

"@babel/plugin-proposal-decorators@7.3.0":
version "7.3.0"
resolved "https://registry.npmjs.org/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.3.0.tgz#637ba075fa780b1f75d08186e8fb4357d03a72a7"
integrity sha512-3W/oCUmsO43FmZIqermmq6TKaRSYhmh/vybPfVFwQWdSb8xwki38uAIvknCRzuyHRuYfCYmJzL9or1v0AffPjg==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.3.0"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-decorators" "^7.2.0"

"@babel/plugin-proposal-json-strings@^7.2.0":
version "7.2.0"
resolved "https://registry.npmjs.org/@babel/plugin-proposal-json-strings/-/plugin-proposal-json-strings-7.2.0.tgz#568ecc446c6148ae6b267f02551130891e29f317"
Expand Down Expand Up @@ -317,6 +337,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-decorators@^7.2.0":
version "7.2.0"
resolved "https://registry.npmjs.org/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.2.0.tgz#c50b1b957dcc69e4b1127b65e1c33eef61570c1b"
integrity sha512-38QdqVoXdHUQfTpZo3rQwqQdWtCn5tMv4uV6r2RMfTqNBuv4ZBhz79SfaQWKTVmxHjeFv/DnXVC/+agHCklYWA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-json-strings@^7.2.0":
version "7.2.0"
resolved "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.2.0.tgz#72bd13f6ffe1d25938129d2a186b11fd62951470"
Expand Down

0 comments on commit 9e2bcf9

Please sign in to comment.