prisma-json-types-generator
Advanced tools
Comparing version
@@ -15,24 +15,15 @@ "use strict"; | ||
const scalarsField = type.members.find((m) => m.name?.getText() === 'scalars'); | ||
if (scalarsField) { | ||
const object = scalarsField?.type | ||
?.typeArguments?.[0]; | ||
if (!object) { | ||
throw new Error(`Payload scalars could not be resolved: ${type.getText()}`); | ||
} | ||
(0, replace_object_1.replaceObject)(model, object, nsName, replacer, typeAlias.name.getText()); | ||
// Besides `scalars` field, the `objects` field also exists, but we don't need to handle it | ||
// because it just contains references to other <model>Payloads that we already change separately | ||
if (!scalarsField) { | ||
return; | ||
} | ||
const objectsField = type.members.find((m) => m.name?.getText() === 'objects'); | ||
// TODO: Implement objects field | ||
if (objectsField) { | ||
const members = objectsField?.type | ||
?.members; | ||
if (members?.length) { | ||
console.log(`You had an object fields, but they aren't supported yet.\nCan you open a issue at https://github.com/arthurfiorette/prisma-json-types-generator?\n`, { | ||
model, | ||
typeAlias: typeAlias.getText() | ||
}); | ||
} | ||
const object = scalarsField?.type | ||
?.typeArguments?.[0]; | ||
if (!object) { | ||
throw new Error(`Payload scalars could not be resolved: ${type.getText()}`); | ||
} | ||
(0, replace_object_1.replaceObject)(model, object, nsName, replacer, typeAlias.name.getText()); | ||
} | ||
exports.handleModelPayload = handleModelPayload; | ||
//# sourceMappingURL=model-payload.js.map |
{ | ||
"name": "prisma-json-types-generator", | ||
"description": "Changes JsonValues to your custom typescript type", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"main": "dist/generator.js", | ||
@@ -27,12 +27,12 @@ "bin": "./dist/bin.js", | ||
"dependencies": { | ||
"@prisma/generator-helper": "4.9.0" | ||
"@prisma/generator-helper": "4.10.0" | ||
}, | ||
"devDependencies": { | ||
"@arthurfiorette/prettier-config": "^1.0.7", | ||
"@prisma/client": "^4.9.0", | ||
"@types/node": "18.11.18", | ||
"@arthurfiorette/prettier-config": "^1.0.8", | ||
"@prisma/client": "^4.10.0", | ||
"@types/node": "18.13.0", | ||
"@types/prettier": "2.7.2", | ||
"prettier": "2.8.3", | ||
"prisma": "^4.9.0", | ||
"typescript": "4.9.4" | ||
"prettier": "2.8.4", | ||
"prisma": "^4.10.0", | ||
"typescript": "4.9.5" | ||
}, | ||
@@ -39,0 +39,0 @@ "engines": { |
@@ -0,1 +1,7 @@ | ||
<p align="center"> | ||
<b>Using this package?</b> Please consider <a href="https://github.com/sponsors/arthurfiorette" target="_blank">donating</a> to support my open source work ❤️ | ||
</p> | ||
<br /> | ||
[](https://github.com/arthurfiorette/prisma-json-types-generator/issues) | ||
@@ -59,5 +65,5 @@ [](https://github.com/arthurfiorette/prisma-json-types-generator/stargazers) | ||
function myFunction(example: Example) { | ||
// example.normal is now a boolean | ||
// example.optional is now a boolean | null | ||
// example.array is now a boolean[] | ||
// example.normal is now a boolean | ||
// example.optional is now a boolean | null | ||
// example.array is now a boolean[] | ||
} | ||
@@ -68,18 +74,20 @@ ``` | ||
It works by using the typescript compiler api to interpret all emitted type declarations | ||
and changes their field type in the original file. | ||
> ⚠️ **It just changes the declaration files of your generated client, no runtime code is | ||
> affected!** | ||
By using the Typescript Compiler API, this generator parses the generated client's types | ||
AST and looks for `Prisma.JsonValue` types [_(or related)_](src/helpers/regex.ts) and | ||
replaces them with their corresponding type. | ||
### Some types are still json! | ||
There are some complex json types like `JsonFilter` and `JsonWithAggregatesFilter` that if | ||
typed, would impact the usability of the client. So, they are still json. | ||
There are some complex json types like `JsonFilter` and `JsonWithAggregatesFilter` that, | ||
if typed, would impact the usability of the client. So, they are still json. | ||
### Limitations | ||
- This project is a temporary workaround _(and possible solution)_ to | ||
- This project **should be** a temporary workaround _(and possible solution)_ to | ||
https://github.com/prisma/prisma/issues/3219. | ||
- Json types inside `type` declarations won't work. (see | ||
https://github.com/prisma/prisma/issues/13726) |
@@ -20,30 +20,16 @@ import ts from 'typescript'; | ||
if (scalarsField) { | ||
const object = ((scalarsField as ts.PropertySignature)?.type as ts.TypeReferenceNode) | ||
?.typeArguments?.[0] as ts.TypeLiteralNode; | ||
// Besides `scalars` field, the `objects` field also exists, but we don't need to handle it | ||
// because it just contains references to other <model>Payloads that we already change separately | ||
if (!scalarsField) { | ||
return; | ||
} | ||
if (!object) { | ||
throw new Error(`Payload scalars could not be resolved: ${type.getText()}`); | ||
} | ||
const object = ((scalarsField as ts.PropertySignature)?.type as ts.TypeReferenceNode) | ||
?.typeArguments?.[0] as ts.TypeLiteralNode; | ||
replaceObject(model, object, nsName, replacer, typeAlias.name.getText()); | ||
if (!object) { | ||
throw new Error(`Payload scalars could not be resolved: ${type.getText()}`); | ||
} | ||
const objectsField = type.members.find((m) => m.name?.getText() === 'objects'); | ||
// TODO: Implement objects field | ||
if (objectsField) { | ||
const members = ((objectsField as ts.PropertySignature)?.type as ts.TypeLiteralNode) | ||
?.members; | ||
if (members?.length) { | ||
console.log( | ||
`You had an object fields, but they aren't supported yet.\nCan you open a issue at https://github.com/arthurfiorette/prisma-json-types-generator?\n`, | ||
{ | ||
model, | ||
typeAlias: typeAlias.getText() | ||
} | ||
); | ||
} | ||
} | ||
replaceObject(model, object, nsName, replacer, typeAlias.name.getText()); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
92
9.52%101901
-0.43%1448
-1.43%+ Added
+ Added
- Removed
- Removed