-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongoose): Switch to native mongoose support
- Loading branch information
1 parent
de34e92
commit 5cdfa39
Showing
44 changed files
with
2,666 additions
and
1,348 deletions.
There are no files selected for viewing
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
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
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
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
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
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
File renamed without changes.
File renamed without changes.
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,4 @@ | ||
export * from './seeds'; | ||
export * from './test.entity'; | ||
export * from './test-reference.entity'; | ||
export * from './connection.fixture'; |
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,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 } }); | ||
}), | ||
); | ||
}), | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/query-mongoose/__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,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
33
packages/query-mongoose/__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 { 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', | ||
}); |
6 changes: 3 additions & 3 deletions
6
.../query-typegoose/__tests__/module.spec.ts → ...s/query-mongoose/__tests__/module.spec.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
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.