Skip to content

Commit

Permalink
fix: datasource and generator blocks generation
Browse files Browse the repository at this point in the history
  • Loading branch information
techsavvyash committed May 12, 2024
1 parent 6f8f060 commit 09fed41
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function generateIfNoSchema(
recursive: true,
});
// fs.writeFileSync(prismaFilePath, result);
await formatValidateAndWrite(schemaString, prismaFilePath);
await formatValidateAndWrite(result, prismaFilePath);
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export async function formatValidateAndWrite(
if (fs.existsSync(filePath + ".bak")) fs.unlinkSync(filePath + ".bak");
} catch (err) {
console.log("Error while running prisma format and validate: ", err);
fs.unlinkSync(filePath);
if (fileExists) {
fs.unlinkSync(filePath);
fs.renameSync(filePath + ".bak", filePath);
}
throw new Error(
Expand Down
18 changes: 18 additions & 0 deletions test/schema-generation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ describe("tests for schema generation", () => {
expect(models).toContain("Comment");
});
});

it("should generate a schema.prisma with dummy_id for a schema with no id and only unique field being optional", async () => {
const fileContent = readJsonFile("./test/schemas/optional_unique.json");
await generatePrismaSchemaFile(fileContent).then((migrationModels) => {
expect(migrationModels).toBeDefined();
expect(fs.existsSync("./prisma/schema.prisma")).toBe(true);
const schemaPrismaContent = fs.readFileSync(
"./prisma/schema.prisma",
"utf8"
);
expect(schemaPrismaContent).toBeDefined();
const models = parsePrismaSchemaModels(schemaPrismaContent);
expect(models).toContain("User");
expect(models).toContain("Post");
expect(models).toContain("Comment");
});
});

it("should generate a schema.prisma for a basic file with different casing for types ", async () => {
const fileContent = readJsonFile("./test/schemas/casing.json");
await generatePrismaSchemaFile(fileContent).then((migrationModels) => {
Expand Down
67 changes: 0 additions & 67 deletions test/schemas/case.json

This file was deleted.

45 changes: 45 additions & 0 deletions test/schemas/optional_unique.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"schema": [
{
"schemaName": "questions01",
"fields": [
{
"fieldName": "question",
"type": "String",
"description": "",
"maxLength": 10,
"isNullable": true,
"isUnique": false,
"isVectorEmbed": true,
"isUuid": true,
"embeddingAlgo": "text-embedding-ada-002"
},
{
"fieldName": "answer",
"type": "String",
"description": "",
"maxLength": "",
"isNullable": true,
"isId": false,
"isUuid": false,
"isUnique": true,
"isVectorEmbed": false
}
],
"description": "schema for questions and answers"
}
],
"dataSource": {
"name": "db",
"provider": "postgresql",
"url": {
"fromEnv": "DATABASE_URL"
},
"extensions": ["vector", "pg_trgm"]
},
"generator": {
"provider": "prisma-client-js",
"generatorName": "client",
"previewFeatures": ["postgresqlExtensions", "views"]
}
}

0 comments on commit 09fed41

Please sign in to comment.