@effectstream/db-emulator 0.100.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/README.md +34 -0
- package/mod.ts +1 -0
- package/package.json +13 -0
- package/patch-emulator.ts +88 -0
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @effectstream/db-emulator
|
|
2
|
+
|
|
3
|
+
Database emulation utilities for Effectstream development and testing.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This package provides utilities for applying database migrations in test
|
|
8
|
+
environments and during development. It was separated from `@effectstream/db` to break
|
|
9
|
+
the circular dependency between `@effectstream/db` and `@effectstream/sm`.
|
|
10
|
+
|
|
11
|
+
## Dependencies
|
|
12
|
+
|
|
13
|
+
- `@effectstream/db` - Core database functionality
|
|
14
|
+
- `@effectstream/sm` - State machine primitives (for built-in primitives map)
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { getConnection } from "@effectstream/db";
|
|
20
|
+
import { standAloneApplyMigrations } from "@effectstream/db-emulator";
|
|
21
|
+
|
|
22
|
+
const db = await getConnection();
|
|
23
|
+
await standAloneApplyMigrations(
|
|
24
|
+
db,
|
|
25
|
+
migrationTable,
|
|
26
|
+
localhostConfig,
|
|
27
|
+
userDefinedPrimitives, // optional
|
|
28
|
+
);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Note
|
|
32
|
+
|
|
33
|
+
This is primarily a development/testing utility. Production applications
|
|
34
|
+
typically use the migration system directly through `@effectstream/db`.
|
package/mod.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { standAloneApplyMigrations } from "./patch-emulator.ts";
|
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { run } from "effection";
|
|
2
|
+
import { createDynamicTables } from "@effectstream/db";
|
|
3
|
+
import type { Client } from "pg";
|
|
4
|
+
import { applyMigrations } from "@effectstream/db/version";
|
|
5
|
+
import type { SyncProtocolWithNetwork } from "@effectstream/config";
|
|
6
|
+
import { builtInPrimitivesMap } from "@effectstream/sm";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This is to generate the user/custom pgtyped files in compilation time
|
|
10
|
+
* MIGRATIONS environment variable is used to specify the path to the migrations folder.
|
|
11
|
+
* Every file in the migrations folder is executed in order.
|
|
12
|
+
* TODO: Implement how to manage the order of the migrations, e.g. 1.sql, 2.sql, 10.sql, etc.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export async function standAloneApplyMigrations(
|
|
16
|
+
db: Client,
|
|
17
|
+
migrationTable: /*DBMigrations[]*/ any[],
|
|
18
|
+
localhostConfig: SyncProtocolWithNetwork,
|
|
19
|
+
userDefinedPrimitives?: Record<string, any>,
|
|
20
|
+
) {
|
|
21
|
+
const l: SyncProtocolWithNetwork = localhostConfig as any;
|
|
22
|
+
const config = Object.entries(l.primitives).map(
|
|
23
|
+
([key, value]: [string, any]) => {
|
|
24
|
+
const primitiveType = value.primitive.type;
|
|
25
|
+
const primitiveUniqueName = value.primitive.name;
|
|
26
|
+
const primitiveConfig = value.primitive;
|
|
27
|
+
const isBuiltInPrimitive = primitiveType in builtInPrimitivesMap;
|
|
28
|
+
const isUserDefinedPrimitive = userDefinedPrimitives &&
|
|
29
|
+
(primitiveType in userDefinedPrimitives);
|
|
30
|
+
const classConfig = {
|
|
31
|
+
...primitiveConfig,
|
|
32
|
+
instanceName: primitiveUniqueName,
|
|
33
|
+
};
|
|
34
|
+
if (isBuiltInPrimitive) {
|
|
35
|
+
new builtInPrimitivesMap[
|
|
36
|
+
primitiveType as keyof typeof builtInPrimitivesMap
|
|
37
|
+
](classConfig as any);
|
|
38
|
+
} else if (isUserDefinedPrimitive) {
|
|
39
|
+
new userDefinedPrimitives[
|
|
40
|
+
primitiveType as keyof typeof userDefinedPrimitives
|
|
41
|
+
](classConfig);
|
|
42
|
+
} else {
|
|
43
|
+
console.error(
|
|
44
|
+
"userDefinedPrimitives",
|
|
45
|
+
userDefinedPrimitives,
|
|
46
|
+
primitiveType,
|
|
47
|
+
);
|
|
48
|
+
throw new Error(`Primitive ${primitiveType} not found`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
config: {
|
|
53
|
+
primitives: [{
|
|
54
|
+
primitive: {
|
|
55
|
+
type: value.primitive.type,
|
|
56
|
+
name: value.primitive.name,
|
|
57
|
+
},
|
|
58
|
+
}],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
await run(function* () {
|
|
65
|
+
return yield* createDynamicTables(
|
|
66
|
+
{
|
|
67
|
+
engine_current_version: "0.0.0",
|
|
68
|
+
engine_previous_version: "0.0.0",
|
|
69
|
+
app_previous_version: "0.0.0",
|
|
70
|
+
is_empty: true,
|
|
71
|
+
},
|
|
72
|
+
0,
|
|
73
|
+
db,
|
|
74
|
+
config as any,
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
const migrations = migrationTable;
|
|
78
|
+
|
|
79
|
+
for (const migration of migrations) {
|
|
80
|
+
await applyMigrations(
|
|
81
|
+
db,
|
|
82
|
+
0,
|
|
83
|
+
migration.name,
|
|
84
|
+
migration.sql,
|
|
85
|
+
false,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|