@avleon/core 0.0.29 → 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -4
- package/src/application.ts +0 -104
- package/src/authentication.ts +0 -16
- package/src/cache.ts +0 -91
- package/src/collection.test.ts +0 -71
- package/src/collection.ts +0 -344
- package/src/config.test.ts +0 -35
- package/src/config.ts +0 -85
- package/src/constants.ts +0 -1
- package/src/container.ts +0 -54
- package/src/controller.ts +0 -125
- package/src/decorators.ts +0 -27
- package/src/environment-variables.ts +0 -53
- package/src/event-dispatcher.ts +0 -100
- package/src/event-subscriber.ts +0 -79
- package/src/exceptions/http-exceptions.ts +0 -86
- package/src/exceptions/index.ts +0 -1
- package/src/exceptions/system-exception.ts +0 -35
- package/src/file-storage.ts +0 -206
- package/src/helpers.ts +0 -324
- package/src/icore.ts +0 -1106
- package/src/index.ts +0 -32
- package/src/interfaces/avleon-application.ts +0 -32
- package/src/logger.ts +0 -72
- package/src/map-types.ts +0 -159
- package/src/middleware.ts +0 -121
- package/src/multipart.ts +0 -116
- package/src/openapi.ts +0 -372
- package/src/params.ts +0 -111
- package/src/queue.ts +0 -126
- package/src/response.ts +0 -74
- package/src/results.ts +0 -30
- package/src/route-methods.ts +0 -186
- package/src/swagger-schema.ts +0 -213
- package/src/testing.ts +0 -220
- package/src/types/app-builder.interface.ts +0 -18
- package/src/types/application.interface.ts +0 -7
- package/src/utils/hash.ts +0 -8
- package/src/utils/index.ts +0 -2
- package/src/utils/optional-require.ts +0 -50
- package/src/validation.ts +0 -160
- package/src/validator-extend.ts +0 -25
- package/src/websocket.ts +0 -47
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avleon/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.31",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "npm run clean && tsc",
|
|
8
8
|
"clean": "rimraf dist",
|
|
9
9
|
"watch": "tsc-watch",
|
|
10
|
-
"postinstall": "husky install",
|
|
11
10
|
"lint": "eslint .",
|
|
12
11
|
"lint:fix": "eslint . --fix",
|
|
13
12
|
"format": "prettier --write .",
|
|
@@ -103,7 +102,6 @@
|
|
|
103
102
|
"url": "git+https://github.com/avleonjs/avleon-core"
|
|
104
103
|
},
|
|
105
104
|
"files": [
|
|
106
|
-
"dist"
|
|
107
|
-
"src"
|
|
105
|
+
"dist"
|
|
108
106
|
]
|
|
109
107
|
}
|
package/src/application.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import Container from "typedi";
|
|
2
|
-
import { Constructor } from "./helpers";
|
|
3
|
-
import fastify, { FastifyInstance, RouteShorthandMethod } from "fastify";
|
|
4
|
-
|
|
5
|
-
export interface AvleonApplication {
|
|
6
|
-
// all public
|
|
7
|
-
useCors: () => void;
|
|
8
|
-
useOpenApi: () => void;
|
|
9
|
-
useView: () => void;
|
|
10
|
-
useAuth: () => void;
|
|
11
|
-
useMultipart: () => void;
|
|
12
|
-
useDataSource: () => void;
|
|
13
|
-
useMiddlewares: () => void;
|
|
14
|
-
useControllers: () => void;
|
|
15
|
-
useAutoControllers: () => void;
|
|
16
|
-
useStaticFiles: () => void;
|
|
17
|
-
useCustomErrorHandler: () => void;
|
|
18
|
-
|
|
19
|
-
// all mapping
|
|
20
|
-
mapGroup: () => any;
|
|
21
|
-
mapGet: () => any;
|
|
22
|
-
mapPost: () => any;
|
|
23
|
-
mapPut: () => any;
|
|
24
|
-
mapPatch: () => any;
|
|
25
|
-
mapDelete: () => any;
|
|
26
|
-
mapView: () => any;
|
|
27
|
-
|
|
28
|
-
// run
|
|
29
|
-
run: (port: number) => void;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface InlineRoutes {
|
|
33
|
-
get: RouteShorthandMethod;
|
|
34
|
-
post: RouteShorthandMethod;
|
|
35
|
-
put: RouteShorthandMethod;
|
|
36
|
-
patch: RouteShorthandMethod;
|
|
37
|
-
delete: RouteShorthandMethod;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface Application {
|
|
41
|
-
inlineRoutes: () => InlineRoutes;
|
|
42
|
-
mapGroup: (path?: string | RegExp) => InlineRoutes;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Start the application
|
|
46
|
-
* @param port
|
|
47
|
-
* @returns void
|
|
48
|
-
*/
|
|
49
|
-
start: (port?: number) => void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface TestApplication {
|
|
53
|
-
getController: <T>(controller: Constructor<T>) => T;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
class IqraTestApplication implements TestApplication {
|
|
57
|
-
getController<T>(controller: Constructor<T>) {
|
|
58
|
-
const con = Container.get(controller);
|
|
59
|
-
return con;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
class IqraApplication implements Application {
|
|
63
|
-
private app!: FastifyInstance;
|
|
64
|
-
|
|
65
|
-
constructor() {
|
|
66
|
-
if (!this.app) {
|
|
67
|
-
this.app = fastify.prototype;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
inlineRoutes() {
|
|
72
|
-
return {
|
|
73
|
-
get: this.app.get,
|
|
74
|
-
post: this.app.post,
|
|
75
|
-
put: this.app.put,
|
|
76
|
-
patch: this.app.patch,
|
|
77
|
-
delete: this.app.delete,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
mapGroup(path?: string | RegExp) {
|
|
82
|
-
return this.inlineRoutes();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
start(port?: number) {
|
|
86
|
-
const p = port ? port : 4000;
|
|
87
|
-
this.app.listen({ port: p });
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export class Builder {
|
|
92
|
-
static createApplication(): Application {
|
|
93
|
-
const app = new IqraApplication();
|
|
94
|
-
return app;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
static createTestApplication(app?: Application): TestApplication {
|
|
98
|
-
const testApp = new IqraTestApplication();
|
|
99
|
-
return testApp;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const app = Builder.createApplication();
|
|
104
|
-
const route = app.inlineRoutes();
|
package/src/authentication.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export type CurrentUser = {};
|
|
9
|
-
|
|
10
|
-
export abstract class BaseAuthetication {
|
|
11
|
-
abstract authenticate(): Promise<Boolean>;
|
|
12
|
-
abstract authorize(): Promise<Boolean>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function Authorized() {}
|
|
16
|
-
export function CurrentUser() {}
|
package/src/cache.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { Redis } from "ioredis";
|
|
2
|
-
|
|
3
|
-
type CacheEntry<T = any> = {
|
|
4
|
-
data: T;
|
|
5
|
-
timestamp: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export class CacheManager {
|
|
9
|
-
private store = new Map<string, CacheEntry>();
|
|
10
|
-
private tagsMap = new Map<string, Set<string>>();
|
|
11
|
-
private redis: Redis | null = null;
|
|
12
|
-
|
|
13
|
-
constructor(redisInstance?: Redis) {
|
|
14
|
-
this.redis = redisInstance || null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
private redisTagKey(tag: string) {
|
|
18
|
-
return `cache-tags:${tag}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async get<T>(key: string): Promise<T | null> {
|
|
22
|
-
if (this.redis) {
|
|
23
|
-
const val = await this.redis.get(key);
|
|
24
|
-
return val ? JSON.parse(val) : null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const cached = this.store.get(key);
|
|
28
|
-
return cached ? cached.data : null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async set<T>(
|
|
32
|
-
key: string,
|
|
33
|
-
value: T,
|
|
34
|
-
tags: string[] = [],
|
|
35
|
-
ttl: number = 3600,
|
|
36
|
-
): Promise<void> {
|
|
37
|
-
const entry: CacheEntry<T> = {
|
|
38
|
-
data: value,
|
|
39
|
-
timestamp: Date.now(),
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
if (this.redis) {
|
|
43
|
-
await this.redis.set(key, JSON.stringify(entry.data), "EX", ttl);
|
|
44
|
-
for (const tag of tags) {
|
|
45
|
-
await this.redis.sadd(this.redisTagKey(tag), key);
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
this.store.set(key, entry);
|
|
49
|
-
for (const tag of tags) {
|
|
50
|
-
if (!this.tagsMap.has(tag)) this.tagsMap.set(tag, new Set());
|
|
51
|
-
this.tagsMap.get(tag)!.add(key);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async delete(key: string): Promise<void> {
|
|
57
|
-
if (this.redis) {
|
|
58
|
-
await this.redis.del(key);
|
|
59
|
-
|
|
60
|
-
// Also clean up from any tag sets
|
|
61
|
-
const tagKeys = await this.redis.keys("cache-tags:*");
|
|
62
|
-
for (const tagKey of tagKeys) {
|
|
63
|
-
await this.redis.srem(tagKey, key);
|
|
64
|
-
}
|
|
65
|
-
} else {
|
|
66
|
-
this.store.delete(key);
|
|
67
|
-
for (const keys of this.tagsMap.values()) {
|
|
68
|
-
keys.delete(key);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async invalidateTag(tag: string): Promise<void> {
|
|
74
|
-
if (this.redis) {
|
|
75
|
-
const tagKey = this.redisTagKey(tag);
|
|
76
|
-
const keys = await this.redis.smembers(tagKey);
|
|
77
|
-
if (keys.length) {
|
|
78
|
-
await this.redis.del(...keys); // delete all cached keys
|
|
79
|
-
await this.redis.del(tagKey); // delete the tag set
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
const keys = this.tagsMap.get(tag);
|
|
83
|
-
if (keys) {
|
|
84
|
-
for (const key of keys) {
|
|
85
|
-
this.store.delete(key);
|
|
86
|
-
}
|
|
87
|
-
this.tagsMap.delete(tag);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
package/src/collection.test.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import Container from "typedi";
|
|
2
|
-
import { CreateConfig, GetConfig } from "./config";
|
|
3
|
-
import { BasicCollection, Collection } from "./collection";
|
|
4
|
-
|
|
5
|
-
type Todo = {
|
|
6
|
-
id: number;
|
|
7
|
-
body: string;
|
|
8
|
-
completed: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
describe("Collection", () => {
|
|
12
|
-
let collection!: BasicCollection<Todo>;
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
collection = Collection.from<Todo>([
|
|
15
|
-
{
|
|
16
|
-
id: 1,
|
|
17
|
-
body: "test 1",
|
|
18
|
-
completed: false,
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
id: 2,
|
|
22
|
-
body: "test 2",
|
|
23
|
-
completed: true,
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
id: 3,
|
|
27
|
-
body: "test 2",
|
|
28
|
-
completed: true,
|
|
29
|
-
},
|
|
30
|
-
]);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
afterEach(() => {
|
|
34
|
-
collection.clear();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe("find()", () => {
|
|
38
|
-
it("should be return collection", () => {
|
|
39
|
-
const result = collection.find();
|
|
40
|
-
expect(result).toHaveProperty("length");
|
|
41
|
-
expect(result.length).toBe(3);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("should return only completed task", () => {
|
|
45
|
-
const result = collection.find((todo) => todo.completed);
|
|
46
|
-
expect(result).toHaveProperty("length");
|
|
47
|
-
expect(result.length).toBe(2);
|
|
48
|
-
expect(result[0].id).toBe(2);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe("findOne()", () => {
|
|
53
|
-
it("should be return todo", () => {
|
|
54
|
-
const result = collection.findOne({ where: { id: 1 } });
|
|
55
|
-
expect(result).toHaveProperty("id");
|
|
56
|
-
expect(result?.id).toBe(1);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("should return only completed task", () => {
|
|
60
|
-
const result = collection.findOne({
|
|
61
|
-
where: {
|
|
62
|
-
id: {
|
|
63
|
-
$in: [3],
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
expect(result).toHaveProperty("id");
|
|
68
|
-
expect(result?.completed).toBe(true);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
package/src/collection.ts
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
import Container from "typedi";
|
|
8
|
-
import { NotFoundException } from "./exceptions";
|
|
9
|
-
import {
|
|
10
|
-
DataSource,
|
|
11
|
-
EntityTarget,
|
|
12
|
-
FindOneOptions,
|
|
13
|
-
ObjectLiteral,
|
|
14
|
-
Repository,
|
|
15
|
-
} from "typeorm";
|
|
16
|
-
import { UpsertOptions } from "typeorm/repository/UpsertOptions";
|
|
17
|
-
|
|
18
|
-
type ObjKey<T> = keyof T;
|
|
19
|
-
type ObjKeys<T> = ObjKey<T>[];
|
|
20
|
-
type PaginationOptions = {
|
|
21
|
-
take: number;
|
|
22
|
-
skip?: number;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
type Predicate<T> = (item: T) => boolean;
|
|
26
|
-
interface TypeormEnitity extends ObjectLiteral {}
|
|
27
|
-
type Primitive = string | number | boolean | null;
|
|
28
|
-
|
|
29
|
-
type ValueOperator<T> = {
|
|
30
|
-
$in?: T[];
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type FieldCondition<T> = T | ValueOperator<T>;
|
|
34
|
-
|
|
35
|
-
type WhereCondition<T> = {
|
|
36
|
-
[K in keyof T]?: FieldCondition<T[K]>;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
type LogicalOperators<T> =
|
|
40
|
-
| { $and: Where<T>[] }
|
|
41
|
-
| { $or: Where<T>[] }
|
|
42
|
-
| { $not: Where<T> };
|
|
43
|
-
|
|
44
|
-
type Where<T> = WhereCondition<T> | LogicalOperators<T>;
|
|
45
|
-
|
|
46
|
-
export interface IFindOneOptions<T = any> {
|
|
47
|
-
where: Where<T>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export type PaginationResult<T> = {
|
|
51
|
-
total: number;
|
|
52
|
-
data: T[];
|
|
53
|
-
next?: number | null;
|
|
54
|
-
prev?: number | null;
|
|
55
|
-
first?: number | null;
|
|
56
|
-
last?: number | null;
|
|
57
|
-
totalPage?: number;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
type ICollection<T> = {
|
|
61
|
-
findAll(): T[] | Promise<T[]>;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
type EntityCollection<T extends ObjectLiteral> = {};
|
|
65
|
-
|
|
66
|
-
export interface BasicCollection<T> {
|
|
67
|
-
clear(): void;
|
|
68
|
-
find(predicate?: Predicate<T>): T[];
|
|
69
|
-
findAsync(predicate?: Predicate<T>): Promise<T[]>;
|
|
70
|
-
findOne(predicate: Predicate<T> | IFindOneOptions<T>): T | undefined;
|
|
71
|
-
findOneAsync(
|
|
72
|
-
predicate: Predicate<T> | IFindOneOptions<T>,
|
|
73
|
-
): Promise<T | undefined>;
|
|
74
|
-
}
|
|
75
|
-
class BasicCollectionImpl<T> implements BasicCollection<T> {
|
|
76
|
-
private items: T[];
|
|
77
|
-
|
|
78
|
-
private constructor(items: T[]) {
|
|
79
|
-
this.items = items;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
static from<T>(items: T[]): BasicCollectionImpl<T> {
|
|
83
|
-
return new BasicCollectionImpl(items);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
clear() {
|
|
87
|
-
this.items = [];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
find(predicate?: Predicate<T>) {
|
|
91
|
-
if (this.isFunction(predicate)) {
|
|
92
|
-
return this.items.filter(predicate as Predicate<T>) as T[];
|
|
93
|
-
}
|
|
94
|
-
const results = Array.from(this.items);
|
|
95
|
-
return results;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async findAsync(predicate?: Predicate<T>): Promise<T[]> {
|
|
99
|
-
const results = Array.from(this.items);
|
|
100
|
-
return results;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private _matches<T>(item: T, where: Where<T>): boolean {
|
|
104
|
-
if ("$or" in where) {
|
|
105
|
-
return where.$or.some((cond) => this._matches(item, cond));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if ("$and" in where) {
|
|
109
|
-
return where.$and.every((cond) => this._matches(item, cond));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if ("$not" in where) {
|
|
113
|
-
return !this._matches(item, where.$not);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Field-based matching
|
|
117
|
-
return Object.entries(where).every(([key, condition]) => {
|
|
118
|
-
const itemValue = item[key as keyof T];
|
|
119
|
-
if (
|
|
120
|
-
condition &&
|
|
121
|
-
typeof condition === "object" &&
|
|
122
|
-
!Array.isArray(condition)
|
|
123
|
-
) {
|
|
124
|
-
const op = condition as ValueOperator<any>;
|
|
125
|
-
|
|
126
|
-
if ("$in" in op && Array.isArray(op.$in)) {
|
|
127
|
-
return op.$in.includes(itemValue);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return itemValue === condition;
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
findOne(predicate: Predicate<T> | IFindOneOptions<T>): T | undefined {
|
|
136
|
-
if (this.isFunction(predicate)) {
|
|
137
|
-
return this.items.find(predicate as Predicate<T>) as T;
|
|
138
|
-
}
|
|
139
|
-
const result = this.items.filter((item) =>
|
|
140
|
-
this._matches(item, predicate.where),
|
|
141
|
-
);
|
|
142
|
-
if (result.length > 0) {
|
|
143
|
-
return result[0];
|
|
144
|
-
}
|
|
145
|
-
return undefined;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async findOneAsync(
|
|
149
|
-
predicate: Predicate<T> | IFindOneOptions<T>,
|
|
150
|
-
): Promise<T | undefined> {
|
|
151
|
-
if (this.isFunction(predicate)) {
|
|
152
|
-
return this.items.find(predicate as Predicate<T>) as T;
|
|
153
|
-
}
|
|
154
|
-
return this.items.find((item) => this._matches(item, predicate.where));
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Utility function to check if a value is a function
|
|
158
|
-
private isFunction(value: unknown): value is Function {
|
|
159
|
-
return typeof value === "function";
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
add(item: Partial<T>): T;
|
|
163
|
-
add(item: Partial<T>): T | Promise<T> {
|
|
164
|
-
this.items.push(item as T);
|
|
165
|
-
return this.items[this.items.length - 1];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
addAll(items: T[]): void {
|
|
169
|
-
this.items.push(...items);
|
|
170
|
-
}
|
|
171
|
-
update(predicate: (item: T) => boolean, updater: Partial<T>): void {
|
|
172
|
-
const item = this.items.find(predicate);
|
|
173
|
-
if (item) {
|
|
174
|
-
const index = this.items.indexOf(item)!;
|
|
175
|
-
this.items[index] = { ...item, ...updater };
|
|
176
|
-
} else {
|
|
177
|
-
throw new NotFoundException("Item not found");
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
updateAll(predicate: (item: T) => boolean, updater: (item: T) => T): void {
|
|
182
|
-
for (let i = 0; i < this.items.length; i++) {
|
|
183
|
-
if (predicate(this.items[i])) {
|
|
184
|
-
this.items[i] = updater(this.items[i]);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
delete(predicate: (item: T) => boolean): void {
|
|
189
|
-
const index = this.items.findIndex(predicate);
|
|
190
|
-
if (index !== -1) {
|
|
191
|
-
this.items.splice(index, 1);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
deleteAll(predicate: (item: T) => boolean): void {
|
|
195
|
-
this.items = this.items.filter((item) => !predicate(item));
|
|
196
|
-
}
|
|
197
|
-
max<K extends keyof T>(key: K & string): number {
|
|
198
|
-
return Math.max(...this.items.map((item) => item[key] as number));
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
min<K extends keyof T>(key: K & string): number {
|
|
202
|
-
return Math.max(...this.items.map((item) => item[key] as number));
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
sum<K extends keyof T>(key: K & string): number {
|
|
206
|
-
const nums = this.items.flatMap((x) => x[key]) as number[];
|
|
207
|
-
return nums.reduce((sum, num) => sum + num, 0);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
avg<K extends keyof T>(key: K & string): number {
|
|
211
|
-
const nums = this.items.flatMap((x) => x[key]) as number[];
|
|
212
|
-
return nums.reduce((sum, num) => sum + num, 0) / nums.length;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
paginate(options?: PaginationOptions) {
|
|
216
|
-
const take = options?.take || 10;
|
|
217
|
-
const skip = options?.skip || 0;
|
|
218
|
-
const total = this.items.length;
|
|
219
|
-
const data = this.items.slice(skip, take);
|
|
220
|
-
return {
|
|
221
|
-
total,
|
|
222
|
-
totalPage: Math.ceil(total / take),
|
|
223
|
-
next: skip + take < total ? skip + take : null,
|
|
224
|
-
data,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
private getDeepValue(item: any, path: string | keyof T): any {
|
|
229
|
-
if (typeof path !== "string") return item[path];
|
|
230
|
-
return path.split(".").reduce((acc, key) => acc?.[key], item);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
class AsynchronousCollection<T extends ObjectLiteral> {
|
|
235
|
-
private model: EntityTarget<T>;
|
|
236
|
-
private repo?: Repository<T>;
|
|
237
|
-
|
|
238
|
-
private constructor(model: EntityTarget<T>) {
|
|
239
|
-
this.model = model;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
static fromRepository<T extends ObjectLiteral>(
|
|
243
|
-
model: EntityTarget<T>,
|
|
244
|
-
): AsynchronousCollection<T> {
|
|
245
|
-
return new AsynchronousCollection(model);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
getRepository() {
|
|
249
|
-
if (!this.repo) {
|
|
250
|
-
const dataSourceKey = "idatasource";
|
|
251
|
-
const dataSource = Container.get(dataSourceKey) as DataSource;
|
|
252
|
-
console.log("datasource", dataSource);
|
|
253
|
-
const repository = dataSource.getRepository<T>(this.model);
|
|
254
|
-
this.repo = repository;
|
|
255
|
-
return repository;
|
|
256
|
-
}
|
|
257
|
-
return this.repo;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Pagination with query builder
|
|
261
|
-
async paginate(options?: PaginationOptions): Promise<PaginationResult<T>> {
|
|
262
|
-
const take = options?.take || 10;
|
|
263
|
-
const skip = options?.skip || 0;
|
|
264
|
-
|
|
265
|
-
const [data, total] = await this.getRepository().findAndCount({
|
|
266
|
-
take,
|
|
267
|
-
skip,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
total,
|
|
272
|
-
totalPage: Math.ceil(total / take),
|
|
273
|
-
next: skip + take < total ? skip + take : null,
|
|
274
|
-
prev: skip + take < total ? skip + take : null,
|
|
275
|
-
data,
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export class Collection {
|
|
281
|
-
private constructor() {}
|
|
282
|
-
|
|
283
|
-
static from<T>(items: T[]): BasicCollection<T> {
|
|
284
|
-
return BasicCollectionImpl.from(items);
|
|
285
|
-
}
|
|
286
|
-
// Example refactoring of Collection.fromRepository for better type safety
|
|
287
|
-
static fromRepository<T extends ObjectLiteral>(
|
|
288
|
-
entity: EntityTarget<T>,
|
|
289
|
-
): Repository<T> {
|
|
290
|
-
const asyncCollection = AsynchronousCollection.fromRepository(entity);
|
|
291
|
-
// Assuming AsynchronousCollection has a method to get the Repository<T>
|
|
292
|
-
return asyncCollection.getRepository();
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export function InjectRepository<T extends Repository<T>>(
|
|
297
|
-
model: EntityTarget<T>,
|
|
298
|
-
) {
|
|
299
|
-
return function (
|
|
300
|
-
object: any,
|
|
301
|
-
propertyName: string | undefined,
|
|
302
|
-
index?: number,
|
|
303
|
-
) {
|
|
304
|
-
let repo!: any | Repository<T>;
|
|
305
|
-
try {
|
|
306
|
-
Container.registerHandler({
|
|
307
|
-
object,
|
|
308
|
-
propertyName,
|
|
309
|
-
index,
|
|
310
|
-
value: (containerInstance) => {
|
|
311
|
-
const dataSource = containerInstance.get<DataSource>("idatasource");
|
|
312
|
-
|
|
313
|
-
repo = dataSource
|
|
314
|
-
.getRepository<T>(model)
|
|
315
|
-
.extend({ paginate: () => {} });
|
|
316
|
-
repo.paginate = async function (
|
|
317
|
-
options: PaginationOptions = { take: 10, skip: 0 },
|
|
318
|
-
): Promise<PaginationResult<T>> {
|
|
319
|
-
const [data, total] = await this.findAndCount({
|
|
320
|
-
take: options.take || 10,
|
|
321
|
-
skip: options.skip || 0,
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
return {
|
|
325
|
-
total,
|
|
326
|
-
totalPage: Math.ceil(total / (options.take || 10)),
|
|
327
|
-
next:
|
|
328
|
-
options.skip! + options.take! < total
|
|
329
|
-
? options.skip! + options.take!
|
|
330
|
-
: null,
|
|
331
|
-
data,
|
|
332
|
-
};
|
|
333
|
-
};
|
|
334
|
-
return repo;
|
|
335
|
-
},
|
|
336
|
-
});
|
|
337
|
-
} catch (error: any) {
|
|
338
|
-
console.log(error);
|
|
339
|
-
if (error.name && error.name == "ServiceNotFoundError") {
|
|
340
|
-
console.log("Database didn't initialized.");
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
}
|
package/src/config.test.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import { AppConfig, CreateConfig, GetConfig, IConfig } from "./config";
|
|
3
|
-
import { Environment } from "./environment-variables";
|
|
4
|
-
|
|
5
|
-
type AppConfig = { name: string; os: string };
|
|
6
|
-
|
|
7
|
-
describe("Config", () => {
|
|
8
|
-
describe("class", () => {
|
|
9
|
-
it("should be call by get config", () => {
|
|
10
|
-
@AppConfig
|
|
11
|
-
class MyConfig {
|
|
12
|
-
config(env: Environment) {
|
|
13
|
-
return {
|
|
14
|
-
name: "avleon",
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
const mConfig = GetConfig(MyConfig);
|
|
19
|
-
expect(mConfig).toHaveProperty("name");
|
|
20
|
-
expect(mConfig["name"]).toBe("avleon");
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe("createConfig()", () => {
|
|
25
|
-
it("it should create config and called with GetConfig", () => {
|
|
26
|
-
CreateConfig("myconfig", (env) => ({
|
|
27
|
-
firstname: "tareq",
|
|
28
|
-
os: env.get("name"),
|
|
29
|
-
}));
|
|
30
|
-
const mConfig = GetConfig("myconfig");
|
|
31
|
-
expect(mConfig).toHaveProperty("firstname");
|
|
32
|
-
expect(mConfig.firstname).toBe("tareq");
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
});
|