Skip to content

Commit f63e971

Browse files
committed
chore: add setDevkey and upsertDocument methods
1 parent 796d461 commit f63e971

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
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,14 +2,16 @@ import { Client, Databases } from "react-native-appwrite";
22

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

77
const databases = new Databases(client);
88

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

1517
console.log(result);

src/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class Client {
108108
jwt: '',
109109
locale: '',
110110
session: '',
111+
devkey: '',
111112
platform: '',
112113
};
113114
headers: Headers = {
@@ -226,6 +227,21 @@ class Client {
226227
return this;
227228
}
228229

230+
/**
231+
* Set DevKey
232+
*
233+
* Your secret dev API key
234+
*
235+
* @param value string
236+
*
237+
* @return {this}
238+
*/
239+
setDevKey(value: string): this {
240+
this.headers['X-Appwrite-Dev-Key'] = value;
241+
this.config.devkey = value;
242+
return this;
243+
}
244+
229245

230246
private realtime: Realtime = {
231247
socket: undefined,

src/services/databases.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,17 @@ export class Databases extends Service {
9797
}
9898

9999
/**
100-
* Create new Documents. Before using this route, you should create a new
101-
* collection resource using either a [server
102-
* integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
103-
* API or directly from your database console.
100+
* Get a document by its unique ID. This endpoint response returns a JSON
101+
* object with the document data.
104102
*
105103
* @param {string} databaseId
106104
* @param {string} collectionId
107-
* @param {object[]} documents
105+
* @param {string} documentId
106+
* @param {string[]} queries
108107
* @throws {AppwriteException}
109108
* @returns {Promise}
110109
*/
111-
createDocuments<Document extends Models.Document>(databaseId: string, collectionId: string, documents: object[]): Promise<Models.DocumentList<Document>> {
110+
getDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, queries?: string[]): Promise<Document> {
112111
if (typeof databaseId === 'undefined') {
113112
throw new AppwriteException('Missing required parameter: "databaseId"');
114113
}
@@ -117,35 +116,37 @@ export class Databases extends Service {
117116
throw new AppwriteException('Missing required parameter: "collectionId"');
118117
}
119118

120-
if (typeof documents === 'undefined') {
121-
throw new AppwriteException('Missing required parameter: "documents"');
119+
if (typeof documentId === 'undefined') {
120+
throw new AppwriteException('Missing required parameter: "documentId"');
122121
}
123122

124-
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId);
123+
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
125124
const payload: Payload = {};
126125

127-
if (typeof documents !== 'undefined') {
128-
payload['documents'] = documents;
126+
if (typeof queries !== 'undefined') {
127+
payload['queries'] = queries;
129128
}
130129

131130
const uri = new URL(this.client.config.endpoint + apiPath);
132-
return this.client.call('post', uri, {
133-
'content-type': 'application/json',
131+
return this.client.call('get', uri, {
134132
}, payload);
135133
}
136134

137135
/**
138-
* Get a document by its unique ID. This endpoint response returns a JSON
139-
* object with the document data.
136+
* Create or update a Document. Before using this route, you should create a
137+
* new collection resource using either a [server
138+
* integration](https://appwrite.io/docs/server/databases#databasesCreateCollection)
139+
* API or directly from your database console.
140140
*
141141
* @param {string} databaseId
142142
* @param {string} collectionId
143143
* @param {string} documentId
144-
* @param {string[]} queries
144+
* @param {object} data
145+
* @param {string[]} permissions
145146
* @throws {AppwriteException}
146147
* @returns {Promise}
147148
*/
148-
getDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, queries?: string[]): Promise<Document> {
149+
upsertDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, data: object, permissions?: string[]): Promise<Document> {
149150
if (typeof databaseId === 'undefined') {
150151
throw new AppwriteException('Missing required parameter: "databaseId"');
151152
}
@@ -158,15 +159,24 @@ export class Databases extends Service {
158159
throw new AppwriteException('Missing required parameter: "documentId"');
159160
}
160161

162+
if (typeof data === 'undefined') {
163+
throw new AppwriteException('Missing required parameter: "data"');
164+
}
165+
161166
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
162167
const payload: Payload = {};
163168

164-
if (typeof queries !== 'undefined') {
165-
payload['queries'] = queries;
169+
if (typeof data !== 'undefined') {
170+
payload['data'] = data;
171+
}
172+
173+
if (typeof permissions !== 'undefined') {
174+
payload['permissions'] = permissions;
166175
}
167176

168177
const uri = new URL(this.client.config.endpoint + apiPath);
169-
return this.client.call('get', uri, {
178+
return this.client.call('put', uri, {
179+
'content-type': 'application/json',
170180
}, payload);
171181
}
172182

0 commit comments

Comments
 (0)