Skip to content

Commit

Permalink
feat(query-typegoose): Adds the ability to use discriminators (#1321)
Browse files Browse the repository at this point in the history
* feat(query-typegoose): Adds the ability to use discriminators 

Fixes #1320

* fix: removes package.json

* Revert "fix: removes package.json"

This reverts commit cc5d821.

* fix: adds package-lock.json

* fix: corrects linting issues adds missing added package

* fix(query-typegoose): minor changes

* fix(query-typegoose): fixes linting (again)

* fix(query-typegoose): fixes conflict in package-lock.json

Co-authored-by: Scott Molinari <scott.molinari@m8a.io>
  • Loading branch information
smolinari and smolinari authored Sep 6, 2021
1 parent 0097d5a commit 2a7da59
Show file tree
Hide file tree
Showing 9 changed files with 1,784 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

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

0 comments on commit 2a7da59

Please sign in to comment.