@sentio/sdk 2.39.4-rc.7 → 2.39.4-rc.8
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/lib/store/codegen.d.ts +0 -2
- package/lib/store/codegen.d.ts.map +1 -1
- package/lib/store/codegen.js +137 -170
- package/lib/store/codegen.js.map +1 -1
- package/lib/store/convert.d.ts +9 -9
- package/lib/store/convert.d.ts.map +1 -1
- package/lib/store/convert.js +64 -27
- package/lib/store/convert.js.map +1 -1
- package/lib/store/decorators.d.ts +25 -7
- package/lib/store/decorators.d.ts.map +1 -1
- package/lib/store/decorators.js +157 -16
- package/lib/store/decorators.js.map +1 -1
- package/lib/store/index.d.ts +0 -1
- package/lib/store/index.d.ts.map +1 -1
- package/lib/store/index.js +0 -1
- package/lib/store/index.js.map +1 -1
- package/lib/store/store.d.ts +10 -4
- package/lib/store/store.d.ts.map +1 -1
- package/lib/store/store.js +38 -16
- package/lib/store/store.js.map +1 -1
- package/lib/store/types.d.ts +21 -2
- package/lib/store/types.d.ts.map +1 -1
- package/lib/store/types.js +14 -1
- package/lib/store/types.js.map +1 -1
- package/package.json +4 -3
- package/src/store/codegen.ts +171 -196
- package/src/store/convert.ts +73 -28
- package/src/store/decorators.ts +178 -16
- package/src/store/index.ts +0 -1
- package/src/store/store.ts +51 -20
- package/src/store/types.ts +24 -2
- package/lib/store/entity.d.ts +0 -23
- package/lib/store/entity.d.ts.map +0 -1
- package/lib/store/entity.js +0 -70
- package/lib/store/entity.js.map +0 -1
- package/src/store/entity.ts +0 -84
package/src/store/decorators.ts
CHANGED
@@ -1,32 +1,194 @@
|
|
1
|
+
import 'reflect-metadata'
|
2
|
+
import {
|
3
|
+
array_,
|
4
|
+
BigDecimalConverter,
|
5
|
+
BigIntConverter,
|
6
|
+
BooleanConverter,
|
7
|
+
BytesConverter,
|
8
|
+
FloatConverter,
|
9
|
+
IDConverter,
|
10
|
+
IntConverter,
|
11
|
+
objectId_,
|
12
|
+
required_,
|
13
|
+
StringConverter,
|
14
|
+
TimestampConverter,
|
15
|
+
TypeConverters,
|
16
|
+
ValueConverter
|
17
|
+
} from './convert.js'
|
18
|
+
import { getStore } from './store.js'
|
19
|
+
import { RichStruct } from '@sentio/protos'
|
20
|
+
|
1
21
|
type Constructor = { new (...args: any[]): any }
|
2
22
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
23
|
+
function handleError(entity: string, key: string, fn: () => any) {
|
24
|
+
try {
|
25
|
+
return fn()
|
26
|
+
} catch (e) {
|
27
|
+
throw new Error(`Get property "${key}" from Entity "${entity}" failed: ${e.message}`)
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
export function Entity(name: string) {
|
32
|
+
return function <T extends Constructor>(BaseClass: T) {
|
33
|
+
return class extends BaseClass {
|
34
|
+
static entityName = name
|
35
|
+
readonly _data: RichStruct = { fields: {}, entityName: name }
|
36
|
+
|
37
|
+
constructor(...args: any[]) {
|
38
|
+
super()
|
39
|
+
const meta = (BaseClass as any).meta
|
40
|
+
for (const [propertyKey, t] of Object.entries(meta)) {
|
41
|
+
const converter = t as ValueConverter<any>
|
42
|
+
if (converter.isRelation && converter.relationName) {
|
43
|
+
const relationName = converter.relationName
|
44
|
+
const idGetter = () => {
|
45
|
+
return handleError(name, propertyKey, () => converter.to(this._data.fields[propertyKey]))
|
46
|
+
}
|
47
|
+
const idSetter = (value: any) => {
|
48
|
+
this._data.fields[propertyKey] = handleError(name, propertyKey, () => converter.from(value))
|
49
|
+
}
|
50
|
+
const idKey = converter.isArray ? propertyKey + 'IDs' : propertyKey + 'ID'
|
51
|
+
|
52
|
+
Object.defineProperty(this, idKey, {
|
53
|
+
get: idGetter,
|
54
|
+
set: idSetter
|
55
|
+
})
|
56
|
+
|
57
|
+
Object.defineProperty(this, propertyKey, {
|
58
|
+
get: () => {
|
59
|
+
const ids = idGetter()
|
60
|
+
if (Array.isArray(ids)) {
|
61
|
+
return Promise.all(
|
62
|
+
ids.map((id) => {
|
63
|
+
return getStore()?.get(relationName, id)
|
64
|
+
})
|
65
|
+
)
|
66
|
+
} else {
|
67
|
+
return getStore()?.get(relationName, ids)
|
68
|
+
}
|
69
|
+
},
|
70
|
+
set: (value) => {
|
71
|
+
if (value instanceof Promise) {
|
72
|
+
value.then((v) => {
|
73
|
+
idSetter(v)
|
74
|
+
})
|
75
|
+
} else {
|
76
|
+
idSetter(value)
|
77
|
+
}
|
78
|
+
}
|
79
|
+
})
|
80
|
+
} else {
|
81
|
+
Object.defineProperty(this, propertyKey, {
|
82
|
+
get: () => {
|
83
|
+
return handleError(name, propertyKey, () => converter.to(this._data.fields[propertyKey]))
|
84
|
+
},
|
85
|
+
set: (value) => {
|
86
|
+
this._data.fields[propertyKey] = handleError(name, propertyKey, () => converter.from(value))
|
87
|
+
}
|
88
|
+
})
|
89
|
+
}
|
90
|
+
const initData = args[0]
|
91
|
+
if (initData) {
|
92
|
+
for (const [key, value] of Object.entries(initData)) {
|
93
|
+
if (this.hasOwnProperty(key)) {
|
94
|
+
this[key] = value
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
7
101
|
}
|
8
102
|
}
|
9
103
|
|
10
|
-
export function
|
11
|
-
return function (target: any,
|
104
|
+
export function Column(type: string) {
|
105
|
+
return function (target: any, propertyKey: string) {
|
106
|
+
let required = false
|
107
|
+
if (type.endsWith('!')) {
|
108
|
+
required = true
|
109
|
+
type = type.slice(0, -1)
|
110
|
+
}
|
111
|
+
let typeConverter = TypeConverters[type]
|
112
|
+
if (!typeConverter) {
|
113
|
+
throw new Error(`Unsupported type ${type}`)
|
114
|
+
}
|
115
|
+
if (required) {
|
116
|
+
typeConverter = required_(typeConverter)
|
117
|
+
}
|
118
|
+
column(typeConverter)(target, propertyKey)
|
119
|
+
}
|
12
120
|
}
|
13
121
|
|
14
|
-
export function
|
15
|
-
return function (target: any,
|
122
|
+
export function ListColumn(type: string = '') {
|
123
|
+
return function (target: any, propertyKey: string) {
|
124
|
+
let required = false
|
125
|
+
if (type.endsWith('!')) {
|
126
|
+
required = true
|
127
|
+
type = type.slice(0, -1)
|
128
|
+
}
|
129
|
+
|
130
|
+
let typeConverter = TypeConverters[type]
|
131
|
+
if (!typeConverter) {
|
132
|
+
// support list of list
|
133
|
+
const meta = target.constructor.meta || {}
|
134
|
+
const type = meta[propertyKey]
|
135
|
+
if (type) {
|
136
|
+
typeConverter = type
|
137
|
+
} else {
|
138
|
+
throw new Error(`Can't find inner type convert`)
|
139
|
+
}
|
140
|
+
}
|
141
|
+
if (required) {
|
142
|
+
typeConverter = required_(typeConverter)
|
143
|
+
}
|
144
|
+
column(array_(typeConverter))(target, propertyKey)
|
145
|
+
}
|
16
146
|
}
|
17
147
|
|
18
|
-
export function
|
19
|
-
return function (target: any,
|
148
|
+
export function column(type?: ValueConverter<any>) {
|
149
|
+
return function (target: any, propertyKey: string) {
|
150
|
+
const reflectType = Reflect.getMetadata('design:type', target, propertyKey)
|
151
|
+
if (!type) {
|
152
|
+
const typeName = reflectType.name
|
153
|
+
type = TypeConverters[typeName]
|
154
|
+
if (!type) {
|
155
|
+
throw new Error(`Unsupported type ${typeName}`)
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
const meta = target.constructor.meta || {}
|
160
|
+
meta[propertyKey] = type
|
161
|
+
target.constructor.meta = meta
|
162
|
+
}
|
20
163
|
}
|
21
164
|
|
22
|
-
export
|
23
|
-
|
165
|
+
export const IDColumn = column(IDConverter)
|
166
|
+
export const IntColumn = column(IntConverter)
|
167
|
+
export const FloatColumn = column(FloatConverter)
|
168
|
+
export const BigDecimalColumn = column(BigDecimalConverter)
|
169
|
+
export const BigIntColumn = column(BigIntConverter)
|
170
|
+
export const StringColumn = column(StringConverter)
|
171
|
+
export const BooleanColumn = column(BooleanConverter)
|
172
|
+
export const TimestampColumn = column(TimestampConverter)
|
173
|
+
export const BytesColumn = column(BytesConverter)
|
174
|
+
|
175
|
+
export function Required(target: any, propertyKey: string) {
|
176
|
+
const meta = target.constructor.meta || {}
|
177
|
+
const type = meta[propertyKey]
|
178
|
+
if (type) {
|
179
|
+
meta[propertyKey] = required_(type)
|
180
|
+
}
|
181
|
+
target.constructor.meta = meta
|
24
182
|
}
|
25
183
|
|
26
|
-
export function
|
27
|
-
return function (target: any,
|
184
|
+
export function One(entity: string) {
|
185
|
+
return function (target: any, propertyKey: string) {
|
186
|
+
column(objectId_(entity))(target, propertyKey)
|
187
|
+
}
|
28
188
|
}
|
29
189
|
|
30
|
-
export function
|
31
|
-
return function (target: any,
|
190
|
+
export function Many(entity: string) {
|
191
|
+
return function (target: any, propertyKey: string) {
|
192
|
+
column(array_(objectId_(entity)))(target, propertyKey)
|
193
|
+
}
|
32
194
|
}
|
package/src/store/index.ts
CHANGED
package/src/store/store.ts
CHANGED
@@ -1,22 +1,30 @@
|
|
1
|
-
import { Entity, EntityClass } from './entity.js'
|
2
1
|
import { StoreContext } from './context.js'
|
3
2
|
import { DatabaseSchema } from '../core/index.js'
|
4
3
|
import { BigDecimal } from '@sentio/bigdecimal'
|
5
|
-
import { Bytes,
|
6
|
-
import { DBRequest_DBOperator, DBResponse } from '@sentio/protos'
|
4
|
+
import { Bytes, Float, ID, Int, Timestamp } from './types.js'
|
7
5
|
import type { RichStruct, RichValue } from '@sentio/protos'
|
6
|
+
import { DBRequest_DBOperator, DBResponse } from '@sentio/protos'
|
8
7
|
import { toBigInteger } from './convert.js'
|
8
|
+
import { PluginManager } from '@sentio/runtime'
|
9
9
|
|
10
10
|
type Value = ID | string | Int | Float | boolean | Timestamp | Bytes | BigDecimal | bigint
|
11
11
|
|
12
|
+
interface Entity {
|
13
|
+
id: ID
|
14
|
+
}
|
15
|
+
|
16
|
+
export interface EntityClass<T> {
|
17
|
+
new (data: Partial<T>): T
|
18
|
+
}
|
19
|
+
|
12
20
|
export class Store {
|
13
21
|
constructor(private readonly context: StoreContext) {}
|
14
22
|
|
15
|
-
async get<T extends Entity>(entity: EntityClass<T> | string, id:
|
23
|
+
async get<T extends Entity>(entity: EntityClass<T> | string, id: ID): Promise<T | undefined> {
|
16
24
|
const promise = this.context.sendRequest({
|
17
25
|
get: {
|
18
26
|
entity: typeof entity == 'string' ? entity : entity.prototype.entityName,
|
19
|
-
id
|
27
|
+
id: id.toString()
|
20
28
|
}
|
21
29
|
})
|
22
30
|
|
@@ -29,20 +37,32 @@ export class Store {
|
|
29
37
|
return undefined
|
30
38
|
}
|
31
39
|
|
32
|
-
async delete(entity: EntityClass<
|
33
|
-
const
|
34
|
-
|
35
|
-
|
36
|
-
|
40
|
+
async delete<T extends Entity>(entity: EntityClass<T> | T | T[], id?: string | string[]): Promise<void> {
|
41
|
+
const request = {
|
42
|
+
entity: [] as string[],
|
43
|
+
id: [] as string[]
|
44
|
+
}
|
45
|
+
if (id) {
|
46
|
+
if (Array.isArray(id)) {
|
47
|
+
for (const i of id) {
|
48
|
+
const items = (entity as any).prototype.entityName
|
49
|
+
request.entity.push(items)
|
50
|
+
request.id.push(i.toString())
|
51
|
+
}
|
52
|
+
} else {
|
53
|
+
request.entity.push((entity as any).prototype.entityName)
|
54
|
+
request.id.push(id)
|
37
55
|
}
|
38
56
|
} else {
|
39
|
-
|
57
|
+
const entities = Array.isArray(entity) ? entity : [entity]
|
58
|
+
for (const e of entities) {
|
59
|
+
request.entity.push(e.constructor.prototype.entityName)
|
60
|
+
request.id.push((e as Entity).id.toString())
|
61
|
+
}
|
40
62
|
}
|
63
|
+
|
41
64
|
await this.context.sendRequest({
|
42
|
-
delete:
|
43
|
-
entity: toBeDeleted.map((e) => e.entity) as string[],
|
44
|
-
id: toBeDeleted.map((e) => e.id) as string[]
|
45
|
-
}
|
65
|
+
delete: request
|
46
66
|
})
|
47
67
|
}
|
48
68
|
|
@@ -52,8 +72,8 @@ export class Store {
|
|
52
72
|
upsert: {
|
53
73
|
entity: entities.map((e) => e.constructor.prototype.entityName),
|
54
74
|
// data: entities.map((e) => serialize(e.data)),
|
55
|
-
id: entities.map((e) => e.id),
|
56
|
-
entityData: entities.map((e) => e.
|
75
|
+
id: entities.map((e) => e.id.toString()),
|
76
|
+
entityData: entities.map((e: any) => e._data)
|
57
77
|
}
|
58
78
|
})
|
59
79
|
|
@@ -89,16 +109,19 @@ export class Store {
|
|
89
109
|
|
90
110
|
private newEntity<T extends Entity>(entity: EntityClass<T> | string, data: RichStruct) {
|
91
111
|
if (typeof entity == 'string') {
|
92
|
-
|
112
|
+
let en = DatabaseSchema.findEntity(entity)
|
93
113
|
if (!en) {
|
94
114
|
// it is an interface
|
95
|
-
|
115
|
+
en = DatabaseSchema.findEntity(data.entityName)
|
116
|
+
if (!en) {
|
117
|
+
throw new Error(`Entity ${entity} not found`)
|
118
|
+
}
|
96
119
|
}
|
97
120
|
entity = en
|
98
121
|
}
|
99
122
|
|
100
123
|
const res = new (entity as EntityClass<T>)({}) as T
|
101
|
-
res.
|
124
|
+
;(res as any)._data = data
|
102
125
|
return res
|
103
126
|
}
|
104
127
|
}
|
@@ -175,3 +198,11 @@ function serializeBigDecimal(v: BigDecimal): RichValue {
|
|
175
198
|
bigdecimalValue: undefined
|
176
199
|
}
|
177
200
|
}
|
201
|
+
|
202
|
+
export function getStore() {
|
203
|
+
const dbContext = PluginManager.INSTANCE.dbContextLocalStorage.getStore()
|
204
|
+
if (dbContext) {
|
205
|
+
return new Store(dbContext)
|
206
|
+
}
|
207
|
+
return undefined
|
208
|
+
}
|
package/src/store/types.ts
CHANGED
@@ -1,7 +1,29 @@
|
|
1
|
-
export type ID = string
|
1
|
+
export type ID = (string | Uint8Array) & { __id__?: void }
|
2
2
|
export type String = string
|
3
|
-
export type Int = number
|
3
|
+
export type Int = number & { __int__?: void }
|
4
4
|
export type Float = number
|
5
5
|
export type Boolean = boolean
|
6
6
|
export type Timestamp = Date
|
7
7
|
export type Bytes = Uint8Array
|
8
|
+
export type BigInt = bigint
|
9
|
+
|
10
|
+
export class Struct<T = unknown, S = unknown> {
|
11
|
+
readonly TYPE!: T
|
12
|
+
constructor(readonly schema: S) {}
|
13
|
+
}
|
14
|
+
export type Infer<T extends Struct<any>> = T['TYPE']
|
15
|
+
export type ObjectSchema = Record<string, Struct<any, any>>
|
16
|
+
|
17
|
+
export function object<S extends ObjectSchema>(schema: S): Struct<ObjectType<S>, S> {
|
18
|
+
return new Struct(schema ?? null)
|
19
|
+
}
|
20
|
+
|
21
|
+
export type Simplify<T> = T extends any[] | Date ? T : { [K in keyof T]: T[K] } & NonNullable<unknown>
|
22
|
+
|
23
|
+
export type ObjectType<S extends ObjectSchema> = Simplify<{ [K in keyof S]: Infer<S[K]> }>
|
24
|
+
|
25
|
+
const StringStruct = new Struct<string, null>(null)
|
26
|
+
|
27
|
+
const UU = object({
|
28
|
+
test: StringStruct
|
29
|
+
})
|
package/lib/store/entity.d.ts
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
import { ID } from './types.js';
|
2
|
-
import { RichStruct } from '@sentio/protos';
|
3
|
-
import { ValueConverter } from './convert.js';
|
4
|
-
export interface EntityClass<T extends Entity> {
|
5
|
-
new (data: any): T;
|
6
|
-
}
|
7
|
-
export declare class Entity {
|
8
|
-
private _data;
|
9
|
-
get id(): ID;
|
10
|
-
protected fromPojo(data: any, converters: Record<string, ValueConverter<any>>): void;
|
11
|
-
constructor(_data?: RichStruct);
|
12
|
-
setData(data: RichStruct): void;
|
13
|
-
private getIdFromEntity;
|
14
|
-
private getStore;
|
15
|
-
get<T>(field: string, converter: ValueConverter<T>): T;
|
16
|
-
set<T>(field: string, value: T, serializer: ValueConverter<T>): void;
|
17
|
-
protected getObject<T extends Entity>(entity: EntityClass<T> | string, field: string): Promise<T | undefined>;
|
18
|
-
protected setObject(field: string, value: any): void;
|
19
|
-
protected getFieldObjectArray<T extends Entity>(entity: EntityClass<T>, field: string): Promise<T[]>;
|
20
|
-
toString(): string;
|
21
|
-
serialize(): RichStruct;
|
22
|
-
}
|
23
|
-
//# sourceMappingURL=entity.d.ts.map
|
@@ -1 +0,0 @@
|
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,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAMpG,QAAQ,IAAI,MAAM;IAMlB,SAAS,IAAI,UAAU;CAGxB"}
|
package/lib/store/entity.js
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
import { PluginManager } from '@sentio/runtime';
|
2
|
-
import { Store } from './store.js';
|
3
|
-
import { array_, IDConverter, required_ } from './convert.js';
|
4
|
-
export class Entity {
|
5
|
-
_data;
|
6
|
-
get id() {
|
7
|
-
return this.get('id', required_(IDConverter));
|
8
|
-
}
|
9
|
-
fromPojo(data, converters) {
|
10
|
-
for (const [field, value] of Object.entries(data)) {
|
11
|
-
if (converters[field] !== undefined) {
|
12
|
-
this.set(field, value, converters[field]);
|
13
|
-
}
|
14
|
-
}
|
15
|
-
}
|
16
|
-
constructor(_data = { fields: {} }) {
|
17
|
-
this._data = _data;
|
18
|
-
}
|
19
|
-
setData(data) {
|
20
|
-
this._data = data;
|
21
|
-
}
|
22
|
-
getIdFromEntity(entity) {
|
23
|
-
if (typeof entity === 'object' && entity.id) {
|
24
|
-
return entity.id;
|
25
|
-
}
|
26
|
-
return entity;
|
27
|
-
}
|
28
|
-
getStore() {
|
29
|
-
const dbContext = PluginManager.INSTANCE.dbContextLocalStorage.getStore();
|
30
|
-
if (dbContext) {
|
31
|
-
return new Store(dbContext);
|
32
|
-
}
|
33
|
-
return undefined;
|
34
|
-
}
|
35
|
-
get(field, converter) {
|
36
|
-
const value = this._data.fields[field];
|
37
|
-
return converter.to(value);
|
38
|
-
}
|
39
|
-
set(field, value, serializer) {
|
40
|
-
if (Array.isArray(value)) {
|
41
|
-
this._data.fields[field] = {
|
42
|
-
listValue: { values: value.map((v) => serializer.from(v)) }
|
43
|
-
};
|
44
|
-
}
|
45
|
-
else {
|
46
|
-
this._data.fields[field] = serializer.from(value);
|
47
|
-
}
|
48
|
-
}
|
49
|
-
getObject(entity, field) {
|
50
|
-
const id = this.get(field, IDConverter);
|
51
|
-
return id ? this.getStore()?.get(entity, id) : Promise.resolve(undefined);
|
52
|
-
}
|
53
|
-
setObject(field, value) {
|
54
|
-
this.set(field, this.getIdFromEntity(value), IDConverter);
|
55
|
-
}
|
56
|
-
getFieldObjectArray(entity, field) {
|
57
|
-
const ids = this.get(field, array_(IDConverter));
|
58
|
-
const promises = ids.map((id) => this.getStore()?.get(entity, id));
|
59
|
-
return Promise.all(promises);
|
60
|
-
}
|
61
|
-
toString() {
|
62
|
-
const entityName = this.constructor.prototype.entityName;
|
63
|
-
const id = this.id;
|
64
|
-
return `${entityName}#${id} ${JSON.stringify(this._data)}`;
|
65
|
-
}
|
66
|
-
serialize() {
|
67
|
-
return this._data;
|
68
|
-
}
|
69
|
-
}
|
70
|
-
//# sourceMappingURL=entity.js.map
|
package/lib/store/entity.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"entity.js","sourceRoot":"","sources":["../../src/store/entity.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAkB,MAAM,cAAc,CAAA;AAM7E,MAAM,OAAO,MAAM;IAaG;IAZpB,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC,CAAA;IAC/C,CAAC;IAES,QAAQ,CAAC,IAAS,EAAE,UAA+C;QAC3E,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAClD,IAAI,UAAU,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;gBACpC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAA;YAC3C,CAAC;QACH,CAAC;IACH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|
-
import { Store } from './store.js'
|
4
|
-
import { RichStruct } from '@sentio/protos'
|
5
|
-
import { array_, IDConverter, required_, ValueConverter } from './convert.js'
|
6
|
-
|
7
|
-
export interface EntityClass<T extends Entity> {
|
8
|
-
new (data: any): T
|
9
|
-
}
|
10
|
-
|
11
|
-
export class Entity {
|
12
|
-
get id(): ID {
|
13
|
-
return this.get('id', required_(IDConverter))
|
14
|
-
}
|
15
|
-
|
16
|
-
protected fromPojo(data: any, converters: Record<string, ValueConverter<any>>) {
|
17
|
-
for (const [field, value] of Object.entries(data)) {
|
18
|
-
if (converters[field] !== undefined) {
|
19
|
-
this.set(field, value, converters[field])
|
20
|
-
}
|
21
|
-
}
|
22
|
-
}
|
23
|
-
|
24
|
-
constructor(private _data: RichStruct = { fields: {} }) {}
|
25
|
-
|
26
|
-
setData(data: RichStruct) {
|
27
|
-
this._data = data
|
28
|
-
}
|
29
|
-
|
30
|
-
private getIdFromEntity(entity: any): any {
|
31
|
-
if (typeof entity === 'object' && entity.id) {
|
32
|
-
return entity.id
|
33
|
-
}
|
34
|
-
return entity
|
35
|
-
}
|
36
|
-
|
37
|
-
private getStore() {
|
38
|
-
const dbContext = PluginManager.INSTANCE.dbContextLocalStorage.getStore()
|
39
|
-
if (dbContext) {
|
40
|
-
return new Store(dbContext)
|
41
|
-
}
|
42
|
-
return undefined
|
43
|
-
}
|
44
|
-
|
45
|
-
get<T>(field: string, converter: ValueConverter<T>): T {
|
46
|
-
const value = this._data.fields[field]
|
47
|
-
return converter.to(value)
|
48
|
-
}
|
49
|
-
|
50
|
-
set<T>(field: string, value: T, serializer: ValueConverter<T>): void {
|
51
|
-
if (Array.isArray(value)) {
|
52
|
-
this._data.fields[field] = {
|
53
|
-
listValue: { values: value.map((v) => serializer.from(v)) }
|
54
|
-
}
|
55
|
-
} else {
|
56
|
-
this._data.fields[field] = serializer.from(value)
|
57
|
-
}
|
58
|
-
}
|
59
|
-
|
60
|
-
protected getObject<T extends Entity>(entity: EntityClass<T> | string, field: string): Promise<T | undefined> {
|
61
|
-
const id = this.get(field, IDConverter)
|
62
|
-
return id ? (this.getStore()?.get(entity, id) as Promise<T>) : Promise.resolve(undefined)
|
63
|
-
}
|
64
|
-
|
65
|
-
protected setObject(field: string, value: any): void {
|
66
|
-
this.set(field, this.getIdFromEntity(value), IDConverter)
|
67
|
-
}
|
68
|
-
|
69
|
-
protected getFieldObjectArray<T extends Entity>(entity: EntityClass<T>, field: string): Promise<T[]> {
|
70
|
-
const ids = this.get(field, array_(IDConverter))
|
71
|
-
const promises = ids.map((id: string) => this.getStore()?.get(entity, id))
|
72
|
-
return Promise.all(promises) as Promise<T[]>
|
73
|
-
}
|
74
|
-
|
75
|
-
toString(): string {
|
76
|
-
const entityName = this.constructor.prototype.entityName
|
77
|
-
const id = this.id
|
78
|
-
return `${entityName}#${id} ${JSON.stringify(this._data)}`
|
79
|
-
}
|
80
|
-
|
81
|
-
serialize(): RichStruct {
|
82
|
-
return this._data
|
83
|
-
}
|
84
|
-
}
|