introduce a performance backend
This commit is contained in:
25
src/versioning/dto/semanticversion.dto.ts
Normal file
25
src/versioning/dto/semanticversion.dto.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class SemanticVersionDto {
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({
|
||||
description: 'The main version to describe downwards compatibility issues',
|
||||
example: 1,
|
||||
})
|
||||
main: number;
|
||||
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({
|
||||
description: 'The major version to describe upwards compatibility issues',
|
||||
example: 1,
|
||||
})
|
||||
major: number;
|
||||
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({
|
||||
description: 'The minor version to describe API stable changes',
|
||||
example: 1,
|
||||
})
|
||||
minor: number;
|
||||
}
|
||||
24
src/versioning/versioning.model.ts
Normal file
24
src/versioning/versioning.model.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document } from 'mongoose';
|
||||
|
||||
export type VersioningDocument = Versioning & Document;
|
||||
|
||||
@Schema()
|
||||
export class Versioning {
|
||||
@Prop({ required: true })
|
||||
componentName: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
createdAt: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
versionMain: number;
|
||||
|
||||
@Prop({ required: true })
|
||||
versionMajor: number;
|
||||
|
||||
@Prop({ required: true })
|
||||
versionMinor: number;
|
||||
}
|
||||
|
||||
export const VersioningSchema = SchemaFactory.createForClass(Versioning);
|
||||
15
src/versioning/versioning.module.ts
Normal file
15
src/versioning/versioning.module.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { VersioningSchema } from './versioning.model';
|
||||
import { VersioningService } from './versioning.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: 'versioning', schema: VersioningSchema },
|
||||
]),
|
||||
],
|
||||
providers: [VersioningService],
|
||||
exports: [MongooseModule, VersioningService],
|
||||
})
|
||||
export class VersioningModule {}
|
||||
18
src/versioning/versioning.service.spec.ts
Normal file
18
src/versioning/versioning.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { VersioningService } from './versioning.service';
|
||||
|
||||
describe('VersioningService', () => {
|
||||
let service: VersioningService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [VersioningService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<VersioningService>(VersioningService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
51
src/versioning/versioning.service.ts
Normal file
51
src/versioning/versioning.service.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { SemanticVersionDto } from './dto/semanticversion.dto';
|
||||
import { Versioning, VersioningDocument } from './versioning.model';
|
||||
|
||||
@Injectable()
|
||||
export class VersioningService {
|
||||
constructor(
|
||||
@InjectModel('versioning')
|
||||
private readonly versioningModel: Model<VersioningDocument>,
|
||||
) {}
|
||||
|
||||
async findComponent(name: string): Promise<Versioning> {
|
||||
return this.versioningModel.findOne({ componentName: name });
|
||||
}
|
||||
|
||||
async updateComponent(
|
||||
name: string,
|
||||
createdAt: string,
|
||||
version: SemanticVersionDto,
|
||||
): Promise<void> {
|
||||
return this.versioningModel.deleteOne({ componentName: name }).then(() => {
|
||||
this.versioningModel.create({
|
||||
componentName: name,
|
||||
createdAt,
|
||||
versionMain: version.main,
|
||||
versionMajor: version.major,
|
||||
versionMinor: version.minor,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static newerVersion(
|
||||
reference: SemanticVersionDto,
|
||||
version: SemanticVersionDto,
|
||||
): boolean {
|
||||
// check special cases for the main part
|
||||
if (reference.main < version.main) return true;
|
||||
if (reference.main > version.main) return false;
|
||||
|
||||
// main versions are equal
|
||||
// check the special cases for the major part
|
||||
if (reference.major < version.major) return true;
|
||||
if (reference.major > version.major) return false;
|
||||
|
||||
// major versions are equal
|
||||
// check if the minor versions increased
|
||||
return reference.minor < version.minor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user