Skip to content

Commit 2cbc14d

Browse files
authored
Merge pull request #15 from techsavvyash/documentation
Documentation #11
2 parents 27804aa + 7e46648 commit 2cbc14d

File tree

5 files changed

+143
-13
lines changed

5 files changed

+143
-13
lines changed

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
# dynamo-prisma
2-
Create Prisma Models Dynamically
2+
3+
Generate Prisma schema dynamically using json file.
4+
5+
## Installation
6+
7+
### using git-clone
8+
9+
```bash
10+
git clone https://github.com/techsavvyash/dynamo-prisma.git
11+
npm link
12+
```
13+
14+
### using npm
15+
16+
```bash
17+
npm install @techsavvyash/dynamo-prisma
18+
```
19+
20+
## Usage
21+
22+
Visit [dynamo-prisma function](./docs/functions.md) for more details.
23+
24+
for info on schema you should follow, visit: [JSON Schema](./docs/schema.md)
25+
26+
## License
27+
28+
[MIT License](./LICENSE)

docs/functions.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Exported Function in dynamo Prisma
2+
3+
## Installation
4+
5+
Follow guide given in [README.md](../README.md)
6+
7+
- #### generateSchemaFromJson(jsonData, prismaFilePath);
8+
9+
Argument of function:
10+
11+
- `jsonData: Schema` - JSON as per schema model which is defined in [JSON Schema](./schema.md)
12+
13+
- OPTIONAL `prismaFilePath?: string` - Path of the prisma schema file. If file is present, it will append the new schema to the existing schema. If file is not present, it will create a new schema file. Default value is `./prisma/schema.prisma`
14+
15+
Returns: {status: boolean, message: string, error: string}
16+
17+
- #### GenerateSchemaFile(filePath);
18+
19+
Argument of function:
20+
21+
- `filePath: String` - file path of the json file.
22+
23+
Returns: {status: boolean, message: string, error: string}

docs/schema.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# dynamo-prisma schema:
2+
3+
example json file: [JSON DEMO](../demo.json)
4+
5+
```yaml
6+
Schema:
7+
schema:
8+
- schemaName: string
9+
fields:
10+
- fieldName: string
11+
type: string
12+
description: string
13+
maxLength?: number | null
14+
nullable: boolean
15+
unique: boolean
16+
default?: string | null
17+
autoincrement?: boolean
18+
uuid?: boolean
19+
isId?: boolean
20+
vectorEmbed?: boolean
21+
embeddingAlgo?: string
22+
isForeignKey?: boolean
23+
isList?: boolean
24+
description: string
25+
dataSource:
26+
name: string
27+
provider: DataSourceProvider
28+
url:
29+
url: string
30+
fromEnv: DataSourceURLEnv
31+
enum:
32+
- name: string
33+
values: [string]
34+
generator:
35+
generatorName: string
36+
provider: string
37+
output: string
38+
binaryTargets: [string]
39+
```

src/fileExists.ts

+52-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export function generateSchemaFromJson(
1111
prismaFilePath: string = "./prisma/schema.prisma"
1212
) {
1313
if (!verifyFilePath(prismaFilePath)) {
14-
throw new Error("File cannot have '-' in between its name");
14+
return {
15+
status: false,
16+
message: "File cannot have '-' in between its name",
17+
};
1518
}
1619
if (fs.existsSync(prismaFilePath)) {
1720
console.log("File exists");
@@ -26,11 +29,13 @@ export function generateSchemaFromJson(
2629
}
2730

2831
JsonChecks(jsonData, models);
29-
generateSchemaWhenFilePresent(jsonData, prismaFilePath);
32+
const output = generateSchemaWhenFilePresent(jsonData, prismaFilePath);
33+
return output;
3034
} else {
3135
console.log("Applying Checks....");
3236
JsonChecks(jsonData, []);
33-
generateIfNoSchema(jsonData);
37+
const output = generateIfNoSchema(jsonData);
38+
return output;
3439
}
3540
}
3641

@@ -85,7 +90,9 @@ export function readJsonFile(filePath: string): Promise<Schema> {
8590
});
8691
}
8792

88-
export async function generateIfNoSchema(jsonData: Schema): Promise<void> {
93+
export async function generateIfNoSchema(
94+
jsonData: Schema
95+
): Promise<{ status: boolean; message?: string; error?: string }> {
8996
const models: any[] = createModels(jsonData.schema);
9097
console.log("Model generated");
9198
// console.log(models);
@@ -138,7 +145,11 @@ export async function generateIfNoSchema(jsonData: Schema): Promise<void> {
138145
fs.mkdirSync("./prisma", { recursive: true });
139146
fs.writeFile("./prisma/schema.prisma", result, (err) => {
140147
if (err) {
141-
console.error("Error writing Prisma schema:", err);
148+
return {
149+
status: false,
150+
message: "Error writing Prisma schema",
151+
error: err,
152+
};
142153
} else {
143154
console.log("Prisma schema generated successfully!");
144155
let migrateModels: string[] = jsonData.schema.map(
@@ -147,13 +158,21 @@ export async function generateIfNoSchema(jsonData: Schema): Promise<void> {
147158
validateAndMigrate(migrateModels);
148159
}
149160
});
161+
162+
return {
163+
status: true,
164+
message: "Prisma schema generated successfully!",
165+
};
150166
}
151167
export async function generateSchemaWhenFilePresent(
152168
jsonData: Schema,
153169
prismaFilePath: string
154170
) {
155171
if (!verifyFilePath(prismaFilePath)) {
156-
throw new Error("File cannot have '-' in between its name");
172+
return {
173+
status: false,
174+
message: "File cannot have '-' in between its name",
175+
};
157176
}
158177
const FileData = fs.readFileSync(prismaFilePath, "utf8");
159178
const models: any[] = createModels(jsonData.schema);
@@ -174,7 +193,11 @@ export async function generateSchemaWhenFilePresent(
174193
fs.mkdirSync("./prisma", { recursive: true });
175194
fs.writeFile("./prisma/schema.prisma", result, (err) => {
176195
if (err) {
177-
console.error("Error writing Prisma schema:", err);
196+
return {
197+
status: false,
198+
message: "Error writing Prisma schema",
199+
error: err,
200+
};
178201
} else {
179202
console.log("Prisma schema generated successfully!");
180203
let migrateModels: string[] = jsonData.schema.map(
@@ -183,16 +206,24 @@ export async function generateSchemaWhenFilePresent(
183206
validateAndMigrate(migrateModels);
184207
}
185208
});
209+
210+
return {
211+
status: true,
212+
message: "Prisma schema generated successfully!",
213+
};
186214
}
187215

188216
function validateAndMigrate(migrateModels: string[]) {
189217
// TODO: DONE RUN: npx prisma validate
190218
exec("npx prisma validate", (error, stdout, stderr) => {
191219
if (error) {
192-
console.error("Error executing 'npx prisma validate':", error);
193-
return;
220+
return {
221+
status: false,
222+
message: "Error executing 'npx prisma validate'",
223+
error: error,
224+
};
194225
}
195-
console.log("commands executed successfully", stdout);
226+
return { status: true, message: "commands executed successfully", stdout };
196227
});
197228

198229
// TODO: DONE RUN: prisma migrate dev
@@ -203,7 +234,18 @@ function validateAndMigrate(migrateModels: string[]) {
203234
stdio: "inherit",
204235
}
205236
);
237+
shell.on("error", (err) => {
238+
return {
239+
status: false,
240+
message: "Error executing 'npx prisma migrate dev'",
241+
error: err,
242+
};
243+
});
206244
shell.on("close", (code) => {
207245
console.log("[shell] terminated:", code);
208246
});
247+
return {
248+
status: true,
249+
message: "commands executed successfully",
250+
};
209251
}

test/schemaGenerator.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Schema, createFields } from "../src/schemaGenerator";
2-
import * as fs from "fs";
1+
// import { Schema, createFields } from "../src/schemaGenerator";
2+
// import * as fs from "fs";
33

44
// describe("createFields", () => {
55
// it("should create an array of fields based on the input", () => {

0 commit comments

Comments
 (0)