@powerhousedao/reactor-local 1.27.7-dev.0 → 1.27.7-dev.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.
@@ -1,9 +1,14 @@
1
1
  import { type DriveInput, type IDocumentDriveServer } from "document-drive";
2
+ export type StorageOptions = {
3
+ type: "filesystem" | "memory" | "postgres" | "browser";
4
+ filesystemPath?: string;
5
+ postgresUrl?: string;
6
+ };
2
7
  export type StartServerOptions = {
3
8
  configFile?: string;
4
9
  dev?: boolean;
5
10
  port?: string | number;
6
- storagePath?: string;
11
+ storage?: StorageOptions;
7
12
  dbPath?: string;
8
13
  drive?: DriveInput;
9
14
  packages?: string[];
@@ -15,7 +20,10 @@ export type StartServerOptions = {
15
20
  };
16
21
  export declare const DefaultStartServerOptions: {
17
22
  port: number;
18
- storagePath: string;
23
+ storage: {
24
+ type: "filesystem";
25
+ filesystemPath: string;
26
+ };
19
27
  dbPath: string;
20
28
  drive: {
21
29
  global: {
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAOA,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,oBAAoB,EAG1B,MAAM,gBAAgB,CAAC;AAuBxB,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,CAAC,EACF;QACE,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;KAClB,GACD,OAAO,GACP,SAAS,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM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AM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,GAAG,QAAQ,CAAC;CACvE,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;CAqBR,CAAC;AAE/B,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;IACjE,MAAM,EAAE,oBAAoB,CAAC;CAC9B,CAAC;AAiCF,QAAA,MAAM,WAAW,GACf,UAAU,kBAAkB,KAC3B,OAAO,CAAC,YAAY,CA0EtB,CAAC;AAqGF,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -1,18 +1,25 @@
1
1
  import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
2
2
  import { isSubgraphClass, startAPI, } from "@powerhousedao/reactor-api";
3
- import { DriveAlreadyExistsError, driveDocumentModelModule, logger, ReactorBuilder, } from "document-drive";
3
+ import Prisma from "@prisma/client";
4
+ import { DriveAlreadyExistsError, driveDocumentModelModule, InMemoryCache, logger, MemoryStorage, ReactorBuilder, } from "document-drive";
5
+ import { BrowserStorage } from "document-drive/storage/browser";
4
6
  import { FilesystemStorage } from "document-drive/storage/filesystem";
7
+ import { PrismaStorage } from "document-drive/storage/prisma";
5
8
  import { documentModelDocumentModelModule, } from "document-model";
6
9
  import dotenv from "dotenv";
7
10
  import { access } from "node:fs/promises";
8
11
  import path from "node:path";
9
12
  import { createServer as createViteServer } from "vite";
10
13
  import { PackagesManager } from "./packages.js";
14
+ const PrismaClient = Prisma.PrismaClient;
11
15
  const dirname = process.cwd();
12
16
  dotenv.config();
13
17
  export const DefaultStartServerOptions = {
14
18
  port: 4001,
15
- storagePath: path.join(dirname, ".ph/file-storage"),
19
+ storage: {
20
+ type: "filesystem",
21
+ filesystemPath: path.join(dirname, ".ph/file-storage"),
22
+ },
16
23
  dbPath: path.join(dirname, ".ph/read-model.db"),
17
24
  drive: {
18
25
  global: {
@@ -33,9 +40,32 @@ const baseDocumentModelModules = [
33
40
  documentModelDocumentModelModule,
34
41
  driveDocumentModelModule,
35
42
  ];
43
+ const createStorage = (options, cache) => {
44
+ switch (options.type) {
45
+ case "filesystem":
46
+ logger.info(`Initializing filesystem storage at '${options.filesystemPath}'.`);
47
+ return new FilesystemStorage(options.filesystemPath);
48
+ case "memory":
49
+ logger.info("Initializing memory storage.");
50
+ return new MemoryStorage();
51
+ case "postgres":
52
+ logger.info(`Initializing postgres storage at '${options.postgresUrl}'.`);
53
+ const db = new PrismaClient({
54
+ datasources: {
55
+ db: {
56
+ url: options.postgresUrl,
57
+ },
58
+ },
59
+ });
60
+ return new PrismaStorage(db, cache);
61
+ case "browser":
62
+ logger.info("Initializing browser storage.");
63
+ return new BrowserStorage();
64
+ }
65
+ };
36
66
  const startServer = async (options) => {
37
67
  process.setMaxListeners(0);
38
- const { port, storagePath, drive, dev, dbPath, packages, configFile, logLevel, } = {
68
+ const { port, storage, drive, dev, dbPath, packages, configFile, logLevel } = {
39
69
  ...DefaultStartServerOptions,
40
70
  ...options,
41
71
  };
@@ -60,9 +90,11 @@ const startServer = async (options) => {
60
90
  : { packages: [] });
61
91
  const packageDocModels = (await packagesManager.loadDocumentModels()) ?? [];
62
92
  docModels = joinDocumentModelModules(docModels, packageDocModels);
63
- // start document drive server with all available document models & filesystem storage
93
+ // start document drive server with all available document models & storage
94
+ const cache = new InMemoryCache();
64
95
  const driveServer = new ReactorBuilder(docModels)
65
- .withStorage(new FilesystemStorage(storagePath))
96
+ .withCache(cache)
97
+ .withStorage(createStorage(storage, cache))
66
98
  .build();
67
99
  // init drive server
68
100
  await driveServer.initialize();
@@ -83,7 +115,7 @@ const startServer = async (options) => {
83
115
  return {
84
116
  driveUrl,
85
117
  getDocumentPath: (driveId, documentId) => {
86
- return path.join(storagePath, driveId, `${documentId}.json`);
118
+ return path.join(storage.filesystemPath, driveId, `${documentId}.json`);
87
119
  },
88
120
  server: driveServer,
89
121
  };