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

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

feat: add JsonField model with json and jsonArray attributes #30

Closed mikana0918 closed 9 hours ago

mikana0918 commented 21 hours ago

Types of changes

Changes

Additional context

Community note

Please upvote with reacting as :+1: to express your agreement.

mikana0918 commented 21 hours ago
import { JsonField as PrismaJsonField } from '@prisma/client';

export interface JsonFieldModelDto {
  id: number;
  json: Record<string, unknown>;
  jsonArray: Record<string, unknown>[];
}

export type JsonFieldModelConstructorArgs = {
  id: number;
  json: Record<string, unknown>;
  jsonArray: Record<string, unknown>[];
};

export type JsonFieldModelFromPrismaValueArgs = {
  self: PrismaJsonField;
};

export class JsonFieldModel {
  private readonly _id: number;
  private readonly _json: Record<string, unknown>;
  private readonly _jsonArray: Record<string, unknown>[];

  constructor(args: JsonFieldModelConstructorArgs) {
    this._id = args.id;
    this._json = args.json;
    this._jsonArray = args.jsonArray;
  }

  static fromPrismaValue(args: JsonFieldModelFromPrismaValueArgs) {
    return new JsonFieldModel({
      id: args.self.id,
      json: args.self.json,
      jsonArray: args.self.jsonArray,
    });
  }

  toDto() {
    return {
      id: this._id,
      json: this._json,
      jsonArray: this._jsonArray,
    };
  }

  get id() {
    return this._id;
  }

  get json() {
    return this._json;
  }

  get jsonArray() {
    return this._jsonArray;
  }
}

どうやって型を流し込むか考える必要あり

mikana0918 commented 21 hours ago

https://github.com/arthurfiorette/prisma-json-types-generator

mikana0918 commented 14 hours ago
mikana0918 commented 14 hours ago
    {
      name: 'jsonObject',
      kind: 'scalar',
      isList: false,
      isRequired: true,
      isUnique: false,
      isId: false,
      isReadOnly: false,
      hasDefaultValue: false,
      type: 'Json',
      isGenerated: false,
      isUpdatedAt: false,
      documentation: '@json(type: [JsonObject])'
    },

Prisma.DMMFからdocumentationでtriple commentをパース可能。

mikana0918 commented 13 hours ago
model JsonField {
  id Int @id @default(autoincrement())

  jsonObject Json /// @json(type: [JsonObject])

  jsonArray Json[]
}

///以降に@jsonアノテーションをつけるとパースして自動で指定した位置に配置した、型配布用のファイルから自動生成モデルにインポートを生成。Prismaの元のJsonValue型をデータアクセス用に保持したまま、自動生成モデルはJsonValue <=> 指定した型 の相互変換を試みる