InterfaceX-co-jp / frourio-framework-prisma-generators

InterfaceX original frourio framework prisma generators
1 stars 0 forks source link

frourio-framework-prisma-generators

Requirements

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

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 🚀

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;
  }
}