@ruiapp/rapid-core 0.5.9 → 0.5.11

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.
@@ -1,5 +1,5 @@
1
1
  import { IRpdServer } from "./server";
2
- export interface FacilityFactory {
2
+ export interface FacilityFactory<TFacility = any, TCreateFacilityOptions = any> {
3
3
  name: string;
4
- createFacility: (server: IRpdServer, options?: any) => Promise<any>;
4
+ createFacility: (server: IRpdServer, options?: TCreateFacilityOptions) => Promise<TFacility>;
5
5
  }
@@ -10,7 +10,7 @@ export interface IRpdServer {
10
10
  queryBuilder: IQueryBuilder;
11
11
  getLogger(): Logger;
12
12
  registerFacilityFactory(factory: FacilityFactory): any;
13
- getFacility<TFacility = any>(name: string, options?: any): Promise<TFacility>;
13
+ getFacility<TFacility = any, TOptions = any>(name: string, options?: TOptions): Promise<TFacility>;
14
14
  getDatabaseAccessor(): IDatabaseAccessor;
15
15
  queryDatabaseObject: (sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient) => Promise<any[]>;
16
16
  tryQueryDatabaseObject: (sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient) => Promise<any[]>;
@@ -1,4 +1,12 @@
1
+ import { IRpdServer } from "../../core/server";
2
+ export interface CacheFactoryConfig {
3
+ providers?: CacheProvider[];
4
+ }
1
5
  export interface CacheProvider {
6
+ providerName: string;
7
+ createCache: (server: IRpdServer) => Promise<Cache>;
8
+ }
9
+ export interface Cache {
2
10
  set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
11
  get: (key: string) => Promise<any>;
4
12
  del: (key: string) => Promise<void>;
@@ -9,3 +17,6 @@ export interface SetValueOptions {
9
17
  */
10
18
  ttl?: number;
11
19
  }
20
+ export interface CreateCacheFacilityOptions {
21
+ providerName: string;
22
+ }
@@ -1,8 +1,9 @@
1
1
  import { FacilityFactory } from "../../core/facility";
2
2
  import { IRpdServer } from "../../core/server";
3
- import MemoryCache from "./MemoryCache";
4
- export default class CacheFactory implements FacilityFactory {
3
+ import { Cache, CacheFactoryConfig, CreateCacheFacilityOptions } from "./CacheFacilityTypes";
4
+ export default class CacheFactory implements FacilityFactory<Cache, CreateCacheFacilityOptions> {
5
+ #private;
5
6
  readonly name: string;
6
- constructor();
7
- createFacility(server: IRpdServer, options?: any): Promise<MemoryCache>;
7
+ constructor(config: CacheFactoryConfig);
8
+ createFacility(server: IRpdServer, options?: CreateCacheFacilityOptions): Promise<Cache>;
8
9
  }
@@ -1,9 +1,9 @@
1
- import { CacheProvider, SetValueOptions } from "./CacheFacilityTypes";
1
+ import { Cache, SetValueOptions } from "./CacheFacilityTypes";
2
2
  export type MemoryCacheItem<TValue = any> = {
3
3
  value: TValue;
4
4
  expireAt: number;
5
5
  };
6
- export default class MemoryCache implements CacheProvider {
6
+ export default class MemoryCache implements Cache {
7
7
  set(key: string, value: any, options?: SetValueOptions): Promise<void>;
8
8
  get(key: string): Promise<any>;
9
9
  del(key: string): Promise<void>;
@@ -0,0 +1,8 @@
1
+ import MemoryCache from "./MemoryCache";
2
+ import { CacheProvider } from "./CacheFacilityTypes";
3
+ import { IRpdServer } from "../../core/server";
4
+ export default class MemoryCacheProvider implements CacheProvider {
5
+ readonly providerName: string;
6
+ constructor();
7
+ createCache(server: IRpdServer): Promise<MemoryCache>;
8
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./types";
2
2
  export * from "./server";
3
+ export * from "./core/facility";
3
4
  export * from "./core/request";
4
5
  export * from "./core/routeContext";
5
6
  export * from "./core/server";
package/dist/index.js CHANGED
@@ -4684,13 +4684,35 @@ class MemoryCache {
4684
4684
  }
4685
4685
  }
4686
4686
 
4687
+ class MemoryCacheProvider {
4688
+ providerName;
4689
+ constructor() {
4690
+ this.providerName = "memory";
4691
+ }
4692
+ async createCache(server) {
4693
+ return new MemoryCache();
4694
+ }
4695
+ }
4696
+
4687
4697
  class CacheFactory {
4688
4698
  name;
4689
- constructor() {
4699
+ #providers;
4700
+ constructor(config) {
4690
4701
  this.name = "cache";
4702
+ const memoryCacheCreator = new MemoryCacheProvider();
4703
+ this.#providers = new Map();
4704
+ this.#providers.set(memoryCacheCreator.providerName, memoryCacheCreator);
4705
+ for (const provider of config.providers) {
4706
+ this.#providers.set(provider.providerName, provider);
4707
+ }
4691
4708
  }
4692
4709
  async createFacility(server, options) {
4693
- return new MemoryCache();
4710
+ const providerName = options?.providerName || "memory";
4711
+ const creator = this.#providers.get(providerName);
4712
+ if (!creator) {
4713
+ throw new Error(`Unkown cache provider name: ${providerName}`);
4714
+ }
4715
+ return creator.createCache(server);
4694
4716
  }
4695
4717
  }
4696
4718
 
package/dist/server.d.ts CHANGED
@@ -40,7 +40,7 @@ export declare class RapidServer implements IRpdServer {
40
40
  start(): Promise<void>;
41
41
  configureApplication(): Promise<void>;
42
42
  registerFacilityFactory(factory: FacilityFactory): void;
43
- getFacility<TFacility = any>(name: string, options?: any, nullIfUnknownFacility?: boolean): Promise<TFacility>;
43
+ getFacility<TFacility = any, TOptions = any>(name: string, options?: TOptions, nullIfUnknownFacility?: boolean): Promise<TFacility>;
44
44
  queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient): Promise<any[]>;
45
45
  tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient): Promise<any[]>;
46
46
  get middlewares(): any[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.5.9",
3
+ "version": "0.5.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  import { IRpdServer } from "./server";
2
2
 
3
- export interface FacilityFactory {
3
+ export interface FacilityFactory<TFacility = any, TCreateFacilityOptions = any> {
4
4
  name: string;
5
5
 
6
- createFacility: (server: IRpdServer, options?: any) => Promise<any>;
6
+ createFacility: (server: IRpdServer, options?: TCreateFacilityOptions) => Promise<TFacility>;
7
7
  }
@@ -30,7 +30,7 @@ export interface IRpdServer {
30
30
 
31
31
  registerFacilityFactory(factory: FacilityFactory);
32
32
 
33
- getFacility<TFacility = any>(name: string, options?: any): Promise<TFacility>;
33
+ getFacility<TFacility = any, TOptions = any>(name: string, options?: TOptions): Promise<TFacility>;
34
34
 
35
35
  getDatabaseAccessor(): IDatabaseAccessor;
36
36
  queryDatabaseObject: (sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient) => Promise<any[]>;
@@ -1,4 +1,15 @@
1
+ import { IRpdServer } from "~/core/server";
2
+
3
+ export interface CacheFactoryConfig {
4
+ providers?: CacheProvider[];
5
+ }
6
+
1
7
  export interface CacheProvider {
8
+ providerName: string;
9
+ createCache: (server: IRpdServer) => Promise<Cache>;
10
+ }
11
+
12
+ export interface Cache {
2
13
  set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
14
  get: (key: string) => Promise<any>;
4
15
  del: (key: string) => Promise<void>;
@@ -10,3 +21,7 @@ export interface SetValueOptions {
10
21
  */
11
22
  ttl?: number;
12
23
  }
24
+
25
+ export interface CreateCacheFacilityOptions {
26
+ providerName: string;
27
+ }
@@ -1,15 +1,31 @@
1
1
  import { FacilityFactory } from "~/core/facility";
2
2
  import { IRpdServer } from "~/core/server";
3
- import MemoryCache from "./MemoryCache";
3
+ import { Cache, CacheProvider, CacheFactoryConfig, CreateCacheFacilityOptions } from "./CacheFacilityTypes";
4
+ import MemoryCacheProvider from "./MemoryCacheProvider";
4
5
 
5
- export default class CacheFactory implements FacilityFactory {
6
+ export default class CacheFactory implements FacilityFactory<Cache, CreateCacheFacilityOptions> {
6
7
  readonly name: string;
8
+ #providers: Map<string, CacheProvider>;
7
9
 
8
- constructor() {
10
+ constructor(config: CacheFactoryConfig) {
9
11
  this.name = "cache";
12
+
13
+ const memoryCacheCreator = new MemoryCacheProvider();
14
+
15
+ this.#providers = new Map();
16
+ this.#providers.set(memoryCacheCreator.providerName, memoryCacheCreator);
17
+
18
+ for (const provider of config.providers) {
19
+ this.#providers.set(provider.providerName, provider);
20
+ }
10
21
  }
11
22
 
12
- async createFacility(server: IRpdServer, options?: any) {
13
- return new MemoryCache();
23
+ async createFacility(server: IRpdServer, options?: CreateCacheFacilityOptions) {
24
+ const providerName = options?.providerName || "memory";
25
+ const creator = this.#providers.get(providerName);
26
+ if (!creator) {
27
+ throw new Error(`Unkown cache provider name: ${providerName}`);
28
+ }
29
+ return creator.createCache(server);
14
30
  }
15
31
  }
@@ -1,4 +1,4 @@
1
- import { CacheProvider, SetValueOptions } from "./CacheFacilityTypes";
1
+ import { Cache, SetValueOptions } from "./CacheFacilityTypes";
2
2
 
3
3
  export type MemoryCacheItem<TValue = any> = {
4
4
  value: TValue;
@@ -7,7 +7,7 @@ export type MemoryCacheItem<TValue = any> = {
7
7
 
8
8
  const values: Map<string, MemoryCacheItem> = new Map();
9
9
 
10
- export default class MemoryCache implements CacheProvider {
10
+ export default class MemoryCache implements Cache {
11
11
  async set(key: string, value: any, options?: SetValueOptions) {
12
12
  let expireAt = -1;
13
13
  if (options && options.ttl > 0) {
@@ -0,0 +1,15 @@
1
+ import MemoryCache from "./MemoryCache";
2
+ import { CacheProvider } from "./CacheFacilityTypes";
3
+ import { IRpdServer } from "~/core/server";
4
+
5
+ export default class MemoryCacheProvider implements CacheProvider {
6
+ readonly providerName: string;
7
+
8
+ constructor() {
9
+ this.providerName = "memory";
10
+ }
11
+
12
+ async createCache(server: IRpdServer) {
13
+ return new MemoryCache();
14
+ }
15
+ }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ fixBigIntJSONSerialize();
4
4
  export * from "./types";
5
5
  export * from "./server";
6
6
 
7
+ export * from "./core/facility";
7
8
  export * from "./core/request";
8
9
  export * from "./core/routeContext";
9
10
  export * from "./core/server";
package/src/server.ts CHANGED
@@ -361,7 +361,7 @@ export class RapidServer implements IRpdServer {
361
361
  this.#facilityFactories.set(factory.name, factory);
362
362
  }
363
363
 
364
- async getFacility<TFacility = any>(name: string, options?: any, nullIfUnknownFacility?: boolean): Promise<TFacility> {
364
+ async getFacility<TFacility = any, TOptions = any>(name: string, options?: TOptions, nullIfUnknownFacility?: boolean): Promise<TFacility> {
365
365
  const factory = this.#facilityFactories.get(name);
366
366
  if (!factory) {
367
367
  if (nullIfUnknownFacility) {