Skip to content

chore: add setDevKey and upsetDocument method #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { Client, Account } from "appwrite";
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:

```html
<script src="https://cdn.jsdelivr.net/npm/appwrite@18.0.0"></script>
<script src="https://cdn.jsdelivr.net/npm/appwrite@18.1.0"></script>
```


Expand Down
17 changes: 17 additions & 0 deletions docs/examples/databases/upsert-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Client, Databases } from "appwrite";

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

const databases = new Databases(client);

const result = await databases.upsertDocument(
'<DATABASE_ID>', // databaseId
'<COLLECTION_ID>', // collectionId
'<DOCUMENT_ID>', // documentId
{}, // data
["read("any")"] // permissions (optional)
);

console.log(result);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "appwrite",
"homepage": "https://appwrite.io/support",
"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",
"version": "18.0.0",
"version": "18.1.0",
"license": "BSD-3-Clause",
"main": "dist/cjs/sdk.js",
"exports": {
Expand Down
17 changes: 16 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class Client {
jwt: '',
locale: '',
session: '',
devkey: '',
};
/**
* Custom headers for API requests.
Expand All @@ -315,7 +316,7 @@ class Client {
'x-sdk-name': 'Web',
'x-sdk-platform': 'client',
'x-sdk-language': 'web',
'x-sdk-version': '18.0.0',
'x-sdk-version': '18.1.0',
'X-Appwrite-Response-Format': '1.7.0',
};

Expand Down Expand Up @@ -409,6 +410,20 @@ class Client {
this.config.session = value;
return this;
}
/**
* Set DevKey
*
* Your secret dev API key
*
* @param value string
*
* @return {this}
*/
setDevKey(value: string): this {
this.headers['X-Appwrite-Dev-Key'] = value;
this.config.devkey = value;
return this;
}

private realtime: Realtime = {
socket: undefined,
Expand Down
45 changes: 45 additions & 0 deletions src/services/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,51 @@ export class Databases {
payload
);
}
/**
* 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.
*
* @param {string} databaseId
* @param {string} collectionId
* @param {string} documentId
* @param {object} data
* @param {string[]} permissions
* @throws {AppwriteException}
* @returns {Promise<Document>}
*/
upsertDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, data: object, permissions?: string[]): Promise<Document> {
if (typeof databaseId === 'undefined') {
throw new AppwriteException('Missing required parameter: "databaseId"');
}
if (typeof collectionId === 'undefined') {
throw new AppwriteException('Missing required parameter: "collectionId"');
}
if (typeof documentId === 'undefined') {
throw new AppwriteException('Missing required parameter: "documentId"');
}
if (typeof data === 'undefined') {
throw new AppwriteException('Missing required parameter: "data"');
}
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
const payload: Payload = {};
if (typeof data !== 'undefined') {
payload['data'] = data;
}
if (typeof permissions !== 'undefined') {
payload['permissions'] = permissions;
}
const uri = new URL(this.client.config.endpoint + apiPath);

const apiHeaders: { [header: string]: string } = {
'content-type': 'application/json',
}

return this.client.call(
'put',
uri,
apiHeaders,
payload
);
}
/**
* Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated.
*
Expand Down