diff --git a/package.json b/package.json index 49858937..8997007b 100755 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "typescript": "4.2.4" }, "scripts": { - "build": "rm -Rf dist && tsc -b", + "build": "rm -Rf dist && tsc -b tsconfig.build.json", "format": "prettier \"**/*.ts\" --ignore-path ./.prettierignore --write && git status", "lint": "eslint .", "doc": "rm -Rf ./docs && typedoc ./src && touch ./docs/.nojekyll", @@ -81,7 +81,7 @@ "json", "ts" ], - "rootDir": "src", + "rootDir": ".", "testRegex": ".spec.(t|j)s$", "transform": { "^.+\\.(t|j)s$": "ts-jest" diff --git a/src/test/module/entity.index.ts b/src/test/module/entity.index.ts deleted file mode 100644 index b5e3340b..00000000 --- a/src/test/module/entity.index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { Index } from '../../indexes/decorators'; - -@Collection('testwithindex') -export class EntityWithIndexTest extends Entity { - @Index({ - unique: true - }) - foo: string; -} diff --git a/src/test/module/entity.relationship.unique.ts b/src/test/module/entity.relationship.unique.ts deleted file mode 100644 index 8832bc76..00000000 --- a/src/test/module/entity.relationship.unique.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { IsString } from 'class-validator'; -import { ObjectId } from 'mongodb'; - -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { Relationship } from '../../relationship/decorators'; -import { EntityChildTest } from './child'; - -export const TEST_COLLECTION_NAME = 'testuniquerelationship'; - -@Collection(TEST_COLLECTION_NAME) -export class EntityUniqueRelationship extends Entity { - @IsString() - foo: string; - - @Relationship({ - type: () => EntityChildTest, - indexSpecification: { - key: { child: 1 }, - unique: true, - sparse: true - } - }) - child?: ObjectId; - - @Relationship({ - type: () => EntityChildTest, - indexSpecification: { - key: { child: 1, child2: 1 }, - unique: true, - sparse: true - } - }) - child2?: ObjectId; -} diff --git a/src/test/decorators.spec.ts b/test/decorators.spec.ts similarity index 100% rename from src/test/decorators.spec.ts rename to test/decorators.spec.ts diff --git a/src/test/module.spec.ts b/test/module.spec.ts similarity index 95% rename from src/test/module.spec.ts rename to test/module.spec.ts index 00802988..ff053a43 100644 --- a/src/test/module.spec.ts +++ b/test/module.spec.ts @@ -1,23 +1,25 @@ +import 'reflect-metadata'; + import { BadRequestException, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { createNamespace } from 'cls-hooked'; import { MongoClient } from 'mongodb'; import request from 'supertest'; -import { DEFAULT_CONNECTION_NAME, MONGO_SESSION_KEY, SESSION_LOADER_NAMESPACE } from '../constants'; -import { DataloaderService } from '../dataloader/service'; -import { getConnectionToken, getManagerToken, getRepositoryToken, ObjectId } from '../helpers'; -import { MongoManager } from '../manager'; -import { MongoModule } from '../module'; -import { MongoCoreModule } from '../module.core'; +import { DEFAULT_CONNECTION_NAME, MONGO_SESSION_KEY, SESSION_LOADER_NAMESPACE } from '../src/constants'; +import { DataloaderService } from '../src/dataloader/service'; +import { getConnectionToken, getManagerToken, getRepositoryToken, ObjectId } from '../src/helpers'; +import { MongoManager } from '../src/manager'; +import { MongoModule } from '../src/module'; +import { MongoCoreModule } from '../src/module.core'; import { - CascadeType, - getChildrenRelationshipMetadata, - getRelationshipCascadesMetadata, - getRelationshipMetadata, - getRelationshipsCascadesMetadata, -} from '../relationship/metadata'; -import { MongoRepository } from '../repository'; + CascadeType, + getChildrenRelationshipMetadata, + getRelationshipCascadesMetadata, + getRelationshipMetadata, + getRelationshipsCascadesMetadata, +} from '../src/relationship/metadata'; +import { MongoRepository } from '../src/repository'; import { MongoDbModuleTest } from './module'; import { RelationshipEntityLevel1Test } from './module/cascade/level1'; import { RelationshipEntityLevel1WithChildrenTest } from './module/cascade/level1WithChildren'; @@ -27,10 +29,10 @@ import { EntityChildTest } from './module/child'; import { TestController } from './module/controller'; import { EntityTest, TEST_COLLECTION_NAME } from './module/entity'; import { - ChildDynamicRelationship, - DynamicRelationshipType, - ParentDynamicRelationship1, - ParentDynamicRelationship2, + ChildDynamicRelationship, + DynamicRelationshipType, + ParentDynamicRelationship1, + ParentDynamicRelationship2, } from './module/entity.dynamic.relationship'; import { EntityWithIndexTest } from './module/entity.index'; import { EntityNestedTest } from './module/entity.nested'; @@ -513,15 +515,27 @@ describe('Indexes', () => { .getCollection(EntityUniqueRelationship) .indexes(); - expect(indexes).toHaveLength(3); - expect(indexes[1].unique).toBe(true); - expect(indexes[1].sparse).toBe(true); - expect(indexes[1].key.child).toBe(1); + expect(indexes).toHaveLength(5); + + // bar + expect(indexes[1].name).toBe('bar_1'); + expect(indexes[1].key.bar).toBe(1); expect(indexes[2].unique).toBe(true); expect(indexes[2].sparse).toBe(true); + expect(indexes[2].name).toBe('child_1_child2_1'); expect(indexes[2].key.child).toBe(1); expect(indexes[2].key.child2).toBe(1); + + expect(indexes[3].key.child).toBe(1); + expect(indexes[3].name).toBe( + 'EntityUniqueRelationship_child_relationship' + ); + + expect(indexes[4].key.child2).toBe(1); + expect(indexes[4].name).toBe( + 'EntityUniqueRelationship_child2_relationship' + ); }); }); diff --git a/src/test/module/cascade/level1.ts b/test/module/cascade/level1.ts similarity index 51% rename from src/test/module/cascade/level1.ts rename to test/module/cascade/level1.ts index b6928d4b..372be767 100644 --- a/src/test/module/cascade/level1.ts +++ b/test/module/cascade/level1.ts @@ -1,5 +1,5 @@ -import { Collection } from '../../../decorators'; -import { Entity } from '../../../entity'; +import { Collection } from '../../../src/decorators'; +import { Entity } from '../../../src/entity'; @Collection('relationshipEntityLevel1Test') export class RelationshipEntityLevel1Test extends Entity {} diff --git a/src/test/module/cascade/level1WithChildren.ts b/test/module/cascade/level1WithChildren.ts similarity index 57% rename from src/test/module/cascade/level1WithChildren.ts rename to test/module/cascade/level1WithChildren.ts index 71223647..5be87085 100644 --- a/src/test/module/cascade/level1WithChildren.ts +++ b/test/module/cascade/level1WithChildren.ts @@ -1,10 +1,10 @@ import { IsDefined } from 'class-validator'; -import { Collection } from '../../../decorators'; -import { Entity } from '../../../entity'; -import { ObjectId } from '../../../helpers'; -import { IsValidRelationship, Relationship } from '../../../relationship/decorators'; -import { CascadeType } from '../../../relationship/metadata'; +import { Collection } from '../../../src/decorators'; +import { Entity } from '../../../src/entity'; +import { ObjectId } from '../../../src/helpers'; +import { IsValidRelationship, Relationship } from '../../../src/relationship/decorators'; +import { CascadeType } from '../../../src/relationship/metadata'; import { EntityTest } from '../entity'; @Collection('relationshipEntityLevel1WithChildrenTest') diff --git a/src/test/module/cascade/level2.ts b/test/module/cascade/level2.ts similarity index 56% rename from src/test/module/cascade/level2.ts rename to test/module/cascade/level2.ts index ee34ae50..b934edd0 100644 --- a/src/test/module/cascade/level2.ts +++ b/test/module/cascade/level2.ts @@ -1,10 +1,10 @@ import { IsDefined } from 'class-validator'; -import { Collection } from '../../../decorators'; -import { Entity } from '../../../entity'; -import { ObjectId } from '../../../helpers'; -import { IsValidRelationship, Relationship } from '../../../relationship/decorators'; -import { CascadeType } from '../../../relationship/metadata'; +import { Collection } from '../../../src/decorators'; +import { Entity } from '../../../src/entity'; +import { ObjectId } from '../../../src/helpers'; +import { IsValidRelationship, Relationship } from '../../../src/relationship/decorators'; +import { CascadeType } from '../../../src/relationship/metadata'; import { RelationshipEntityLevel1Test } from './level1'; @Collection('relationshipEntityLevel2Test') diff --git a/src/test/module/cascade/level3.ts b/test/module/cascade/level3.ts similarity index 56% rename from src/test/module/cascade/level3.ts rename to test/module/cascade/level3.ts index c9fbfa81..5812db55 100644 --- a/src/test/module/cascade/level3.ts +++ b/test/module/cascade/level3.ts @@ -1,10 +1,10 @@ import { IsDefined } from 'class-validator'; -import { Collection } from '../../../decorators'; -import { Entity } from '../../../entity'; -import { ObjectId } from '../../../helpers'; -import { IsValidRelationship, Relationship } from '../../../relationship/decorators'; -import { CascadeType } from '../../../relationship/metadata'; +import { Collection } from '../../../src/decorators'; +import { Entity } from '../../../src/entity'; +import { ObjectId } from '../../../src/helpers'; +import { IsValidRelationship, Relationship } from '../../../src/relationship/decorators'; +import { CascadeType } from '../../../src/relationship/metadata'; import { RelationshipEntityLevel2Test } from './level2'; @Collection('relationshipEntityLevel3Test') diff --git a/src/test/module/child.ts b/test/module/child.ts similarity index 78% rename from src/test/module/child.ts rename to test/module/child.ts index 5f747b53..1d51a626 100644 --- a/src/test/module/child.ts +++ b/test/module/child.ts @@ -1,10 +1,10 @@ import { Type } from 'class-transformer'; import { IsDefined, IsOptional, IsString, ValidateNested } from 'class-validator'; -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { ObjectId } from '../../helpers'; -import { IsValidRelationship, Relationship } from '../../relationship/decorators'; +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { ObjectId } from '../../src/helpers'; +import { IsValidRelationship, Relationship } from '../../src/relationship/decorators'; import { EntityTest } from './entity'; import { EntityNestedTest } from './entity.nested'; import { EntityRelationship } from './entity.relationship'; diff --git a/src/test/module/controller.ts b/test/module/controller.ts similarity index 78% rename from src/test/module/controller.ts rename to test/module/controller.ts index c71c60dd..bbcde81f 100644 --- a/src/test/module/controller.ts +++ b/test/module/controller.ts @@ -1,10 +1,10 @@ import { Controller, Get, Req } from '@nestjs/common'; import { Request } from 'express'; -import { DATA_LOADER_NAMESPACE } from '../../constants'; -import { InjectManager } from '../../decorators'; -import { ObjectId } from '../../helpers'; -import { MongoManager } from '../../manager'; +import { DATA_LOADER_NAMESPACE } from '../../src/constants'; +import { InjectManager } from '../../src/decorators'; +import { ObjectId } from '../../src/helpers'; +import { MongoManager } from '../../src/manager'; import { EntityTest } from './entity'; @Controller('test') diff --git a/src/test/module/entity.dynamic.relationship.ts b/test/module/entity.dynamic.relationship.ts similarity index 88% rename from src/test/module/entity.dynamic.relationship.ts rename to test/module/entity.dynamic.relationship.ts index 78cd37a8..130e867b 100644 --- a/src/test/module/entity.dynamic.relationship.ts +++ b/test/module/entity.dynamic.relationship.ts @@ -1,10 +1,10 @@ import { IsIn, IsString } from 'class-validator'; import { ObjectId } from 'mongodb'; -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { Relationship } from '../../relationship/decorators'; -import { CascadeType } from '../../relationship/metadata'; +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { Relationship } from '../../src/relationship/decorators'; +import { CascadeType } from '../../src/relationship/metadata'; @Collection('testdynamicparentrelationship1') export class ParentDynamicRelationship1 extends Entity { diff --git a/test/module/entity.index.ts b/test/module/entity.index.ts new file mode 100644 index 00000000..c1c41c8b --- /dev/null +++ b/test/module/entity.index.ts @@ -0,0 +1,11 @@ +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { Index } from '../../src/indexes/decorators'; + +@Collection('testwithindex') +export class EntityWithIndexTest extends Entity { + @Index({ + unique: true + }) + foo: string; +} diff --git a/src/test/module/entity.nested.ts b/test/module/entity.nested.ts similarity index 62% rename from src/test/module/entity.nested.ts rename to test/module/entity.nested.ts index 45ce4618..a34bf8ea 100644 --- a/src/test/module/entity.nested.ts +++ b/test/module/entity.nested.ts @@ -1,9 +1,9 @@ import { IsDefined } from 'class-validator'; -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { ObjectId } from '../../helpers'; -import { Relationship } from '../../relationship/decorators'; +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { ObjectId } from '../../src/helpers'; +import { Relationship } from '../../src/relationship/decorators'; import { EntityTest } from './entity'; @Collection('testnested') diff --git a/src/test/module/entity.relationship.ts b/test/module/entity.relationship.ts similarity index 71% rename from src/test/module/entity.relationship.ts rename to test/module/entity.relationship.ts index 9c2f1300..0a75add7 100644 --- a/src/test/module/entity.relationship.ts +++ b/test/module/entity.relationship.ts @@ -1,9 +1,9 @@ import { IsString } from 'class-validator'; import { ObjectId } from 'mongodb'; -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; -import { Relationship } from '../../relationship/decorators'; +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { Relationship } from '../../src/relationship/decorators'; import { EntityChildTest } from './child'; @Collection('testrelationship') diff --git a/test/module/entity.relationship.unique.ts b/test/module/entity.relationship.unique.ts new file mode 100644 index 00000000..02e789bc --- /dev/null +++ b/test/module/entity.relationship.unique.ts @@ -0,0 +1,32 @@ +import { IsString } from 'class-validator'; +import { ObjectId } from 'mongodb'; + +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; +import { Index } from '../../src/indexes/decorators'; +import { Relationship } from '../../src/relationship/decorators'; +import { EntityChildTest } from './child'; + +export const TEST_COLLECTION_NAME = 'testuniquerelationship'; + +@Collection(TEST_COLLECTION_NAME) +export class EntityUniqueRelationship extends Entity { + @IsString() + @Index() + bar: string; + + @Relationship({ + type: () => EntityChildTest + }) + @Index({ + key: { child: 1, child2: 1 }, + unique: true, + sparse: true + }) + child?: ObjectId; + + @Relationship({ + type: () => EntityChildTest + }) + child2?: ObjectId; +} diff --git a/src/test/module/entity.slug.ts b/test/module/entity.slug.ts similarity index 93% rename from src/test/module/entity.slug.ts rename to test/module/entity.slug.ts index fa2b1227..16b66857 100644 --- a/src/test/module/entity.slug.ts +++ b/test/module/entity.slug.ts @@ -1,6 +1,6 @@ import { Type } from 'class-transformer'; -import { Slugify } from '../../decorators'; +import { Slugify } from '../../src/decorators'; export class EntitySlugTest { constructor(firstName: string, lastName: string) { diff --git a/src/test/module/entity.ts b/test/module/entity.ts similarity index 71% rename from src/test/module/entity.ts rename to test/module/entity.ts index 7b7b5199..0c76c0ca 100644 --- a/src/test/module/entity.ts +++ b/test/module/entity.ts @@ -1,7 +1,7 @@ import { IsString } from 'class-validator'; -import { Collection } from '../../decorators'; -import { Entity } from '../../entity'; +import { Collection } from '../../src/decorators'; +import { Entity } from '../../src/entity'; export const TEST_COLLECTION_NAME = 'test'; diff --git a/src/test/module/index.ts b/test/module/index.ts similarity index 88% rename from src/test/module/index.ts rename to test/module/index.ts index 502ab744..380ba3f5 100644 --- a/src/test/module/index.ts +++ b/test/module/index.ts @@ -1,9 +1,9 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; -import { DataLoaderMiddleware } from '../../dataloader/middleware'; -import { InjectRepository } from '../../decorators'; -import { MongoModule } from '../../module'; -import { MongoRepository } from '../../repository'; +import { DataLoaderMiddleware } from '../../src/dataloader/middleware'; +import { InjectRepository } from '../../src/decorators'; +import { MongoModule } from '../../src/module'; +import { MongoRepository } from '../../src/repository'; import { RelationshipEntityLevel1Test } from './cascade/level1'; import { RelationshipEntityLevel1WithChildrenTest } from './cascade/level1WithChildren'; import { RelationshipEntityLevel2Test } from './cascade/level2'; diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100755 index 00000000..e20f937f --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": ["src"] +} diff --git a/tsconfig.json b/tsconfig.json index f94fdd2c..385f2f65 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,6 @@ "allowSyntheticDefaultImports": true, "esModuleInterop": true, "strictNullChecks": true - }, - "include": ["src/**/*"] + } + // "include": ["src/**/*", "test"] }