2.2.1 • Published 11 months ago
frourio-framework-prisma-generators v2.2.1
frourio-framework-prisma-generators
- Generate model from prisma schema
- Can be compatible with
Jsontype field by specifying type
Requirements
- prisma, @prisma/client@5.20.0 (both needs to be same version!)
Install
npm install -D frourio-framework-prisma-generators// model generator
generator frourio_framework_prisma_model_generator {
provider = "frourio-framework-prisma-model-generator"
output = "__generated__/models"
additionalTypePath = "./@additionalType/index.ts" // If you need to type Json type field
}Typing Prisma Json field
- Please make sure you configure
additionalTypePathattribute of generateor.
1. Add type annotation on your schema
model JsonField {
id Int @id @default(autoincrement())
rawJson Json
jsonObject Json /// @json(type: [JsonObject])
jsonArray Json /// @json(type: [JsonArray])
}2. Write your type equvalent to the annotated type
export type JsonObject = {
foo: string;
bar: number;
};
export type JsonArray = JsonObject[];3. Boom 🚀
- Now your model can type the Json field with your annotated type
import type { JsonValue } from '@prisma/client/runtime/library';
import { JsonField as PrismaJsonField } from '@prisma/client';
import { JsonObject, JsonArray } from '../../@additionalType/index.ts';
export interface JsonFieldModelDto {
id: number;
rawJson: JsonValue;
jsonObject: JsonObject;
jsonArray: JsonArray;
}
export type JsonFieldModelConstructorArgs = {
id: number;
rawJson: JsonValue;
jsonObject: JsonObject;
jsonArray: JsonArray;
};
export type JsonFieldModelFromPrismaValueArgs = {
self: PrismaJsonField;
};
export class JsonFieldModel {
private readonly _id: number;
private readonly _rawJson: 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 JsonObject,
jsonArray: args.self.jsonArray 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;
}
}2.2.1
11 months ago
2.2.0
12 months ago
2.1.4
12 months ago
2.1.3
12 months ago
2.1.2
1 year ago
2.1.1
1 year ago
2.1.0
1 year ago
2.0.1
1 year ago
2.0.0
1 year ago
1.2.0
1 year ago
1.1.1
1 year ago
1.1.0
1 year ago
1.0.9
1 year ago
1.0.8
1 year ago
1.0.7
1 year ago
1.0.6
1 year ago
1.0.5
1 year ago
1.0.4
1 year ago
1.0.3
1 year ago
1.0.2
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago