-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongodb): Use typegoose for MongoDB support
- Loading branch information
1 parent
22decdf
commit 702dc83
Showing
27 changed files
with
313 additions
and
265 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
packages/query-typegoose/__tests__/__fixtures__/connection.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { connections } from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import { seed } from './seeds'; | ||
|
||
const mongoServer = new MongoMemoryServer(); | ||
|
||
export function getConnectionUri(): Promise<string> { | ||
return mongoServer.getUri(); | ||
} | ||
|
||
export const dropDatabase = async (): Promise<void> => { | ||
await connections[connections.length - 1].dropDatabase(); | ||
}; | ||
|
||
export const prepareDb = async (): Promise<void> => { | ||
await seed(connections[connections.length - 1]); | ||
}; | ||
|
||
export const closeDbConnection = async (): Promise<void> => { | ||
await connections[connections.length - 1].close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Connection } from 'mongoose'; | ||
import { plainToClass } from 'class-transformer'; | ||
import { TestEntity } from './test.entity'; | ||
import { TestReference } from './test-reference.entity'; | ||
|
||
export const TEST_ENTITIES: TestEntity[] = new Array(15) | ||
.fill(0) | ||
.map((e, i) => i + 1) | ||
.map((i) => { | ||
return plainToClass(TestEntity, { | ||
boolType: i % 2 === 0, | ||
dateType: new Date(`2020-02-${i}`), | ||
numberType: i, | ||
stringType: `foo${i}`, | ||
}); | ||
}); | ||
|
||
export const TEST_REFERENCES: TestReference[] = [1, 2, 3, 4, 5].map((i) => { | ||
return plainToClass(TestReference, { | ||
name: `name${i}`, | ||
}); | ||
}); | ||
|
||
export const seed = async (connection: Connection): Promise<void> => { | ||
await connection.collection('testentities').insertMany(TEST_ENTITIES); | ||
await connection.collection('testreferences').insertMany(TEST_REFERENCES); | ||
|
||
await Promise.all( | ||
TEST_REFERENCES.map((testReference, i) => { | ||
return connection | ||
.collection('testentities') | ||
.updateOne({ stringType: TEST_ENTITIES[i + 10].stringType }, { $set: { testReference: testReference.id } }); | ||
}), | ||
); | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/query-typegoose/__tests__/__fixtures__/test-reference.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mongoose, prop } from '@typegoose/typegoose'; | ||
|
||
export class TestReference { | ||
get id() { | ||
const idKey = '_id'; | ||
return ((this as unknown) as Record<string, mongoose.Types.ObjectId>)[idKey]?.toString(); | ||
} | ||
|
||
@prop({ required: true }) | ||
name!: string; | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/query-typegoose/__tests__/__fixtures__/test.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { mongoose, prop, Ref } from '@typegoose/typegoose'; | ||
import { TestReference } from './test-reference.entity'; | ||
|
||
export class TestEntity { | ||
get id() { | ||
const idKey = '_id'; | ||
return ((this as unknown) as Record<string, mongoose.Types.ObjectId>)[idKey]?.toString(); | ||
} | ||
|
||
@prop({ required: true }) | ||
stringType!: string; | ||
|
||
@prop({ required: true }) | ||
boolType!: boolean; | ||
|
||
@prop({ required: true }) | ||
numberType!: number; | ||
|
||
@prop({ required: true }) | ||
dateType!: Date; | ||
|
||
@prop({ ref: TestReference }) | ||
testReference?: Ref<TestReference>; | ||
|
||
getInputData() { | ||
return { | ||
stringType: this.stringType, | ||
boolType: this.boolType, | ||
numberType: this.numberType, | ||
dateType: this.dateType, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NestjsQueryTypegooseModule } from '../src'; | ||
|
||
describe('NestjsQueryTypegooseModule', () => { | ||
it('should create a module', () => { | ||
class TestEntity {} | ||
const typeOrmModule = NestjsQueryTypegooseModule.forFeature([TestEntity]); | ||
expect(typeOrmModule.imports).toHaveLength(1); | ||
expect(typeOrmModule.module).toBe(NestjsQueryTypegooseModule); | ||
expect(typeOrmModule.providers).toHaveLength(1); | ||
expect(typeOrmModule.exports).toHaveLength(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getQueryServiceToken } from '@nestjs-query/core'; | ||
import { instance } from 'ts-mockito'; | ||
import { createTypegooseQueryServiceProviders } from '../src/providers'; | ||
import { TypegooseQueryService } from '../src/services'; | ||
|
||
describe('createTypegooseQueryServiceProviders', () => { | ||
it('should create a provider for the entity', () => { | ||
class TestEntity {} | ||
const providers = createTypegooseQueryServiceProviders([TestEntity]); | ||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.