lemon-model 1.0.6 → 1.1.1

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.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * @copyright (C) 2023 LemonCloud Co Ltd. - All Rights Reserved.
10
10
  */
11
- import { CoreModel } from '../types/core-storage';
11
+ import { CoreModel, CoreModifier } from '../types/core-storage';
12
12
  /**
13
13
  * internal type `Model`
14
14
  */
@@ -16,7 +16,7 @@ declare type Model = CoreModel<string>;
16
16
  /**
17
17
  * partial set of `CoreModel`
18
18
  */
19
- export interface Cores {
19
+ export interface Cores extends Partial<CoreModifier> {
20
20
  /**
21
21
  * site-id of model
22
22
  * - inited by `identity-token` if use session
@@ -59,7 +59,9 @@ export interface View {
59
59
  */
60
60
  error?: string;
61
61
  /**
62
- * (optional) internal cores data
62
+ * (optional) internal cores modifier info.
63
+ * - present if `hasCores` is true in `modelAsView()`
64
+ * - or can be shown only if model has cores data.
63
65
  */
64
66
  readonly $?: Cores;
65
67
  }
@@ -126,13 +128,19 @@ export declare abstract class AbstractTransformer<MyModel extends Model, MyView
126
128
  * extract only the defined attribute.
127
129
  * ex) `{ a:1, b: undefined }` -> `{ a:1 }`
128
130
  */
129
- onlyDefined: <T extends object>(N: T) => T;
131
+ onlyDefined: <T extends object>(N?: T) => T;
132
+ /**
133
+ * transform the cores data
134
+ * @param cores the cores data to transform
135
+ * @returns transformed cores data or null
136
+ */
137
+ asCore<T extends Cores>(cores?: T | null): T | null;
130
138
  /**
131
139
  * transform from model to view
132
140
  *
133
141
  * @see Transformable.modelAsView
134
142
  */
135
- modelAsView(model: MyModel, hasCores?: boolean): MyView;
143
+ modelAsView(model?: MyModel | null, hasCores?: boolean): MyView;
136
144
  /**
137
145
  * transform from body(req-body) to model
138
146
  *
@@ -25,13 +25,32 @@ class AbstractTransformer {
25
25
  * extract only the defined attribute.
26
26
  * ex) `{ a:1, b: undefined }` -> `{ a:1 }`
27
27
  */
28
- this.onlyDefined = (N) => N && typeof N === 'object'
29
- ? Object.entries(N).reduce((N, [k, v]) => {
30
- if (v !== undefined)
31
- N[k] = v;
32
- return N;
33
- }, {})
34
- : null;
28
+ this.onlyDefined = (N) => N === undefined || N === null
29
+ ? N
30
+ : N && typeof N === 'object'
31
+ ? Object.entries(N).reduce((N, [k, v]) => {
32
+ if (v !== undefined)
33
+ N[k] = v;
34
+ return N;
35
+ }, {})
36
+ : null;
37
+ }
38
+ /**
39
+ * transform the cores data
40
+ * @param cores the cores data to transform
41
+ * @returns transformed cores data or null
42
+ */
43
+ asCore(cores) {
44
+ if (!cores || typeof cores !== 'object')
45
+ return null;
46
+ const ret = {};
47
+ if (cores.sid !== undefined)
48
+ ret.sid = String(cores.sid);
49
+ if (cores.gid !== undefined)
50
+ ret.gid = String(cores.gid);
51
+ if (cores.uid !== undefined)
52
+ ret.uid = String(cores.uid);
53
+ return this.onlyDefined(ret);
35
54
  }
36
55
  /**
37
56
  * transform from model to view
@@ -39,22 +58,20 @@ class AbstractTransformer {
39
58
  * @see Transformable.modelAsView
40
59
  */
41
60
  modelAsView(model, hasCores) {
61
+ var _a;
42
62
  if (!model || typeof model !== 'object')
43
63
  return null;
44
- const view = {
64
+ const view = this.onlyDefined({
65
+ $: (_a = model === null || model === void 0 ? void 0 : model.$) !== null && _a !== void 0 ? _a : undefined,
45
66
  id: model.id,
46
67
  createdAt: model.createdAt,
47
68
  updatedAt: model.updatedAt,
48
69
  deletedAt: model.deletedAt,
49
- };
70
+ });
50
71
  if (model.error)
51
72
  view.error = model.error;
52
73
  if (hasCores) {
53
- const $ = this.onlyDefined({
54
- sid: model.sid,
55
- gid: model.gid,
56
- uid: model.uid,
57
- });
74
+ const $ = this.asCore(model);
58
75
  Object.assign(view, { $ });
59
76
  }
60
77
  return this.onlyDefined(view);
@@ -1 +1 @@
1
- {"version":3,"file":"transformer.js","sourceRoot":"","sources":["../../src/cores/transformer.ts"],"names":[],"mappings":";;;AA0HA;;;GAGG;AAC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C,CAAC,EAAE,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,CAAC,mCAAI,SAAS;YACxB,EAAE,EAAE,KAAK,CAAC,EAAE;YACZ,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,SAAS,EAAE,KAAK,CAAC,SAAS;SAC7B,CAAC,CAAC;QACH,IAAI,KAAK,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QAC1C,IAAI,QAAQ,EAAE;YACV,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAQ,KAAK,CAAC,CAAC;YACpC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC,WAAW,CAAS,IAAc,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,IAAkB,EAAE,QAAkB;;QACrD,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACnD,MAAM,KAAK,GAAG,EAAa,CAAC;QAE5B,IAAI,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,EAAE,MAAK,SAAS;YAAE,KAAK,CAAC,EAAE,GAAG,GAAG,MAAA,IAAI,CAAC,EAAE,mCAAI,EAAE,EAAE,CAAC;QAE1D,OAAO,KAAgB,CAAC;IAC5B,CAAC;CACJ;AA/ED,kDA+EC"}
@@ -32,11 +32,29 @@ export interface StorageModel extends InternalKey {
32
32
  /** json formated string (or parsed object). */
33
33
  meta?: string | any;
34
34
  }
35
+ /**
36
+ * class: `CoreModifier`
37
+ * - common modifier information for core-model
38
+ */
39
+ export interface CoreModifier {
40
+ /**
41
+ * site-id
42
+ */
43
+ sid?: string;
44
+ /**
45
+ * user-id
46
+ */
47
+ uid?: string;
48
+ /**
49
+ * group-id
50
+ */
51
+ gid?: string;
52
+ }
35
53
  /**
36
54
  * class: `InternalModel`
37
55
  * - common internal properties. (ONLY FOR INTERNAL PROCESSING)
38
56
  */
39
- export interface InternalModel {
57
+ export interface InternalModel extends CoreModifier {
40
58
  /**
41
59
  * internal unique partition-key (valid if using default idName )
42
60
  */
@@ -47,6 +65,11 @@ export interface InternalModel {
47
65
  * - general model out of base Model to support the common usage
48
66
  */
49
67
  export interface CoreModel<ModelType extends string = string> extends StorageModel, InternalModel {
68
+ /**
69
+ * the last modifier info
70
+ * - set automatically when model is created/updated/deleted.
71
+ */
72
+ readonly $?: CoreModifier;
50
73
  /**
51
74
  * namespace
52
75
  */
@@ -14,7 +14,20 @@
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.CORE_FIELDS = void 0;
16
16
  //NOTE! - BE WARE TO USE `ts-transformer-keys` DUE TO MISSING `ttypescript`
17
- // export const CORE_FIELDS: string[] = keys<CoreModel>().filter(_ => !_.startsWith('_'));
18
- // _inf(NS, '! CORE_FIELDS =', CORE_FIELDS.join(', ')); // for debugging.
19
- exports.CORE_FIELDS = 'ns,type,stereo,sid,uid,gid,lock,next,meta,createdAt,updatedAt,deletedAt'.split(',');
17
+ exports.CORE_FIELDS = [
18
+ '$',
19
+ 'ns',
20
+ 'type',
21
+ 'stereo',
22
+ 'sid',
23
+ 'uid',
24
+ 'gid',
25
+ 'lock',
26
+ 'next',
27
+ 'meta',
28
+ 'createdAt',
29
+ 'updatedAt',
30
+ 'deletedAt',
31
+ 'error',
32
+ ];
20
33
  //# sourceMappingURL=core-storage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"core-storage.js","sourceRoot":"","sources":["../../src/types/core-storage.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAgGH,2EAA2E;AAC3E,0FAA0F;AAC1F,yEAAyE;AAC5D,QAAA,WAAW,GAAa,yEAAyE,CAAC,KAAK,CAChH,GAAG,CACN,CAAC"}
1
+ {"version":3,"file":"core-storage.js","sourceRoot":"","sources":["../../src/types/core-storage.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAwHH,2EAA2E;AAC9D,QAAA,WAAW,GAAa;IACjC,GAAG;IACH,IAAI;IACJ,MAAM;IACN,QAAQ;IACR,KAAK;IACL,KAAK;IACL,KAAK;IACL,MAAM;IACN,MAAM;IACN,MAAM;IACN,WAAW;IACX,WAAW;IACX,WAAW;IACX,OAAO;CACV,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lemon-model",
3
- "version": "1.0.6",
3
+ "version": "1.1.1",
4
4
  "description": "Common shared model definitions for Lemon Micro-Service Platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",