Skip to content

Commit 5a0e36c

Browse files
authored
Merge pull request #126 from appwrite/dev
chore: add setDevKey and upsetDocument method
2 parents 12e0762 + dc2f617 commit 5a0e36c

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { Client, Account } from "appwrite";
3333
To install with a CDN (content delivery network) add the following scripts to the bottom of your <body> tag, but before you use any Appwrite services:
3434

3535
```html
36-
<script src="https://cdn.jsdelivr.net/npm/appwrite@18.0.0"></script>
36+
<script src="https://cdn.jsdelivr.net/npm/appwrite@18.1.0"></script>
3737
```
3838

3939

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Client, Databases } from "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": "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": "18.0.0",
5+
"version": "18.1.0",
66
"license": "BSD-3-Clause",
77
"main": "dist/cjs/sdk.js",
88
"exports": {

src/client.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class Client {
307307
jwt: '',
308308
locale: '',
309309
session: '',
310+
devkey: '',
310311
};
311312
/**
312313
* Custom headers for API requests.
@@ -315,7 +316,7 @@ class Client {
315316
'x-sdk-name': 'Web',
316317
'x-sdk-platform': 'client',
317318
'x-sdk-language': 'web',
318-
'x-sdk-version': '18.0.0',
319+
'x-sdk-version': '18.1.0',
319320
'X-Appwrite-Response-Format': '1.7.0',
320321
};
321322

@@ -409,6 +410,20 @@ class Client {
409410
this.config.session = value;
410411
return this;
411412
}
413+
/**
414+
* Set DevKey
415+
*
416+
* Your secret dev API key
417+
*
418+
* @param value string
419+
*
420+
* @return {this}
421+
*/
422+
setDevKey(value: string): this {
423+
this.headers['X-Appwrite-Dev-Key'] = value;
424+
this.config.devkey = value;
425+
return this;
426+
}
412427

413428
private realtime: Realtime = {
414429
socket: undefined,

src/services/databases.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,51 @@ export class Databases {
127127
payload
128128
);
129129
}
130+
/**
131+
* Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
132+
*
133+
* @param {string} databaseId
134+
* @param {string} collectionId
135+
* @param {string} documentId
136+
* @param {object} data
137+
* @param {string[]} permissions
138+
* @throws {AppwriteException}
139+
* @returns {Promise<Document>}
140+
*/
141+
upsertDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, data: object, permissions?: string[]): Promise<Document> {
142+
if (typeof databaseId === 'undefined') {
143+
throw new AppwriteException('Missing required parameter: "databaseId"');
144+
}
145+
if (typeof collectionId === 'undefined') {
146+
throw new AppwriteException('Missing required parameter: "collectionId"');
147+
}
148+
if (typeof documentId === 'undefined') {
149+
throw new AppwriteException('Missing required parameter: "documentId"');
150+
}
151+
if (typeof data === 'undefined') {
152+
throw new AppwriteException('Missing required parameter: "data"');
153+
}
154+
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
155+
const payload: Payload = {};
156+
if (typeof data !== 'undefined') {
157+
payload['data'] = data;
158+
}
159+
if (typeof permissions !== 'undefined') {
160+
payload['permissions'] = permissions;
161+
}
162+
const uri = new URL(this.client.config.endpoint + apiPath);
163+
164+
const apiHeaders: { [header: string]: string } = {
165+
'content-type': 'application/json',
166+
}
167+
168+
return this.client.call(
169+
'put',
170+
uri,
171+
apiHeaders,
172+
payload
173+
);
174+
}
130175
/**
131176
* Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated.
132177
*

0 commit comments

Comments
 (0)