@sentio/sdk 2.36.2-rc.1 → 2.37.0-rc.3
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/core/base-context.d.ts +4 -0
- package/lib/core/base-context.d.ts.map +1 -1
- package/lib/core/base-context.js +20 -4
- package/lib/core/base-context.js.map +1 -1
- package/lib/core/core-plugin.d.ts.map +1 -1
- package/lib/core/core-plugin.js +6 -0
- package/lib/core/core-plugin.js.map +1 -1
- package/lib/core/database-schema.d.ts +8 -0
- package/lib/core/database-schema.d.ts.map +1 -0
- package/lib/core/database-schema.js +10 -0
- package/lib/core/database-schema.js.map +1 -0
- package/lib/core/index.d.ts +1 -0
- package/lib/core/index.d.ts.map +1 -1
- package/lib/core/index.js +1 -0
- package/lib/core/index.js.map +1 -1
- package/lib/store/codegen.d.ts +4 -0
- package/lib/store/codegen.d.ts.map +1 -0
- package/lib/store/codegen.js +193 -0
- package/lib/store/codegen.js.map +1 -0
- package/lib/store/context.d.ts +2 -0
- package/lib/store/context.d.ts.map +1 -0
- package/lib/store/context.js +2 -0
- package/lib/store/context.js.map +1 -0
- package/lib/store/decorators.d.ts +7 -0
- package/lib/store/decorators.d.ts.map +1 -0
- package/lib/store/decorators.js +7 -0
- package/lib/store/decorators.js.map +1 -0
- package/lib/store/entity.d.ts +18 -0
- package/lib/store/entity.d.ts.map +1 -0
- package/lib/store/entity.js +51 -0
- package/lib/store/entity.js.map +1 -0
- package/lib/store/index.d.ts +6 -0
- package/lib/store/index.d.ts.map +1 -0
- package/lib/store/index.js +6 -0
- package/lib/store/index.js.map +1 -0
- package/lib/store/run.d.ts +2 -0
- package/lib/store/run.d.ts.map +1 -0
- package/lib/store/run.js +11 -0
- package/lib/store/run.js.map +1 -0
- package/lib/store/schema.d.ts +7 -0
- package/lib/store/schema.d.ts.map +1 -0
- package/lib/store/schema.js +30 -0
- package/lib/store/schema.js.map +1 -0
- package/lib/store/store.d.ts +12 -0
- package/lib/store/store.d.ts.map +1 -0
- package/lib/store/store.js +61 -0
- package/lib/store/store.js.map +1 -0
- package/lib/store/types.d.ts +10 -0
- package/lib/store/types.d.ts.map +1 -0
- package/lib/store/types.js +2 -0
- package/lib/store/types.js.map +1 -0
- package/lib/testing/test-processor-server.d.ts +2 -5
- package/lib/testing/test-processor-server.d.ts.map +1 -1
- package/lib/testing/test-processor-server.js +2 -2
- package/lib/testing/test-processor-server.js.map +1 -1
- package/package.json +8 -5
- package/src/core/base-context.ts +22 -4
- package/src/core/core-plugin.ts +7 -0
- package/src/core/database-schema.ts +11 -0
- package/src/core/index.ts +1 -0
- package/src/store/codegen.ts +226 -0
- package/src/store/context.ts +1 -0
- package/src/store/decorators.ts +9 -0
- package/src/store/entity.ts +61 -0
- package/src/store/index.ts +5 -0
- package/src/store/run.ts +10 -0
- package/src/store/schema.ts +35 -0
- package/src/store/store.ts +68 -0
- package/src/store/types.ts +10 -0
- package/src/testing/test-processor-server.ts +13 -2
@@ -0,0 +1,12 @@
|
|
1
|
+
import { Entity, EntityClass } from './entity.js';
|
2
|
+
import { StoreContext } from './context.js';
|
3
|
+
export declare class Store {
|
4
|
+
private readonly context;
|
5
|
+
constructor(context: StoreContext);
|
6
|
+
get<T extends Entity>(entity: EntityClass<T> | string, id: string): Promise<T | undefined>;
|
7
|
+
delete(entity: EntityClass<any>, id: string | string[]): Promise<void>;
|
8
|
+
upsert<T extends Entity>(entity: T | T[]): Promise<T>;
|
9
|
+
list<T extends Entity>(entity: EntityClass<T>, limit?: number, offset?: number): Promise<T[]>;
|
10
|
+
private newEntity;
|
11
|
+
}
|
12
|
+
//# sourceMappingURL=store.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,qBAAa,KAAK;IACJ,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY;IAE5C,GAAG,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAe1F,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAStE,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC;IAiBrD,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAenG,OAAO,CAAC,SAAS;CAKlB"}
|
@@ -0,0 +1,61 @@
|
|
1
|
+
export class Store {
|
2
|
+
context;
|
3
|
+
constructor(context) {
|
4
|
+
this.context = context;
|
5
|
+
}
|
6
|
+
async get(entity, id) {
|
7
|
+
const promise = this.context.sendRequest({
|
8
|
+
get: {
|
9
|
+
entity: typeof entity == 'string' ? entity : entity.name,
|
10
|
+
id
|
11
|
+
}
|
12
|
+
});
|
13
|
+
const data = await promise;
|
14
|
+
if (data?.["id"] != null) {
|
15
|
+
return this.newEntity(entity, data);
|
16
|
+
}
|
17
|
+
return undefined;
|
18
|
+
}
|
19
|
+
async delete(entity, id) {
|
20
|
+
await this.context.sendRequest({
|
21
|
+
delete: {
|
22
|
+
entity: entity.name,
|
23
|
+
id: Array.isArray(id) ? id : [id]
|
24
|
+
}
|
25
|
+
});
|
26
|
+
}
|
27
|
+
async upsert(entity) {
|
28
|
+
const promise = this.context.sendRequest({
|
29
|
+
upsert: {
|
30
|
+
entity: entity.constructor.name,
|
31
|
+
data: Array.isArray(entity) ? entity.map(e => e.data) : [entity.data]
|
32
|
+
}
|
33
|
+
});
|
34
|
+
if (Array.isArray(entity)) {
|
35
|
+
entity.forEach((e) => (e.store = this));
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
entity.store = this;
|
39
|
+
}
|
40
|
+
return promise;
|
41
|
+
}
|
42
|
+
async list(entity, limit, offset) {
|
43
|
+
const promise = this.context.sendRequest({
|
44
|
+
list: {
|
45
|
+
entity: entity.name,
|
46
|
+
limit,
|
47
|
+
offset
|
48
|
+
}
|
49
|
+
});
|
50
|
+
const list = await promise;
|
51
|
+
return list.map((data) => {
|
52
|
+
return this.newEntity(entity, data);
|
53
|
+
});
|
54
|
+
}
|
55
|
+
newEntity(entity, data) {
|
56
|
+
const e = new entity(data);
|
57
|
+
e.store = this;
|
58
|
+
return e;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
//# sourceMappingURL=store.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,KAAK;IACa;IAA7B,YAA6B,OAAqB;QAArB,YAAO,GAAP,OAAO,CAAc;IAAG,CAAC;IAEtD,KAAK,CAAC,GAAG,CAAmB,MAA+B,EAAE,EAAU;QACrE,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC;YACvC,GAAG,EAAE;gBACH,MAAM,EAAE,OAAO,MAAM,IAAI,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI;gBACxD,EAAE;aACH;SACF,CAAC,CAAA;QAEF,MAAM,IAAI,GAAI,MAAM,OAAc,CAAA;QAClC,IAAI,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC,SAAS,CAAC,MAAwB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KAAK;gBACL,MAAM;aACP;SACF,CAAC,CAAA;QAEF,MAAM,IAAI,GAAG,MAAM,OAAgB,CAAA;QACnC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;YACvB,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QACrC,CAAC,CAAC,CAAA;IACJ,CAAC;IAEO,SAAS,CAAmB,MAAsB,EAAE,IAAS;QACnE,MAAM,CAAC,GAAG,IAAK,MAAyB,CAAC,IAAI,CAAC,CAAA;QAC9C,CAAC,CAAC,KAAK,GAAG,IAAI,CAAA;QACd,OAAO,CAAC,CAAA;IACV,CAAC;CACF"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export type { BigDecimal } from '@sentio/bigdecimal';
|
2
|
+
export type ID = string;
|
3
|
+
export type String = string;
|
4
|
+
export type Int = number;
|
5
|
+
export type Float = number;
|
6
|
+
export type Boolean = boolean;
|
7
|
+
export type DateTime = Date;
|
8
|
+
export type Json = any;
|
9
|
+
export type Bytes = Uint8Array;
|
10
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/store/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAEpD,MAAM,MAAM,EAAE,GAAG,MAAM,CAAA;AACvB,MAAM,MAAM,MAAM,GAAG,MAAM,CAAA;AAC3B,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AACxB,MAAM,MAAM,KAAK,GAAG,MAAM,CAAA;AAC1B,MAAM,MAAM,OAAO,GAAG,OAAO,CAAA;AAC7B,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAA;AAC3B,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,MAAM,MAAM,KAAK,GAAG,UAAU,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/store/types.ts"],"names":[],"mappings":""}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { AccountConfig, ContractConfig, DataBinding, Empty, ProcessBindingResponse, ProcessBindingsRequest, ProcessConfigRequest, ProcessConfigResponse, ProcessorServiceImplementation, StartRequest } from '@sentio/protos';
|
1
|
+
import { AccountConfig, ContractConfig, DataBinding, DeepPartial, Empty, ProcessBindingResponse, ProcessBindingsRequest, ProcessConfigRequest, ProcessConfigResponse, ProcessorServiceImplementation, ProcessStreamRequest, ProcessStreamResponse, ServerStreamingMethodResult, StartRequest } from '@sentio/protos';
|
2
2
|
import { CallContext } from 'nice-grpc-common';
|
3
3
|
import { ProcessorServiceImpl } from '@sentio/runtime';
|
4
4
|
import { AptosFacet } from './aptos-facet.js';
|
@@ -23,9 +23,6 @@ export declare class TestProcessorServer implements ProcessorServiceImplementati
|
|
23
23
|
getConfig(request: ProcessConfigRequest, context?: CallContext): Promise<ProcessConfigResponse>;
|
24
24
|
processBindings(request: ProcessBindingsRequest, context?: CallContext): Promise<ProcessBindingResponse>;
|
25
25
|
processBinding(request: DataBinding, context?: CallContext): Promise<ProcessBindingResponse>;
|
26
|
-
processBindingsStream(
|
27
|
-
result: import("@sentio/protos").ProcessResult;
|
28
|
-
configUpdated: boolean;
|
29
|
-
}, void, unknown>;
|
26
|
+
processBindingsStream(requests: AsyncIterable<ProcessStreamRequest>, context: CallContext): ServerStreamingMethodResult<DeepPartial<ProcessStreamResponse>>;
|
30
27
|
}
|
31
28
|
//# sourceMappingURL=test-processor-server.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"test-processor-server.d.ts","sourceRoot":"","sources":["../../src/testing/test-processor-server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,cAAc,EACd,WAAW,EACX,KAAK,EACL,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,qBAAqB,EACrB,8BAA8B,EAC9B,YAAY,EACb,MA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E,KAAK,CAAC,OAAO,GAAE,YAAwC,EAAE,OAAO,cAAe,GAAG,OAAO,CAAC,KAAK,CAAC;IAQtG,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,cAAe,GAAG,OAAO,CAAC,KAAK,CAAC;IAI5D,SAAS,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,cAAe,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAIhG,eAAe,CACb,OAAO,EAAE,sBAAsB,EAC/B,OAAO,GAAE,WAA0B,GAClC,OAAO,CAAC,sBAAsB,CAAC;IAIlC,cAAc,CAAC,OAAO,EAAE,WAAW,EAAE,OAAO,GAAE,WAA0B,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAI1G,qBAAqB,CACnB,QAAQ,EAAE,aAAa,CAAC,oBAAoB,CAAC,EAC7C,OAAO,EAAE,WAAW,GACnB,2BAA2B,CAAC,WAAW,CAAC,qBAAqB,CAAC,CAAC;CAOnE"}
|
@@ -50,8 +50,8 @@ export class TestProcessorServer {
|
|
50
50
|
processBinding(request, context = TEST_CONTEXT) {
|
51
51
|
return this.service.processBindings({ bindings: [request] }, context);
|
52
52
|
}
|
53
|
-
processBindingsStream(
|
54
|
-
|
53
|
+
processBindingsStream(requests, context) {
|
54
|
+
throw new Error('Method not implemented.');
|
55
55
|
}
|
56
56
|
}
|
57
57
|
//# sourceMappingURL=test-processor-server.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"test-processor-server.js","sourceRoot":"","sources":["../../src/testing/test-processor-server.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"test-processor-server.js","sourceRoot":"","sources":["../../src/testing/test-processor-server.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAEzC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA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"type": "module",
|
6
6
|
"exports": {
|
@@ -35,7 +35,9 @@
|
|
35
35
|
"./sui/builtin/0x2": "./lib/sui/builtin/0x2.js",
|
36
36
|
"./sui/builtin/0x3": "./lib/sui/builtin/0x3.js",
|
37
37
|
"./fuel": "./lib/fuel/index.js",
|
38
|
-
"./fuel/codegen": "./lib/fuel/codegen/index.js"
|
38
|
+
"./fuel/codegen": "./lib/fuel/codegen/index.js",
|
39
|
+
"./store": "./lib/store/index.js",
|
40
|
+
"./store/codegen": "./lib/store/codegen.js"
|
39
41
|
},
|
40
42
|
"files": [
|
41
43
|
"{lib,src}",
|
@@ -77,8 +79,8 @@
|
|
77
79
|
"typedoc": "^0.25.7",
|
78
80
|
"utility-types": "^3.11.0",
|
79
81
|
"yaml": "^2.3.4",
|
80
|
-
"@sentio/
|
81
|
-
"@sentio/
|
82
|
+
"@sentio/protos": "2.37.0-rc.3",
|
83
|
+
"@sentio/runtime": "^2.37.0-rc.3"
|
82
84
|
},
|
83
85
|
"peerDependencies": {
|
84
86
|
"tsup": "npm:@sentio/tsup@^6.7.2"
|
@@ -97,7 +99,7 @@
|
|
97
99
|
"build": "pnpm gen && pnpm compile",
|
98
100
|
"build:all": "pnpm --filter=$(node -p \"require('./package.json').name\")... build",
|
99
101
|
"compile": "tsc && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
|
100
|
-
"gen": "pnpm gen:eth && pnpm gen:aptos && pnpm gen:sui && pnpm gen:solana && pnpm gen:fuel",
|
102
|
+
"gen": "pnpm gen:eth && pnpm gen:aptos && pnpm gen:sui && pnpm gen:solana && pnpm gen:fuel && pnpm gen:store",
|
101
103
|
"gen:aptos": "tsx src/aptos/codegen/run.ts src/aptos/abis src/aptos/builtin && pnpm gen:aptos_test",
|
102
104
|
"gen:aptos_test": "tsx src/aptos/codegen/run.ts src/aptos/tests/abis src/aptos/tests/types",
|
103
105
|
"gen:docs": "typedoc --options typedoc.json",
|
@@ -106,6 +108,7 @@
|
|
106
108
|
"gen:fuel": "tsx src/fuel/codegen/run.ts src/fuel/abis src/sui/builtin && pnpm gen:fuel_test",
|
107
109
|
"gen:fuel_test": "tsx src/fuel/codegen/run.ts src/fuel/tests/abis src/fuel/tests/types",
|
108
110
|
"gen:solana": "tsx src/solana/codegen/run.ts src/solana/tests/abis src/solana/tests/types",
|
111
|
+
"gen:store": "tsx src/store/run.ts src/store/tests src/store/tests/generated",
|
109
112
|
"gen:sui": "tsx src/sui/codegen/run.ts src/sui/abis src/sui/builtin && pnpm gen:sui_test",
|
110
113
|
"gen:sui_test": "tsx src/sui/codegen/run.ts src/sui/tests/abis src/sui/tests/types",
|
111
114
|
"test": "NODE_OPTIONS=--experimental-vm-modules pnpm jest --runInBand --detectOpenHandles"
|
package/src/core/base-context.ts
CHANGED
@@ -2,13 +2,15 @@ import { ProcessResult, RecordMetaData } from '@sentio/protos'
|
|
2
2
|
import { EventLoggerBinding } from './event-logger.js'
|
3
3
|
import { Meter, Labels } from './meter.js'
|
4
4
|
import { ChainId } from '@sentio/chain'
|
5
|
-
import { mergeProcessResults } from '@sentio/runtime'
|
5
|
+
import { mergeProcessResults, PluginManager } from '@sentio/runtime'
|
6
6
|
import { Required } from 'utility-types'
|
7
7
|
import { ServerError, Status } from 'nice-grpc'
|
8
|
+
import { Store } from '../store/store.js'
|
8
9
|
|
9
10
|
export abstract class BaseContext {
|
10
11
|
meter: Meter
|
11
12
|
eventLogger: EventLoggerBinding
|
13
|
+
private _store: Store
|
12
14
|
protected baseLabels: Labels
|
13
15
|
private active: boolean
|
14
16
|
|
@@ -18,8 +20,8 @@ export abstract class BaseContext {
|
|
18
20
|
exports: [],
|
19
21
|
gauges: [],
|
20
22
|
states: {
|
21
|
-
configUpdated: false
|
22
|
-
}
|
23
|
+
configUpdated: false
|
24
|
+
}
|
23
25
|
}
|
24
26
|
|
25
27
|
public update(res: Partial<ProcessResult>) {
|
@@ -35,6 +37,7 @@ export abstract class BaseContext {
|
|
35
37
|
this.eventLogger = new EventLoggerBinding(this)
|
36
38
|
this.baseLabels = baseLabels || {}
|
37
39
|
this.active = true
|
40
|
+
this.initStore()
|
38
41
|
}
|
39
42
|
|
40
43
|
stopAndGetResult(): ProcessResult {
|
@@ -49,11 +52,26 @@ export abstract class BaseContext {
|
|
49
52
|
getMetaData(name: string, labels: Labels): RecordMetaData {
|
50
53
|
return {
|
51
54
|
...this.baseLabels,
|
52
|
-
...this.getMetaDataInternal(name, labels)
|
55
|
+
...this.getMetaDataInternal(name, labels)
|
53
56
|
}
|
54
57
|
}
|
55
58
|
|
56
59
|
protected abstract getMetaDataInternal(name: string, labels: Labels): RecordMetaData
|
57
60
|
|
58
61
|
abstract getChainId(): ChainId
|
62
|
+
|
63
|
+
get store() {
|
64
|
+
if (this._store == null) {
|
65
|
+
console.warn('Store is not set, please initialize the processor with your database schema first.')
|
66
|
+
}
|
67
|
+
return this._store
|
68
|
+
}
|
69
|
+
|
70
|
+
// this method must be called within the dbContextLocalStorage scope
|
71
|
+
initStore() {
|
72
|
+
const dbContext = PluginManager.INSTANCE.dbContextLocalStorage.getStore()
|
73
|
+
if (dbContext) {
|
74
|
+
this._store = new Store(dbContext)
|
75
|
+
}
|
76
|
+
}
|
59
77
|
}
|
package/src/core/core-plugin.ts
CHANGED
@@ -6,6 +6,7 @@ import { ExporterState } from './exporter.js'
|
|
6
6
|
import { EventTrackerState } from './event-tracker.js'
|
7
7
|
import { TemplateInstanceState } from './template.js'
|
8
8
|
import { EventLoggerState } from './event-logger.js'
|
9
|
+
import { DatabaseSchemaState } from './database-schema.js'
|
9
10
|
|
10
11
|
export class CorePlugin extends Plugin {
|
11
12
|
name: string = 'CorePlugin'
|
@@ -44,6 +45,12 @@ export class CorePlugin extends Plugin {
|
|
44
45
|
channel: exporter.channel
|
45
46
|
})
|
46
47
|
}
|
48
|
+
|
49
|
+
if (DatabaseSchemaState.INSTANCE.getValues().length > 0) {
|
50
|
+
config.dbSchema = {
|
51
|
+
gqlSchema: DatabaseSchemaState.INSTANCE.getValues().join('\n\n')
|
52
|
+
}
|
53
|
+
}
|
47
54
|
}
|
48
55
|
}
|
49
56
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { ListStateStorage } from '@sentio/runtime'
|
2
|
+
|
3
|
+
export class DatabaseSchemaState extends ListStateStorage<string> {
|
4
|
+
static INSTANCE = new DatabaseSchemaState()
|
5
|
+
}
|
6
|
+
|
7
|
+
export class DatabaseSchema {
|
8
|
+
static register(schema: string) {
|
9
|
+
DatabaseSchemaState.INSTANCE.addValue(schema)
|
10
|
+
}
|
11
|
+
}
|
package/src/core/index.ts
CHANGED
@@ -0,0 +1,226 @@
|
|
1
|
+
import {
|
2
|
+
DirectiveNode,
|
3
|
+
GraphQLEnumType,
|
4
|
+
GraphQLField,
|
5
|
+
GraphQLInterfaceType,
|
6
|
+
GraphQLList,
|
7
|
+
GraphQLNonNull,
|
8
|
+
GraphQLObjectType,
|
9
|
+
GraphQLOutputType,
|
10
|
+
GraphQLScalarType,
|
11
|
+
GraphQLSchema,
|
12
|
+
Kind,
|
13
|
+
isObjectType,
|
14
|
+
isInterfaceType,
|
15
|
+
isListType
|
16
|
+
} from 'graphql'
|
17
|
+
import * as fs from 'node:fs'
|
18
|
+
import path from 'path'
|
19
|
+
import mkdirp from 'mkdirp'
|
20
|
+
import { schemaFromFile } from './schema.js'
|
21
|
+
import chalk from 'chalk'
|
22
|
+
|
23
|
+
export async function codegen(srcDir: string, outputDir: string) {
|
24
|
+
for (const file of fs.readdirSync(srcDir)) {
|
25
|
+
const f = path.join(srcDir, file)
|
26
|
+
const filePath = path.parse(f)
|
27
|
+
if (filePath.ext == '.graphql') {
|
28
|
+
const { schema, source } = schemaFromFile(f)
|
29
|
+
const target = path.join(outputDir, filePath.name + '.ts')
|
30
|
+
await codegenInternal(schema, source, target)
|
31
|
+
console.log(chalk.green(`Generated ${target}`))
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
async function codegenInternal(schema: GraphQLSchema, source: string, target: string) {
|
37
|
+
const results: string[] = [
|
38
|
+
'/* Autogenerated file. Do not edit manually. */\n',
|
39
|
+
'/* tslint:disable */',
|
40
|
+
'/* eslint-disable */',
|
41
|
+
"import {entity, derivedFrom, BigDecimal, DateTime, Json, Bytes, ID, Entity, Store} from '@sentio/sdk/store'",
|
42
|
+
`import { DatabaseSchema } from "@sentio/sdk"`
|
43
|
+
]
|
44
|
+
|
45
|
+
for (const t of Object.values(schema.getTypeMap())) {
|
46
|
+
if (t.name.startsWith('__')) {
|
47
|
+
continue
|
48
|
+
}
|
49
|
+
|
50
|
+
if (t instanceof GraphQLObjectType) {
|
51
|
+
const entityContent = genEntity(t)
|
52
|
+
results.push(entityContent)
|
53
|
+
}
|
54
|
+
if (t instanceof GraphQLEnumType) {
|
55
|
+
results.push(genEnum(t))
|
56
|
+
}
|
57
|
+
if (t instanceof GraphQLInterfaceType) {
|
58
|
+
results.push(genInterface(t))
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
const contents =
|
63
|
+
results.join('\n') +
|
64
|
+
`\n
|
65
|
+
const source = \`${source.replaceAll('`', '`')}\`
|
66
|
+
DatabaseSchema.register(source)
|
67
|
+
`
|
68
|
+
await mkdirp(path.dirname(target))
|
69
|
+
|
70
|
+
fs.writeFileSync(target, contents)
|
71
|
+
}
|
72
|
+
|
73
|
+
const JsTypes: Record<string, string> = {
|
74
|
+
ID: 'string',
|
75
|
+
String: 'string',
|
76
|
+
Int: 'number',
|
77
|
+
Float: 'number',
|
78
|
+
Boolean: 'boolean',
|
79
|
+
BigInt: 'bigint',
|
80
|
+
BigDecimal: 'BigDecimal',
|
81
|
+
DateTime: 'Date',
|
82
|
+
Json: 'any',
|
83
|
+
Bytes: 'Uint8Array'
|
84
|
+
}
|
85
|
+
const graphqlTypes = Object.entries(JsTypes).reduce(
|
86
|
+
(acc, [k, v]) => {
|
87
|
+
acc[v] = k
|
88
|
+
return acc
|
89
|
+
},
|
90
|
+
{} as Record<string, string>
|
91
|
+
)
|
92
|
+
|
93
|
+
function genType(type: GraphQLOutputType): string {
|
94
|
+
if (type instanceof GraphQLNonNull) {
|
95
|
+
return genType(type.ofType)
|
96
|
+
} else if (type instanceof GraphQLScalarType) {
|
97
|
+
return type.name
|
98
|
+
} else if (type instanceof GraphQLObjectType || type instanceof GraphQLInterfaceType) {
|
99
|
+
return type.name
|
100
|
+
} else if (type instanceof GraphQLList) {
|
101
|
+
return `Array<${genType(type.ofType)}>`
|
102
|
+
} else if (type instanceof GraphQLEnumType) {
|
103
|
+
return type.name
|
104
|
+
} else {
|
105
|
+
throw new Error('Unsupported type: ' + type)
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
function isObject(type: GraphQLOutputType) {
|
110
|
+
if (type instanceof GraphQLNonNull) {
|
111
|
+
return isObject(type.ofType)
|
112
|
+
} else if (type instanceof GraphQLList) {
|
113
|
+
return isObject(type.ofType)
|
114
|
+
}
|
115
|
+
return isObjectType(type) || isInterfaceType(type)
|
116
|
+
}
|
117
|
+
|
118
|
+
function isList(type: GraphQLOutputType) {
|
119
|
+
if (type instanceof GraphQLNonNull) {
|
120
|
+
return isListType(type.ofType)
|
121
|
+
} else {
|
122
|
+
return isListType(type)
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
function getElementType(type: GraphQLOutputType) {
|
127
|
+
if (type instanceof GraphQLNonNull) {
|
128
|
+
return getElementType(type.ofType)
|
129
|
+
} else if (type instanceof GraphQLList) {
|
130
|
+
return type.ofType
|
131
|
+
}
|
132
|
+
return type
|
133
|
+
}
|
134
|
+
|
135
|
+
function genField(field: GraphQLField<any, any>) {
|
136
|
+
const isNonNull = field.type instanceof GraphQLNonNull
|
137
|
+
const directives = field.astNode?.directives?.map((d) => '\t' + directive2decorator(d) + '\n') || []
|
138
|
+
|
139
|
+
const type = genType(field.type)
|
140
|
+
const returnType = isNonNull ? type : `${type} | undefined`
|
141
|
+
|
142
|
+
if (isObject(field.type)) {
|
143
|
+
const t = getElementType(field.type)
|
144
|
+
const typeAsArg = isInterfaceType(t) ? `"${t}"` : genType(t)
|
145
|
+
|
146
|
+
if (isList(field.type)) {
|
147
|
+
return `${directives.join()}\tget ${field.name}(): Promise<${genType(t)}[]> { return this.getFieldObjectArray(${typeAsArg}, "${field.name}") as Promise<${genType(t)}[]> }
|
148
|
+
set ${field.name}(value: Array<${type}> | ID[]) { this.set("${field.name}", value) }
|
149
|
+
get ${field.name}Ids(): ID[] { return this.get("${field.name}") }`
|
150
|
+
}
|
151
|
+
|
152
|
+
return `${directives.join()}\tget ${field.name}(): Promise<${genType(t)} | undefined> { return this.getFieldObject(${typeAsArg},"${field.name}") as Promise<${genType(t)} | undefined> }
|
153
|
+
set ${field.name}(value: ${type} | ID) { this.set("${field.name}", value) }
|
154
|
+
get ${field.name}Id(): ID | undefined { return this.get("${field.name}") }`
|
155
|
+
}
|
156
|
+
|
157
|
+
return `${directives.join()}\tget ${field.name}(): ${returnType} { return this.get("${field.name}") }
|
158
|
+
set ${field.name}(value: ${type}) { this.set("${field.name}", value) }`
|
159
|
+
}
|
160
|
+
|
161
|
+
function genDataType(t: GraphQLObjectType<any, any>) {
|
162
|
+
let output = `type ${t.name}Data = `
|
163
|
+
const relationsFields = Object.values(t.getFields()).filter((f) => isObject(f.type))
|
164
|
+
if (relationsFields.length > 0) {
|
165
|
+
output += `Omit<${t.name}, ${relationsFields.map((f) => `"${f.name}"`).join(' | ')}>`
|
166
|
+
output += ' & {' + relationsFields.map((f) => `${f.name}?: ${isList(f.type) ? 'ID[]' : 'ID'}`).join(', ') + '}'
|
167
|
+
} else {
|
168
|
+
output += `${t.name}`
|
169
|
+
}
|
170
|
+
return output
|
171
|
+
}
|
172
|
+
|
173
|
+
function genEntity(t: GraphQLObjectType<any, any>) {
|
174
|
+
const decorators = t.astNode?.directives?.map(directive2decorator) || []
|
175
|
+
|
176
|
+
let impls = ''
|
177
|
+
if (t.getInterfaces().length > 0) {
|
178
|
+
impls +=
|
179
|
+
' implements ' +
|
180
|
+
t
|
181
|
+
.getInterfaces()
|
182
|
+
.map((i) => i.name)
|
183
|
+
.join(', ')
|
184
|
+
}
|
185
|
+
|
186
|
+
return `
|
187
|
+
${genDataType(t)}
|
188
|
+
${decorators.join('\n')}
|
189
|
+
export class ${t.name} extends Entity${impls} {
|
190
|
+
constructor(data: Partial<${t.name}Data>) {
|
191
|
+
super(data)
|
192
|
+
}
|
193
|
+
${Object.values(t.getFields()).map(genField).join('\n')}
|
194
|
+
}`
|
195
|
+
}
|
196
|
+
|
197
|
+
function genInterface(t: GraphQLInterfaceType) {
|
198
|
+
return `
|
199
|
+
export interface ${t.name} {
|
200
|
+
${Object.values(t.getFields())
|
201
|
+
.map((f) => `\t${f.name}: ${genType(f.type)}`)
|
202
|
+
.join('\n')}
|
203
|
+
}`
|
204
|
+
}
|
205
|
+
|
206
|
+
export function directive2decorator(directive: DirectiveNode) {
|
207
|
+
let s = `@${directive.name.value}`
|
208
|
+
if (directive.arguments?.length) {
|
209
|
+
s += `(${directive.arguments
|
210
|
+
?.map((arg) => {
|
211
|
+
return arg.value.kind === Kind.STRING ? `"${arg.value.value}"` : `${arg.value}`
|
212
|
+
})
|
213
|
+
.join(', ')})`
|
214
|
+
}
|
215
|
+
return s
|
216
|
+
}
|
217
|
+
|
218
|
+
function genEnum(t: GraphQLEnumType) {
|
219
|
+
return `
|
220
|
+
export enum ${t.name} {
|
221
|
+
${t
|
222
|
+
.getValues()
|
223
|
+
.map((v) => `\t${v.name}`)
|
224
|
+
.join(', ')}
|
225
|
+
}`
|
226
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export { StoreContext } from '@sentio/runtime'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import { Store } from './store.js'
|
2
|
+
import { ID } from './types.js'
|
3
|
+
|
4
|
+
export interface EntityClass<T extends Entity> {
|
5
|
+
new (data: any): T
|
6
|
+
}
|
7
|
+
|
8
|
+
export abstract class Entity {
|
9
|
+
get id(): ID {
|
10
|
+
return this.get('id')
|
11
|
+
}
|
12
|
+
|
13
|
+
private _store: Store | undefined
|
14
|
+
data: Record<string, any> = {}
|
15
|
+
protected constructor(data: any) {
|
16
|
+
Object.entries(data).forEach(([key, value]) => {
|
17
|
+
if (Array.isArray(value)) {
|
18
|
+
this.data[key] = value.map((v) => this.getIdFromEntity(v))
|
19
|
+
} else {
|
20
|
+
this.data[key] = this.getIdFromEntity(value)
|
21
|
+
}
|
22
|
+
})
|
23
|
+
}
|
24
|
+
|
25
|
+
private getIdFromEntity(entity: any): any {
|
26
|
+
if (entity instanceof Entity) {
|
27
|
+
return entity.id
|
28
|
+
} else if (typeof entity === 'object' && entity.id) {
|
29
|
+
return entity.id
|
30
|
+
}
|
31
|
+
return entity
|
32
|
+
}
|
33
|
+
|
34
|
+
set store(store: Store) {
|
35
|
+
this._store = store
|
36
|
+
}
|
37
|
+
|
38
|
+
get<T>(field: string): T {
|
39
|
+
return this.data[field]
|
40
|
+
}
|
41
|
+
|
42
|
+
set<T>(field: string, value: T | T[] | ID | ID[]): void {
|
43
|
+
if (Array.isArray(value) && value instanceof Entity) {
|
44
|
+
this.data[field] = value.map((v) => (v as Entity).id)
|
45
|
+
} else if (value instanceof Entity) {
|
46
|
+
this.data[field] = (value as Entity).id
|
47
|
+
}
|
48
|
+
this.data[field] = value
|
49
|
+
}
|
50
|
+
|
51
|
+
protected getFieldObject<T extends Entity>(entity: EntityClass<T> | string, field: string): Promise<T | undefined> {
|
52
|
+
const id = this.data[field]
|
53
|
+
return id ? (this._store?.get(entity, id) as Promise<T>) : Promise.resolve(undefined)
|
54
|
+
}
|
55
|
+
|
56
|
+
protected getFieldObjectArray<T extends Entity>(entity: EntityClass<T>, field: string): Promise<T[]> {
|
57
|
+
const ids = this.data[field]
|
58
|
+
const promises = ids.map((id: string) => this._store?.get(entity, id))
|
59
|
+
return Promise.all(promises) as Promise<T[]>
|
60
|
+
}
|
61
|
+
}
|
package/src/store/run.ts
ADDED