Skip to content

Commit

Permalink
Merge pull request #34 from techsavvyash/fix/refactor
Browse files Browse the repository at this point in the history
feat: fix some bugs and add github actions
  • Loading branch information
techsavvyash authored Apr 19, 2024
2 parents fb3fc68 + 5d9a1f1 commit 6114079
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 53 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Generate Prisma schema dynamically using json file.
## Installation

```bash
npm install @techsavvyash/dynamo-prisma@0.0.1
npm install @techsavvyash/dynamo-prisma
```


Expand Down
27 changes: 26 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ services:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}

minio:
image: minio/minio
ports:
- "9000:9000"
- "9001:9001"
volumes:
- minio-data:/data
environment:
MINIO_ROOT_USER: ${MINIO_USERNAME}
MINIO_ROOT_PASSWORD: ${MINIO_PASSWORD}
command: server --console-address ":9001" /data

createbuckets:
image: minio/mc
depends_on:
- minio
entrypoint: >
/bin/sh -c "
/usr/bin/mc config host add myminio http://minio:9000 ${MINIO_USERNAME} ${MINIO_PASSWORD};
/usr/bin/mc rm -r --force myminio/${MINIO_BUCKETNAME};
/usr/bin/mc mb myminio/${MINIO_BUCKETNAME};
/usr/bin/mc anonymous set public myminio/${MINIO_BUCKETNAME};
exit 0;
"
volumes:
dataset-db:
minio-data:
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "@techsavvyash/dynamo-prisma",
"version": "0.0.1",
"version": "0.0.2",
"description": "Create Prisma Models Dynamically",
"main": "src/index.ts",
"scripts": {
"test": "jest",
"cleanup": "rm -r ./prisma"
},
"author": "Himanshu, Sooraj, Yash",
Expand All @@ -17,7 +18,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"jest": "^29.5.0",
"jest": "^29.7.0",
"prisma": "5.12.1",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
Expand Down
5 changes: 3 additions & 2 deletions src/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export function checkJSON(
const newModelObjects = [];
const models = jsonData.schema.map((model) => {
if (!existingData.models.includes(model.schemaName)) {
newModelObjects.push(model);
return model.schemaName;
} else {
console.warn(
`Model ${model.schemaName} is already defined in the schema, please use a different name, skipping this one.`
);
newModelObjects.push(model);
return model.schemaName;
}
});
const newEnums = [];
Expand Down
22 changes: 20 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ export function runDBPull() {
exec("npx prisma db pull", (error, stdout, stderr) => {
if (error) {
console.error('Error executing "npx prisma db pull"', error);
return;
throw new Error(
JSON.stringify({
error: true,
message: `Error while performing npx prisma db pull ${error}`,
})
);
}
console.log("Prisma migrations fetched from the database.");
console.log(
"🚀 Prisma schema has been populated with old tables fetched from the database"
);
return {
status: true,
message:
"Prisma schema has been populated with old tables fetched from the database.",
};
});
}

Expand Down Expand Up @@ -57,6 +69,12 @@ export function validateAndMigrate(migrateModels: string[]) {
})
.catch((error) => {
console.error("An error occurred:", error);
throw new Error(
JSON.stringify({
error: true,
message: `Error validating and migrating Prisma schema: ${error}`,
})
);
});

return {
Expand Down
77 changes: 37 additions & 40 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { createModels } from "./dsl-helper";
import { checkJSON } from "./checks";
import { createSchema, print } from "prisma-schema-dsl";
import { Schema } from "./types/dynamoPrisma.types";
import { parseExistingEnums, parseExistingModels } from "./utils/utils";
import { parseExistingEnums, parsePrismaSchemaModels } from "./utils/utils";
import { validateAndMigrate } from "./commands";

export async function generateIfNoSchema(jsonData: Schema): Promise<string[]> {
export async function generateIfNoSchema(
jsonData: Schema,
prismaFilePath: string
): Promise<string[]> {
if (!jsonData.dataSource || !jsonData.generator) {
throw new Error(
JSON.stringify({
Expand Down Expand Up @@ -38,22 +41,22 @@ export async function generateIfNoSchema(jsonData: Schema): Promise<string[]> {
const schemaString = await print(schema);
result = Generator + "\n" + DataSource + "\n" + schemaString;

console.warn("schema generated");
const migrateModels: string[] = [];
fs.mkdirSync("./prisma", { recursive: true });
fs.writeFile("./prisma/schema.prisma", result, (err) => {
if (err) {
return {
status: false,
try {
fs.mkdirSync(prismaFilePath.split("/schema.prisma")[0], {
recursive: true,
});
fs.writeFileSync(prismaFilePath, result);
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
} catch (err) {
throw new Error(
JSON.stringify({
error: true,
message: "Error writing Prisma schema",
error: err,
};
} else {
console.log("Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
// validateAndMigrate(migrateModels);
}
});
})
);
}

return migrateModels;
}
Expand All @@ -68,28 +71,22 @@ export async function generateSchemaWhenFilePresent(
const Enum = jsonData.enum ? jsonData.enum! : [];
const schema = createSchema(models, Enum, undefined, undefined);
const schemaString = await print(schema);
fs.appendFileSync(prismaFilePath, "\n\n" + schemaString, "utf8");
const migrateModels: string[] = [];
fs.mkdirSync("./prisma", { recursive: true });
fs.writeFile(
"./prisma/schema.prisma",
fs.readFileSync(prismaFilePath, "utf8"),
(err) => {
if (err) {
throw new Error(
JSON.stringify({
status: false,
message: "Error writing Prisma schema",
error: err,
})
);
} else {
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
// validateAndMigrate(migrateModels);
}
}
);

try {
fs.appendFileSync(prismaFilePath, "\n\n" + schemaString, "utf8");
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
} catch (err) {
throw new Error(
JSON.stringify({
status: false,
message: "Error writing Prisma schema",
error: err,
})
);
}

return migrateModels;
}

Expand All @@ -103,11 +100,11 @@ export async function generatePrismaSchemaFile(
jsonData: Schema,
prismaFilePath: string = "./prisma/schema.prisma",
failOnWarn: boolean = false
) {
): Promise<string[]> {
const prismaFileExists = fs.existsSync(prismaFilePath);

const models = prismaFileExists
? parseExistingModels(fs.readFileSync(prismaFilePath, "utf8"))
? parsePrismaSchemaModels(fs.readFileSync(prismaFilePath, "utf8"))
: [];
const enums = prismaFileExists
? parseExistingEnums(fs.readFileSync(prismaFilePath, "utf8"))
Expand All @@ -119,6 +116,6 @@ export async function generatePrismaSchemaFile(
console.log("📝 Prisma Schema file exists.");
return await generateSchemaWhenFilePresent(jsonData, prismaFilePath);
} else {
return await generateIfNoSchema(jsonData);
return await generateIfNoSchema(jsonData, prismaFilePath);
}
}
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function checkIllegalCombinationOfFieldAttributes(
}

// TODO: Change this to use the `pp package`(https://github.com/techsavvyash/pp) package which uses internal prisma DMMF
export function parseExistingModels(fileContent: string) {
export function parsePrismaSchemaModels(fileContent: string) {
const modelRegex = /model\s+(\w+)\s+{/g;
const models: string[] = [];
let match;
Expand Down
3 changes: 2 additions & 1 deletion test/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @description This is just for manual testing
* run: npx ts-node src/cli.ts ./test/schemas/no_unique.json
* run: npx ts-node test/cli.ts ./test/schemas/no_unique.json
*/
import { generatePrismaSchemaFile } from "../src/schemaGenerator";
import { readJsonFile } from "../src/utils/utils";
Expand All @@ -16,6 +16,7 @@ export async function main(argv: string[]) {
const filePath = argv[2];
const data = readJsonFile(filePath);
const migrateModels: any = await generatePrismaSchemaFile(data);
console.log("migration models: ", migrateModels);
validateAndMigrate(migrateModels);

return filePath;
Expand Down
Loading

0 comments on commit 6114079

Please sign in to comment.