@powerhousedao/codegen 3.3.0-dev.5 → 3.3.0-dev.7

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.
@@ -2,6 +2,7 @@
2
2
  to: "<%= rootDir %>/<%= h.changeCase.param(name) %>/index.ts"
3
3
  force: true
4
4
  ---
5
+ import { type IOperationalStore } from "document-drive/processors/types";
5
6
  import { OperationalProcessor, type OperationalProcessorFilter } from "document-drive/processors/operational-processor";
6
7
  import { type InternalTransmitterUpdate } from "document-drive/server/listener/transmitter/internal";
7
8
  import { up } from "./migrations.js";
@@ -24,7 +25,7 @@ export class <%= pascalName %>Processor extends OperationalProcessor<DB> {
24
25
  }
25
26
 
26
27
  async initAndUpgrade(): Promise<void> {
27
- await up(this.operationalStore);
28
+ await up(this.operationalStore as IOperationalStore);
28
29
  }
29
30
 
30
31
  async onStrands(
@@ -2,9 +2,9 @@
2
2
  to: "<%= rootDir %>/<%= h.changeCase.param(name) %>/migrations.ts"
3
3
  force: true
4
4
  ---
5
- import { sql, type Kysely } from "kysely";
5
+ import { type IOperationalStore } from "document-drive/processors/types"
6
6
 
7
- export async function up(db: Kysely<any>): Promise<void> {
7
+ export async function up(db: IOperationalStore): Promise<void> {
8
8
  // Create table
9
9
  await db.schema
10
10
  .createTable("todo")
@@ -15,12 +15,9 @@ export async function up(db: Kysely<any>): Promise<void> {
15
15
  ])
16
16
  .ifNotExists()
17
17
  .execute();
18
-
19
- const tables = await db.introspection.getTables();
20
- console.log(tables);
21
18
  }
22
19
 
23
- export async function down(db: Kysely<any>): Promise<void> {
20
+ export async function down(db: IOperationalStore): Promise<void> {
24
21
  // drop table
25
22
  await db.schema.dropTable("todo").execute();
26
23
  }
@@ -41,16 +41,7 @@ export class <%= pascalName %>Subgraph extends Subgraph {
41
41
  example: "test"
42
42
  }
43
43
 
44
- async onSetup() {
45
- await this.createOperationalTables();
46
- }
47
-
48
- async createOperationalTables() {
49
- await this.operationalStore.schema.createTableIfNotExists("example", (table) => {
50
- table.string("id").primary();
51
- table.string("name");
52
- });
53
- }
44
+ async onSetup() {}
54
45
 
55
46
  async onDisconnect() {}
56
47
  <% } %>
@@ -1 +1 @@
1
- {"version":3,"file":"kysely.d.ts","sourceRoot":"","sources":["../../../src/codegen/kysely.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,QAAQ;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,wBAAsB,gBAAgB,CAAC,EACrC,aAAa,EACb,UAAU,GACX,EAAE,QAAQ,iBAwBV"}
1
+ {"version":3,"file":"kysely.d.ts","sourceRoot":"","sources":["../../../src/codegen/kysely.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,QAAQ;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAgBD,wBAAsB,gBAAgB,CAAC,EACrC,aAAa,EACb,UAAU,GACX,EAAE,QAAQ,iBAoBV"}
@@ -1,12 +1,25 @@
1
- import { generateId } from "document-model";
2
1
  import { Kysely } from "kysely";
3
2
  import { Codegen, KyselyPGlite } from "kysely-pglite";
3
+ import { readFileSync } from "node:fs";
4
+ import { resolve } from "node:path";
5
+ import { transform } from "sucrase";
6
+ async function loadMigration(file) {
7
+ // read ts code as a string and compile it to a js string
8
+ const code = readFileSync(file, "utf-8");
9
+ const compiledCode = transform(code, {
10
+ transforms: ["typescript"],
11
+ }).code;
12
+ // create esm module from js code
13
+ const base64 = Buffer.from(compiledCode).toString("base64");
14
+ const dataUrl = `data:text/javascript;base64,${base64}`;
15
+ const mod = (await import(dataUrl));
16
+ return mod;
17
+ }
4
18
  export async function generateDBSchema({ migrationFile, schemaFile, }) {
5
- const dataDir = `memory://${generateId()}`;
6
- const { dialect } = new KyselyPGlite({ dataDir });
19
+ const { dialect } = new KyselyPGlite("memory://");
7
20
  const db = new Kysely({ dialect });
8
21
  try {
9
- const migration = (await import(migrationFile));
22
+ const migration = await loadMigration(migrationFile);
10
23
  await migration.up(db);
11
24
  }
12
25
  catch (error) {
@@ -14,12 +27,13 @@ export async function generateDBSchema({ migrationFile, schemaFile, }) {
14
27
  throw error;
15
28
  }
16
29
  const codegen = new Codegen(dialect);
30
+ const outFile = schemaFile ?? resolve(migrationFile, "../schema.ts");
17
31
  // TODO: Do not pass in outFile, so we can replace the kysely import
18
- const typesStr = await codegen.generate({
32
+ await codegen.generate({
19
33
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
20
34
  db: db,
21
- outFile: schemaFile,
35
+ outFile,
22
36
  });
23
- console.log(`Types generated at ${schemaFile}`);
37
+ console.log(`Schema types generated at ${outFile}`);
24
38
  }
25
39
  //# sourceMappingURL=kysely.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"kysely.js","sourceRoot":"","sources":["../../../src/codegen/kysely.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAOtD,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,EACrC,aAAa,EACb,UAAU,GACD;IACT,MAAM,OAAO,GAAG,YAAY,UAAU,EAAE,EAAE,CAAC;IAC3C,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,YAAY,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;IAClD,MAAM,EAAE,GAAG,IAAI,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;IAEnC,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,CAAC,MAAM,MAAM,CAAC,aAAa,CAAC,CAE7C,CAAC;QACF,MAAM,SAAS,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACzB,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,OAAO,CAAC,KAAK,CAAC,0BAA0B,E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