valtyr/prisma-kysely

Feature Request: Pass Prisma comments to the generated TS types

Closed this issue ยท 4 comments

Hey ๐Ÿ‘‹ Using Prisma purely for migrations only + Kysely for querying and this lib is the perfect bridge between the two. Great lib!

So as the title says. Seems like we can grab the the field-level comments from Prisma via DMMF.Field['documentation'].

image

I've made some attempts locally to try and contribute but doing Step 4 on the Contributions section of README causes an error. Figured prisma/schema.prisma doesn't exist so I placed my own schema but then it doesn't detect prisma-kysely as the generator. Wondering what your Prisma Schema file looks like during local development for it to work.

janpio commented

(Note that there are unfortunately some bugs around Prisma picking up all appropriate comments into documentation, so you might realize that DMMF does not include all of the ones you want. Usually there already is an issue in the prisma/prisma repo for these that you can +1. Thanks.)
((Do not keep you from adding this of course - we at Prisma should totally fix that!))

valtyr commented

Hey @zeferinix. I think this is a pretty cool idea, it would be awesome if you want to give implementing it a go. Here's an example schema file that can detect the generator (I should probably add this to the contributor guide heh):

datasource db {
    provider = "sqlite"
    url      = "file:./dev.db"
}

generator kysely {
    provider  = "node ./dist/bin.js"
}

model Widget {
    int      Int      @id @default(autoincrement())
    dateTime DateTime @default(now())
    string   String   @default("hello")
    boolean  Boolean  @default(true)
    bytes    Bytes
    decimal  Decimal  @default(1.0)
    bigInt   BigInt   @default(1)
    float    Float    @default(1.0)
}

Let me know if you have any other questions about the setup.

Seems to me it should be pretty easy to implement once you have the comments from Prisma. I'd probably start by doing something like this, after passing the comment down from generateModel.ts.

diff --git a/src/helpers/generateField.ts b/src/helpers/generateField.ts
index 69b1504..9c0ff42 100644
--- a/src/helpers/generateField.ts
+++ b/src/helpers/generateField.ts
@@ -32,10 +32,20 @@ export const generateField = (
 
   if (list) fieldType = ts.factory.createArrayTypeNode(fieldType);
 
-  return ts.factory.createPropertySignature(
+  const propertySignature = ts.factory.createPropertySignature(
     undefined,
     ts.factory.createIdentifier(name),
     undefined,
     fieldType
   );
+
+  if (someCommentVariable)
+    return ts.addSyntheticLeadingComment(
+      propertySignature,
+      ts.SyntaxKind.MultiLineCommentTrivia,
+      ` -- DOC COMMENT GOES HERE BASED ON ${someCommentVariable} -- `,
+      true
+    );
+
+  return propertySignature;
 };

I'll give it a try! Thanks for the hints @valtyr

valtyr commented

@zeferinix No worries man. Excited to see the PR! ๐Ÿคฉ