@sentio/sdk 2.39.4-rc.1 → 2.39.4-rc.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.
Files changed (56) hide show
  1. package/lib/eth/builtin/internal/eacaggregatorproxy-processor.d.ts.map +1 -1
  2. package/lib/eth/builtin/internal/erc1155-processor.d.ts.map +1 -1
  3. package/lib/eth/builtin/internal/erc20-processor.d.ts.map +1 -1
  4. package/lib/eth/builtin/internal/erc20bytes-processor.d.ts.map +1 -1
  5. package/lib/eth/builtin/internal/erc721-processor.d.ts.map +1 -1
  6. package/lib/eth/builtin/internal/weth9-processor.d.ts.map +1 -1
  7. package/lib/eth/provider.d.ts.map +1 -1
  8. package/lib/eth/provider.js +3 -3
  9. package/lib/eth/provider.js.map +1 -1
  10. package/lib/fuel/context.d.ts.map +1 -1
  11. package/lib/solana/solana-processor.d.ts +0 -1
  12. package/lib/solana/solana-processor.d.ts.map +1 -1
  13. package/lib/store/codegen.d.ts +0 -2
  14. package/lib/store/codegen.d.ts.map +1 -1
  15. package/lib/store/codegen.js +137 -170
  16. package/lib/store/codegen.js.map +1 -1
  17. package/lib/store/convert.d.ts +10 -10
  18. package/lib/store/convert.d.ts.map +1 -1
  19. package/lib/store/convert.js +75 -31
  20. package/lib/store/convert.js.map +1 -1
  21. package/lib/store/convert.test.js.map +1 -1
  22. package/lib/store/decorators.d.ts +24 -7
  23. package/lib/store/decorators.d.ts.map +1 -1
  24. package/lib/store/decorators.js +160 -16
  25. package/lib/store/decorators.js.map +1 -1
  26. package/lib/store/index.d.ts +0 -1
  27. package/lib/store/index.d.ts.map +1 -1
  28. package/lib/store/index.js +0 -1
  29. package/lib/store/index.js.map +1 -1
  30. package/lib/store/schema.d.ts.map +1 -1
  31. package/lib/store/schema.js +39 -1
  32. package/lib/store/schema.js.map +1 -1
  33. package/lib/store/store.d.ts +11 -5
  34. package/lib/store/store.d.ts.map +1 -1
  35. package/lib/store/store.js +41 -19
  36. package/lib/store/store.js.map +1 -1
  37. package/lib/store/types.d.ts +22 -3
  38. package/lib/store/types.d.ts.map +1 -1
  39. package/lib/store/types.js +14 -1
  40. package/lib/store/types.js.map +1 -1
  41. package/lib/utils/price.d.ts +30 -30
  42. package/lib/utils/price.d.ts.map +1 -1
  43. package/package.json +4 -3
  44. package/src/eth/provider.ts +8 -3
  45. package/src/store/codegen.ts +171 -196
  46. package/src/store/convert.ts +84 -34
  47. package/src/store/decorators.ts +185 -16
  48. package/src/store/index.ts +0 -1
  49. package/src/store/schema.ts +41 -1
  50. package/src/store/store.ts +56 -25
  51. package/src/store/types.ts +25 -3
  52. package/lib/store/entity.d.ts +0 -23
  53. package/lib/store/entity.d.ts.map +0 -1
  54. package/lib/store/entity.js +0 -70
  55. package/lib/store/entity.js.map +0 -1
  56. package/src/store/entity.ts +0 -84
@@ -1,32 +1,201 @@
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 { RichStruct } from '@sentio/protos'
19
+ import { getStore } from './store.js'
20
+
1
21
  type Constructor = { new (...args: any[]): any }
2
22
 
3
- export function entity(name: string) {
4
- return function <T extends Constructor>(constructor: T, context: any) {
5
- constructor.prototype.entityName = name
6
- return constructor
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
+ Object.defineProperty(BaseClass, 'entityName', {
34
+ value: name
35
+ })
36
+
37
+ const meta: Record<string, ValueConverter<any>> = (BaseClass as any).meta || {}
38
+ const target = BaseClass.prototype
39
+ for (const [propertyKey, type] of Object.entries(meta)) {
40
+ if (type.isRelation && type.relationName) {
41
+ const relationName = type.relationName
42
+ const idGetter = function (this: any) {
43
+ return handleError(this.constructor.entityName, propertyKey, () => type!.to(this._data.fields[propertyKey]))
44
+ }
45
+ const idSetter = function (this: any, value: any) {
46
+ this._data.fields[propertyKey] = handleError(this.constructor.entityName, propertyKey, () =>
47
+ type!.from(value)
48
+ )
49
+ }
50
+ const idKey = type.isArray ? propertyKey + 'IDs' : propertyKey + 'ID'
51
+
52
+ Reflect.defineProperty(target, idKey, {
53
+ get: idGetter,
54
+ set: idSetter
55
+ })
56
+ Reflect.defineProperty(target, propertyKey, {
57
+ get: function () {
58
+ const ids = idGetter.call(this)
59
+ if (Array.isArray(ids)) {
60
+ return Promise.all(
61
+ ids.map((id) => {
62
+ return getStore()?.get(relationName, id)
63
+ })
64
+ )
65
+ } else {
66
+ return getStore()?.get(relationName, ids)
67
+ }
68
+ },
69
+ set: function (value) {
70
+ if (value instanceof Promise) {
71
+ value.then((v) => {
72
+ idSetter.call(this, v)
73
+ })
74
+ } else {
75
+ idSetter.call(this, value)
76
+ }
77
+ }
78
+ })
79
+ } else {
80
+ Reflect.defineProperty(target, propertyKey, {
81
+ configurable: true,
82
+ get: function () {
83
+ return handleError(this.constructor.entityName, propertyKey, () => type!.to(this._data.fields[propertyKey]))
84
+ },
85
+ set: function (value) {
86
+ this._data.fields[propertyKey] = handleError(this.constructor.entityName, propertyKey, () =>
87
+ type!.from(value)
88
+ )
89
+ }
90
+ })
91
+ }
92
+ }
93
+
94
+ return class extends BaseClass {
95
+ readonly _data: RichStruct = { fields: {} }
96
+ constructor(...args: any[]) {
97
+ super()
98
+ for (const key of Object.getOwnPropertyNames(this)) {
99
+ if (BaseClass.prototype.hasOwnProperty(key)) {
100
+ delete this[key]
101
+ }
102
+ }
103
+ if (args[0]) {
104
+ Object.assign(this, args[0])
105
+ }
106
+ }
107
+ }
7
108
  }
8
109
  }
9
110
 
10
- export function derivedFrom(field: string) {
11
- return function (target: any, context: any) {}
111
+ export function Column(type: string) {
112
+ return function (target: any, propertyKey: string) {
113
+ let required = false
114
+ if (type.endsWith('!')) {
115
+ required = true
116
+ type = type.slice(0, -1)
117
+ }
118
+ let typeConverter = TypeConverters[type]
119
+ if (!typeConverter) {
120
+ throw new Error(`Unsupported type ${type}`)
121
+ }
122
+ if (required) {
123
+ typeConverter = required_(typeConverter)
124
+ }
125
+ column(typeConverter)(target, propertyKey)
126
+ }
12
127
  }
13
128
 
14
- export function unique(field: string) {
15
- return function (target: any, context: any) {}
129
+ export function ListColumn(type: string = '') {
130
+ return function (target: any, propertyKey: string) {
131
+ let required = false
132
+ if (type.endsWith('!')) {
133
+ required = true
134
+ type = type.slice(0, -1)
135
+ }
136
+
137
+ let typeConverter = TypeConverters[type]
138
+ if (!typeConverter) {
139
+ // support list of list
140
+ const meta = target.constructor.meta || {}
141
+ const type = meta[propertyKey]
142
+ if (type) {
143
+ typeConverter = type
144
+ } else {
145
+ throw new Error(`Can't find inner type convert`)
146
+ }
147
+ }
148
+ if (required) {
149
+ typeConverter = required_(typeConverter)
150
+ }
151
+ column(array_(typeConverter))(target, propertyKey)
152
+ }
16
153
  }
17
154
 
18
- export function index(field: string[]) {
19
- return function (target: any, context: any) {}
155
+ export function column(type?: ValueConverter<any>) {
156
+ return function (target: any, propertyKey: string) {
157
+ const reflectType = Reflect.getMetadata('design:type', target, propertyKey)
158
+ if (!type) {
159
+ const typeName = reflectType.name
160
+ type = TypeConverters[typeName]
161
+ if (!type) {
162
+ throw new Error(`Unsupported type ${typeName}`)
163
+ }
164
+ }
165
+
166
+ const meta = target.constructor.meta || {}
167
+ meta[propertyKey] = type
168
+ target.constructor.meta = meta
169
+ }
20
170
  }
21
171
 
22
- export function fulltext(query: string) {
23
- return function (target: any, context: any) {}
172
+ export const IDColumn = column(IDConverter)
173
+ export const IntColumn = column(IntConverter)
174
+ export const FloatColumn = column(FloatConverter)
175
+ export const BigDecimalColumn = column(BigDecimalConverter)
176
+ export const BigIntColumn = column(BigIntConverter)
177
+ export const StringColumn = column(StringConverter)
178
+ export const BooleanColumn = column(BooleanConverter)
179
+ export const TimestampColumn = column(TimestampConverter)
180
+ export const BytesColumn = column(BytesConverter)
181
+
182
+ export function Required(target: any, propertyKey: string) {
183
+ const meta = target.constructor.meta || {}
184
+ const type = meta[propertyKey]
185
+ if (type) {
186
+ meta[propertyKey] = required_(type)
187
+ }
188
+ target.constructor.meta = meta
24
189
  }
25
190
 
26
- export function cardinality(value: number) {
27
- return function (target: any, context: any) {}
191
+ export function One(entity: string) {
192
+ return function (target: any, propertyKey: string) {
193
+ column(objectId_(entity))(target, propertyKey)
194
+ }
28
195
  }
29
196
 
30
- export function byteWeight(value: number) {
31
- return function (target: any, context: any) {}
197
+ export function Many(entity: string) {
198
+ return function (target: any, propertyKey: string) {
199
+ column(array_(objectId_(entity)))(target, propertyKey)
200
+ }
32
201
  }
@@ -1,6 +1,5 @@
1
1
  export * from './decorators.js'
2
2
  export * from './types.js'
3
- export * from './entity.js'
4
3
  export * from './store.js'
5
4
  export * from './context.js'
6
5
  export * from './convert.js'
@@ -1,7 +1,8 @@
1
1
  import { buildASTSchema, DocumentNode, extendSchema, GraphQLSchema, parse, validateSchema } from 'graphql/index.js'
2
2
  import * as fs from 'node:fs'
3
+ import { GraphQLObjectType, GraphQLOutputType, isListType, isNonNullType } from 'graphql'
3
4
 
4
- const customScalars = ['BigInt', 'BigDecimal', 'DateTime', 'JSON', 'Bytes', 'ID']
5
+ const customScalars = ['BigInt', 'BigDecimal', 'Timestamp', 'JSON', 'Bytes', 'ID']
5
6
 
6
7
  const baseSchema = buildASTSchema(
7
8
  parse(`
@@ -19,12 +20,51 @@ const baseSchema = buildASTSchema(
19
20
  `)
20
21
  )
21
22
 
23
+ function getElemType(type: GraphQLOutputType) {
24
+ if (isNonNullType(type)) {
25
+ return getElemType(type.ofType)
26
+ }
27
+ if (isListType(type)) {
28
+ return getElemType(type.ofType)
29
+ }
30
+ return type
31
+ }
32
+
33
+ function checkRelations(schema: GraphQLSchema) {
34
+ for (const t of Object.values(schema.getTypeMap())) {
35
+ if (t.name.startsWith('__')) {
36
+ continue
37
+ }
38
+ if (t instanceof GraphQLObjectType) {
39
+ for (const f of Object.values(t.getFields())) {
40
+ if (f.astNode) {
41
+ for (const d of f.astNode.directives ?? []) {
42
+ if (d.name.value === 'derivedFrom') {
43
+ const arg = (d.arguments ?? []).find((a) => a.name.value === 'field')
44
+ if (!arg || !arg.value || arg.value.kind !== 'StringValue') {
45
+ throw new Error(`@derivedFrom directive must have a 'field' argument`)
46
+ }
47
+ const fieldName = arg.value.value
48
+ const targetType = getElemType(f.type) as GraphQLObjectType
49
+ // Check if the target type has a field with the same name
50
+ if (!targetType.getFields()[fieldName]) {
51
+ throw new Error(`Field '${fieldName}' not found in type '${targetType.name}'`)
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+
22
61
  export function buildSchema(doc: DocumentNode): GraphQLSchema {
23
62
  const schema = extendSchema(baseSchema, doc)
24
63
  const errors = validateSchema(schema).filter((err) => !/query root/i.test(err.message))
25
64
  if (errors.length > 0) {
26
65
  throw errors[0]
27
66
  }
67
+ checkRelations(schema)
28
68
  return schema
29
69
  }
30
70
 
@@ -1,48 +1,68 @@
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, DateTime, Float, ID, Int } from './types.js'
4
+ import { Bytes, Float, ID, Int, Timestamp } from './types.js'
5
+ import type { Entity as EntityStruct, RichValue } from '@sentio/protos'
6
6
  import { DBRequest_DBOperator, DBResponse } from '@sentio/protos'
7
- import type { RichStruct, RichValue } from '@sentio/protos'
8
7
  import { toBigInteger } from './convert.js'
8
+ import { PluginManager } from '@sentio/runtime'
9
9
 
10
- type Value = ID | string | Int | Float | boolean | DateTime | Bytes | BigDecimal | bigint
10
+ type Value = ID | string | Int | Float | boolean | Timestamp | Bytes | BigDecimal | bigint
11
+
12
+ interface Entity {
13
+ id: ID
14
+ }
15
+
16
+ export interface EntityClass<T> {
17
+ new (data: Partial<T>): T
18
+ }
11
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: string): Promise<T | undefined> {
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
 
23
31
  const data = (await promise) as DBResponse
24
- if (data.entities?.entities[0]) {
25
- const entityData = data.entities.entities[0]
32
+ if (data.entityList?.entities[0]) {
33
+ const entityData = data.entityList?.entities[0]
26
34
  return this.newEntity(entity, entityData)
27
35
  }
28
36
 
29
37
  return undefined
30
38
  }
31
39
 
32
- async delete(entity: EntityClass<any>, id: string | string[]): Promise<void> {
33
- const toBeDeleted = []
34
- if (Array.isArray(id)) {
35
- for (const i of id) {
36
- toBeDeleted.push({ entity: entity.prototype.entityName, id: i })
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
- toBeDeleted.push({ entity: entity.prototype.entityName, id })
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.serialize())
75
+ id: entities.map((e) => e.id.toString()),
76
+ entityData: entities.map((e: any) => e._data)
57
77
  }
58
78
  })
59
79
 
@@ -77,7 +97,7 @@ export class Store {
77
97
  }
78
98
  })
79
99
  const response = (await promise) as DBResponse
80
- for (const data of response.entities?.entities || []) {
100
+ for (const data of response.entityList?.entities || []) {
81
101
  yield this.newEntity(entity, data)
82
102
  }
83
103
  if (!response.nextCursor) {
@@ -87,18 +107,21 @@ export class Store {
87
107
  }
88
108
  }
89
109
 
90
- private newEntity<T extends Entity>(entity: EntityClass<T> | string, data: RichStruct) {
110
+ private newEntity<T extends Entity>(entity: EntityClass<T> | string, data: EntityStruct) {
91
111
  if (typeof entity == 'string') {
92
- const en = DatabaseSchema.findEntity(entity)
112
+ let en = DatabaseSchema.findEntity(entity)
93
113
  if (!en) {
94
114
  // it is an interface
95
- return new Entity() as T
115
+ en = DatabaseSchema.findEntity(data.entity)
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.setData(data)
124
+ ;(res as any)._data = 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
+ }
@@ -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
- export type DateTime = Date
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
+ })
@@ -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(): R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MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAMpG,QAAQ,IAAI,MAAM;IAMlB,SAAS,IAAI,UAAU;CAGxB"}
@@ -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
@@ -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,CAAC;IAED,YAAoB,QAAoB,EAAE,MAAM,EAAE,EAAE,EAAE;QAAlC,UAAK,GAAL,KAAK,CAA6B;IAAG,CAAC;IAE1D,OAAO,CAAC,IAAgB;QACtB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACnB,CAAC;IAEO,eAAe,CAAC,MAAW;QACjC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;YAC5C,OAAO,MAAM,CAAC,EAAE,CAAA;QAClB,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;IAEO,QAAQ;QACd,MAAM,SAAS,GAAG,aAAa,CAAC,QAAQ,CAAC,qBA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,CAAA;IAC3D,CAAC;IAES,mBAAmB,CAAmB,MAAsB,EAAE,KAAa;QACnF,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAA;QAChD,MAAM,QAAQ,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAA;QAC1E,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAiB,CAAA;IAC9C,CAAC;IAED,QAAQ;QACN,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,UAAU,CAAA;QACxD,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,CAAA;QAClB,OAAO,GAAG,UAAU,IAAI,EAAE,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAA;IAC5D,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;CACF"}
@@ -1,84 +0,0 @@
1
- import { ID } from './types.js'
2
- import { PluginManager } from '@sentio/runtime'
3
- 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
- }