@powerhousedao/reactor-api 1.10.9 → 1.11.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powerhousedao/reactor-api",
3
- "version": "1.10.9",
3
+ "version": "1.11.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,8 +21,8 @@
21
21
  "esbuild": "^0.24.0",
22
22
  "graphql-tag": "^2.12.6",
23
23
  "@powerhousedao/scalars": "1.13.0",
24
- "document-model": "2.13.0",
25
- "document-drive": "1.11.5"
24
+ "document-drive": "1.11.6",
25
+ "document-model": "2.13.0"
26
26
  },
27
27
  "dependencies": {
28
28
  "@apollo/server": "^4.11.0",
@@ -42,7 +42,7 @@
42
42
  "nanoevents": "^9.0.0",
43
43
  "pg": "^8.13.0",
44
44
  "uuid": "^9.0.1",
45
- "document-model-libs": "1.122.5"
45
+ "document-model-libs": "1.122.6"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from "./server";
2
- export * from "./router";
3
- export * from "./utils/create-schema";
2
+ export * from "./subgraphs/manager";
3
+ export * from "./utils";
4
4
  export * from "./processors";
5
5
  export * from "./types";
6
+ export * from "./subgraphs";
7
+ export * from "@powerhousedao/analytics-engine-core";
@@ -1,7 +1,7 @@
1
1
  import { IAnalyticsStore } from "@powerhousedao/analytics-engine-core";
2
2
  import { Document, OperationScope } from "document-model/document";
3
3
  import { Processor } from "./processor";
4
- import { ProcessorOptions, ProcessorSetupArgs } from "src/types";
4
+ import { ProcessorOptions, ProcessorSetupArgs } from "../types";
5
5
 
6
6
  export * from "@powerhousedao/analytics-engine-core";
7
7
 
@@ -13,11 +13,6 @@ export abstract class AnalyticsProcessor<
13
13
 
14
14
  constructor(args: ProcessorSetupArgs, options?: ProcessorOptions) {
15
15
  super(args, options);
16
- this.analyticsStore = args.dataSources.analyticsStore;
17
- }
18
-
19
- onSetup(args: ProcessorSetupArgs) {
20
- super.onSetup(args);
21
- this.analyticsStore = args.dataSources.analyticsStore;
16
+ this.analyticsStore = args.analyticsStore;
22
17
  }
23
18
  }
@@ -1,2 +1,3 @@
1
1
  export * from "./analytics-processor";
2
2
  export * from "./processor";
3
+ export * from "./manager";
@@ -1,8 +1,14 @@
1
- import { IAnalyticsStore } from "@powerhousedao/analytics-engine-core";
2
1
  import { IDocumentDriveServer } from "document-drive";
3
2
  import { DocumentDriveDocument } from "document-model-libs/document-drive";
4
- import { IProcessor, IProcessorManager, ProcessorSetupArgs } from "./types";
5
- import { ProcessorClass, isProcessorClass } from "./processors";
3
+
4
+ import {
5
+ Db,
6
+ IProcessor,
7
+ IProcessorManager,
8
+ ProcessorSetupArgs,
9
+ } from "../types";
10
+ import { IAnalyticsStore } from "./analytics-processor";
11
+ import { isProcessorClass, ProcessorClass } from "./processor";
6
12
 
7
13
  export class ProcessorManager implements IProcessorManager {
8
14
  private reactor: IDocumentDriveServer;
@@ -10,6 +16,7 @@ export class ProcessorManager implements IProcessorManager {
10
16
 
11
17
  constructor(
12
18
  driveServer: IDocumentDriveServer,
19
+ private operationalStore: Db,
13
20
  private analyticsStore: IAnalyticsStore,
14
21
  ) {
15
22
  this.reactor = driveServer;
@@ -52,9 +59,8 @@ export class ProcessorManager implements IProcessorManager {
52
59
  async registerProcessor(module: IProcessor | ProcessorClass) {
53
60
  const args: ProcessorSetupArgs = {
54
61
  reactor: this.reactor,
55
- dataSources: {
56
- analyticsStore: this.analyticsStore,
57
- },
62
+ operationalStore: this.operationalStore,
63
+ analyticsStore: this.analyticsStore,
58
64
  };
59
65
 
60
66
  const processor = isProcessorClass(module) ? new module(args) : module;
@@ -3,7 +3,7 @@ import {
3
3
  InternalTransmitterUpdate,
4
4
  } from "document-drive";
5
5
  import { Document, OperationScope } from "document-model/document";
6
- import { IProcessor, ProcessorOptions, ProcessorSetupArgs } from "src/types";
6
+ import { IProcessor, ProcessorOptions, ProcessorSetupArgs } from "../types";
7
7
 
8
8
  export type ProcessorUpdate<
9
9
  D extends Document = Document,
package/src/server.ts CHANGED
@@ -1,18 +1,15 @@
1
1
  import { PGlite } from "@electric-sql/pglite";
2
- import { AnalyticsQueryEngine } from "@powerhousedao/analytics-engine-core";
3
- import { AnalyticsModel } from "@powerhousedao/analytics-engine-graphql";
4
- import {
5
- KnexAnalyticsStore,
6
- KnexQueryExecutor,
7
- } from "@powerhousedao/analytics-engine-knex";
8
2
  import { IDocumentDriveServer } from "document-drive";
9
3
  import express, { Express } from "express";
10
4
  import { Pool } from "pg";
11
- import { ReactorRouterManager } from "./router";
12
- import { getKnexClient } from "./utils/get-knex-client";
13
- import { ProcessorManager } from "./processor-manager";
5
+ import { ProcessorManager } from "./processors";
6
+ import { SubgraphManager } from "./subgraphs/manager";
14
7
  import { API } from "./types";
15
- import { initialize } from "./subgraphs/analytics";
8
+ import { getDbClient } from "./utils/get-db-client";
9
+ import {
10
+ KnexAnalyticsStore,
11
+ KnexQueryExecutor,
12
+ } from "@powerhousedao/analytics-engine-knex";
16
13
 
17
14
  type Options = {
18
15
  express?: Express;
@@ -29,25 +26,21 @@ export async function startAPI(
29
26
  ): Promise<API> {
30
27
  const port = options.port ?? DEFAULT_PORT;
31
28
  const app = options.express ?? express();
32
-
33
- const knex = getKnexClient(options.dbPath);
34
- await initialize(knex);
35
-
29
+ const db = getDbClient(options.dbPath);
36
30
  const analyticsStore = new KnexAnalyticsStore({
37
31
  executor: new KnexQueryExecutor(),
38
- knex,
39
- });
40
- const reactorRouterManager = new ReactorRouterManager("/", app, reactor);
41
- reactorRouterManager.setAdditionalContextFields({
42
- dataSources: {
43
- db: {
44
- Analytics: new AnalyticsModel(new AnalyticsQueryEngine(analyticsStore)),
45
- },
46
- },
32
+ knex: db,
47
33
  });
48
- await reactorRouterManager.init();
49
- const processorManager = new ProcessorManager(reactor, analyticsStore);
34
+ const subgraphManager = new SubgraphManager(
35
+ "/",
36
+ app,
37
+ reactor,
38
+ db,
39
+ analyticsStore,
40
+ );
41
+ await subgraphManager.init();
42
+ const processorManager = new ProcessorManager(reactor, db, analyticsStore);
50
43
 
51
44
  app.listen(port);
52
- return { app, reactorRouterManager, processorManager };
45
+ return { app, subgraphManager, processorManager };
53
46
  }
@@ -1,12 +1,119 @@
1
1
  import {
2
- AnalyticsResolvers as resolvers,
3
- typedefs as typeDefs,
2
+ AnalyticsQueryEngine,
3
+ IAnalyticsStore,
4
+ } from "@powerhousedao/analytics-engine-core";
5
+ import {
6
+ AnalyticsModel,
7
+ typedefs,
8
+ AnalyticsResolvers,
4
9
  } from "@powerhousedao/analytics-engine-graphql";
5
- import { createSchema } from "./db.js";
6
- import { Knex } from "knex";
10
+ import gql from "graphql-tag";
11
+ import { Subgraph } from "../base/index.js";
12
+ import { Context, SubgraphArgs } from "../index.js";
13
+ import { GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper/resolverMap.js";
14
+
15
+ export class AnalyticsSubgraph extends Subgraph {
16
+ analyticsStore: IAnalyticsStore;
17
+ analyticsModel: AnalyticsModel;
18
+
19
+ name = "analytics";
20
+ typeDefs = gql`
21
+ ${typedefs}
22
+ `;
23
+
24
+ resolvers = AnalyticsResolvers as GraphQLResolverMap<Context>;
25
+
26
+ constructor(args: SubgraphArgs) {
27
+ super(args);
28
+ this.analyticsStore = args.analyticsStore;
29
+
30
+ this.analyticsModel = new AnalyticsModel(
31
+ new AnalyticsQueryEngine(this.analyticsStore),
32
+ );
33
+
34
+ args.subgraphManager.setAdditionalContextFields({
35
+ dataSources: {
36
+ db: {
37
+ Analytics: this.analyticsModel,
38
+ },
39
+ },
40
+ });
41
+ }
42
+
43
+ async onSetup() {
44
+ await this.#createTables();
45
+ }
46
+
47
+ async #createTables() {
48
+ if (!(await this.operationalStore.schema.hasTable("AnalyticsDimension"))) {
49
+ await this.operationalStore.schema.createTable(
50
+ "AnalyticsDimension",
51
+ (table) => {
52
+ table.increments("id").primary();
53
+ table
54
+ .string("dimension")
55
+ .notNullable()
56
+ .index("analyticsdimension_dimension_index");
57
+ table
58
+ .string("path")
59
+ .notNullable()
60
+ .index("analyticsdimension_path_index");
61
+ table.string("label").nullable();
62
+ table.string("icon").nullable();
63
+ table.text("description").nullable();
64
+ },
65
+ );
66
+ }
7
67
 
8
- export { resolvers, typeDefs };
68
+ if (!(await this.operationalStore.schema.hasTable("AnalyticsSeries"))) {
69
+ await this.operationalStore.schema.createTable(
70
+ "AnalyticsSeries",
71
+ (table) => {
72
+ table.increments("id").primary();
73
+ table
74
+ .string("source")
75
+ .notNullable()
76
+ .index("analyticsseries_source_index");
77
+ table
78
+ .timestamp("start")
79
+ .notNullable()
80
+ .index("analyticsseries_start_index");
81
+ table.timestamp("end").nullable().index("analyticsseries_end_index");
82
+ table
83
+ .string("metric")
84
+ .notNullable()
85
+ .index("analyticsseries_metric_index");
86
+ table
87
+ .float("value")
88
+ .notNullable()
89
+ .index("analyticsseries_value_index");
90
+ table.string("unit").nullable().index("analyticsseries_unit_index");
91
+ table.string("fn").notNullable().index("analyticsseries_fn_index");
92
+ table.json("params").nullable();
93
+ },
94
+ );
95
+ }
9
96
 
10
- export async function initialize(knex: Knex) {
11
- await createSchema(knex);
97
+ if (
98
+ !(await this.operationalStore.schema.hasTable(
99
+ "AnalyticsSeries_AnalyticsDimension",
100
+ ))
101
+ ) {
102
+ await this.operationalStore.schema.createTable(
103
+ "AnalyticsSeries_AnalyticsDimension",
104
+ (table) => {
105
+ table
106
+ .integer("seriesId")
107
+ .references("AnalyticsSeries.id")
108
+ .onDelete("CASCADE")
109
+ .index("analyticsseries_analyticsdimension_seriesid_index");
110
+ table
111
+ .integer("dimensionId")
112
+ .references("AnalyticsDimension.id")
113
+ .onDelete("CASCADE")
114
+ .index("analyticsseries_analyticsdimension_dimensionid_index");
115
+ },
116
+ );
117
+ }
118
+ }
12
119
  }
@@ -0,0 +1,31 @@
1
+ import { ISubgraph } from "../types";
2
+ import { DocumentNode } from "graphql";
3
+ import { IDocumentDriveServer } from "document-drive";
4
+ import { SubgraphArgs } from "../types";
5
+ import { GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper";
6
+ import { gql } from "graphql-tag";
7
+ import { Context } from "../types";
8
+ import { Db } from "src/types";
9
+
10
+ export class Subgraph implements ISubgraph {
11
+ name = "example";
12
+ resolvers: GraphQLResolverMap<Context> = {
13
+ Query: {
14
+ hello: () => this.name,
15
+ },
16
+ };
17
+ typeDefs: DocumentNode = gql`
18
+ type Query {
19
+ hello: String
20
+ }
21
+ `;
22
+ reactor: IDocumentDriveServer;
23
+ operationalStore: Db;
24
+ constructor(args: SubgraphArgs) {
25
+ this.reactor = args.reactor;
26
+ this.operationalStore = args.operationalStore;
27
+ }
28
+ async onSetup() {
29
+ // noop
30
+ }
31
+ }