1
1
/** @typedef {import('../attribute').Attribute} Attribute */
2
2
const { AttributeType } = require('../attribute');
3
3
const { LanguageMeta } = require("./language");
4
+ const fs = require('fs');
5
+ const path = require('path');
4
6
5
7
class Dart extends LanguageMeta {
8
+ getPackageName() {
9
+ const pubspecPath = path.join(this.getCurrentDirectory(), 'pubspec.yaml');
10
+ if (fs.existsSync(pubspecPath)) {
11
+ const pubspecContent = fs.readFileSync(pubspecPath, 'utf8');
12
+ const lines = pubspecContent.split('\n');
13
+
14
+ const dependenciesIndex = lines.findIndex(line => line.trim() === 'dependencies:');
15
+
16
+ if (dependenciesIndex !== -1) {
17
+ const indent = lines[dependenciesIndex].search(/\S|$/);
18
+ const dependencies = [];
19
+ for (let i = dependenciesIndex + 1; i < lines.length; i++) {
20
+ const line = lines[i];
21
+ if (line.trim() === '') continue;
22
+
23
+ const lineIndent = line.search(/\S|$/);
24
+ if (lineIndent < = indent && line.trim() !== '') {
25
+ break;
26
+ }
27
+
28
+ dependencies.push(line.trim());
29
+ }
30
+
31
+ if (dependencies.some(dep => dep.startsWith('dart_appwrite:'))) {
32
+ return 'dart_appwrite';
33
+ }
34
+ if (dependencies.some(dep => dep.startsWith('appwrite:'))) {
35
+ return 'appwrite';
36
+ }
37
+ }
38
+ }
39
+
40
+ return 'appwrite';
41
+ }
42
+
6
43
getType(attribute) {
7
44
let type = "";
8
45
switch (attribute.type) {
@@ -46,16 +83,15 @@ class Dart extends LanguageMeta {
46
83
}
47
84
48
85
getTemplate() {
49
- return `<% for (const attribute of collection.attributes) { -%>
86
+ return `import 'package:${this.getPackageName()}/models.dart';
87
+ <% for (const attribute of collection.attributes) { -%>
50
88
<% if (attribute.type === ' relationship' ) { -%>
51
89
import '<% - attribute.relatedCollection.toLowerCase() %> .dart';
52
90
53
91
<% } -%>
54
92
<% } -%>
55
- /**
56
- * This file is auto-generated by the Appwrite CLI.
57
- * You can regenerate it by running \`appwrite types -l dart ${this.getCurrentDirectory()}\`.
58
- */
93
+ /// This file is auto-generated by the Appwrite CLI.
94
+ /// You can regenerate it by running \`appwrite types -l dart ${this.getCurrentDirectory()}\`.
59
95
60
96
<% for (const attribute of collection.attributes) { -%>
61
97
<% if (attribute.format === ' enum' ) { -%>
@@ -67,19 +103,33 @@ enum <%- toPascalCase(attribute.key) %> {
67
103
68
104
<% } -%>
69
105
<% } -%>
70
- class <%= toPascalCase(collection.name) %> {
106
+ class <%= toPascalCase(collection.name) %> extends Document {
71
107
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
72
108
<% - getType(attribute) %> <%= toCamelCase(attribute.key) %> ;
73
109
<% } -%>
74
110
75
111
<%= toPascalCase(collection.name) %> ({
112
+ required super.$id,
113
+ required super.$collectionId,
114
+ required super.$databaseId,
115
+ required super.$createdAt,
116
+ required super.$updatedAt,
117
+ required super.$permissions,
118
+ required super.data,
76
119
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
77
120
<% if (attribute.required) { %> required <% } %> this.<%= toCamelCase(attribute.key) %><% if (index < collection.attributes.length - 1 ) { %> ,<% } %>
78
121
<% } -%>
79
122
});
80
123
81
124
factory <%= toPascalCase(collection.name) %> .fromMap(Map<String , dynamic > map) {
82
125
return <%= toPascalCase(collection.name) %> (
126
+ $id: map['\\$id'].toString(),
127
+ $collectionId: map['\\$collectionId'].toString(),
128
+ $databaseId: map['\\$databaseId'].toString(),
129
+ $createdAt: map['\\$createdAt'].toString(),
130
+ $updatedAt: map['\\$updatedAt'].toString(),
131
+ $permissions: List<String >.from(map['\\$permissions'] ?? []),
132
+ data: map,
83
133
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
84
134
<%= toCamelCase(attribute.key) %> : <% if (attribute.type === ' string' || attribute.type === ' email' || attribute.type === ' datetime' ) { -%>
85
135
<% if (attribute.format === ' enum' ) { -%>
@@ -130,6 +180,12 @@ map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollecti
130
180
131
181
Map<String , dynamic > toMap() {
132
182
return {
183
+ "\\$id": $id,
184
+ "\\$collectionId": $collectionId,
185
+ "\\$databaseId": $databaseId,
186
+ "\\$createdAt": $createdAt,
187
+ "\\$updatedAt": $updatedAt,
188
+ "\\$permissions": $permissions,
133
189
<% for (const [index, attribute] of Object .entries(collection.attributes)) { -%>
134
190
"<%= attribute.key %> ": <% if (attribute.type === ' relationship' ) { -%>
135
191
<% if ((attribute.relationType === ' oneToMany' && attribute.side === ' parent' ) || (attribute.relationType === ' manyToOne' && attribute.side === ' child' ) || attribute.relationType === ' manyToMany' ) { -%>
0 commit comments