prisma_dart_generator 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { getDMMF } from "@prisma/internals";
4
+ import Class from "./parts/class";
5
+ import Field from "./parts/field";
6
+ import Enum from "./parts/enum";
7
+
8
+ const [, , schemaPath, outputPath] = process.argv;
9
+
10
+ async function main() {
11
+ console.log("Starting the generator...");
12
+ if (!schemaPath || !fs.existsSync(schemaPath))
13
+ throw new Error("Prisma schema not found!");
14
+
15
+ if (!outputPath)
16
+ throw new Error("Output path is required as the second argument");
17
+
18
+ const schema = fs.readFileSync(schemaPath, "utf-8");
19
+ const dmmf = await getDMMF({ datamodel: schema });
20
+ const models = dmmf.datamodel.models;
21
+ const enums = dmmf.datamodel.enums;
22
+
23
+ let dartString = "";
24
+
25
+ for (const e of enums) {
26
+ console.log("Generating enum: ", e.name);
27
+ const dart = new Enum({
28
+ name: e.name,
29
+ values: e.values.map((value) => value.name),
30
+ });
31
+
32
+ dartString += dart.toString();
33
+ }
34
+
35
+ for (const model of models) {
36
+ console.log("Generating model: ", model.name);
37
+ const dart = new Class({
38
+ name: model.name,
39
+ fields: model.fields.map((field) => {
40
+ return new Field(field);
41
+ }),
42
+ });
43
+
44
+ dartString += dart.toString();
45
+ }
46
+
47
+ const text = `
48
+ //*** AUTO-GENERATED FILE - DO NOT MODIFY ***//
49
+
50
+ ${dartString}
51
+ `;
52
+
53
+ if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
54
+ fs.writeFileSync(path.join(outputPath, "models.dart"), text, "utf-8");
55
+ }
56
+
57
+ main().catch(console.error);
@@ -0,0 +1,236 @@
1
+ import { enumToDartName } from "./enum";
2
+ import type Field from "./field";
3
+ import { snakeToCamel } from "./utils";
4
+
5
+ export const modelNameToDartName = (name: string): string => {
6
+ const modelName = name
7
+ .split("_")
8
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
9
+ .join("");
10
+
11
+ return `${modelName}Model`;
12
+ };
13
+
14
+ export default class Class {
15
+ public name: string;
16
+ public fields: Field[];
17
+
18
+ constructor({ name, fields }: { name: string; fields: Field[] }) {
19
+ this.name = name;
20
+ this.fields = fields;
21
+ }
22
+
23
+ toString(): string {
24
+ return `
25
+ class ${modelNameToDartName(this.name)} {
26
+ ${this.getFields()}
27
+
28
+ ${this.getConstructor()}
29
+
30
+ ${this.getFromJson()}
31
+
32
+ ${this.toJson()}
33
+
34
+ ${this.copyWith()}
35
+
36
+ ${this.toStr()}
37
+ }`;
38
+ }
39
+
40
+ private getFields(): string {
41
+ return " " + this.fields.map((field) => field.toString()).join("\n ");
42
+ }
43
+
44
+ private getConstructor(): string {
45
+ return `
46
+ ${modelNameToDartName(this.name)}({
47
+ ${this.fields
48
+ .map((field) => {
49
+ return ` this.${snakeToCamel(field.field.name)}`;
50
+ })
51
+ .join(",\n")}
52
+ });`;
53
+ }
54
+
55
+ private getFromJson(): string {
56
+ function parseFromJson(field: Field): string {
57
+ const prismaField = field.field;
58
+ let valueExpr = "";
59
+
60
+ // Number types
61
+ if (prismaField.type === "Int" || prismaField.type === "Float")
62
+ valueExpr = `num.tryParse("\${json["${prismaField.name}"]}")`;
63
+ // DateTime
64
+ else if (prismaField.type === "DateTime")
65
+ valueExpr = `json["${prismaField.name}"] != null ? DateTime.parse(json["${prismaField.name}"]) : null`;
66
+ // scalar types
67
+ else if (prismaField.kind === "scalar")
68
+ valueExpr = `json["${prismaField.name}"]`;
69
+ // Enum
70
+ else if (prismaField.kind === "enum")
71
+ valueExpr = `json["${prismaField.name}"] != null ? ${enumToDartName(
72
+ prismaField.type.replace("[]", "")
73
+ )}.fromText(json["${prismaField.name}"]) : null`;
74
+ // Relation (another class, object or list of objects)
75
+ else if (prismaField.relationName !== null)
76
+ if (prismaField.isList)
77
+ valueExpr = `json["${prismaField.name}"] != null
78
+ ? List.from(json["${
79
+ prismaField.name
80
+ }"]).map((d) => ${modelNameToDartName(
81
+ prismaField.type.replace("[]", "")
82
+ )}.fromJson(d)).toList()
83
+ : null`;
84
+ else
85
+ valueExpr = `json["${prismaField.name}"] != null
86
+ ? ${modelNameToDartName(
87
+ prismaField.type.replace("[]", "")
88
+ )}.fromJson(json['${prismaField.name}'])
89
+ : null`;
90
+ // Fallback: Just assign and type-cast
91
+ else valueExpr = `json["${prismaField.name}"] as ${prismaField.type}`;
92
+
93
+ return valueExpr;
94
+ }
95
+
96
+ return `
97
+ factory ${modelNameToDartName(
98
+ this.name
99
+ )}.fromJson(Map<String, dynamic> json) => ${modelNameToDartName(this.name)}(
100
+ ${this.fields
101
+ .map((field) => {
102
+ return `${snakeToCamel(field.field.name)}: ${parseFromJson(field)},`;
103
+ })
104
+ .join("\n ")}
105
+ );`;
106
+ }
107
+
108
+ private toJson(): string {
109
+ return `
110
+ Map<String, dynamic> toJson() => {
111
+ ${this.fields
112
+ .map((field) => {
113
+ const prismaField = field.field;
114
+ const name = snakeToCamel(prismaField.name);
115
+ let valueExpr = "";
116
+
117
+ // DateTime
118
+ if (prismaField.type === "DateTime")
119
+ valueExpr = `${name}?.toIso8601String()`;
120
+ // Scalar
121
+ else if (prismaField.kind === "scalar") valueExpr = `${name}`;
122
+ // Enum
123
+ else if (prismaField.kind === "enum") valueExpr = `${name}?.text`;
124
+ else if (prismaField.relationName !== null)
125
+ if (prismaField.isList)
126
+ valueExpr = `${snakeToCamel(
127
+ prismaField.name
128
+ )}?.map((d) => d.toJson()).toList()`;
129
+ else valueExpr = `${name}?.toJson()`;
130
+ // Fallback: Just assign and type-cast
131
+ else valueExpr = `${name}`;
132
+
133
+ return `if(${name} != null) '${prismaField.name}': ${valueExpr}`;
134
+ })
135
+ .join(",\n ")}
136
+ };`;
137
+ }
138
+
139
+ private copyWith(): string {
140
+ function parseFromJson(field: Field): string {
141
+ const prismaField = field.field;
142
+ let valueExpr = "";
143
+
144
+ // Number types
145
+ if (prismaField.type === "Int" || prismaField.type === "Float")
146
+ valueExpr = `json["${
147
+ prismaField.name
148
+ }"] != null ? num.tryParse("\${json["${
149
+ prismaField.name
150
+ }"]}") : ${snakeToCamel(field.field.name)}`;
151
+ // JSON
152
+ else if (prismaField.type === "Json")
153
+ valueExpr = `json["${prismaField.name}"] != null ? Map<String, dynamic>.from(json["${prismaField.name}"]) : null`;
154
+ else if (prismaField.type === "Json?")
155
+ valueExpr = `json["${prismaField.name}"] != null ? Map<String, dynamic>.from(json["${prismaField.name}"]) : null`;
156
+ // DateTime
157
+ else if (prismaField.type === "DateTime")
158
+ valueExpr = `json["${prismaField.name}"] != null ? DateTime.parse(json["${prismaField.name}"]) : null`;
159
+ // scalar types
160
+ else if (prismaField.kind === "scalar")
161
+ valueExpr = `json["${prismaField.name}"] ?? ${snakeToCamel(
162
+ field.field.name
163
+ )}`;
164
+ // Enum
165
+ else if (prismaField.kind === "enum")
166
+ valueExpr = `json["${prismaField.name}"] != null ? ${enumToDartName(
167
+ prismaField.type.replace("[]", "")
168
+ )}.fromText(json["${prismaField.name}"]) : null`;
169
+ // Relation (another class, object or list of objects)
170
+ else if (prismaField.relationName !== null)
171
+ if (prismaField.isList)
172
+ valueExpr = `json["${prismaField.name}"] != null
173
+ ? List.from(json["${
174
+ prismaField.name
175
+ }"]).map((d) => ${modelNameToDartName(
176
+ prismaField.type.replace("[]", "")
177
+ )}.fromJson(d)).toList()
178
+ : ${snakeToCamel(field.field.name)}`;
179
+ else
180
+ valueExpr = `json["${prismaField.name}"] != null
181
+ ? ${modelNameToDartName(
182
+ prismaField.type.replace("[]", "")
183
+ )}.fromJson(json['${prismaField.name}'])
184
+ : ${snakeToCamel(field.field.name)}`;
185
+ // Fallback: Just assign and type-cast
186
+ else valueExpr = `json["${prismaField.name}"] as ${prismaField.type}`;
187
+
188
+ return valueExpr;
189
+ }
190
+
191
+ return `
192
+ ${modelNameToDartName(
193
+ this.name
194
+ )} copyWith(Map<String, dynamic> json) => ${modelNameToDartName(this.name)}(
195
+ ${this.fields
196
+ .map((field) => {
197
+ return `${snakeToCamel(field.field.name)}: ${parseFromJson(field)}`;
198
+ })
199
+ .join(",\n ")}
200
+ );`;
201
+ }
202
+
203
+ private toStr(): string {
204
+ return `
205
+ @override
206
+ String toString() => """
207
+ ${modelNameToDartName(this.name)}(
208
+ ${this.fields
209
+ .map((field) => {
210
+ const prismaField = field.field;
211
+ const name = snakeToCamel(prismaField.name);
212
+ let valueExpr = "";
213
+
214
+ // DateTime
215
+ if (prismaField.type === "DateTime")
216
+ valueExpr = `${name}?.toIso8601String()`;
217
+ // Scalar
218
+ else if (prismaField.kind === "scalar") valueExpr = `${name}`;
219
+ // Enum
220
+ else if (prismaField.kind === "enum")
221
+ valueExpr = `${name}?.text`;
222
+ else if (prismaField.relationName !== null)
223
+ if (prismaField.isList)
224
+ valueExpr = `${snakeToCamel(
225
+ prismaField.name
226
+ )}?.map((d) => d.toJson()).toList()`;
227
+ else valueExpr = `${name}?.toJson()`;
228
+ // Fallback: Just assign and type-cast
229
+ else valueExpr = `${name}`;
230
+
231
+ return `'${prismaField.name}': \${${valueExpr}}`;
232
+ })
233
+ .join(",\n ")}
234
+ ) """;`;
235
+ }
236
+ }
@@ -0,0 +1,51 @@
1
+ import { snakeToCamel } from "./utils";
2
+
3
+ export const enumToDartName = (name: string): string => {
4
+ const modelName = name
5
+ .split("_")
6
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
7
+ .join("");
8
+
9
+ return `${modelName}Enum`;
10
+ };
11
+
12
+ export default class Enum {
13
+ public name: string;
14
+ public values: string[];
15
+
16
+ constructor({ name, values }: { name: string; values: string[] }) {
17
+ this.name = name;
18
+ this.values = values;
19
+ }
20
+
21
+ toString(): string {
22
+ return `
23
+ enum ${enumToDartName(this.name)} {
24
+
25
+ ${this.values
26
+ .map((value) => ` ${snakeToCamel(value)}("${value}")`)
27
+ .join(",\n")};
28
+
29
+ final String _text;
30
+
31
+ const ${enumToDartName(this.name)}(String text):_text = text;
32
+
33
+ static ${enumToDartName(this.name)}? fromText(String? value) {
34
+ if(value == null) return null;
35
+ try {
36
+ return ${enumToDartName(
37
+ this.name
38
+ )}.values.firstWhere((v) => v._text == value);
39
+ } catch (e) {
40
+ return null;
41
+ }
42
+ }
43
+
44
+ String get text => _text;
45
+
46
+ @override
47
+ String toString() => _text;
48
+ }
49
+ `;
50
+ }
51
+ }
@@ -0,0 +1,62 @@
1
+ import type { Field as PrismaField } from "@prisma/dmmf";
2
+
3
+ import { modelNameToDartName } from "./class";
4
+ import { enumToDartName } from "./enum";
5
+ import { snakeToCamel } from "./utils";
6
+
7
+ export default class Field {
8
+ field: PrismaField;
9
+
10
+ constructor(field: PrismaField) {
11
+ this.field = field;
12
+ }
13
+
14
+ toString(): string {
15
+ const type = getDartTypeFromPrismaType(this.field.type);
16
+ const name = snakeToCamel(this.field.name);
17
+
18
+ if (this.field.kind === "scalar") return `final ${type}? ${name};`;
19
+ else if (this.field.isList)
20
+ if (this.field.relationName !== null)
21
+ return `final List<${modelNameToDartName(
22
+ type.replace("[]", "")
23
+ )}>? ${name};`;
24
+ else return `final List<${type.replace("[]", "")}>? ${name};`;
25
+ else if (this.field.kind === "enum")
26
+ return `final ${enumToDartName(type.replace("[]", ""))}? ${name};`;
27
+ else if (this.field.relationName !== null)
28
+ return `final ${modelNameToDartName(type.replace("[]", ""))}? ${name};`;
29
+ else return `final ${type}? ${name};`;
30
+ }
31
+ }
32
+
33
+ const getDartTypeFromPrismaType = (type: string): string => {
34
+ switch (type) {
35
+ case "String":
36
+ return "String";
37
+ case "String?":
38
+ return "String?";
39
+ case "Int":
40
+ return "num";
41
+ case "Int?":
42
+ return "num?";
43
+ case "Float":
44
+ return "num";
45
+ case "Float?":
46
+ return "num?";
47
+ case "Boolean":
48
+ return "bool";
49
+ case "Boolean?":
50
+ return "bool?";
51
+ case "DateTime":
52
+ return "DateTime";
53
+ case "DateTime?":
54
+ return "DateTime?";
55
+ case "Json":
56
+ return "Map<String, dynamic>";
57
+ case "Json?":
58
+ return "Map<String, dynamic>?";
59
+ default:
60
+ return type;
61
+ }
62
+ };
@@ -0,0 +1,11 @@
1
+ export const capitalize = (text: string) =>
2
+ text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();
3
+
4
+ export const snakeToCamel = (text: string) => {
5
+ if (!text.includes("_")) return text.toLowerCase();
6
+
7
+ return text
8
+ .split("_")
9
+ .map((t, i) => (i == 0 ? t.toLowerCase() : capitalize(t)))
10
+ .join("");
11
+ };
package/package.json CHANGED
@@ -1,14 +1,20 @@
1
1
  {
2
2
  "name": "prisma_dart_generator",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "token": "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",
8
+ "bin": {
9
+ "prisma_dart": "bun ./bin/index.ts ./test/prisma/schema.prisma ./test/dart/data/"
10
+ },
8
11
  "devDependencies": {
9
12
  "@types/bun": "latest"
10
13
  },
11
14
  "peerDependencies": {
12
15
  "typescript": "^5"
16
+ },
17
+ "dependencies": {
18
+ "@prisma/internals": "^7.2.0"
13
19
  }
14
20
  }
@@ -0,0 +1,622 @@
1
+ //*** AUTO-GENERATED FILE - DO NOT MODIFY ***//
2
+
3
+ enum UserRoleEnum {
4
+ admin("ADMIN"),
5
+ user("USER");
6
+
7
+ final String _text;
8
+
9
+ const UserRoleEnum(String text) : _text = text;
10
+
11
+ static UserRoleEnum? fromText(String? value) {
12
+ if (value == null) return null;
13
+ try {
14
+ return UserRoleEnum.values.firstWhere((v) => v._text == value);
15
+ } catch (e) {
16
+ return null;
17
+ }
18
+ }
19
+
20
+ String get text => _text;
21
+
22
+ @override
23
+ String toString() => _text;
24
+ }
25
+
26
+ enum UserStatusEnum {
27
+ active("ACTIVE"),
28
+ inactive("INACTIVE"),
29
+ pending("PENDING"),
30
+ blocked("BLOCKED");
31
+
32
+ final String _text;
33
+
34
+ const UserStatusEnum(String text) : _text = text;
35
+
36
+ static UserStatusEnum? fromText(String? value) {
37
+ if (value == null) return null;
38
+ try {
39
+ return UserStatusEnum.values.firstWhere((v) => v._text == value);
40
+ } catch (e) {
41
+ return null;
42
+ }
43
+ }
44
+
45
+ String get text => _text;
46
+
47
+ @override
48
+ String toString() => _text;
49
+ }
50
+
51
+ enum UserGenderEnum {
52
+ male("MALE"),
53
+ female("FEMALE"),
54
+ other("OTHER");
55
+
56
+ final String _text;
57
+
58
+ const UserGenderEnum(String text) : _text = text;
59
+
60
+ static UserGenderEnum? fromText(String? value) {
61
+ if (value == null) return null;
62
+ try {
63
+ return UserGenderEnum.values.firstWhere((v) => v._text == value);
64
+ } catch (e) {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ String get text => _text;
70
+
71
+ @override
72
+ String toString() => _text;
73
+ }
74
+
75
+ enum UserSubscriptionEnum {
76
+ free("FREE"),
77
+ premium("PREMIUM"),
78
+ enterprise("ENTERPRISE");
79
+
80
+ final String _text;
81
+
82
+ const UserSubscriptionEnum(String text) : _text = text;
83
+
84
+ static UserSubscriptionEnum? fromText(String? value) {
85
+ if (value == null) return null;
86
+ try {
87
+ return UserSubscriptionEnum.values.firstWhere((v) => v._text == value);
88
+ } catch (e) {
89
+ return null;
90
+ }
91
+ }
92
+
93
+ String get text => _text;
94
+
95
+ @override
96
+ String toString() => _text;
97
+ }
98
+
99
+ class UserModel {
100
+ final String? id;
101
+ final String? name;
102
+ final String? email;
103
+ final String? password;
104
+ final DateTime? createdat;
105
+ final DateTime? updatedat;
106
+ final List<PostModel>? posts;
107
+ final List<UserProfileModel>? userprofiles;
108
+
109
+ UserModel({
110
+ this.id,
111
+ this.name,
112
+ this.email,
113
+ this.password,
114
+ this.createdat,
115
+ this.updatedat,
116
+ this.posts,
117
+ this.userprofiles,
118
+ });
119
+
120
+ factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
121
+ id: json["id"],
122
+ name: json["name"],
123
+ email: json["email"],
124
+ password: json["password"],
125
+ createdat: json["createdAt"] != null
126
+ ? DateTime.parse(json["createdAt"])
127
+ : null,
128
+ updatedat: json["updatedAt"] != null
129
+ ? DateTime.parse(json["updatedAt"])
130
+ : null,
131
+ posts: json["posts"] != null
132
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
133
+ : null,
134
+ userprofiles: json["userProfiles"] != null
135
+ ? List.from(
136
+ json["userProfiles"],
137
+ ).map((d) => UserProfileModel.fromJson(d)).toList()
138
+ : null,
139
+ );
140
+
141
+ Map<String, dynamic> toJson() => {
142
+ if (id != null) 'id': id,
143
+ if (name != null) 'name': name,
144
+ if (email != null) 'email': email,
145
+ if (password != null) 'password': password,
146
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
147
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
148
+ if (posts != null) 'posts': posts?.map((d) => d.toJson()).toList(),
149
+ if (userprofiles != null)
150
+ 'userProfiles': userprofiles?.map((d) => d.toJson()).toList(),
151
+ };
152
+
153
+ UserModel copyWith(Map<String, dynamic> json) => UserModel(
154
+ id: json["id"] ?? id,
155
+ name: json["name"] ?? name,
156
+ email: json["email"] ?? email,
157
+ password: json["password"] ?? password,
158
+ createdat: json["createdAt"] != null
159
+ ? DateTime.parse(json["createdAt"])
160
+ : null,
161
+ updatedat: json["updatedAt"] != null
162
+ ? DateTime.parse(json["updatedAt"])
163
+ : null,
164
+ posts: json["posts"] != null
165
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
166
+ : posts,
167
+ userprofiles: json["userProfiles"] != null
168
+ ? List.from(
169
+ json["userProfiles"],
170
+ ).map((d) => UserProfileModel.fromJson(d)).toList()
171
+ : userprofiles,
172
+ );
173
+
174
+ @override
175
+ String toString() =>
176
+ """
177
+ UserModel(
178
+ 'id': ${id},
179
+ 'name': ${name},
180
+ 'email': ${email},
181
+ 'password': ${password},
182
+ 'createdAt': ${createdat?.toIso8601String()},
183
+ 'updatedAt': ${updatedat?.toIso8601String()},
184
+ 'posts': ${posts?.map((d) => d.toJson()).toList()},
185
+ 'userProfiles': ${userprofiles?.map((d) => d.toJson()).toList()}
186
+ ) """;
187
+ }
188
+
189
+ class PostModel {
190
+ final String? id;
191
+ final String? title;
192
+ final String? content;
193
+ final DateTime? createdat;
194
+ final DateTime? updatedat;
195
+ final String? authorid;
196
+ final UserModel? author;
197
+ final List<CommentModel>? comments;
198
+ final CategoryModel? category;
199
+ final String? categoryid;
200
+ final TagModel? tag;
201
+ final String? tagid;
202
+ final List<ImageModel>? images;
203
+
204
+ PostModel({
205
+ this.id,
206
+ this.title,
207
+ this.content,
208
+ this.createdat,
209
+ this.updatedat,
210
+ this.authorid,
211
+ this.author,
212
+ this.comments,
213
+ this.category,
214
+ this.categoryid,
215
+ this.tag,
216
+ this.tagid,
217
+ this.images,
218
+ });
219
+
220
+ factory PostModel.fromJson(Map<String, dynamic> json) => PostModel(
221
+ id: json["id"],
222
+ title: json["title"],
223
+ content: json["content"],
224
+ createdat: json["createdAt"] != null
225
+ ? DateTime.parse(json["createdAt"])
226
+ : null,
227
+ updatedat: json["updatedAt"] != null
228
+ ? DateTime.parse(json["updatedAt"])
229
+ : null,
230
+ authorid: json["authorId"],
231
+ author: json["author"] != null ? UserModel.fromJson(json['author']) : null,
232
+ comments: json["comments"] != null
233
+ ? List.from(
234
+ json["comments"],
235
+ ).map((d) => CommentModel.fromJson(d)).toList()
236
+ : null,
237
+ category: json["category"] != null
238
+ ? CategoryModel.fromJson(json['category'])
239
+ : null,
240
+ categoryid: json["categoryId"],
241
+ tag: json["tag"] != null ? TagModel.fromJson(json['tag']) : null,
242
+ tagid: json["tagId"],
243
+ images: json["images"] != null
244
+ ? List.from(json["images"]).map((d) => ImageModel.fromJson(d)).toList()
245
+ : null,
246
+ );
247
+
248
+ Map<String, dynamic> toJson() => {
249
+ if (id != null) 'id': id,
250
+ if (title != null) 'title': title,
251
+ if (content != null) 'content': content,
252
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
253
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
254
+ if (authorid != null) 'authorId': authorid,
255
+ if (author != null) 'author': author?.toJson(),
256
+ if (comments != null) 'comments': comments?.map((d) => d.toJson()).toList(),
257
+ if (category != null) 'category': category?.toJson(),
258
+ if (categoryid != null) 'categoryId': categoryid,
259
+ if (tag != null) 'tag': tag?.toJson(),
260
+ if (tagid != null) 'tagId': tagid,
261
+ if (images != null) 'images': images?.map((d) => d.toJson()).toList(),
262
+ };
263
+
264
+ PostModel copyWith(Map<String, dynamic> json) => PostModel(
265
+ id: json["id"] ?? id,
266
+ title: json["title"] ?? title,
267
+ content: json["content"] ?? content,
268
+ createdat: json["createdAt"] != null
269
+ ? DateTime.parse(json["createdAt"])
270
+ : null,
271
+ updatedat: json["updatedAt"] != null
272
+ ? DateTime.parse(json["updatedAt"])
273
+ : null,
274
+ authorid: json["authorId"] ?? authorid,
275
+ author: json["author"] != null
276
+ ? UserModel.fromJson(json['author'])
277
+ : author,
278
+ comments: json["comments"] != null
279
+ ? List.from(
280
+ json["comments"],
281
+ ).map((d) => CommentModel.fromJson(d)).toList()
282
+ : comments,
283
+ category: json["category"] != null
284
+ ? CategoryModel.fromJson(json['category'])
285
+ : category,
286
+ categoryid: json["categoryId"] ?? categoryid,
287
+ tag: json["tag"] != null ? TagModel.fromJson(json['tag']) : tag,
288
+ tagid: json["tagId"] ?? tagid,
289
+ images: json["images"] != null
290
+ ? List.from(json["images"]).map((d) => ImageModel.fromJson(d)).toList()
291
+ : images,
292
+ );
293
+
294
+ @override
295
+ String toString() =>
296
+ """
297
+ PostModel(
298
+ 'id': ${id},
299
+ 'title': ${title},
300
+ 'content': ${content},
301
+ 'createdAt': ${createdat?.toIso8601String()},
302
+ 'updatedAt': ${updatedat?.toIso8601String()},
303
+ 'authorId': ${authorid},
304
+ 'author': ${author?.toJson()},
305
+ 'comments': ${comments?.map((d) => d.toJson()).toList()},
306
+ 'category': ${category?.toJson()},
307
+ 'categoryId': ${categoryid},
308
+ 'tag': ${tag?.toJson()},
309
+ 'tagId': ${tagid},
310
+ 'images': ${images?.map((d) => d.toJson()).toList()}
311
+ ) """;
312
+ }
313
+
314
+ class CommentModel {
315
+ final String? id;
316
+ final String? content;
317
+ final DateTime? createdat;
318
+ final DateTime? updatedat;
319
+ final String? postid;
320
+ final PostModel? post;
321
+
322
+ CommentModel({
323
+ this.id,
324
+ this.content,
325
+ this.createdat,
326
+ this.updatedat,
327
+ this.postid,
328
+ this.post,
329
+ });
330
+
331
+ factory CommentModel.fromJson(Map<String, dynamic> json) => CommentModel(
332
+ id: json["id"],
333
+ content: json["content"],
334
+ createdat: json["createdAt"] != null
335
+ ? DateTime.parse(json["createdAt"])
336
+ : null,
337
+ updatedat: json["updatedAt"] != null
338
+ ? DateTime.parse(json["updatedAt"])
339
+ : null,
340
+ postid: json["postId"],
341
+ post: json["post"] != null ? PostModel.fromJson(json['post']) : null,
342
+ );
343
+
344
+ Map<String, dynamic> toJson() => {
345
+ if (id != null) 'id': id,
346
+ if (content != null) 'content': content,
347
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
348
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
349
+ if (postid != null) 'postId': postid,
350
+ if (post != null) 'post': post?.toJson(),
351
+ };
352
+
353
+ CommentModel copyWith(Map<String, dynamic> json) => CommentModel(
354
+ id: json["id"] ?? id,
355
+ content: json["content"] ?? content,
356
+ createdat: json["createdAt"] != null
357
+ ? DateTime.parse(json["createdAt"])
358
+ : null,
359
+ updatedat: json["updatedAt"] != null
360
+ ? DateTime.parse(json["updatedAt"])
361
+ : null,
362
+ postid: json["postId"] ?? postid,
363
+ post: json["post"] != null ? PostModel.fromJson(json['post']) : post,
364
+ );
365
+
366
+ @override
367
+ String toString() =>
368
+ """
369
+ CommentModel(
370
+ 'id': ${id},
371
+ 'content': ${content},
372
+ 'createdAt': ${createdat?.toIso8601String()},
373
+ 'updatedAt': ${updatedat?.toIso8601String()},
374
+ 'postId': ${postid},
375
+ 'post': ${post?.toJson()}
376
+ ) """;
377
+ }
378
+
379
+ class CategoryModel {
380
+ final String? id;
381
+ final String? name;
382
+ final DateTime? createdat;
383
+ final DateTime? updatedat;
384
+ final List<PostModel>? posts;
385
+
386
+ CategoryModel({
387
+ this.id,
388
+ this.name,
389
+ this.createdat,
390
+ this.updatedat,
391
+ this.posts,
392
+ });
393
+
394
+ factory CategoryModel.fromJson(Map<String, dynamic> json) => CategoryModel(
395
+ id: json["id"],
396
+ name: json["name"],
397
+ createdat: json["createdAt"] != null
398
+ ? DateTime.parse(json["createdAt"])
399
+ : null,
400
+ updatedat: json["updatedAt"] != null
401
+ ? DateTime.parse(json["updatedAt"])
402
+ : null,
403
+ posts: json["posts"] != null
404
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
405
+ : null,
406
+ );
407
+
408
+ Map<String, dynamic> toJson() => {
409
+ if (id != null) 'id': id,
410
+ if (name != null) 'name': name,
411
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
412
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
413
+ if (posts != null) 'posts': posts?.map((d) => d.toJson()).toList(),
414
+ };
415
+
416
+ CategoryModel copyWith(Map<String, dynamic> json) => CategoryModel(
417
+ id: json["id"] ?? id,
418
+ name: json["name"] ?? name,
419
+ createdat: json["createdAt"] != null
420
+ ? DateTime.parse(json["createdAt"])
421
+ : null,
422
+ updatedat: json["updatedAt"] != null
423
+ ? DateTime.parse(json["updatedAt"])
424
+ : null,
425
+ posts: json["posts"] != null
426
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
427
+ : posts,
428
+ );
429
+
430
+ @override
431
+ String toString() =>
432
+ """
433
+ CategoryModel(
434
+ 'id': ${id},
435
+ 'name': ${name},
436
+ 'createdAt': ${createdat?.toIso8601String()},
437
+ 'updatedAt': ${updatedat?.toIso8601String()},
438
+ 'posts': ${posts?.map((d) => d.toJson()).toList()}
439
+ ) """;
440
+ }
441
+
442
+ class TagModel {
443
+ final String? id;
444
+ final String? name;
445
+ final DateTime? createdat;
446
+ final DateTime? updatedat;
447
+ final List<PostModel>? posts;
448
+
449
+ TagModel({this.id, this.name, this.createdat, this.updatedat, this.posts});
450
+
451
+ factory TagModel.fromJson(Map<String, dynamic> json) => TagModel(
452
+ id: json["id"],
453
+ name: json["name"],
454
+ createdat: json["createdAt"] != null
455
+ ? DateTime.parse(json["createdAt"])
456
+ : null,
457
+ updatedat: json["updatedAt"] != null
458
+ ? DateTime.parse(json["updatedAt"])
459
+ : null,
460
+ posts: json["posts"] != null
461
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
462
+ : null,
463
+ );
464
+
465
+ Map<String, dynamic> toJson() => {
466
+ if (id != null) 'id': id,
467
+ if (name != null) 'name': name,
468
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
469
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
470
+ if (posts != null) 'posts': posts?.map((d) => d.toJson()).toList(),
471
+ };
472
+
473
+ TagModel copyWith(Map<String, dynamic> json) => TagModel(
474
+ id: json["id"] ?? id,
475
+ name: json["name"] ?? name,
476
+ createdat: json["createdAt"] != null
477
+ ? DateTime.parse(json["createdAt"])
478
+ : null,
479
+ updatedat: json["updatedAt"] != null
480
+ ? DateTime.parse(json["updatedAt"])
481
+ : null,
482
+ posts: json["posts"] != null
483
+ ? List.from(json["posts"]).map((d) => PostModel.fromJson(d)).toList()
484
+ : posts,
485
+ );
486
+
487
+ @override
488
+ String toString() =>
489
+ """
490
+ TagModel(
491
+ 'id': ${id},
492
+ 'name': ${name},
493
+ 'createdAt': ${createdat?.toIso8601String()},
494
+ 'updatedAt': ${updatedat?.toIso8601String()},
495
+ 'posts': ${posts?.map((d) => d.toJson()).toList()}
496
+ ) """;
497
+ }
498
+
499
+ class ImageModel {
500
+ final String? id;
501
+ final String? url;
502
+ final DateTime? createdat;
503
+ final DateTime? updatedat;
504
+ final String? postid;
505
+ final PostModel? post;
506
+
507
+ ImageModel({
508
+ this.id,
509
+ this.url,
510
+ this.createdat,
511
+ this.updatedat,
512
+ this.postid,
513
+ this.post,
514
+ });
515
+
516
+ factory ImageModel.fromJson(Map<String, dynamic> json) => ImageModel(
517
+ id: json["id"],
518
+ url: json["url"],
519
+ createdat: json["createdAt"] != null
520
+ ? DateTime.parse(json["createdAt"])
521
+ : null,
522
+ updatedat: json["updatedAt"] != null
523
+ ? DateTime.parse(json["updatedAt"])
524
+ : null,
525
+ postid: json["postId"],
526
+ post: json["post"] != null ? PostModel.fromJson(json['post']) : null,
527
+ );
528
+
529
+ Map<String, dynamic> toJson() => {
530
+ if (id != null) 'id': id,
531
+ if (url != null) 'url': url,
532
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
533
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
534
+ if (postid != null) 'postId': postid,
535
+ if (post != null) 'post': post?.toJson(),
536
+ };
537
+
538
+ ImageModel copyWith(Map<String, dynamic> json) => ImageModel(
539
+ id: json["id"] ?? id,
540
+ url: json["url"] ?? url,
541
+ createdat: json["createdAt"] != null
542
+ ? DateTime.parse(json["createdAt"])
543
+ : null,
544
+ updatedat: json["updatedAt"] != null
545
+ ? DateTime.parse(json["updatedAt"])
546
+ : null,
547
+ postid: json["postId"] ?? postid,
548
+ post: json["post"] != null ? PostModel.fromJson(json['post']) : post,
549
+ );
550
+
551
+ @override
552
+ String toString() =>
553
+ """
554
+ ImageModel(
555
+ 'id': ${id},
556
+ 'url': ${url},
557
+ 'createdAt': ${createdat?.toIso8601String()},
558
+ 'updatedAt': ${updatedat?.toIso8601String()},
559
+ 'postId': ${postid},
560
+ 'post': ${post?.toJson()}
561
+ ) """;
562
+ }
563
+
564
+ class UserProfileModel {
565
+ final String? id;
566
+ final String? userid;
567
+ final UserModel? user;
568
+ final DateTime? createdat;
569
+ final DateTime? updatedat;
570
+
571
+ UserProfileModel({
572
+ this.id,
573
+ this.userid,
574
+ this.user,
575
+ this.createdat,
576
+ this.updatedat,
577
+ });
578
+
579
+ factory UserProfileModel.fromJson(Map<String, dynamic> json) =>
580
+ UserProfileModel(
581
+ id: json["id"],
582
+ userid: json["userId"],
583
+ user: json["user"] != null ? UserModel.fromJson(json['user']) : null,
584
+ createdat: json["createdAt"] != null
585
+ ? DateTime.parse(json["createdAt"])
586
+ : null,
587
+ updatedat: json["updatedAt"] != null
588
+ ? DateTime.parse(json["updatedAt"])
589
+ : null,
590
+ );
591
+
592
+ Map<String, dynamic> toJson() => {
593
+ if (id != null) 'id': id,
594
+ if (userid != null) 'userId': userid,
595
+ if (user != null) 'user': user?.toJson(),
596
+ if (createdat != null) 'createdAt': createdat?.toIso8601String(),
597
+ if (updatedat != null) 'updatedAt': updatedat?.toIso8601String(),
598
+ };
599
+
600
+ UserProfileModel copyWith(Map<String, dynamic> json) => UserProfileModel(
601
+ id: json["id"] ?? id,
602
+ userid: json["userId"] ?? userid,
603
+ user: json["user"] != null ? UserModel.fromJson(json['user']) : user,
604
+ createdat: json["createdAt"] != null
605
+ ? DateTime.parse(json["createdAt"])
606
+ : null,
607
+ updatedat: json["updatedAt"] != null
608
+ ? DateTime.parse(json["updatedAt"])
609
+ : null,
610
+ );
611
+
612
+ @override
613
+ String toString() =>
614
+ """
615
+ UserProfileModel(
616
+ 'id': ${id},
617
+ 'userId': ${userid},
618
+ 'user': ${user?.toJson()},
619
+ 'createdAt': ${createdat?.toIso8601String()},
620
+ 'updatedAt': ${updatedat?.toIso8601String()}
621
+ ) """;
622
+ }
@@ -0,0 +1,107 @@
1
+ // This is your Prisma schema file,
2
+ // learn more about it in the docs: https://pris.ly/d/prisma-schema
3
+
4
+ // Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5
+ // Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6
+
7
+ generator client {
8
+ provider = "prisma-client"
9
+ output = "../generated/prisma"
10
+ }
11
+
12
+ datasource db {
13
+ provider = "postgresql"
14
+ }
15
+
16
+ model User {
17
+ id String @id @default(uuid())
18
+ name String
19
+ email String @unique
20
+ password String
21
+ createdAt DateTime @default(now())
22
+ updatedAt DateTime @updatedAt
23
+ posts Post[]
24
+ userProfiles UserProfile[]
25
+ }
26
+
27
+ model Post {
28
+ id String @id @default(uuid())
29
+ title String
30
+ content String
31
+ createdAt DateTime @default(now())
32
+ updatedAt DateTime @updatedAt
33
+ authorId String
34
+ author User @relation(fields: [authorId], references: [id])
35
+ comments Comment[]
36
+ category Category? @relation(fields: [categoryId], references: [id])
37
+ categoryId String?
38
+ tag Tag? @relation(fields: [tagId], references: [id])
39
+ tagId String?
40
+ images Image[]
41
+ }
42
+
43
+ model Comment {
44
+ id String @id @default(uuid())
45
+ content String
46
+ createdAt DateTime @default(now())
47
+ updatedAt DateTime @updatedAt
48
+ postId String
49
+ post Post @relation(fields: [postId], references: [id])
50
+ }
51
+
52
+ model Category {
53
+ id String @id @default(uuid())
54
+ name String
55
+ createdAt DateTime @default(now())
56
+ updatedAt DateTime @updatedAt
57
+ posts Post[]
58
+ }
59
+
60
+ model Tag {
61
+ id String @id @default(uuid())
62
+ name String
63
+ createdAt DateTime @default(now())
64
+ updatedAt DateTime @updatedAt
65
+ posts Post[]
66
+ }
67
+
68
+ model Image {
69
+ id String @id @default(uuid())
70
+ url String
71
+ createdAt DateTime @default(now())
72
+ updatedAt DateTime @updatedAt
73
+ postId String
74
+ post Post @relation(fields: [postId], references: [id])
75
+ }
76
+
77
+ model UserProfile {
78
+ id String @id @default(uuid())
79
+ userId String
80
+ user User @relation(fields: [userId], references: [id])
81
+ createdAt DateTime @default(now())
82
+ updatedAt DateTime @updatedAt
83
+ }
84
+
85
+ enum UserRole {
86
+ ADMIN
87
+ USER
88
+ }
89
+
90
+ enum UserStatus {
91
+ ACTIVE
92
+ INACTIVE
93
+ PENDING
94
+ BLOCKED
95
+ }
96
+
97
+ enum UserGender {
98
+ MALE
99
+ FEMALE
100
+ OTHER
101
+ }
102
+
103
+ enum UserSubscription {
104
+ FREE
105
+ PREMIUM
106
+ ENTERPRISE
107
+ }
@@ -0,0 +1,16 @@
1
+
2
+ // This file was generated by Prisma and assumes you have installed the following:
3
+ // npm install --save-dev prisma dotenv
4
+ import "dotenv/config";
5
+ import { defineConfig, env } from "prisma/config";
6
+
7
+ export default defineConfig({
8
+ schema: "prisma/schema.prisma",
9
+ migrations: {
10
+ path: "prisma/migrations",
11
+ },
12
+ engine: "classic",
13
+ datasource: {
14
+ url: env("DATABASE_URL"),
15
+ },
16
+ });
package/bun.lock DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "configVersion": 1,
4
- "workspaces": {
5
- "": {
6
- "name": "prisma_dart_generator",
7
- "devDependencies": {
8
- "@types/bun": "latest",
9
- },
10
- "peerDependencies": {
11
- "typescript": "^5",
12
- },
13
- },
14
- },
15
- "packages": {
16
- "@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
17
-
18
- "@types/node": ["@types/node@25.0.8", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-powIePYMmC3ibL0UJ2i2s0WIbq6cg6UyVFQxSCpaPxxzAaziRfimGivjdF943sSGV6RADVbk0Nvlm5P/FB44Zg=="],
19
-
20
- "bun-types": ["bun-types@1.3.6", "", { "dependencies": { "@types/node": "*" } }, "sha512-OlFwHcnNV99r//9v5IIOgQ9Uk37gZqrNMCcqEaExdkVq3Avwqok1bJFmvGMCkCE0FqzdY8VMOZpfpR3lwI+CsQ=="],
21
-
22
- "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
23
-
24
- "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
25
- }
26
- }