Skip to content

Commit c9213cc

Browse files
authored
Merge pull request #57 from appwrite/dev
chore: add setDevkey and upsertDocument methods
2 parents 7b21d10 + f63e971 commit c9213cc

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change log
22

3+
## 0.9.0
4+
5+
* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage
6+
* Update default `quality` for `getFilePreview` from 0 to -1
7+
* Remove `Gif` from ImageFormat enum
8+
* Remove `search` param from `listExecutions` method
9+
310
## 0.7.4
411

512
* Upgrade dependencies to resolve PlatformConstants error with Expo 53
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Client, Databases } from "react-native-appwrite";
2+
3+
const client = new Client()
4+
.setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint
5+
.setProject('<YOUR_PROJECT_ID>'); // Your project ID
6+
7+
const databases = new Databases(client);
8+
9+
const result = await databases.upsertDocument(
10+
'<DATABASE_ID>', // databaseId
11+
'<COLLECTION_ID>', // collectionId
12+
'<DOCUMENT_ID>', // documentId
13+
{}, // data
14+
["read("any")"] // permissions (optional)
15+
);
16+
17+
console.log(result);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-native-appwrite",
33
"homepage": "https://appwrite.io/support",
44
"description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API",
5-
"version": "0.9.0",
5+
"version": "0.9.1",
66
"license": "BSD-3-Clause",
77
"main": "dist/cjs/sdk.js",
88
"exports": {

src/client.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ class Client {
108108
jwt: '',
109109
locale: '',
110110
session: '',
111+
devkey: '',
111112
platform: '',
112113
};
113114
headers: Headers = {
114115
'x-sdk-name': 'React Native',
115116
'x-sdk-platform': 'client',
116117
'x-sdk-language': 'reactnative',
117-
'x-sdk-version': '0.9.0',
118+
'x-sdk-version': '0.9.1',
118119
'X-Appwrite-Response-Format': '1.7.0',
119120
};
120121

@@ -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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,54 @@ export class Databases extends Service {
132132
}, payload);
133133
}
134134

135+
/**
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.
140+
*
141+
* @param {string} databaseId
142+
* @param {string} collectionId
143+
* @param {string} documentId
144+
* @param {object} data
145+
* @param {string[]} permissions
146+
* @throws {AppwriteException}
147+
* @returns {Promise}
148+
*/
149+
upsertDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, data: object, permissions?: string[]): Promise<Document> {
150+
if (typeof databaseId === 'undefined') {
151+
throw new AppwriteException('Missing required parameter: "databaseId"');
152+
}
153+
154+
if (typeof collectionId === 'undefined') {
155+
throw new AppwriteException('Missing required parameter: "collectionId"');
156+
}
157+
158+
if (typeof documentId === 'undefined') {
159+
throw new AppwriteException('Missing required parameter: "documentId"');
160+
}
161+
162+
if (typeof data === 'undefined') {
163+
throw new AppwriteException('Missing required parameter: "data"');
164+
}
165+
166+
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
167+
const payload: Payload = {};
168+
169+
if (typeof data !== 'undefined') {
170+
payload['data'] = data;
171+
}
172+
173+
if (typeof permissions !== 'undefined') {
174+
payload['permissions'] = permissions;
175+
}
176+
177+
const uri = new URL(this.client.config.endpoint + apiPath);
178+
return this.client.call('put', uri, {
179+
'content-type': 'application/json',
180+
}, payload);
181+
}
182+
135183
/**
136184
* Update a document by its unique ID. Using the patch method you can pass
137185
* only specific fields that will get updated.

0 commit comments

Comments
 (0)