Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query-typegoose): Adds the ability to use discriminators #1321

Merged
merged 8 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,507 changes: 1,414 additions & 1,093 deletions package-lock.json

Large diffs are not rendered by default.

64 changes: 63 additions & 1 deletion packages/query-typegoose/__tests__/__fixtures__/seeds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* 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(
Expand All @@ -14,6 +15,19 @@ export const TEST_ENTITIES: DocumentType<TestEntity>[] = [1, 2, 3, 4, 5, 6, 7, 8
} 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(
(relations, te) => [
...relations,
Expand All @@ -30,14 +44,44 @@ 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);
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}-`));
Expand All @@ -53,4 +97,22 @@ 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,7 @@
import { prop } from '@typegoose/typegoose';
import { TestEntity } from './test.entity';

export class TestDiscriminatedEntity extends TestEntity {
@prop({ required: true })
stringType2!: string;
}
Loading