- Generate model from prisma schema - Can be compatible with `Json` type field by specifying type
npm install frourio-framework-prisma-generatorsJson type field by specifying type ``bash`
npm install -D frourio-framework-prisma-generators
`prisma`
// model generator
generator frourio_framework_prisma_model_generator {
provider = "frourio-framework-prisma-model-generator"
output = "__generated__/models"
additionalTypePath = "./@additionalType/index" // If you need to type Json type field
}
attribute of generateor.$3
`prisma
model JsonField {
id Int @id @default(autoincrement()) rawJson Json
jsonObject Json /// @json(type: [JsonObject])
jsonArray Json /// @json(type: [JsonArray])
}
`$3
`ts
export type JsonObject = {
foo: string;
bar: number;
};export type JsonArray = JsonObject[];
` $3
- Now your model can type the Json field with your annotated type`ts
import { Prisma, JsonField as PrismaJsonField } from '@prisma/client';
import { JsonObject, JsonArray } from '../../@additionalType/index';export type JsonFieldModelDto = {
id: number;
rawJson: Prisma.JsonValue;
jsonObject: JsonObject;
jsonArray: JsonArray;
};
export type JsonFieldModelConstructorArgs = {
id: number;
rawJson: Prisma.JsonValue;
jsonObject: JsonObject;
jsonArray: JsonArray;
};
export type JsonFieldModelFromPrismaValueArgs = {
self: PrismaJsonField;
};
export class JsonFieldModel {
private readonly _id: number;
private readonly _rawJson: Prisma.JsonValue;
private readonly _jsonObject: JsonObject;
private readonly _jsonArray: JsonArray;
constructor(args: JsonFieldModelConstructorArgs) {
this._id = args.id;
this._rawJson = args.rawJson;
this._jsonObject = args.jsonObject;
this._jsonArray = args.jsonArray;
}
static fromPrismaValue(args: JsonFieldModelFromPrismaValueArgs) {
return new JsonFieldModel({
id: args.self.id,
rawJson: args.self.rawJson,
jsonObject: args.self.jsonObject as unknown as JsonObject,
jsonArray: args.self.jsonArray as unknown as JsonArray,
});
}
toDto() {
return {
id: this._id,
rawJson: this._rawJson,
jsonObject: this._jsonObject,
jsonArray: this._jsonArray,
};
}
get id() {
return this._id;
}
get rawJson() {
return this._rawJson;
}
get jsonObject() {
return this._jsonObject;
}
get jsonArray() {
return this._jsonArray;
}
}
``