@ruiapp/rapid-core 0.5.10 → 0.5.12

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,6 +1,16 @@
1
+ import { IRpdServer } from "../../core/server";
2
+ export interface CacheFactoryConfig {
3
+ providers?: CacheProvider[];
4
+ }
1
5
  export interface CacheProvider {
2
- set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
- get: (key: string) => Promise<any>;
6
+ providerName: string;
7
+ createCache: (server: IRpdServer) => Promise<Cache>;
8
+ }
9
+ export interface Cache {
10
+ set: (key: string, value: string, options?: SetValueOptions) => Promise<void>;
11
+ setObject: (key: string, value: Record<string, any>, options?: SetValueOptions) => Promise<void>;
12
+ get: (key: string) => Promise<string | null>;
13
+ getObject: (key: string) => Promise<Record<string, any> | null>;
4
14
  del: (key: string) => Promise<void>;
5
15
  }
6
16
  export interface SetValueOptions {
@@ -9,3 +19,6 @@ export interface SetValueOptions {
9
19
  */
10
20
  ttl?: number;
11
21
  }
22
+ export interface CreateCacheFacilityOptions {
23
+ providerName: string;
24
+ }
@@ -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,10 +1,12 @@
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 {
7
- set(key: string, value: any, options?: SetValueOptions): Promise<void>;
6
+ export default class MemoryCache implements Cache {
7
+ set(key: string, value: string, options?: SetValueOptions): Promise<void>;
8
+ setObject(key: string, value: Record<string, any>, options?: SetValueOptions): Promise<void>;
8
9
  get(key: string): Promise<any>;
10
+ getObject(key: string): Promise<any>;
9
11
  del(key: string): Promise<void>;
10
12
  }
@@ -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
@@ -6,7 +6,10 @@ export * from "./core/routeContext";
6
6
  export * from "./core/server";
7
7
  export * from "./core/http-types";
8
8
  export * from "./core/actionHandler";
9
+ export * from "./utilities/accessControlUtility";
10
+ export * from "./utilities/entityUtility";
9
11
  export * from "./utilities/jwtUtility";
12
+ export * from "./utilities/timeUtility";
10
13
  export * from "./deno-std/http/cookie";
11
14
  export { mapDbRowToEntity } from "./dataAccess/entityMapper";
12
15
  export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
package/dist/index.js CHANGED
@@ -2509,6 +2509,9 @@ function getEntityPartChanges(server, model, before, after) {
2509
2509
  return null;
2510
2510
  }
2511
2511
 
2512
+ function getNowString() {
2513
+ return dayjs__default["default"]().format("YYYY-MM-DD HH:mm:ss.SSS");
2514
+ }
2512
2515
  function getNowStringWithTimezone() {
2513
2516
  return dayjs__default["default"]().format("YYYY-MM-DD HH:mm:ss.SSSZ");
2514
2517
  }
@@ -4511,6 +4514,46 @@ class RapidServer {
4511
4514
  }
4512
4515
  }
4513
4516
 
4517
+ function isAccessAllowed(policy, allowedActions) {
4518
+ let isAnyCheckPassed = true;
4519
+ let isAllCheckPassed = true;
4520
+ if (policy.any) {
4521
+ isAnyCheckPassed = false;
4522
+ for (const action of policy.any) {
4523
+ if (lodash.find(allowedActions, (item) => item === action) != null) {
4524
+ isAnyCheckPassed = true;
4525
+ break;
4526
+ }
4527
+ }
4528
+ }
4529
+ if (policy.all) {
4530
+ isAllCheckPassed = true;
4531
+ for (const action of policy.all) {
4532
+ if (lodash.find(allowedActions, (item) => item === action) == null) {
4533
+ isAnyCheckPassed = false;
4534
+ break;
4535
+ }
4536
+ }
4537
+ }
4538
+ return isAnyCheckPassed && isAllCheckPassed;
4539
+ }
4540
+
4541
+ function getEntityRelationTargetId(entity, propName, targetColumnName) {
4542
+ if (!entity) {
4543
+ throw new Error(`"entity" parameter is required.`);
4544
+ }
4545
+ let targetId = entity[propName];
4546
+ if (!targetId && targetColumnName) {
4547
+ targetId = entity[targetColumnName];
4548
+ }
4549
+ if (lodash.isObject(targetId)) {
4550
+ return targetId.id;
4551
+ }
4552
+ else {
4553
+ return targetId;
4554
+ }
4555
+ }
4556
+
4514
4557
  // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
4515
4558
  // This module is browser compatible.
4516
4559
  /**
@@ -4655,42 +4698,76 @@ async function generateJwtSecretKey() {
4655
4698
  }
4656
4699
 
4657
4700
  const values = new Map();
4701
+ async function set(key, value, options) {
4702
+ let expireAt = -1;
4703
+ if (options && options.ttl > 0) {
4704
+ expireAt = new Date().valueOf() + options.ttl;
4705
+ }
4706
+ const cacheItem = {
4707
+ value,
4708
+ expireAt,
4709
+ };
4710
+ values.set(key, cacheItem);
4711
+ }
4712
+ async function get(key) {
4713
+ const cacheItem = values.get(key);
4714
+ if (cacheItem) {
4715
+ if (cacheItem.expireAt === -1) {
4716
+ return cacheItem.value;
4717
+ }
4718
+ if (cacheItem.expireAt >= new Date().valueOf()) {
4719
+ return cacheItem.value;
4720
+ }
4721
+ }
4722
+ return undefined;
4723
+ }
4658
4724
  class MemoryCache {
4659
4725
  async set(key, value, options) {
4660
- let expireAt = -1;
4661
- if (options && options.ttl > 0) {
4662
- expireAt = new Date().valueOf() + options.ttl;
4663
- }
4664
- const cacheItem = {
4665
- value,
4666
- expireAt,
4667
- };
4668
- values.set(key, cacheItem);
4726
+ await set(key, value, options);
4727
+ }
4728
+ async setObject(key, value, options) {
4729
+ await set(key, value, options);
4669
4730
  }
4670
4731
  async get(key) {
4671
- const cacheItem = values.get(key);
4672
- if (cacheItem) {
4673
- if (cacheItem.expireAt === -1) {
4674
- return cacheItem.value;
4675
- }
4676
- if (cacheItem.expireAt >= new Date().valueOf()) {
4677
- return cacheItem.value;
4678
- }
4679
- }
4680
- return undefined;
4732
+ return get(key);
4733
+ }
4734
+ async getObject(key) {
4735
+ return get(key);
4681
4736
  }
4682
4737
  async del(key) {
4683
4738
  values.delete(key);
4684
4739
  }
4685
4740
  }
4686
4741
 
4742
+ class MemoryCacheProvider {
4743
+ providerName;
4744
+ constructor() {
4745
+ this.providerName = "memory";
4746
+ }
4747
+ async createCache(server) {
4748
+ return new MemoryCache();
4749
+ }
4750
+ }
4751
+
4687
4752
  class CacheFactory {
4688
4753
  name;
4689
- constructor() {
4754
+ #providers;
4755
+ constructor(config) {
4690
4756
  this.name = "cache";
4757
+ const memoryCacheProvider = new MemoryCacheProvider();
4758
+ this.#providers = new Map();
4759
+ this.#providers.set(memoryCacheProvider.providerName, memoryCacheProvider);
4760
+ for (const provider of config.providers) {
4761
+ this.#providers.set(provider.providerName, provider);
4762
+ }
4691
4763
  }
4692
4764
  async createFacility(server, options) {
4693
- return new MemoryCache();
4765
+ const providerName = options?.providerName || "memory";
4766
+ const creator = this.#providers.get(providerName);
4767
+ if (!creator) {
4768
+ throw new Error(`Unkown cache provider name: ${providerName}`);
4769
+ }
4770
+ return creator.createCache(server);
4694
4771
  }
4695
4772
  }
4696
4773
 
@@ -8569,30 +8646,6 @@ function getStateMachineCode(model, property) {
8569
8646
  }
8570
8647
  }
8571
8648
 
8572
- function isAccessAllowed(policy, allowedActions) {
8573
- let isAnyCheckPassed = true;
8574
- let isAllCheckPassed = true;
8575
- if (policy.any) {
8576
- isAnyCheckPassed = false;
8577
- for (const action of policy.any) {
8578
- if (lodash.find(allowedActions, (item) => item === action) != null) {
8579
- isAnyCheckPassed = true;
8580
- break;
8581
- }
8582
- }
8583
- }
8584
- if (policy.all) {
8585
- isAllCheckPassed = true;
8586
- for (const action of policy.all) {
8587
- if (lodash.find(allowedActions, (item) => item === action) == null) {
8588
- isAnyCheckPassed = false;
8589
- break;
8590
- }
8591
- }
8592
- }
8593
- return isAnyCheckPassed && isAllCheckPassed;
8594
- }
8595
-
8596
8649
  class EntityAccessControlPlugin {
8597
8650
  constructor() { }
8598
8651
  get code() {
@@ -8740,7 +8793,11 @@ exports.decodeJwt = decodeJwt;
8740
8793
  exports.deleteCookie = deleteCookie;
8741
8794
  exports.generateJwtSecretKey = generateJwtSecretKey;
8742
8795
  exports.getCookies = getCookies;
8796
+ exports.getEntityRelationTargetId = getEntityRelationTargetId;
8797
+ exports.getNowString = getNowString;
8798
+ exports.getNowStringWithTimezone = getNowStringWithTimezone;
8743
8799
  exports.getSetCookies = getSetCookies;
8800
+ exports.isAccessAllowed = isAccessAllowed;
8744
8801
  exports.mapDbRowToEntity = mapDbRowToEntity;
8745
8802
  exports.setCookie = setCookie;
8746
8803
  exports.verifyJwt = verifyJwt;
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[];
@@ -0,0 +1 @@
1
+ export declare function getEntityRelationTargetId(entity: Record<string, any>, propName: string, targetColumnName?: string): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.5.10",
3
+ "version": "0.5.12",
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,6 +1,19 @@
1
+ import { IRpdServer } from "~/core/server";
2
+
3
+ export interface CacheFactoryConfig {
4
+ providers?: CacheProvider[];
5
+ }
6
+
1
7
  export interface CacheProvider {
2
- set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
- get: (key: string) => Promise<any>;
8
+ providerName: string;
9
+ createCache: (server: IRpdServer) => Promise<Cache>;
10
+ }
11
+
12
+ export interface Cache {
13
+ set: (key: string, value: string, options?: SetValueOptions) => Promise<void>;
14
+ setObject: (key: string, value: Record<string, any>, options?: SetValueOptions) => Promise<void>;
15
+ get: (key: string) => Promise<string | null>;
16
+ getObject: (key: string) => Promise<Record<string, any> | null>;
4
17
  del: (key: string) => Promise<void>;
5
18
  }
6
19
 
@@ -10,3 +23,7 @@ export interface SetValueOptions {
10
23
  */
11
24
  ttl?: number;
12
25
  }
26
+
27
+ export interface CreateCacheFacilityOptions {
28
+ providerName: string;
29
+ }
@@ -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 memoryCacheProvider = new MemoryCacheProvider();
14
+
15
+ this.#providers = new Map();
16
+ this.#providers.set(memoryCacheProvider.providerName, memoryCacheProvider);
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,33 +7,49 @@ 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 {
11
- async set(key: string, value: any, options?: SetValueOptions) {
12
- let expireAt = -1;
13
- if (options && options.ttl > 0) {
14
- expireAt = new Date().valueOf() + options.ttl;
10
+ async function set(key: string, value: any, options?: SetValueOptions) {
11
+ let expireAt = -1;
12
+ if (options && options.ttl > 0) {
13
+ expireAt = new Date().valueOf() + options.ttl;
14
+ }
15
+
16
+ const cacheItem = {
17
+ value,
18
+ expireAt,
19
+ };
20
+ values.set(key, cacheItem);
21
+ }
22
+
23
+ async function get(key: string) {
24
+ const cacheItem = values.get(key);
25
+ if (cacheItem) {
26
+ if (cacheItem.expireAt === -1) {
27
+ return cacheItem.value;
15
28
  }
16
29
 
17
- const cacheItem = {
18
- value,
19
- expireAt,
20
- };
21
- values.set(key, cacheItem);
30
+ if (cacheItem.expireAt >= new Date().valueOf()) {
31
+ return cacheItem.value;
32
+ }
33
+ }
34
+
35
+ return undefined;
36
+ }
37
+
38
+ export default class MemoryCache implements Cache {
39
+ async set(key: string, value: string, options?: SetValueOptions) {
40
+ await set(key, value, options);
41
+ }
42
+
43
+ async setObject(key: string, value: Record<string, any>, options?: SetValueOptions) {
44
+ await set(key, value, options);
22
45
  }
23
46
 
24
47
  async get(key: string) {
25
- const cacheItem = values.get(key);
26
- if (cacheItem) {
27
- if (cacheItem.expireAt === -1) {
28
- return cacheItem.value;
29
- }
30
-
31
- if (cacheItem.expireAt >= new Date().valueOf()) {
32
- return cacheItem.value;
33
- }
34
- }
48
+ return get(key);
49
+ }
35
50
 
36
- return undefined;
51
+ async getObject(key: string) {
52
+ return get(key);
37
53
  }
38
54
 
39
55
  async del(key: string) {
@@ -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
@@ -11,7 +11,10 @@ export * from "./core/server";
11
11
  export * from "./core/http-types";
12
12
  export * from "./core/actionHandler";
13
13
 
14
+ export * from "./utilities/accessControlUtility";
15
+ export * from "./utilities/entityUtility";
14
16
  export * from "./utilities/jwtUtility";
17
+ export * from "./utilities/timeUtility";
15
18
 
16
19
  export * from "./deno-std/http/cookie";
17
20
 
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) {
@@ -0,0 +1,18 @@
1
+ import { isObject } from "lodash";
2
+
3
+ export function getEntityRelationTargetId(entity: Record<string, any>, propName: string, targetColumnName?: string) {
4
+ if (!entity) {
5
+ throw new Error(`"entity" parameter is required.`);
6
+ }
7
+
8
+ let targetId: number | { id?: number } = entity[propName];
9
+ if (!targetId && targetColumnName) {
10
+ targetId = entity[targetColumnName];
11
+ }
12
+
13
+ if (isObject(targetId)) {
14
+ return targetId.id;
15
+ } else {
16
+ return targetId;
17
+ }
18
+ }