Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
6 changes: 6 additions & 0 deletions packages/core/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
}
3 changes: 3 additions & 0 deletions packages/core/cache/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './interfaces/ttlCache.interface';
export * from './redisTtlCache';
export * from './types/ttlCacheRecord.types';
84 changes: 42 additions & 42 deletions packages/core/cache/interfaces/ttlCache.interface.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import { TtlCacheRecord } from "../types/ttlCacheRecord";
import { TtlCacheRecord } from '../types/ttlCacheRecord.types';

/**
* Interface representing a TTL (Time-To-Live) cache.
*/
export interface TtlCache {
/**
* Puts a record into the cache.
*
* @param {TtlCacheRecord} cacheRecord - The cache record to put into the cache.
* @returns {Promise<void>} - A promise that resolves when the record is put into the cache.
*/
putRecord(cacheRecord: TtlCacheRecord): Promise<void>;
/**
* Puts a record into the cache.
*
* @param {TtlCacheRecord} cacheRecord - The cache record to put into the cache.
* @returns {Promise<void>} - A promise that resolves when the record is put into the cache.
*/
putRecord(cacheRecord: TtlCacheRecord): Promise<void>;

/**
* Deletes a record from the cache.
*
* @param {string} cacheRecordKey - The key of the cache record to delete.
* @returns {Promise<void>} - A promise that resolves when the record is deleted from the cache.
*/
deleteRecord(cacheRecordKey: string): Promise<void>;
/**
* Deletes a record from the cache.
*
* @param {string} cacheRecordKey - The key of the cache record to delete.
* @returns {Promise<void>} - A promise that resolves when the record is deleted from the cache.
*/
deleteRecord(cacheRecordKey: string): Promise<void>;

/**
* Reads a record from the cache.
*
* @param {string} cacheRecordKey - The key of the cache record to read.
* @returns {Promise<TtlCacheRecord>} - A promise that resolves with the cache record.
*/
readRecord(cacheRecordKey: string): Promise<TtlCacheRecord>;
/**
* Reads a record from the cache.
*
* @param {string} cacheRecordKey - The key of the cache record to read.
* @returns {Promise<TtlCacheRecord>} - A promise that resolves with the cache record.
*/
readRecord(cacheRecordKey: string): Promise<TtlCacheRecord>;

/**
* Peeks at a record in the cache to check if it exists.
*
* @param {string} cacheRecordKey - The key of the cache record to peek at.
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating if the record exists.
*/
peekRecord(cacheRecordKey: string): Promise<boolean>;
/**
* Peeks at a record in the cache to check if it exists.
*
* @param {string} cacheRecordKey - The key of the cache record to peek at.
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating if the record exists.
*/
peekRecord(cacheRecordKey: string): Promise<boolean>;

/**
* Gets the TTL (Time-To-Live) in milliseconds.
*
* @returns {number} - The TTL in milliseconds.
*/
getTtlMilliseconds(): number;
/**
* Gets the TTL (Time-To-Live) in milliseconds.
*
* @returns {number} - The TTL in milliseconds.
*/
getTtlMilliseconds(): number;

/**
* Lists the keys in the cache that match a pattern prefix.
*
* @param {string} pattern_prefix - The pattern prefix to match.
* @returns {Promise<string[]>} - A promise that resolves with an array of keys matching the pattern prefix.
*/
listKeys(pattern_prefix: string): Promise<string[]>;
/**
* Lists the keys in the cache that match a pattern prefix.
*
* @param {string} pattern_prefix - The pattern prefix to match.
* @returns {Promise<string[]>} - A promise that resolves with an array of keys matching the pattern prefix.
*/
listKeys(pattern_prefix: string): Promise<string[]>;
}
39 changes: 22 additions & 17 deletions packages/core/cache/redisTtlCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient } from 'redis';
import { RedisClientOptions, createClient } from 'redis';
import { TtlCache } from './interfaces/ttlCache.interface';
import { TtlCacheRecord } from './types/ttlCacheRecord';
import { TtlCacheRecord } from './types/ttlCacheRecord.types';

/**
* Class representing a Redis-based TTL (Time-To-Live) cache.
Expand All @@ -11,37 +11,42 @@ export class RedisTtlCache implements TtlCache {

/**
* Creates an instance of RedisTtlCache.
*
*
* @param {number} ttlMilliseconds - The default TTL in milliseconds.
*/
constructor(private ttlMilliseconds: number) {
constructor(
private ttlMilliseconds: number,
hostingOptions?: RedisClientOptions
) {
// Connects to localhost:6379 by default
// url usage: redis[s]://[[username][:password]@][host][:port][/db-number]
this.client = createClient({
url: process.env.REDIS_URL
});
this.client = createClient(hostingOptions);
this.client.on('error', (err) => console.log('Redis Client Error', err));
this.client.on('connect', () => {
console.log('\x1b[32m%s\x1b[0m', 'Successfully Connected to Redis'); // Green text
console.log('\x1b[32m%s\x1b[0m', 'Successfully Connected to Redis'); // Green text
});
this.client.connect().catch(console.error);
}

/**
* Puts a record into the Redis cache.
*
*
* @param {TtlCacheRecord} param0 - The cache record to put into the cache.
* @returns {Promise<void>} - A promise that resolves when the record is put into the cache.
*/
async putRecord({ key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord): Promise<void> {
async putRecord({
key,
value,
ttlMilliseconds = this.ttlMilliseconds
}: TtlCacheRecord): Promise<void> {
await this.client.set(key, JSON.stringify(value), {
PX: ttlMilliseconds
});
}

/**
* Deletes a record from the Redis cache.
*
*
* @param {string} cacheRecordKey - The key of the cache record to delete.
* @returns {Promise<void>} - A promise that resolves when the record is deleted from the cache.
*/
Expand All @@ -51,7 +56,7 @@ export class RedisTtlCache implements TtlCache {

/**
* Reads a record from the Redis cache.
*
*
* @param {string} cacheRecordKey - The key of the cache record to read.
* @returns {Promise<TtlCacheRecord>} - A promise that resolves with the cache record.
* @throws {Error} - Throws an error if the record is not found.
Expand All @@ -66,12 +71,12 @@ export class RedisTtlCache implements TtlCache {
key: cacheRecordKey,
value: JSON.parse(value),
ttlMilliseconds: ttl * 1000
};
};
}

/**
* Lists the keys in the Redis cache that match a pattern prefix.
*
*
* @param {string} pattern_prefix - The pattern prefix to match.
* @returns {Promise<string[]>} - A promise that resolves with an array of keys matching the pattern prefix.
*/
Expand All @@ -82,7 +87,7 @@ export class RedisTtlCache implements TtlCache {

/**
* Peeks at a record in the Redis cache to check if it exists.
*
*
* @param {string} cacheRecordKey - The key of the cache record to peek at.
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating if the record exists.
*/
Expand All @@ -93,7 +98,7 @@ export class RedisTtlCache implements TtlCache {

/**
* Disconnects the Redis client.
*
*
* @returns {Promise<void>} - A promise that resolves when the client is disconnected.
*/
async disconnect(): Promise<void> {
Expand All @@ -102,7 +107,7 @@ export class RedisTtlCache implements TtlCache {

/**
* Gets the default TTL (Time-To-Live) in milliseconds.
*
*
* @returns {number} - The TTL in milliseconds.
*/
getTtlMilliseconds(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @property {number} ttlMilliseconds - The time-to-live of the cache record in milliseconds.
*/
export type TtlCacheRecord = {
key: string;
value: unknown;
ttlMilliseconds: number;
key: string;
value: unknown;
ttlMilliseconds: number;
};
1 change: 1 addition & 0 deletions packages/core/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './interfaces/controller.interface';
16 changes: 8 additions & 8 deletions packages/core/controllers/interfaces/controller.interface.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Interface representing a controller.
*
*
* @interface Controller
*/
interface Controller {
/**
* The base path for the controller.
*
* @type {string}
* @readonly
*/
readonly basePath: string;
/**
* The base path for the controller.
*
* @type {string}
* @readonly
*/
readonly basePath: string;
}

export default Controller;
1 change: 1 addition & 0 deletions packages/core/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mikro/models/entities/base.entity';
10 changes: 5 additions & 5 deletions packages/core/database/mikro/models/entities/base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { v4 } from 'uuid';
import { PrimaryKey, Property } from "@mikro-orm/core";
import { PrimaryKey, Property } from '@mikro-orm/core';

/**
* Abstract class representing a base entity.
*/
export abstract class BaseEntity {
/**
* The unique identifier for the entity.
*
*
* @type {string}
* @readonly
*/
@PrimaryKey({ type: "uuid" })
@PrimaryKey({ type: 'uuid' })
id: string = v4();

/**
* The date when the entity was created.
*
*
* @type {Date}
*/
@Property()
createdAt: Date = new Date();

/**
* The date when the entity was last updated.
*
*
* @type {Date}
* @readonly
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/core/entityMapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './models/requestEntityMapper.model';
export * from './models/responseEntityMapper.model';
20 changes: 10 additions & 10 deletions packages/core/entityMapper/interfaces/entityMapper.interface.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { AnySchemaValidator } from "@forklaunch/validator";
import { AnySchemaValidator } from '@forklaunch/validator';

/**
* Interface representing a constructor for an entity mapper.
*
*
* @template T - The type of the entity mapper.
* @interface EntityMapperConstructor
*/
export interface EntityMapperConstructor<T, SV extends AnySchemaValidator> {
/**
* Creates a new instance of the entity mapper.
*
* @param {AnySchemaValidator} schemaValidator - The arguments to pass to the constructor.
* @returns {T} - A new instance of the entity mapper.
*/
new (schemaValidator: SV): T;
}
/**
* Creates a new instance of the entity mapper.
*
* @param {AnySchemaValidator} schemaValidator - The arguments to pass to the constructor.
* @returns {T} - A new instance of the entity mapper.
*/
new (schemaValidator: SV): T;
}
Loading