Skip to content

chore: add setDevkey and upsertDocument methods #57

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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change log

## 0.9.0

* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage
* Update default `quality` for `getFilePreview` from 0 to -1
* Remove `Gif` from ImageFormat enum
* Remove `search` param from `listExecutions` method

## 0.7.4

* Upgrade dependencies to resolve PlatformConstants error with Expo 53
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 "react-native-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": "react-native-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": "0.9.0",
"version": "0.9.1",
"license": "BSD-3-Clause",
"main": "dist/cjs/sdk.js",
"exports": {
Expand Down
18 changes: 17 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ class Client {
jwt: '',
locale: '',
session: '',
devkey: '',
platform: '',
};
headers: Headers = {
'x-sdk-name': 'React Native',
'x-sdk-platform': 'client',
'x-sdk-language': 'reactnative',
'x-sdk-version': '0.9.0',
'x-sdk-version': '0.9.1',
'X-Appwrite-Response-Format': '1.7.0',
};

Expand Down Expand Up @@ -226,6 +227,21 @@ class Client {
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
48 changes: 48 additions & 0 deletions src/services/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,54 @@ export class Databases extends Service {
}, 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}
*/
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);
return this.client.call('put', uri, {
'content-type': 'application/json',
}, payload);
}

/**
* Update a document by its unique ID. Using the patch method you can pass
* only specific fields that will get updated.
Expand Down