compile app

This commit is contained in:
Simon Larsen
2022-05-02 20:30:57 +01:00
parent 51643d85c4
commit b64febb5d0
13 changed files with 137 additions and 242 deletions

View File

@@ -19,7 +19,7 @@ export default class Probe extends BaseModel {
type: 'text',
nullable: false,
unique: true,
transformer: ObjectID.getDatabaseTransformer()
// transformer: ObjectID.getDatabaseTransformer()
})
public key!: ObjectID;
@@ -41,12 +41,12 @@ export default class Probe extends BaseModel {
public lastAlive!: Date;
@Column({ nullable: true })
public icon!: string;
public icon?: string;
// If this probe is custom to the project and only monitoring reosurces in this project.
@Column({ nullable: true })
public project?: Project;
@Column({ nullable: true })
public deletedByUser!: User;
public deletedByUser?: User;
}

View File

@@ -0,0 +1,12 @@
import { ValueTransformer } from "typeorm/decorator/options/ValueTransformer";
import NotImplementedException from "./Exception/NotImplementedException";
export default class DatabaseProperty {
constructor() {
}
public static getDatabaseTransformer(): ValueTransformer {
throw new NotImplementedException();
}
}

View File

@@ -1,6 +1,7 @@
import DatabaseProperty from './DatabaseProperty';
import BadDataException from './Exception/BadDataException';
export default class Email {
export default class Email extends DatabaseProperty {
private _email: string = '';
public get email(): string {
return this._email;
@@ -10,6 +11,7 @@ export default class Email {
}
public constructor(email: string) {
super()
const re: RegExp =
/^(([^<>()[\].,;:\s@"]+(.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+.)+[^<>()[\].,;:\s@"]{2,})$/i;
const isValid: boolean = re.test(email);

View File

@@ -1,8 +1,9 @@
// This is for Object ID for all the things in our database.
import { ValueTransformer } from 'typeorm';
import { FindOperator, ValueTransformer } from 'typeorm';
import UUID from '../Utils/UUID';
import DatabaseProperty from './DatabaseProperty';
export default class ObjectID {
export default class ObjectID extends DatabaseProperty {
private _id: string = '';
public get id(): string {
return this._id;
@@ -12,10 +13,11 @@ export default class ObjectID {
}
public constructor(id: string) {
super()
this.id = id;
}
public toString(): string {
public override toString(): string {
return this.id;
}
@@ -23,9 +25,12 @@ export default class ObjectID {
return new this(UUID.generate());
}
public static getDatabaseTransformer(): ValueTransformer {
public static override getDatabaseTransformer(): ValueTransformer {
return {
to(value: ObjectID): string {
to(value: ObjectID | FindOperator<ObjectID>): string {
if (value instanceof FindOperator) {
return value.value.toString();
}
return value.toString();
},
from(value: string): ObjectID {

View File

@@ -1,6 +1,7 @@
import DatabaseProperty from './DatabaseProperty';
import BadDataException from './Exception/BadDataException';
export default class Version {
export default class Version extends DatabaseProperty {
private _version: string = '';
public get version(): string {
return this._version;
@@ -10,6 +11,7 @@ export default class Version {
}
public constructor(version: string) {
super();
const re: RegExp =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z\d][-a-zA-Z.\d]*)?(\+[a-zA-Z\d][-a-zA-Z.\d]*)?$/i;
const isValid: boolean = re.test(version);
@@ -19,7 +21,7 @@ export default class Version {
this.version = version;
}
public toString(): string {
public override toString(): string {
return this.version;
}
}

View File

@@ -123,6 +123,7 @@ export default {
LocalCache.getModel<Probe>('probe', probeName)
).id
} else {
const probe: Probe | null = await ProbeService.findOneBy({
query: {
name: probeName,
@@ -153,14 +154,13 @@ export default {
if (!probeId) {
//Create a new probe.
const probe: Probe = await ProbeService.create({
data: {
key: probeKey
? probeKey
: ObjectID.generate(),
name: probeName,
probeVersion: probeVersion,
},
let probe: Probe = new Probe();
probe.name = probeName;
probe.probeVersion = probeVersion;
probe.key = probeKey;
probe = await ProbeService.create({
data: probe
});
probeId = probe.id;

View File

@@ -15,10 +15,10 @@ import Exception from 'Common/Types/Exception/Exception';
import SearchResult from '../Types/DB/SearchResult';
import Encryption from '../Utils/Encryption';
import { JSONObject } from 'Common/Types/JSON';
import SortOrder from '../Types/DB/SortOrder';
import BaseModel from 'Common/Models/BaseModel';
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import { DataSource, Repository } from 'typeorm';
import SortOrder from '../Types/DB/SortOrder';
class DatabaseService<TBaseModel extends BaseModel> {
@@ -60,6 +60,7 @@ class DatabaseService<TBaseModel extends BaseModel> {
}
protected encrypt(data: TBaseModel): TBaseModel {
const iv: Buffer = Encryption.getIV();
(data as any)['iv'] = iv;
@@ -223,9 +224,18 @@ class DatabaseService<TBaseModel extends BaseModel> {
}
public async countBy({ query, skip, limit }: CountBy<TBaseModel>): Promise<PositiveNumber> {
try {
if (!skip) {
skip = new PositiveNumber(0);
}
if (!limit) {
limit = new PositiveNumber(Infinity);
}
const count: number = await this.getRepository().count({
where: query,
where: query as any,
skip: skip.toNumber(),
take: limit.toNumber()
})
@@ -260,7 +270,7 @@ class DatabaseService<TBaseModel extends BaseModel> {
}
private async _hardDeleteBy(query: Query<TBaseModel>): Promise<number> {
return (await this.getRepository().delete(query)).affected || 0;
return (await this.getRepository().delete(query as any)).affected || 0;
}
private async _deleteBy({
@@ -273,7 +283,7 @@ class DatabaseService<TBaseModel extends BaseModel> {
deletedByUserId,
});
let numberOfDocsAffected: number = (await this.getRepository().softDelete(beforeDeleteBy.query)).affected || 0;
let numberOfDocsAffected: number = (await this.getRepository().softDelete(beforeDeleteBy.query as any)).affected || 0;
await this.onDeleteSuccess();
return numberOfDocsAffected;
@@ -283,212 +293,66 @@ class DatabaseService<TBaseModel extends BaseModel> {
}
}
public async getListForViewer({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
sort,
populate,
select
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy({
query,
skip,
limit,
populate,
select,
sort,
});
public async getListForViewer(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy(findBy);
}
public async getListForAdmin({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy({
query,
skip,
limit,
populate: this.adminListProps.populate,
select: this.adminListProps.select,
sort,
});
public async getListForAdmin(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy(findBy);
}
public async getListForOwner({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy({
query,
skip,
limit,
populate: this.ownerListProps.populate,
select: this.ownerListProps.select,
sort,
});
public async getListForOwner(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy(findBy);
}
public async getListForMember({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy({
query,
skip,
limit,
populate: this.memberListProps.populate,
select: this.memberListProps.select,
sort,
});
public async getListForMember(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy(findBy);
}
public async getListForPublic({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy({
query,
skip,
limit,
populate: this.publicListProps.populate,
select: this.publicListProps.select,
sort,
});
public async getListForPublic(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this.findBy(findBy);
}
public async getItemForViewer({
query,
sort,
populate,
select
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy({
query,
populate,
select,
sort,
});
public async getItemForViewer(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy(findOneBy);
}
public async getItemForAdmin({
query,
sort,
populate,
select,
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy({
query,
populate,
select,
sort,
});
public async getItemForAdmin(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy(findOneBy);
}
public async getItemForMember({
query,
sort,
populate,
select,
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy({
query,
populate,
select,
sort,
});
public async getItemForMember(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy(findOneBy);
}
public async getItemForOwner({
query,
sort,
populate,
select,
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy({
query,
populate,
select,
sort,
});
public async getItemForOwner(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy(findOneBy);
}
public async getItemForPublic({
query,
sort,
populate,
select,
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy({
query,
populate,
select,
sort,
});
public async getItemForPublic(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
return await this.findOneBy(findOneBy);
}
public async findBy({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
populate,
select,
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this._findBy({
query,
skip,
limit,
populate,
select,
sort,
});
public async findBy(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
return await this._findBy(findBy);
}
private async _findBy({
query,
skip = new PositiveNumber(0),
limit = new PositiveNumber(10),
populate,
select,
sort,
}: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
private async _findBy(findBy: FindBy<TBaseModel>): Promise<Array<TBaseModel>> {
try {
const onBeforeFind: FindBy<TBaseModel> = await this.onBeforeFind({
query,
skip,
limit,
populate,
select,
sort,
});
const onBeforeFind: FindBy<TBaseModel> = await this.onBeforeFind(findBy);
if (!onBeforeFind.sort) {
onBeforeFind.sort = [
{
createdAt: SortOrder.Descending,
},
];
onBeforeFind.sort = {
createdAt: SortOrder.Ascending
}
}
//convert populate to dbpopulate
const items: Array<TBaseModel> = await this.getRepository().find({
skip: skip.toNumber(),
take: limit.toNumber(),
where: query,
order: sort,
relations: populate,
select: select
skip: onBeforeFind.skip.toNumber(),
take: onBeforeFind.limit.toNumber(),
where: onBeforeFind.query as any,
order: onBeforeFind.sort as any,
relations: onBeforeFind.populate as any
});
const decryptedItems: Array<TBaseModel> = [];
@@ -506,24 +370,13 @@ class DatabaseService<TBaseModel extends BaseModel> {
}
}
public async findOneBy({
query,
populate,
select = {
_id: true
},
sort = {
createdAt: SortOrder.Ascending
},
}: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
const documents: Array<TBaseModel> = await this._findBy({
query,
skip: new PositiveNumber(0),
limit: new PositiveNumber(1),
populate,
select,
sort
});
public async findOneBy(findOneBy: FindOneBy<TBaseModel>): Promise<TBaseModel | null> {
const findBy: FindBy<TBaseModel> = findOneBy as FindBy<TBaseModel>;
findBy.limit = new PositiveNumber(1);
findBy.skip = new PositiveNumber(0);
const documents: Array<TBaseModel> = await this._findBy(findBy);
if (documents && documents[0]) {
return documents[0];
@@ -542,7 +395,7 @@ class DatabaseService<TBaseModel extends BaseModel> {
data,
});
let numberOfDocsAffected: number = (await this.getRepository().update(beforeUpdateBy.query, beforeUpdateBy.data)).affected || 0;
let numberOfDocsAffected: number = (await this.getRepository().update(beforeUpdateBy.query as any, beforeUpdateBy.data)).affected || 0;
await this.onUpdateSuccess();

View File

@@ -1,10 +0,0 @@
import Model from 'Common/Models/DefaultManager';
import DatabaseService from './DatabaseService';
class Service extends DatabaseService<Model> {
public constructor() {
super(Model);
}
}
export default new Service();

View File

@@ -4,6 +4,6 @@ import Query from './Query';
export default interface CountBy<TBaseModel extends BaseModel> {
query: Query<TBaseModel>;
skip: PositiveNumber,
limit: PositiveNumber
skip?: PositiveNumber,
limit?: PositiveNumber
}

View File

@@ -1,6 +1,16 @@
import BaseModel from 'Common/Models/BaseModel';
import { FindOptionsWhere } from 'typeorm';
import DatabaseProperty from 'Common/Types/DatabaseProperty';
import { FindOperator, FindOptionsWhereProperty } from 'typeorm';
declare type Query<TBaseModel extends BaseModel> = FindOptionsWhere<TBaseModel>;
export declare type FindWhereProperty<Property> = Property extends DatabaseProperty ? Property | FindOperator<Property> : FindOptionsWhereProperty<Property>;
/** :
* Used for find operations.
*/
export declare type FindWhere<Entity> = {
[P in keyof Entity]?: FindWhereProperty<NonNullable<Entity[P]>>;
};
declare type Query<TBaseModel extends BaseModel> = FindWhere<TBaseModel>;
export default Query;

View File

@@ -1,5 +1,15 @@
import BaseModel from "Common/Models/BaseModel";
import { FindOptionsSelect } from "typeorm";
import DatabaseProperty from "Common/Types/DatabaseProperty";
import { FindOptionsSelectProperty } from "typeorm";
type Select<TBaseModel extends BaseModel> = FindOptionsSelect<TBaseModel>;
export type SelectPropertyOptions<Property> = Property extends DatabaseProperty ? boolean : FindOptionsSelectProperty<Property>;
/**
* Select find options.
*/
export declare type SelectOptions<Entity> = {
[P in keyof Entity]?: SelectPropertyOptions<NonNullable<Entity[P]>>;
};
type Select<TBaseModel extends BaseModel> = SelectOptions<TBaseModel>;
export default Select;

View File

@@ -1,6 +1,17 @@
import BaseModel from 'Common/Models/BaseModel';
import { FindOptionsOrder } from 'typeorm';
type Sort<TBaseModel extends BaseModel> = FindOptionsOrder<TBaseModel>
import BaseModel from 'Common/Models/BaseModel';
import DatabaseProperty from 'Common/Types/DatabaseProperty';
import { FindOptionsOrderProperty, FindOptionsOrderValue } from 'typeorm';
import SortOrder from './SortOrder';
export declare type FindOrderProperty<Property> = Property extends DatabaseProperty ? SortOrder : FindOptionsOrderProperty<Property> extends FindOptionsOrderValue ? SortOrder : never;
/**
* Order by find options.
*/
export declare type FindOrder<Entity> = {
[P in keyof Entity]?: SortOrder;
};
type Sort<TBaseModel extends BaseModel> = FindOrder<TBaseModel>
export default Sort

View File

@@ -1,6 +1,6 @@
enum SortOrder {
Ascending = "ASC",
Descending = "DESC",
Ascending = "ASC",
Descending = "DESC"
}
export default SortOrder;
export default SortOrder;