Closed mikana0918 closed 9 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;
}
}
どうやって型を流し込むか考える必要あり
{
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をパース可能。
model JsonField {
id Int @id @default(autoincrement())
jsonObject Json /// @json(type: [JsonObject])
jsonArray Json[]
}
///
以降に@jsonアノテーションをつけるとパースして自動で指定した位置に配置した、型配布用のファイルから自動生成モデルにインポートを生成。Prismaの元のJsonValue
型をデータアクセス用に保持したまま、自動生成モデルはJsonValue
<=> 指定した型 の相互変換を試みる
Types of changes
Changes
Additional context
Community note
Please upvote with reacting as :+1: to express your agreement.