@effectify/prisma 1.0.0 → 1.1.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 +33 -38
- package/dist/src/cli.d.ts +1 -1
- package/dist/src/cli.js +15 -14
- package/dist/src/commands/init.d.ts +1 -1
- package/dist/src/commands/init.js +36 -47
- package/dist/src/commands/prisma.d.ts +4 -4
- package/dist/src/commands/prisma.js +12 -12
- package/dist/src/generators/sql-schema-generator.js +15 -16
- package/dist/src/runtime/index.d.ts +303 -0
- package/dist/src/runtime/index.js +216 -0
- package/dist/src/schema-generator/effect/enum.d.ts +12 -0
- package/dist/src/schema-generator/effect/enum.js +18 -0
- package/dist/src/schema-generator/effect/generator.d.ts +16 -0
- package/dist/src/schema-generator/effect/generator.js +42 -0
- package/dist/src/schema-generator/effect/join-table.d.ts +12 -0
- package/dist/src/schema-generator/effect/join-table.js +28 -0
- package/dist/src/schema-generator/effect/type.d.ts +18 -0
- package/dist/src/schema-generator/effect/type.js +82 -0
- package/dist/src/schema-generator/index.d.ts +11 -0
- package/dist/src/schema-generator/index.js +83 -0
- package/dist/src/schema-generator/kysely/generator.d.ts +11 -0
- package/dist/src/schema-generator/kysely/generator.js +7 -0
- package/dist/src/schema-generator/kysely/type.d.ts +14 -0
- package/dist/src/schema-generator/kysely/type.js +44 -0
- package/dist/src/schema-generator/prisma/enum.d.ts +19 -0
- package/dist/src/schema-generator/prisma/enum.js +19 -0
- package/dist/src/schema-generator/prisma/generator.d.ts +53 -0
- package/dist/src/schema-generator/prisma/generator.js +29 -0
- package/dist/src/schema-generator/prisma/relation.d.ts +83 -0
- package/dist/src/schema-generator/prisma/relation.js +165 -0
- package/dist/src/schema-generator/prisma/type.d.ts +108 -0
- package/dist/src/schema-generator/prisma/type.js +85 -0
- package/dist/src/schema-generator/utils/annotations.d.ts +32 -0
- package/dist/src/schema-generator/utils/annotations.js +79 -0
- package/dist/src/schema-generator/utils/codegen.d.ts +9 -0
- package/dist/src/schema-generator/utils/codegen.js +14 -0
- package/dist/src/schema-generator/utils/naming.d.ts +29 -0
- package/dist/src/schema-generator/utils/naming.js +68 -0
- package/dist/src/schema-generator/utils/type-mappings.d.ts +62 -0
- package/dist/src/schema-generator/utils/type-mappings.js +70 -0
- package/dist/src/services/formatter-service.d.ts +10 -0
- package/dist/src/services/formatter-service.js +18 -0
- package/dist/src/services/generator-context.d.ts +2 -2
- package/dist/src/services/generator-context.js +2 -2
- package/dist/src/services/generator-service.d.ts +9 -8
- package/dist/src/services/generator-service.js +39 -43
- package/dist/src/services/render-service.d.ts +3 -3
- package/dist/src/services/render-service.js +8 -8
- package/dist/src/templates/effect-branded-id.eta +2 -0
- package/dist/src/templates/effect-enums.eta +9 -0
- package/dist/src/templates/effect-index.eta +4 -0
- package/dist/src/templates/effect-join-table.eta +8 -0
- package/dist/src/templates/effect-model.eta +6 -0
- package/dist/src/templates/effect-types-header.eta +7 -0
- package/dist/src/templates/index-default.eta +2 -1
- package/dist/src/templates/kysely-db-interface.eta +6 -0
- package/dist/src/templates/prisma-repository.eta +57 -32
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A powerful Prisma generator that creates **Effect** services and layers from you
|
|
|
13
13
|
## 📦 Installation
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
pnpm add -D @effectify/prisma
|
|
16
|
+
pnpm add -D @effectify/prisma
|
|
17
17
|
pnpm add effect @prisma/client
|
|
18
18
|
```
|
|
19
19
|
|
|
@@ -26,11 +26,6 @@ generator client {
|
|
|
26
26
|
provider = "prisma-client-js"
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
generator effect_schemas {
|
|
30
|
-
provider = "prisma-effect-kysely"
|
|
31
|
-
output = "./generated/effect/schemas"
|
|
32
|
-
}
|
|
33
|
-
|
|
34
29
|
generator effect {
|
|
35
30
|
provider = "effect-prisma"
|
|
36
31
|
output = "./generated/effect"
|
|
@@ -57,17 +52,17 @@ pnpm prisma generate
|
|
|
57
52
|
The generator creates a `Prisma` service for transactions and raw queries, and Model classes that you can use to create repositories.
|
|
58
53
|
|
|
59
54
|
```typescript
|
|
60
|
-
import { Effect, Layer } from "effect"
|
|
61
|
-
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
62
|
-
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
55
|
+
import { Effect, Layer } from "effect"
|
|
56
|
+
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
57
|
+
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
63
58
|
|
|
64
59
|
// Define a program using the generated Prisma service
|
|
65
|
-
const program = Effect.gen(function*
|
|
60
|
+
const program = Effect.gen(function*() {
|
|
66
61
|
// Create a repository for the User model
|
|
67
62
|
const userRepo = yield* PrismaRepository.make(UserModel, {
|
|
68
63
|
modelName: "user",
|
|
69
64
|
spanPrefix: "User",
|
|
70
|
-
})
|
|
65
|
+
})
|
|
71
66
|
|
|
72
67
|
// Create a new user
|
|
73
68
|
const newUser = yield* userRepo.create({
|
|
@@ -75,24 +70,24 @@ const program = Effect.gen(function* () {
|
|
|
75
70
|
email: "hello@effect.website",
|
|
76
71
|
name: "Effect User",
|
|
77
72
|
},
|
|
78
|
-
})
|
|
73
|
+
})
|
|
79
74
|
|
|
80
75
|
// Find the user
|
|
81
76
|
const user = yield* userRepo.findUnique({
|
|
82
77
|
where: { id: newUser.id },
|
|
83
|
-
})
|
|
78
|
+
})
|
|
84
79
|
|
|
85
|
-
return user
|
|
86
|
-
})
|
|
80
|
+
return user
|
|
81
|
+
})
|
|
87
82
|
|
|
88
83
|
// Provide the Prisma layer
|
|
89
84
|
const MainLayer = Prisma.layer({
|
|
90
85
|
// Prisma Client options
|
|
91
86
|
log: ["query", "info", "warn", "error"],
|
|
92
|
-
})
|
|
87
|
+
})
|
|
93
88
|
|
|
94
89
|
// Run the program
|
|
95
|
-
Effect.runPromise(program.pipe(Effect.provide(MainLayer)))
|
|
90
|
+
Effect.runPromise(program.pipe(Effect.provide(MainLayer)))
|
|
96
91
|
```
|
|
97
92
|
|
|
98
93
|
## 🧪 Testing
|
|
@@ -100,27 +95,26 @@ Effect.runPromise(program.pipe(Effect.provide(MainLayer)));
|
|
|
100
95
|
The generated layers make testing easy by allowing you to provide alternative implementations or test databases.
|
|
101
96
|
|
|
102
97
|
```typescript
|
|
103
|
-
import { it } from "@effect/vitest"
|
|
104
|
-
import { Effect } from "effect"
|
|
105
|
-
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
106
|
-
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
98
|
+
import { it } from "@effect/vitest"
|
|
99
|
+
import { Effect } from "effect"
|
|
100
|
+
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
101
|
+
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
107
102
|
|
|
108
103
|
it.effect("should create a user", () =>
|
|
109
|
-
Effect.gen(function*
|
|
104
|
+
Effect.gen(function*() {
|
|
110
105
|
const userRepo = yield* PrismaRepository.make(UserModel, {
|
|
111
106
|
modelName: "user",
|
|
112
107
|
spanPrefix: "User",
|
|
113
|
-
})
|
|
108
|
+
})
|
|
114
109
|
|
|
115
110
|
const user = yield* userRepo.create({
|
|
116
111
|
data: { email: "test@example.com" },
|
|
117
|
-
})
|
|
112
|
+
})
|
|
118
113
|
|
|
119
|
-
expect(user.email).toBe("test@example.com")
|
|
114
|
+
expect(user.email).toBe("test@example.com")
|
|
120
115
|
}).pipe(
|
|
121
|
-
Effect.provide(Prisma.layer()) // In tests, you might want to use a specific test DB url
|
|
122
|
-
)
|
|
123
|
-
);
|
|
116
|
+
Effect.provide(Prisma.layer()), // In tests, you might want to use a specific test DB url
|
|
117
|
+
))
|
|
124
118
|
```
|
|
125
119
|
|
|
126
120
|
## ⚠️ Error Handling
|
|
@@ -128,25 +122,26 @@ it.effect("should create a user", () =>
|
|
|
128
122
|
All Prisma errors are mapped to specific tagged errors in Effect, allowing you to handle them precisely.
|
|
129
123
|
|
|
130
124
|
```typescript
|
|
131
|
-
import { Effect } from "effect"
|
|
132
|
-
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
133
|
-
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
125
|
+
import { Effect } from "effect"
|
|
126
|
+
import { Prisma, UserModel } from "./generated/effect/index.js"
|
|
127
|
+
import * as PrismaRepository from "./generated/effect/prisma-repository.js"
|
|
134
128
|
|
|
135
129
|
const createUser = (email: string) =>
|
|
136
|
-
Effect.gen(function*
|
|
130
|
+
Effect.gen(function*() {
|
|
137
131
|
const userRepo = yield* PrismaRepository.make(UserModel, {
|
|
138
132
|
modelName: "user",
|
|
139
133
|
spanPrefix: "User",
|
|
140
|
-
})
|
|
134
|
+
})
|
|
141
135
|
|
|
142
136
|
return yield* userRepo.create({
|
|
143
137
|
data: { email },
|
|
144
|
-
})
|
|
138
|
+
})
|
|
145
139
|
}).pipe(
|
|
146
|
-
Effect.catchTag(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
140
|
+
Effect.catchTag(
|
|
141
|
+
"PrismaUniqueConstraintError",
|
|
142
|
+
(error) => Effect.logError(`User with email ${email} already exists`),
|
|
143
|
+
),
|
|
144
|
+
)
|
|
150
145
|
```
|
|
151
146
|
|
|
152
147
|
## 📝 License
|
package/dist/src/cli.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
export {};
|
package/dist/src/cli.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
2
|
-
import * as Command from
|
|
3
|
-
import * as NodeContext from
|
|
4
|
-
import * as NodeRuntime from
|
|
5
|
-
import * as Effect from
|
|
6
|
-
import * as Layer from
|
|
7
|
-
import { initCommand } from
|
|
8
|
-
import { prismaCommand } from
|
|
9
|
-
import { GeneratorService } from
|
|
10
|
-
import { RenderService } from
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as Command from "@effect/cli/Command";
|
|
3
|
+
import * as NodeContext from "@effect/platform-node/NodeContext";
|
|
4
|
+
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Layer from "effect/Layer";
|
|
7
|
+
import { initCommand } from "./commands/init.js";
|
|
8
|
+
import { prismaCommand } from "./commands/prisma.js";
|
|
9
|
+
import { GeneratorService } from "./services/generator-service.js";
|
|
10
|
+
import { RenderService } from "./services/render-service.js";
|
|
11
|
+
import { FormatterService } from "./services/formatter-service.js";
|
|
11
12
|
const cli = Command.run(prismaCommand.pipe(Command.withSubcommands([initCommand])), {
|
|
12
|
-
name:
|
|
13
|
-
version:
|
|
13
|
+
name: "@effectify/prisma CLI",
|
|
14
|
+
version: "0.1.0",
|
|
14
15
|
});
|
|
15
|
-
const GeneratorLayer = GeneratorService.Live.pipe(Layer.provide(RenderService.Live), Layer.provide(NodeContext.layer));
|
|
16
|
-
const MainLayer = Layer.mergeAll(GeneratorLayer, RenderService.Live, NodeContext.layer);
|
|
16
|
+
const GeneratorLayer = GeneratorService.Live.pipe(Layer.provide(RenderService.Live), Layer.provide(FormatterService.Live), Layer.provide(NodeContext.layer));
|
|
17
|
+
const MainLayer = Layer.mergeAll(GeneratorLayer, RenderService.Live, FormatterService.Live, NodeContext.layer);
|
|
17
18
|
cli(process.argv).pipe(Effect.provide(MainLayer), NodeRuntime.runMain);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import * as Command from
|
|
2
|
-
import * as Options from
|
|
3
|
-
import * as
|
|
4
|
-
import * as
|
|
5
|
-
import * as
|
|
6
|
-
import * as Console from
|
|
7
|
-
import * as Effect from
|
|
8
|
-
import * as Match from
|
|
1
|
+
import * as Command from "@effect/cli/Command";
|
|
2
|
+
import * as Options from "@effect/cli/Options";
|
|
3
|
+
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
|
|
4
|
+
import * as NodePath from "@effect/platform-node/NodePath";
|
|
5
|
+
import * as FileSystem from "@effect/platform/FileSystem";
|
|
6
|
+
import * as Console from "effect/Console";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Match from "effect/Match";
|
|
9
9
|
// Options for the init command
|
|
10
|
-
const outputOption = Options.text(
|
|
10
|
+
const outputOption = Options.text("output").pipe(Options.withAlias("o"), Options.withDescription("Output directory path for generated files"), Options.withDefault("src"));
|
|
11
11
|
// Check if file exists
|
|
12
12
|
const fileExists = (path) => Effect.gen(function* () {
|
|
13
13
|
const fs = yield* FileSystem.FileSystem;
|
|
@@ -26,74 +26,63 @@ const writeFileContent = (path, content) => Effect.gen(function* () {
|
|
|
26
26
|
});
|
|
27
27
|
// Detect package manager using pattern matching
|
|
28
28
|
const detectPackageManager = () => Effect.gen(function* () {
|
|
29
|
-
const pnpmExists = yield* fileExists(
|
|
30
|
-
const bunExists = yield* fileExists(
|
|
31
|
-
const npmExists = yield* fileExists(
|
|
29
|
+
const pnpmExists = yield* fileExists("pnpm-lock.yaml");
|
|
30
|
+
const bunExists = yield* fileExists("bun.lockb");
|
|
31
|
+
const npmExists = yield* fileExists("package-lock.json");
|
|
32
32
|
// Create a tuple to match against
|
|
33
33
|
const lockFiles = [pnpmExists, bunExists, npmExists];
|
|
34
|
-
return Match.value(lockFiles).pipe(Match.when([true, false, false], () =>
|
|
34
|
+
return Match.value(lockFiles).pipe(Match.when([true, false, false], () => "pnpm"), Match.when([false, true, false], () => "bun"), Match.when([false, false, true], () => "npm"), Match.orElse(() => "npm"));
|
|
35
35
|
});
|
|
36
36
|
// Check if Prisma is already initialized
|
|
37
37
|
const checkPrismaSetup = () => Effect.gen(function* () {
|
|
38
|
-
const schemaExists = yield* fileExists(
|
|
38
|
+
const schemaExists = yield* fileExists("prisma/schema.prisma");
|
|
39
39
|
if (!schemaExists) {
|
|
40
40
|
const packageManager = yield* detectPackageManager();
|
|
41
|
-
yield* Console.log(
|
|
42
|
-
yield* Console.log(
|
|
43
|
-
yield* Console.log(
|
|
44
|
-
const initCommand = Match.value(packageManager).pipe(Match.when(
|
|
41
|
+
yield* Console.log("❌ Prisma is not initialized in this project.");
|
|
42
|
+
yield* Console.log("");
|
|
43
|
+
yield* Console.log("Please run the following command first:");
|
|
44
|
+
const initCommand = Match.value(packageManager).pipe(Match.when("pnpm", () => "pnpm dlx prisma init"), Match.when("bun", () => "bunx prisma init"), Match.when("npm", () => "npx prisma init"), Match.exhaustive);
|
|
45
45
|
yield* Console.log(` ${initCommand}`);
|
|
46
|
-
yield* Console.log(
|
|
47
|
-
yield* Console.log(
|
|
48
|
-
yield* Console.log(
|
|
49
|
-
yield* Effect.fail(new Error(
|
|
46
|
+
yield* Console.log("");
|
|
47
|
+
yield* Console.log("For more information, visit:");
|
|
48
|
+
yield* Console.log(" https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/relational-databases-typescript-prismaPostgres");
|
|
49
|
+
yield* Effect.fail(new Error("Prisma not initialized"));
|
|
50
50
|
}
|
|
51
51
|
});
|
|
52
52
|
// Initialize Prisma schema logic
|
|
53
53
|
const initializePrismaSchema = (options) => Effect.gen(function* () {
|
|
54
|
-
yield* Console.log(
|
|
54
|
+
yield* Console.log("🔧 Configuring Prisma schema with Effect generators...");
|
|
55
55
|
yield* Console.log(`📁 Output path: ${options.output}`);
|
|
56
56
|
// Check if Prisma is already set up
|
|
57
57
|
yield* checkPrismaSetup();
|
|
58
|
-
const schemaPath =
|
|
59
|
-
yield* Console.log(
|
|
58
|
+
const schemaPath = "prisma/schema.prisma";
|
|
59
|
+
yield* Console.log("📄 Schema file already exists.");
|
|
60
60
|
// Read existing content and check if it has our generators
|
|
61
61
|
const existingContent = yield* readFileContent(schemaPath);
|
|
62
|
-
if (existingContent.includes('
|
|
63
|
-
yield* Console.log(
|
|
62
|
+
if (existingContent.includes('provider = "effect-prisma"')) {
|
|
63
|
+
yield* Console.log("✅ Effect generators already configured!");
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
// Add our generators to existing schema
|
|
67
|
-
yield* Console.log(
|
|
67
|
+
yield* Console.log("🔧 Adding Effect generators to existing schema...");
|
|
68
68
|
const updatedContent = existingContent +
|
|
69
69
|
`
|
|
70
70
|
|
|
71
71
|
// Effect generators added by @effectify/prisma
|
|
72
|
-
generator effectServices {
|
|
73
|
-
provider = "@effectify/prisma generate-effect"
|
|
74
|
-
output = "../${options.output}/generated/effect-prisma"
|
|
75
|
-
}
|
|
76
|
-
|
|
77
72
|
generator effect {
|
|
78
|
-
provider = "
|
|
79
|
-
output = "../${options.output}/generated/effect
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
generator sqlSchema {
|
|
83
|
-
provider = "@effectify/prisma generate-sql-schema"
|
|
84
|
-
output = "../${options.output}/generated"
|
|
73
|
+
provider = "effect-prisma"
|
|
74
|
+
output = "../${options.output}/generated/effect"
|
|
85
75
|
}
|
|
86
76
|
`;
|
|
87
77
|
yield* writeFileContent(schemaPath, updatedContent);
|
|
88
|
-
yield* Console.log(
|
|
89
|
-
yield* Console.log(
|
|
90
|
-
yield* Console.log(
|
|
91
|
-
yield* Console.log(
|
|
92
|
-
yield* Console.log(
|
|
93
|
-
yield* Console.log(' 3. Run: @effectify/prisma prisma generate-sql-schema');
|
|
78
|
+
yield* Console.log("✅ Effect generators added to existing schema!");
|
|
79
|
+
yield* Console.log("🎉 Prisma schema initialization completed!");
|
|
80
|
+
yield* Console.log("💡 Next steps:");
|
|
81
|
+
yield* Console.log(" 1. Set your DATABASE_URL environment variable");
|
|
82
|
+
yield* Console.log(" 2. Run: npx prisma generate");
|
|
94
83
|
yield* Effect.sync(() => process.exit(0));
|
|
95
84
|
});
|
|
96
|
-
export const initCommand = Command.make(
|
|
85
|
+
export const initCommand = Command.make("init", {
|
|
97
86
|
output: outputOption,
|
|
98
87
|
}, ({ output }) => initializePrismaSchema({
|
|
99
88
|
output,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as Command from
|
|
2
|
-
import type * as NodeContext from
|
|
3
|
-
import { GeneratorService } from
|
|
4
|
-
import type { RenderService } from
|
|
1
|
+
import * as Command from "@effect/cli/Command";
|
|
2
|
+
import type * as NodeContext from "@effect/platform-node/NodeContext";
|
|
3
|
+
import { GeneratorService } from "../services/generator-service.js";
|
|
4
|
+
import type { RenderService } from "../services/render-service.js";
|
|
5
5
|
export declare const prismaCommand: Command.Command<"prisma", RenderService | GeneratorService | NodeContext.NodeContext, never, {}>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import * as Command from
|
|
2
|
-
import generatorHelper from
|
|
3
|
-
import * as Deferred from
|
|
4
|
-
import * as Effect from
|
|
5
|
-
import * as Layer from
|
|
6
|
-
import * as Runtime from
|
|
7
|
-
import * as Stream from
|
|
8
|
-
import { GeneratorContext } from
|
|
9
|
-
import { GeneratorService } from
|
|
10
|
-
export const prismaCommand = Command.make(
|
|
1
|
+
import * as Command from "@effect/cli/Command";
|
|
2
|
+
import generatorHelper from "@prisma/generator-helper";
|
|
3
|
+
import * as Deferred from "effect/Deferred";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
6
|
+
import * as Runtime from "effect/Runtime";
|
|
7
|
+
import * as Stream from "effect/Stream";
|
|
8
|
+
import { GeneratorContext } from "../services/generator-context.js";
|
|
9
|
+
import { GeneratorService } from "../services/generator-service.js";
|
|
10
|
+
export const prismaCommand = Command.make("prisma", {}, () => Effect.gen(function* () {
|
|
11
11
|
const generator = yield* GeneratorService;
|
|
12
12
|
const runtime = yield* Effect.runtime();
|
|
13
13
|
const run = Runtime.runPromise(runtime);
|
|
@@ -15,8 +15,8 @@ export const prismaCommand = Command.make('prisma', {}, () => Effect.gen(functio
|
|
|
15
15
|
generatorHelper.generatorHandler({
|
|
16
16
|
onManifest() {
|
|
17
17
|
return {
|
|
18
|
-
defaultOutput:
|
|
19
|
-
prettyName:
|
|
18
|
+
defaultOutput: "../generated/effect",
|
|
19
|
+
prettyName: "Prisma Effect Generator",
|
|
20
20
|
requiresEngines: [],
|
|
21
21
|
};
|
|
22
22
|
},
|
|
@@ -1,49 +1,48 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
|
-
|
|
3
|
-
import { execSync } from
|
|
4
|
-
import fs from
|
|
5
|
-
import path from
|
|
6
|
-
import gh from '@prisma/generator-helper';
|
|
2
|
+
import gh from "@prisma/generator-helper";
|
|
3
|
+
import { execSync } from "node:child_process";
|
|
4
|
+
import fs from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
7
6
|
const header = `-- This file was generated by sql-schema-generator, do not edit manually.
|
|
8
7
|
-- Generated at: ${new Date().toISOString()}
|
|
9
8
|
|
|
10
9
|
`;
|
|
11
10
|
export async function generateSqlSchema(options) {
|
|
12
|
-
const outputDir = options.generator.output?.value ||
|
|
13
|
-
const datasourceUrl =
|
|
11
|
+
const outputDir = options.generator.output?.value || "../generated";
|
|
12
|
+
const datasourceUrl = "prisma/dev.db";
|
|
14
13
|
if (!datasourceUrl) {
|
|
15
|
-
throw new Error(
|
|
14
|
+
throw new Error("No datasource URL found");
|
|
16
15
|
}
|
|
17
16
|
await fs.mkdir(outputDir, { recursive: true });
|
|
18
17
|
try {
|
|
19
18
|
let dbPath = datasourceUrl;
|
|
20
|
-
if (dbPath.startsWith(
|
|
21
|
-
dbPath = dbPath.replace(
|
|
19
|
+
if (dbPath.startsWith("file:")) {
|
|
20
|
+
dbPath = dbPath.replace("file:", "");
|
|
22
21
|
}
|
|
23
22
|
if (!path.isAbsolute(dbPath)) {
|
|
24
23
|
dbPath = path.resolve(process.cwd(), dbPath);
|
|
25
24
|
}
|
|
26
|
-
const schemaOutput = execSync(`sqlite3 "${dbPath}" ".schema"`, { encoding:
|
|
27
|
-
const outputPath = path.join(outputDir,
|
|
25
|
+
const schemaOutput = execSync(`sqlite3 "${dbPath}" ".schema"`, { encoding: "utf8" });
|
|
26
|
+
const outputPath = path.join(outputDir, "schema.sql");
|
|
28
27
|
await fs.writeFile(outputPath, header + schemaOutput);
|
|
29
28
|
}
|
|
30
29
|
catch (error) {
|
|
31
|
-
console.error(
|
|
30
|
+
console.error("❌ Failed to generate schema dump:", error);
|
|
32
31
|
throw error;
|
|
33
32
|
}
|
|
34
33
|
}
|
|
35
34
|
gh.generatorHandler({
|
|
36
35
|
onManifest() {
|
|
37
36
|
return {
|
|
38
|
-
defaultOutput:
|
|
39
|
-
prettyName:
|
|
37
|
+
defaultOutput: "../generated",
|
|
38
|
+
prettyName: "SQL Schema Generator",
|
|
40
39
|
requiresEngines: [],
|
|
41
40
|
};
|
|
42
41
|
},
|
|
43
42
|
async onGenerate(options) {
|
|
44
43
|
await generateSqlSchema({
|
|
45
44
|
generator: {
|
|
46
|
-
output: options?.generator?.output ? { value: options.generator.output.value ||
|
|
45
|
+
output: options?.generator?.output ? { value: options.generator.output.value || "" } : undefined,
|
|
47
46
|
},
|
|
48
47
|
});
|
|
49
48
|
},
|