Skip to content

Commit

Permalink
feat(mongoose): Switch to native mongoose support
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Oct 16, 2020
1 parent de34e92 commit 5cdfa39
Show file tree
Hide file tree
Showing 44 changed files with 2,666 additions and 1,348 deletions.
1 change: 1 addition & 0 deletions jest.e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
preset: 'ts-jest',
testMatch: ['**/e2e/**/*.spec.ts'],
collectCoverageFrom: ['packages/**/*.ts', '!**/__tests__/**', '!**/dist/**', '!**/node_modules/**'],
testEnvironment: 'node',
moduleNameMapper: {
'^@nestjs-query/(.*)$': '<rootDir>/packages/$1/src',
},
Expand Down
1 change: 1 addition & 0 deletions jest.unit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
preset: 'ts-jest',
collectCoverageFrom: ['packages/**/*.ts', '!**/__tests__/**', '!**/dist/**', '!**/node_modules/**'],
testMatch: ['**/__tests__/**/*.spec.ts'],
testEnvironment: 'node',
moduleNameMapper: {
'^@nestjs-query/(.*)$': '<rootDir>/packages/$1/src',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"url": "https://github.com/doug-martin/nestjs-query/issues"
},
"dependencies": {
"lodash.merge": "4.6.2"
"lodash.merge": "^4.6.2"
},
"peerDependencies": {
"@nestjs/common": "^7.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/common/deep-partial.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export declare type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U2>
? ReadonlyArray<DeepPartial<U2>>
: DeepPartial<T[P]>;
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]> | T[P];
};
2 changes: 1 addition & 1 deletion packages/core/src/decorators/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assembler } from '../assemblers';
import { Class } from '../common';

export function getQueryServiceToken<DTO>(DTOClass: Class<DTO>): string {
export function getQueryServiceToken(DTOClass: { name: string }): string {
return `${DTOClass.name}QueryService`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
{
assertFunctionNames: [
'expect',
'assertFilterQuery',
'expectEqualEntities',
'expectEqualCreate'
],
},
],
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions packages/query-mongoose/__tests__/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './seeds';
export * from './test.entity';
export * from './test-reference.entity';
export * from './connection.fixture';
55 changes: 55 additions & 0 deletions packages/query-mongoose/__tests__/__fixtures__/seeds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-underscore-dangle */
import { Connection } from 'mongoose';
import { TestEntity } from './test.entity';
import { TestReference } from './test-reference.entity';

export const TEST_ENTITIES: TestEntity[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((i) => {
return {
boolType: i % 2 === 0,
dateType: new Date(`2020-02-${i}`),
numberType: i,
stringType: `foo${i}`,
} as TestEntity;
});

export const TEST_REFERENCES: TestReference[] = TEST_ENTITIES.reduce((relations, te) => {
return [
...relations,
{
referenceName: `${te.stringType}-test-reference-1-one`,
} as TestReference,
{
referenceName: `${te.stringType}-test-reference-2-two`,
} as TestReference,
{
referenceName: `${te.stringType}-test-reference-3-three`,
} as TestReference,
];
}, [] as TestReference[]);

export const seed = async (connection: Connection): Promise<void> => {
const TestEntityModel = connection.model<TestEntity>('TestEntity');
const TestReferencesModel = connection.model<TestReference>('TestReference');

const testEntities = await TestEntityModel.create(TEST_ENTITIES);
const testReferences = await TestReferencesModel.create(TEST_REFERENCES);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
testEntities.forEach((te, index) => Object.assign(TEST_ENTITIES[index], te.toObject()));
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
testReferences.forEach((tr, index) => Object.assign(TEST_REFERENCES[index], tr.toObject()));
await Promise.all(
testEntities.map(async (te, index) => {
const references = testReferences.filter((tr) => tr.referenceName.includes(`${te.stringType}-`));
TEST_ENTITIES[index].testReference = references[0]._id;
TEST_ENTITIES[index].testReferences = references.map((r) => r._id);
await te.update({ $set: { testReferences: references.map((r) => r._id), testReference: references[0]._id } });
await Promise.all(
references.map((r) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
TEST_REFERENCES.find((tr) => tr._id.toString() === r._id.toString())!.testEntity = te._id;
return r.update({ $set: { testEntity: te._id } });
}),
);
}),
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { SchemaTypes, Document, Types } from 'mongoose';

@Schema()
export class TestReference extends Document {
@Prop({ required: true })
referenceName!: string;

@Prop({ type: SchemaTypes.ObjectId, ref: 'TestEntity' })
testEntity?: Types.ObjectId;
}
export const TestReferenceSchema = SchemaFactory.createForClass(TestReference);
TestReferenceSchema.virtual('virtualTestEntity', {
ref: 'TestEntity',
localField: 'testEntity',
foreignField: '_id',
justOne: true,
});
33 changes: 33 additions & 0 deletions packages/query-mongoose/__tests__/__fixtures__/test.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Document, Types, SchemaTypes } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { TestReference } from 'packages/query-mongoose/__tests__/__fixtures__/test-reference.entity';

@Schema()
export class TestEntity extends Document {
@Prop({ required: true })
stringType!: string;

@Prop({ required: true })
boolType!: boolean;

@Prop({ required: true })
numberType!: number;

@Prop({ required: true })
dateType!: Date;

@Prop({ type: SchemaTypes.ObjectId, ref: 'TestReference' })
testReference?: Types.ObjectId;

@Prop([{ type: SchemaTypes.ObjectId, ref: 'TestReference' }])
testReferences?: Types.ObjectId[];

virtualTestReference?: TestReference;
}

export const TestEntitySchema = SchemaFactory.createForClass(TestEntity);
TestEntitySchema.virtual('virtualTestReferences', {
ref: 'TestReference',
localField: '_id',
foreignField: 'testEntity',
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NestjsQueryTypegooseModule } from '../src';
import { NestjsQueryMongooseModule } from '../src';

describe('NestjsQueryTypegooseModule', () => {
it('should create a module', () => {
class TestEntity {}
const typeOrmModule = NestjsQueryTypegooseModule.forFeature([TestEntity]);
const typeOrmModule = NestjsQueryMongooseModule.forFeature([TestEntity]);
expect(typeOrmModule.imports).toHaveLength(1);
expect(typeOrmModule.module).toBe(NestjsQueryTypegooseModule);
expect(typeOrmModule.module).toBe(NestjsQueryMongooseModule);
expect(typeOrmModule.providers).toHaveLength(1);
expect(typeOrmModule.exports).toHaveLength(2);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getQueryServiceToken } from '@nestjs-query/core';
import { instance } from 'ts-mockito';
import { createTypegooseQueryServiceProviders } from '../src/providers';
import { TypegooseQueryService } from '../src/services';
import { MongooseQueryService } from '../src/services';

describe('createTypegooseQueryServiceProviders', () => {
it('should create a provider for the entity', () => {
Expand All @@ -10,6 +10,6 @@ describe('createTypegooseQueryServiceProviders', () => {
expect(providers).toHaveLength(1);
expect(providers[0].provide).toBe(getQueryServiceToken(TestEntity));
expect(providers[0].inject).toEqual([`${TestEntity.name}Model`]);
expect(providers[0].useFactory(instance(() => {}))).toBeInstanceOf(TypegooseQueryService);
expect(providers[0].useFactory(instance(() => {}))).toBeInstanceOf(MongooseQueryService);
});
});
Loading

0 comments on commit 5cdfa39

Please sign in to comment.