Skip to content

Commit

Permalink
feat(query-typegoose): Adds the ability to use discriminators
Browse files Browse the repository at this point in the history
Fixes #1320
  • Loading branch information
smolinari committed Aug 30, 2021
1 parent 56bb56c commit f403f7d
Show file tree
Hide file tree
Showing 8 changed files with 1,938 additions and 255 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 67 additions & 13 deletions packages/query-typegoose/__tests__/__fixtures__/seeds.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
/* eslint-disable no-underscore-dangle,@typescript-eslint/no-unsafe-return */
import { Connection } from 'mongoose';
import { getModelForClass, DocumentType } from '@typegoose/typegoose';
import { getModelForClass, DocumentType, getDiscriminatorModelForClass } from '@typegoose/typegoose';
import { TestEntity } from './test.entity';
import { TestDiscriminatedEntity } from './test-discriminated.entity';
import { TestReference } from './test-reference.entity';

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

export const TEST_DISCRIMINATED_ENTITIES: DocumentType<TestDiscriminatedEntity>[] = [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ].map(
(i) =>
({
boolType: i % 2 === 0,
dateType: new Date(`2020-02-${i}`),
numberType: i,
stringType: `foo${i}`,
stringType2: `bar${i}`,
} as DocumentType<TestDiscriminatedEntity>),
);

export const TEST_REFERENCES: DocumentType<TestReference>[] = TEST_ENTITIES.reduce(
Expand All @@ -30,20 +42,46 @@ export const TEST_REFERENCES: DocumentType<TestReference>[] = TEST_ENTITIES.redu
[] as DocumentType<TestReference>[],
);

export const TEST_REFERENCES_FOR_DISCRIMINATES: DocumentType<TestReference>[] = TEST_DISCRIMINATED_ENTITIES.reduce(
(relations, tde) => [
...relations,
{
referenceName: `${tde.stringType}-test-reference-1-one`,
} as DocumentType<TestReference>,
{
referenceName: `${tde.stringType}-test-reference-2-two`,
} as DocumentType<TestReference>,
{
referenceName: `${tde.stringType}-test-reference-3-three`,
} as DocumentType<TestReference>,
],
[] as DocumentType<TestReference>[],
);

export const seed = async (connection: Connection): Promise<void> => {
const TestEntityModel = getModelForClass(TestEntity, { existingConnection: connection });
const TestReferencesModel = getModelForClass(TestReference, { existingConnection: connection });
const TestDiscriminatedModel = getDiscriminatorModelForClass(TestEntityModel, TestDiscriminatedEntity);

const testEntities = await TestEntityModel.create(TEST_ENTITIES);
const testDiscriminatedEntities = await TestDiscriminatedModel.create(TEST_DISCRIMINATED_ENTITIES);
const testReferences = await TestReferencesModel.create(TEST_REFERENCES);
testEntities.forEach((te, index) => Object.assign(TEST_ENTITIES[index], te.toObject({ virtuals: true })));
testReferences.forEach((tr, index) => Object.assign(TEST_REFERENCES[index], tr.toObject({ virtuals: true })));
const testReferencesForDiscriminates = await TestReferencesModel.create(TEST_REFERENCES_FOR_DISCRIMINATES);

testEntities.forEach((te, index) => Object.assign(TEST_ENTITIES[ index ], te.toObject({ virtuals: true })));

testDiscriminatedEntities.forEach((tde, index) => Object.assign(TEST_DISCRIMINATED_ENTITIES[ index ], tde.toObject({ virtuals: true })));

testReferences.forEach((tr, index) => Object.assign(TEST_REFERENCES[ index ], tr.toObject({ virtuals: true })));

testReferencesForDiscriminates.forEach((trfd, index) => Object.assign(TEST_REFERENCES_FOR_DISCRIMINATES[ index ], trfd.toObject({ virtuals: true })));

await Promise.all(
testEntities.map(async (te, index) => {
const references = testReferences.filter((tr: TestReference) => tr.referenceName.includes(`${te.stringType}-`));
TEST_ENTITIES[index].testReference = references[0]._id;
TEST_ENTITIES[index].testReferences = references.map((r) => r._id);
await te.updateOne({ $set: { testReferences: references.map((r) => r._id), testReference: references[0]._id } });
TEST_ENTITIES[ index ].testReference = references[ 0 ]._id;
TEST_ENTITIES[ index ].testReferences = references.map((r) => r._id);
await te.updateOne({ $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
Expand All @@ -53,4 +91,20 @@ export const seed = async (connection: Connection): Promise<void> => {
);
}),
);

await Promise.all(
testDiscriminatedEntities.map(async (tde, index) => {
const references = testReferencesForDiscriminates.filter((trfd: TestReference) => trfd.referenceName.includes(`${tde.stringType}-`));
TEST_DISCRIMINATED_ENTITIES[ index ].testReference = references[ 0 ]._id;
TEST_DISCRIMINATED_ENTITIES[ index ].testReferences = references.map((r) => r._id);
await tde.updateOne({ $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_FOR_DISCRIMINATES.find((tr) => tr._id.toString() === r._id.toString())!.testEntity = tde._id;
return r.updateOne({ $set: { testEntity: tde._id } });
}),
);
}),
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { prop } from '@typegoose/typegoose';
import { TestEntity } from './test.entity';

export class TestDiscriminatedEntity extends TestEntity {

@prop({ required: true })
stringType2!: string;

}
Loading

0 comments on commit f403f7d

Please sign in to comment.