Skip to content

Commit 9512fde

Browse files
committed
chore: add setDevkey and upsertDocument methods
1 parent a3e5399 commit 9512fde

File tree

7 files changed

+59
-29
lines changed

7 files changed

+59
-29
lines changed

docs/examples/databases/create-documents.md renamed to docs/examples/databases/upsert-document.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import 'package:appwrite/appwrite.dart';
22

33
Client client = Client()
44
.setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint
5-
.setKey(''); //
5+
.setProject('<YOUR_PROJECT_ID>'); // Your project ID
66

77
Databases databases = Databases(client);
88

9-
DocumentList result = await databases.createDocuments(
9+
Document result = await databases.upsertDocument(
1010
databaseId: '<DATABASE_ID>',
1111
collectionId: '<COLLECTION_ID>',
12-
documents: [],
12+
documentId: '<DOCUMENT_ID>',
13+
data: {},
14+
permissions: ["read("any")"], // optional
1315
);

lib/services/databases.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,42 @@ class Databases extends Service {
4848

4949
}
5050

51-
/// Create new Documents. Before using this route, you should create a new
52-
/// collection resource using either a [server
53-
/// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
54-
/// API or directly from your database console.
55-
Future<models.DocumentList> createDocuments({required String databaseId, required String collectionId, required List<Map> documents}) async {
56-
final String apiPath = '/databases/{databaseId}/collections/{collectionId}/documents'.replaceAll('{databaseId}', databaseId).replaceAll('{collectionId}', collectionId);
51+
/// Get a document by its unique ID. This endpoint response returns a JSON
52+
/// object with the document data.
53+
Future<models.Document> getDocument({required String databaseId, required String collectionId, required String documentId, List<String>? queries}) async {
54+
final String apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replaceAll('{databaseId}', databaseId).replaceAll('{collectionId}', collectionId).replaceAll('{documentId}', documentId);
5755

5856
final Map<String, dynamic> apiParams = {
59-
'documents': documents,
57+
'queries': queries,
6058
};
6159

6260
final Map<String, String> apiHeaders = {
63-
'content-type': 'application/json',
61+
6462
};
6563

66-
final res = await client.call(HttpMethod.post, path: apiPath, params: apiParams, headers: apiHeaders);
64+
final res = await client.call(HttpMethod.get, path: apiPath, params: apiParams, headers: apiHeaders);
6765

68-
return models.DocumentList.fromMap(res.data);
66+
return models.Document.fromMap(res.data);
6967

7068
}
7169

72-
/// Get a document by its unique ID. This endpoint response returns a JSON
73-
/// object with the document data.
74-
Future<models.Document> getDocument({required String databaseId, required String collectionId, required String documentId, List<String>? queries}) async {
70+
/// Create or update a Document. Before using this route, you should create a
71+
/// new collection resource using either a [server
72+
/// integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
73+
/// API or directly from your database console.
74+
Future<models.Document> upsertDocument({required String databaseId, required String collectionId, required String documentId, required Map data, List<String>? permissions}) async {
7575
final String apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replaceAll('{databaseId}', databaseId).replaceAll('{collectionId}', collectionId).replaceAll('{documentId}', documentId);
7676

7777
final Map<String, dynamic> apiParams = {
78-
'queries': queries,
78+
'data': data,
79+
'permissions': permissions,
7980
};
8081

8182
final Map<String, String> apiHeaders = {
82-
83+
'content-type': 'application/json',
8384
};
8485

85-
final res = await client.call(HttpMethod.get, path: apiPath, params: apiParams, headers: apiHeaders);
86+
final res = await client.call(HttpMethod.put, path: apiPath, params: apiParams, headers: apiHeaders);
8687

8788
return models.Document.fromMap(res.data);
8889

lib/src/client.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ abstract class Client {
7171
/// The user session to authenticate with.
7272
Client setSession(value);
7373

74+
/// Set DevKey.
75+
///
76+
/// Your secret dev API key.
77+
Client setDevKey(value);
78+
7479
/// Add headers that should be sent with all API calls.
7580
Client addHeader(String key, String value);
7681

lib/src/client_base.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ abstract class ClientBase implements Client {
1414
/// The user session to authenticate with
1515
@override
1616
ClientBase setSession(value);
17+
/// Your secret dev API key
18+
@override
19+
ClientBase setDevKey(value);
1720

1821
@override
1922
ClientBase setSelfSigned({bool status = true});

lib/src/client_browser.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ class ClientBrowser extends ClientBase with ClientMixin {
8484
addHeader('X-Appwrite-Session', value);
8585
return this;
8686
}
87+
/// Your secret dev API key
88+
@override
89+
ClientBrowser setDevKey(value) {
90+
config['devKey'] = value;
91+
addHeader('X-Appwrite-Dev-Key', value);
92+
return this;
93+
}
8794

8895
@override
8996
ClientBrowser setSelfSigned({bool status = true}) {

lib/src/client_io.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ class ClientIO extends ClientBase with ClientMixin {
113113
addHeader('X-Appwrite-Session', value);
114114
return this;
115115
}
116+
/// Your secret dev API key
117+
@override
118+
ClientIO setDevKey(value) {
119+
config['devKey'] = value;
120+
addHeader('X-Appwrite-Dev-Key', value);
121+
return this;
122+
}
116123

117124
@override
118125
ClientIO setSelfSigned({bool status = true}) {

test/services/databases_test.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,31 @@ void main() {
9898

9999
});
100100

101-
test('test method createDocuments()', () async {
101+
test('test method getDocument()', () async {
102102
final Map<String, dynamic> data = {
103-
'total': 5,
104-
'documents': [],};
103+
'\$id': '5e5ea5c16897e',
104+
'\$collectionId': '5e5ea5c15117e',
105+
'\$databaseId': '5e5ea5c15117e',
106+
'\$createdAt': '2020-10-15T06:38:00.000+00:00',
107+
'\$updatedAt': '2020-10-15T06:38:00.000+00:00',
108+
'\$permissions': [],};
105109

106110

107111
when(client.call(
108-
HttpMethod.post,
112+
HttpMethod.get,
109113
)).thenAnswer((_) async => Response(data: data));
110114

111115

112-
final response = await databases.createDocuments(
116+
final response = await databases.getDocument(
113117
databaseId: '<DATABASE_ID>',
114118
collectionId: '<COLLECTION_ID>',
115-
documents: [],
119+
documentId: '<DOCUMENT_ID>',
116120
);
117-
expect(response, isA<models.DocumentList>());
121+
expect(response, isA<models.Document>());
118122

119123
});
120124

121-
test('test method getDocument()', () async {
125+
test('test method upsertDocument()', () async {
122126
final Map<String, dynamic> data = {
123127
'\$id': '5e5ea5c16897e',
124128
'\$collectionId': '5e5ea5c15117e',
@@ -129,14 +133,15 @@ void main() {
129133

130134

131135
when(client.call(
132-
HttpMethod.get,
136+
HttpMethod.put,
133137
)).thenAnswer((_) async => Response(data: data));
134138

135139

136-
final response = await databases.getDocument(
140+
final response = await databases.upsertDocument(
137141
databaseId: '<DATABASE_ID>',
138142
collectionId: '<COLLECTION_ID>',
139143
documentId: '<DOCUMENT_ID>',
144+
data: {},
140145
);
141146
expect(response, isA<models.Document>());
142147

0 commit comments

Comments
 (0)