Skip to content

Commit

Permalink
refactor(api): Simplified Dao API
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh4 committed Jun 15, 2024
1 parent 08d3fe7 commit b4ea255
Show file tree
Hide file tree
Showing 111 changed files with 4,007 additions and 3,849 deletions.
6,282 changes: 3,325 additions & 2,957 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { IAppConfig } from '@lyvely/interface';

describe('AppConfigService', () => {
let testingModule: ILyvelyTestingModule;
let app: INestApplication;
let appConfigService: AppConfigService;

@Injectable()
Expand All @@ -27,14 +26,12 @@ describe('AppConfigService', () => {
testingModule = await buildTest('AppConfigService')
.imports([TestModule])
.providers([AppConfigService])
.withApp()
.compile();
app = testingModule.createNestApplication();
appConfigService = app.get(AppConfigService);
await app.init();
appConfigService = testingModule.get(AppConfigService);
});

afterEach(async () => {
await app.close();
await testingModule.afterEach();
});

Expand Down
4 changes: 0 additions & 4 deletions packages/core/api/src/avatars/services/avatar.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ describe('AvatarService', () => {
return testingModule.afterAll();
});

it('Should be defined', () => {
expect(avatarService).toBeDefined();
});

const getBufferedAvatar = (): IMemoryFileInfo => {
if (!testMemoryAvatar) {
const avatarPath = path.join(__dirname, '../testing/data/avatar.jpeg');
Expand Down
16 changes: 3 additions & 13 deletions packages/core/api/src/captcha/daos/captcha.dao.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { AbstractDao, Model } from '@/core';
import { AbstractDao, Dao } from '@/core';
import { Captcha } from '../schemas';
import { InjectModel } from '@nestjs/mongoose';

@Dao(Captcha)
export class CaptchaDao extends AbstractDao<Captcha> {
constructor(@InjectModel(Captcha.name) protected model: Model<Captcha>) {
super();
}
protected modelName = Captcha.name;

async findCaptchaByIdentity(identity: string) {
return this.findOne({ identity: identity });
}

getModelConstructor() {
return Captcha;
}

getModuleId(): string {
return 'captcha';
}
}
8 changes: 1 addition & 7 deletions packages/core/api/src/content/content.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,16 @@ describe('content module', () => {
testingModule = await buildContentTest('content_module')
.imports([TestModule])
.models(TestModels)
.withApp()
.compile();
contentService = testingModule.get<ContentService>(ContentService);
contentTypeRegistry = testingModule.get<ContentTypeRegistry>(ContentTypeRegistry);
app = testingModule.createNestApplication();
await app.init();
});

afterEach(async () => {
return testingModule.afterEach();
});

it('should be defined', () => {
expect(contentService).toBeDefined();
expect(contentTypeRegistry).toBeDefined();
});

describe('auto content registration', () => {
it('assure content types are registered', async () => {
expect(contentTypeRegistry.getTypeDefinition(TestContent.name)).toBeDefined();
Expand Down
20 changes: 3 additions & 17 deletions packages/core/api/src/content/daos/content-score.dao.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import { ProfileScoreTypeDao } from '@/profiles';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from '@/core';
import { ContentScore } from '../schemas';
import { CONTENT_MODULE_ID } from '@lyvely/interface';
import { Dao } from '@/core';

@Injectable()
export class ContentScoreDao extends ProfileScoreTypeDao<ContentScore> {
@InjectModel(ContentScore.name)
protected model: Model<ContentScore>;

getModelConstructor() {
return ContentScore;
}

getModuleId(): string {
return CONTENT_MODULE_ID;
}
}
@Dao(ContentScore)
export class ContentScoreDao extends ProfileScoreTypeDao<ContentScore> {}
21 changes: 5 additions & 16 deletions packages/core/api/src/content/daos/content.dao.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Content } from '../schemas';
import { Injectable } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import { ContentTypeRegistry } from '../components';
import { InjectModel } from '@nestjs/mongoose';
import { Model, DocumentIdentity, LeanDoc, TObjectId } from '@/core';
import { Dao, DocumentIdentity, TObjectId } from '@/core';
import { ContentTypeDao } from './content-type.dao';
import { ProfileShardData } from '@/profiles';

@Injectable()
@Dao(Content)
export class ContentDao extends ContentTypeDao<Content> {
constructor(
@InjectModel(Content.name) protected model: Model<Content>,
protected contentTypeRegistry: ContentTypeRegistry
) {
super();
}
@Inject()
protected override typeRegistry: ContentTypeRegistry;

incrementChildCount(context: ProfileShardData, parent: DocumentIdentity<Content>) {
return this.updateOneByProfileAndId(context, parent, { $inc: { 'meta.childCount': 1 } });
Expand All @@ -36,12 +31,6 @@ export class ContentDao extends ContentTypeDao<Content> {
return this.updateOneByProfileAndIdSet(context, content, { 'meta.mid': mid });
}

getModelConstructor(model: LeanDoc<Content>) {
return model?.type && this.contentTypeRegistry.isRegisteredType(model.type)
? this.contentTypeRegistry.getTypeConstructor(model.type) || Content
: Content;
}

getModuleId(): string {
return 'content';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ describe('ContentScore', () => {
return testingModule.afterEach();
});

it('should be defined', () => {
expect(ExtendedTestContentScoreModel).toBeDefined();
});

describe('constructor', () => {
it('Construct ExtendedTestContentScoreModel', async () => {
const { user, profile, context } = await testDataUtils.createUserAndProfile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ describe('ContentScoreService', () => {
return testingModule.afterEach();
});

it('should be defined', () => {
expect(contentScoreService).toBeDefined();
});

async function createGroupAndContent() {
const testData = await testDataUtils.createSimpleGroup();
const content = new TestContent(testData.ownerContext, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ describe('content dao', () => {
return new TestContent({ profile, user }, entity.toObject());
}

it('should be defined', () => {
expect(contentService).toBeDefined();
});

describe('archive', () => {
it('archive content', async () => {
const { owner, ownerContext, member, profile } = await testData.createSimpleGroup();
Expand Down
5 changes: 2 additions & 3 deletions packages/core/api/src/content/testing/content-test.builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LyvelyTestBuilder } from '@/testing';
import { ProfileTestBuilder } from '@/profiles';
import { contentITestPlugin } from './content-test.plugin';

Expand All @@ -9,6 +8,6 @@ export class ContentTestBuilder extends ProfileTestBuilder {
}
}

export function buildContentTest(id: string, init: Partial<LyvelyTestBuilder> = {}) {
return new ContentTestBuilder(id, init);
export function buildContentTest(id: string) {
return new ContentTestBuilder(id);
}
20 changes: 3 additions & 17 deletions packages/core/api/src/content/testing/test-content.dao.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import { ContentTypeDao } from '../daos';
import { TestContent } from './test-content.schema';
import { Type } from '@lyvely/common';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from '@/core';
import { Dao } from '@/core';

@Injectable()
export class TestContentDao extends ContentTypeDao<TestContent> {
@InjectModel(TestContent.name)
protected model: Model<TestContent>;

getModelConstructor(): Type<any> {
return TestContent;
}

getModuleId(): string {
return 'test';
}
}
@Dao(TestContent)
export class TestContentDao extends ContentTypeDao<TestContent> {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const EVENT_REGISTRATION = 'EVENT_REGISTRATION';
export abstract class AbstractTypeRegistry<T, TMeta = any, TModuleMetaView = any> {
protected abstract logger: Logger;

protected fallBackType?: Type<T>;

private typeMapping: Map<InjectionToken, ITypeRegistryDefinition<T>> = new Map();
private typeMeta: Map<InjectionToken, TMeta> = new Map();
protected emitter: EventEmitter2;
Expand Down Expand Up @@ -123,7 +125,7 @@ export abstract class AbstractTypeRegistry<T, TMeta = any, TModuleMetaView = any
getTypeConstructor(token: InjectionToken, defaultType: Type<T>): Type<T>;
getTypeConstructor(token: InjectionToken, defaultType?: Type<T>): Type<T> | undefined {
const definition = this.getTypeDefinition(token);
return definition ? definition.constructor : defaultType;
return definition ? definition.constructor : defaultType || this.fallBackType;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/core/api/src/core/core.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ describe('CoreModule', () => {
await afterEachTest(TEST_KEY, testingModule);
});

it('registry is defined', () => {
expect(moduleRegistry).toBeDefined();
});

it('core module is registered', () => {
expect(moduleRegistry.getTypeMeta('core')).toBeDefined();
expect(moduleRegistry.getTypeMeta('core')?.id).toEqual('core');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { InjectModel, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { AbstractDao } from './abstract.dao';
import { Model, TObjectId } from './db.type';
import { createCoreTestingModule, afterEachTest } from '../testing/core-test.util';
import { TObjectId } from '../interfaces';
import { createCoreTestingModule, afterEachTest } from '../../testing/core-test.util';
import { ModelDefinition } from '@nestjs/mongoose/dist/interfaces';
import { Injectable } from '@nestjs/common';
import { type INestApplication } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import {
BaseDocument,
type BaseDocumentData,
type BaseDocumentData, Dao,
DiscriminatorDocumentTransformer,
DiscriminatorTransformation,
IDocumentTransformation,
Expand Down Expand Up @@ -62,21 +62,15 @@ class TestEntityTransformer extends DiscriminatorDocumentTransformer<TestEntity>
}
}

@Injectable()
@Dao(TestEntity, {
discriminator: {
[TestEntityA.name]: TestEntityA,
[TestEntityB.name]: TestEntityB,
}
})
class TestEntityDao extends AbstractDao<TestEntity> {
@InjectModel(TestEntity.name) protected model: Model<TestEntity>;

protected modelName = TestEntity.name;
override transformer = new TestEntityTransformer();

getModelConstructor(model: LeanDoc<TestEntity>) {
if (model.type === TestEntityA.name) return TestEntityA;
if (model.type === TestEntityB.name) return TestEntityB;
return TestEntity;
}

getModuleId(): string {
return 'test';
}
}

const TEST_KEY = 'abstract_dao';
Expand All @@ -93,24 +87,24 @@ const TestEntityModelDefinition: ModelDefinition = {
describe('AbstractDao', () => {
let testingModule: TestingModule;
let dao: TestEntityDao;
let app: INestApplication;

beforeEach(async () => {
testingModule = await createCoreTestingModule(
TEST_KEY,
[TestEntityDao],
[TestEntityModelDefinition]
).compile();

app = testingModule.createNestApplication();
dao = testingModule.get(TestEntityDao);
});

afterEach(async () => {
await app.close();
await afterEachTest(TEST_KEY, testingModule);
});

it('should be defined', () => {
expect(dao).toBeDefined();
});

describe('AbstractDao discriminator transformation', () => {
it('Generic transformation is applied to all documents', async () => {
class TestEntityTransformation implements IDocumentTransformation<TestEntity> {
Expand Down
Loading

0 comments on commit b4ea255

Please sign in to comment.