Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Upgraded to version 9
- Fixed linting issues
- Fixed formatting issues
  • Loading branch information
sandeepsuvit committed Oct 28, 2022
1 parent 5f5151d commit 34f6515
Show file tree
Hide file tree
Showing 26 changed files with 711 additions and 662 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ module.exports = {
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
root: true,
env: {
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ npm-debug.log
/.nyc_output

# dist
dist
dist
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ index.ts
package-lock.json
.eslintrc.js
tsconfig.json
tsconfig.build.json
.prettierrc
.prettierrc
.commitlintrc.json
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"singleQuote": true
}
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ npm i --save @needle-innovision/nestjs-tenancy

| Nest versions | Command |
|---------------|---------------------------------------------------------|
| v8.x | `npm i --save @needle-innovision/nestjs-tenancy` |
| v7.x | `npm i --save @needle-innovision/nestjs-tenancy |
| v8.x | `npm i --save @needle-innovision/nestjs-tenancy@2.0.0 |
| v6.x or v7.x | `npm i --save @needle-innovision/[email protected]` |

## Basic usage
Expand Down Expand Up @@ -329,10 +330,10 @@ we can make use of the property in `TenancyModuleOptions` which is `forceCreateC

## Requirements

1. @nest/mongoose ^6.x.x || ^8.x.x
2. @nestjs/common ^6.x.x || ^8.x.x
3. @nestjs/core ^6.x.x || ^8.x.x
4. mongoose ^5.7.12 (with typings `@types/mongoose`) || ^6.3.8
1. @nest/mongoose ^6.x.x || ^8.x.x || ^9.x.x
2. @nestjs/common ^6.x.x || ^8.x.x || ^9.x.x
3. @nestjs/core ^6.x.x || ^8.x.x || ^9.x.x
4. mongoose ^5.7.12 (with typings `@types/mongoose`) || ^6.3.8 || ^6.7.0

## Test

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './dist';
export * from './dist';
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
"use strict";

function __export(m) {
for (var p in m)
if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("./dist"));
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./dist"), exports);
2 changes: 1 addition & 1 deletion lib/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './tenancy.decorator';
export * from './tenancy.decorator';
6 changes: 4 additions & 2 deletions lib/decorators/tenancy.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { getTenantConnectionToken, getTenantModelToken } from '../utils';
*
* @param model any
*/
export const InjectTenancyModel = (model: string) => Inject(getTenantModelToken(model));
export const InjectTenancyModel = (model: string) =>
Inject(getTenantModelToken(model));

/**
* Get the instance of the tenant connection
*
* @param name any
*/
export const InjectTenancyConnection = (name?: string) => Inject(getTenantConnectionToken(name));
export const InjectTenancyConnection = (name?: string) =>
Inject(getTenantConnectionToken(name));
2 changes: 1 addition & 1 deletion lib/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './tenancy.factory';
export * from './tenancy.factory';
78 changes: 42 additions & 36 deletions lib/factories/tenancy.factory.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
import { Provider } from '@nestjs/common';
import { Connection } from 'mongoose';
import { ModelDefinition } from '../interfaces';
import { CONNECTION_MAP, MODEL_DEFINITION_MAP, TENANT_CONNECTION } from '../tenancy.constants';
import {
CONNECTION_MAP,
MODEL_DEFINITION_MAP,
TENANT_CONNECTION,
} from '../tenancy.constants';
import { ConnectionMap, ModelDefinitionMap } from '../types';
import { getTenantModelDefinitionToken, getTenantModelToken } from '../utils';

export const createTenancyProviders = (definitions: ModelDefinition[]): Provider[] => {
const providers: Provider[] = [];
export const createTenancyProviders = (
definitions: ModelDefinition[],
): Provider[] => {
const providers: Provider[] = [];

for (const definition of definitions) {
// Extract the definition data
const { name, schema, collection } = definition;
for (const definition of definitions) {
// Extract the definition data
const { name, schema, collection } = definition;

providers.push({
provide: getTenantModelDefinitionToken(name),
useFactory: (
modelDefinitionMap: ModelDefinitionMap,
connectionMap: ConnectionMap,
) => {
const exists = modelDefinitionMap.has(name);
if (!exists) {
modelDefinitionMap.set(name, { ...definition });
providers.push({
provide: getTenantModelDefinitionToken(name),
useFactory: (
modelDefinitionMap: ModelDefinitionMap,
connectionMap: ConnectionMap,
) => {
const exists = modelDefinitionMap.has(name);
if (!exists) {
modelDefinitionMap.set(name, { ...definition });

connectionMap.forEach((connection: Connection) => {
connection.model(name, schema, collection);
});
}
},
inject: [
MODEL_DEFINITION_MAP,
CONNECTION_MAP,
],
});
connectionMap.forEach((connection: Connection) => {
connection.model(name, schema, collection);
});
}
},
inject: [MODEL_DEFINITION_MAP, CONNECTION_MAP],
});

// Creating Models with connections attached
providers.push({
provide: getTenantModelToken(name),
useFactory(tenantConnection: Connection) {
return tenantConnection.models[name] || tenantConnection.model(name, schema, collection);
},
inject: [TENANT_CONNECTION],
});
}
// Creating Models with connections attached
providers.push({
provide: getTenantModelToken(name),
useFactory(tenantConnection: Connection) {
return (
tenantConnection.models[name] ||
tenantConnection.model(name, schema, collection)
);
},
inject: [TENANT_CONNECTION],
});
}

// Return the list of providers mapping
return providers;
// Return the list of providers mapping
return providers;
};
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './interfaces';
export * from './decorators';
export * from './utils';
export * from './factories';
export * from './tenancy.module';
export * from './tenancy.module';
2 changes: 1 addition & 1 deletion lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './model-definition.interface';
export * from './tenancy-options.interface';
export * from './tenancy-options.interface';
6 changes: 3 additions & 3 deletions lib/interfaces/model-definition.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Schema } from 'mongoose';

export interface ModelDefinition {
name: string;
schema: Schema;
collection?: string;
name: string;
schema: Schema;
collection?: string;
}
112 changes: 57 additions & 55 deletions lib/interfaces/tenancy-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@ import { ModuleMetadata, Type } from '@nestjs/common/interfaces';
* @interface TenancyModuleOptions
*/
export interface TenancyModuleOptions extends Record<string, any> {
/**
* If `true`, tenant id will be extracted from the subdomain
*/
isTenantFromSubdomain?: boolean;
/**
* If `true`, tenant id will be extracted from the subdomain
*/
isTenantFromSubdomain?: boolean;

/**
* Tenant id will be extracted using the keyword from the request header
*/
tenantIdentifier?: string;
/**
* Tenant id will be extracted using the keyword from the request header
*/
tenantIdentifier?: string;

/**
* URI for the tenant database
*/
uri: (uri: string) => Promise<string> | string;
/**
* URI for the tenant database
*/
uri: (uri: string) => Promise<string> | string;

/**
* Used for applying custom validations
*/
validator?: (tenantId: string) => TenancyValidator;
/**
* Used for applying custom validations
*/
validator?: (tenantId: string) => TenancyValidator;

/**
* Options for the database
*/
options?: any;
/**
* Options for the database
*/
options?: any;

/**
* Whitelist following subdomains
*/
whitelist?: any;
/**
* Whitelist following subdomains
*/
whitelist?: any;

/**
* Option to create the collections that are mapped to the tenant module
* automatically while requesting for the tenant connection for the
* first time. This option is useful in case on mongo transactions, where
* transactions doens't create a collection if it does't exist already.
*/
forceCreateCollections?: boolean;
/**
* Option to create the collections that are mapped to the tenant module
* automatically while requesting for the tenant connection for the
* first time. This option is useful in case on mongo transactions, where
* transactions doens't create a collection if it does't exist already.
*/
forceCreateCollections?: boolean;
}

/**
Expand All @@ -56,7 +56,7 @@ export interface TenancyModuleOptions extends Record<string, any> {
* @interface TenancyOptionsFactory
*/
export interface TenancyOptionsFactory {
createTenancyOptions():Promise<TenancyModuleOptions> | TenancyModuleOptions;
createTenancyOptions(): Promise<TenancyModuleOptions> | TenancyModuleOptions;
}

/**
Expand All @@ -65,11 +65,14 @@ export interface TenancyOptionsFactory {
* @export
* @interface TenancyModuleAsyncOptions
*/
export interface TenancyModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<TenancyOptionsFactory>;
useClass?: Type<TenancyOptionsFactory>;
useFactory?: (...args: any[]) => Promise<TenancyModuleOptions> | TenancyModuleOptions;
inject?: any[];
export interface TenancyModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<TenancyOptionsFactory>;
useClass?: Type<TenancyOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<TenancyModuleOptions> | TenancyModuleOptions;
inject?: any[];
}

/**
Expand All @@ -82,22 +85,21 @@ export interface TenancyModuleAsyncOptions extends Pick<ModuleMetadata, 'imports
* @interface TenancyValidator
*/
export interface TenancyValidator {
/**
* Set the tenant id and return the instance of the class
* Note: This is the method that should be called by the implementing
* application
*
* @param {string} tenantId
* @returns {TenancyValidator}
* @memberof TenancyValidator
*/
setTenantId(tenantId: string): TenancyValidator;

/**
* Set the tenant id and return the instance of the class
* Note: This is the method that should be called by the implementing
* application
*
* @param {string} tenantId
* @returns {TenancyValidator}
* @memberof TenancyValidator
*/
setTenantId(tenantId: string): TenancyValidator;

/**
* This call will be invoked internally by the library
*
* @memberof TenancyValidator
*/
validate(): Promise<void>;
}
/**
* This call will be invoked internally by the library
*
* @memberof TenancyValidator
*/
validate(): Promise<void>;
}
Loading

0 comments on commit 34f6515

Please sign in to comment.