Skip to content

Commit

Permalink
Merge pull request #39 from techsavvyash/fix/edge-cases
Browse files Browse the repository at this point in the history
Fix/edge cases
  • Loading branch information
techsavvyash authored May 12, 2024
2 parents 5417678 + 09fed41 commit 12daf2f
Show file tree
Hide file tree
Showing 18 changed files with 597 additions and 155 deletions.
48 changes: 24 additions & 24 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +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
# 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;
"
# 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:
# minio-data:
10 changes: 5 additions & 5 deletions docs/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Schema:
type: string
description: string
maxLength?: number | null
nullable: boolean
unique: boolean
isNullable: boolean
isUnique: boolean
default?: string | null
autoincrement?: boolean
uuid?: boolean
isAutoIncrement?: boolean
isUuid?: boolean
isId?: boolean
vectorEmbed?: boolean
isVectorEmbed?: boolean
embeddingAlgo?: string
isForeignKey?: boolean
isList?: boolean
Expand Down
18 changes: 16 additions & 2 deletions src/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function checkJSON(
failOnWarn = false
): Schema {
const newModelObjects = [];

const models = jsonData.schema.map((model) => {
if (!existingData.models.includes(model.schemaName)) {
newModelObjects.push(model);
Expand Down Expand Up @@ -89,9 +90,22 @@ function sanitizeJSONSchema(
for (const model of jsonData.schema) {
model.schemaName = fixDashesAndSpaces(model.schemaName);
let isPrimaryPresent = false;
let isUnqiueAndNullable = false;
let isUniqueAndNotNullable = false;
const uniqueWithNullables = [];

for (const field of model.fields) {
field.fieldName = fixDashesAndSpaces(field.fieldName);
isPrimaryPresent = isPrimaryPresent || field.isId || field.unique;
isPrimaryPresent = isPrimaryPresent || field.isId;

if (field.isNullable && field.isUnique) {
isUnqiueAndNullable = true;
uniqueWithNullables.push(field.fieldName);
} else if (!field.isNullable && field.isUnique) {
isUniqueAndNotNullable = true;
isPrimaryPresent = true;
}

checkIllegalCombinationOfFieldAttributes(
field,
definedTypes,
Expand All @@ -101,7 +115,7 @@ function sanitizeJSONSchema(

if (!isPrimaryPresent) {
console.warn(
`Model ${model.schemaName} does not have a primary key, adding a default 'dummy_id_<random>' field with "unique" constraint`
`Model ${model.schemaName} does not have a primary key or a non optional/nullable unique field adding a default 'dummy_id' field with "unique" constraint`
);
if (failOnWarn) process.exit(1);
else {
Expand Down
100 changes: 93 additions & 7 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function runCommand(command, args) {
});
}

export function runDBPull() {
export function runPrismaDBPull() {
exec("npx prisma db pull", (error, stdout, stderr) => {
if (error) {
console.error('Error executing "npx prisma db pull"', error);
Expand All @@ -45,9 +45,96 @@ export function runDBPull() {
});
}

export function runPrismaFormat() {
return runCommand("npx", ["prisma", "format"])
.then(() => {
console.log("Prisma schema has been formatted successfully.");
return {
status: true,
message: "Prisma schema has been formatted successfully.",
};
})
.catch((error) => {
console.error("An error occurred:", error);
throw new Error(
JSON.stringify({
error: true,
message: `Error formatting Prisma schema: ${error}`,
})
);
});
}

export function runPrismaValidate() {
return runCommand("npx", ["prisma", "validate"])
.then(() => {
console.log("Prisma schema is valid.");
return {
status: true,
message: "Prisma schema is valid.",
};
})
.catch((error) => {
console.error("An error occurred:", error);
throw new Error(
JSON.stringify({
error: true,
message: `Error validating Prisma schema: ${error}`,
})
);
});
}

export function createMigrations(migrateModels) {
return runCommand("npx", [
"prisma",
"migrate",
"dev",
"--name",
`${Date.now()}_dynamo_prisma_` + migrateModels.join("_"),
"--create-only",
])
.then(() => {
console.log("Migrations have been generated successfully.");
return {
status: true,
message: "Migrations have been generated successfully.",
};
})
.catch((error) => {
console.error("An error occurred:", error);
throw new Error(
JSON.stringify({
error: true,
message: `Error applying migrations: ${error}`,
})
);
});
}

export function applyMigrations() {
return runCommand("npx", ["prisma", "migrate", "deploy"])
.then(() => {
console.log("Migrations have been successfully deployed.");
return {
status: true,
message: "Migrations have been successfully deployed.",
};
})
.catch((error) => {
console.error("An error occurred:", error);
throw new Error(
JSON.stringify({
error: true,
message: `Error applying migrations: ${error}`,
})
);
});
}

export function validateAndMigrate(migrateModels: string[]) {
console.log("migrateModels: ", migrateModels);
runCommand("npx", ["prisma", "validate"])
return runCommand("npx", ["prisma", "validate"])
.then(() => {
console.log("Prisma schema is valid.");

Expand All @@ -66,6 +153,10 @@ export function validateAndMigrate(migrateModels: string[]) {
})
.then(() => {
console.log("Migrations have been successfully deployed.");
return {
status: true,
message: "migrations applied and generated successfully",
};
})
.catch((error) => {
console.error("An error occurred:", error);
Expand All @@ -76,9 +167,4 @@ export function validateAndMigrate(migrateModels: string[]) {
})
);
});

return {
status: true,
message: "migrations applied and generated successfully",
};
}
12 changes: 6 additions & 6 deletions src/dsl-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export function createFields(fields: Field[]): any[] {
fieldData.fieldName,
fieldData.type as ScalarType,
fieldData.isList || undefined, //isList boolean | undefined
!fieldData.nullable || false, //isRequired boolean | undefined
fieldData.isId ? fieldData.isId : fieldData.unique || false,
!fieldData.isNullable || false, //isRequired boolean | undefined
fieldData.isId ? fieldData.isId : fieldData.isUnique || false,
fieldData.isId || false,
undefined, // isUpdatedAt
fieldData.isId && fieldData.autoincrement
fieldData.isId && fieldData.isAutoIncrement
? { callee: AUTO_INCREMENT }
: fieldData.isId && fieldData.uuid
: fieldData.isId && fieldData.isUuid
? { callee: UUID }
: fieldData.default || undefined, // default values SaclarFeildDefault | undefined
undefined, // documentation string | undefined
Expand All @@ -61,7 +61,7 @@ export function createFields(fields: Field[]): any[] {
)
);

if (fieldData.vectorEmbed) {
if (fieldData.isVectorEmbed) {
if (
Object.keys(EMBEDDING_ALGO_SIZE).includes(fieldData.embeddingAlgo) ===
false
Expand Down Expand Up @@ -89,7 +89,7 @@ export function createFields(fields: Field[]): any[] {
`${fieldData.fieldName}Embedding`,
`Unsupported("vector(${
EMBEDDING_ALGO_SIZE[fieldData.embeddingAlgo]
})")` as ScalarType,
})")?` as ScalarType,
false,
true,
false,
Expand Down
12 changes: 9 additions & 3 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { createModels } from "./dsl-helper";
import { checkJSON } from "./checks";
import { createSchema, print } from "prisma-schema-dsl";
import { Schema } from "./types/dynamoPrisma.types";
import { parseExistingEnums, parsePrismaSchemaModels } from "./utils/utils";
import {
formatValidateAndWrite,
parseExistingEnums,
parsePrismaSchemaModels,
} from "./utils/utils";
import { validateAndMigrate } from "./commands";

export async function generateIfNoSchema(
Expand Down Expand Up @@ -46,7 +50,8 @@ export async function generateIfNoSchema(
fs.mkdirSync(prismaFilePath.split("/schema.prisma")[0], {
recursive: true,
});
fs.writeFileSync(prismaFilePath, result);
// fs.writeFileSync(prismaFilePath, result);
await formatValidateAndWrite(result, prismaFilePath);
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
} catch (err) {
Expand Down Expand Up @@ -74,7 +79,8 @@ export async function generateSchemaWhenFilePresent(
const migrateModels: string[] = [];

try {
fs.appendFileSync(prismaFilePath, "\n\n" + schemaString, "utf8");
// fs.appendFileSync(prismaFilePath, "\n\n" + schemaString, "utf8");
await formatValidateAndWrite(schemaString, prismaFilePath);
console.log("🚀 Prisma schema generated successfully!");
migrateModels.push(...jsonData.schema.map((model) => model.schemaName));
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions src/types/dynamoPrisma.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export interface Field {
type: string;
description: string;
maxLength?: number | null;
nullable: boolean;
unique: boolean;
isNullable: boolean;
isUnique: boolean;
default?: string | null;
autoincrement?: boolean;
uuid?: boolean;
isAutoIncrement?: boolean;
isUuid?: boolean;
isId?: boolean;
vectorEmbed?: boolean;
isVectorEmbed?: boolean;
embeddingAlgo?: string;
isForeignKey?: boolean;
isList?: boolean;
Expand Down
Loading

0 comments on commit 12daf2f

Please sign in to comment.