Skip to content

Commit d36470c

Browse files
committed
chore: add setDevkey and upsertDocument methods
1 parent b046ac8 commit d36470c

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
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 "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: 15 additions & 0 deletions
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.
@@ -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: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,53 +91,54 @@ export class Databases {
9191
);
9292
}
9393
/**
94-
* Create new Documents. 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.
94+
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
9595
*
9696
* @param {string} databaseId
9797
* @param {string} collectionId
98-
* @param {object[]} documents
98+
* @param {string} documentId
99+
* @param {string[]} queries
99100
* @throws {AppwriteException}
100-
* @returns {Promise<Models.DocumentList<Document>>}
101+
* @returns {Promise<Document>}
101102
*/
102-
createDocuments<Document extends Models.Document>(databaseId: string, collectionId: string, documents: object[]): Promise<Models.DocumentList<Document>> {
103+
getDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, queries?: string[]): Promise<Document> {
103104
if (typeof databaseId === 'undefined') {
104105
throw new AppwriteException('Missing required parameter: "databaseId"');
105106
}
106107
if (typeof collectionId === 'undefined') {
107108
throw new AppwriteException('Missing required parameter: "collectionId"');
108109
}
109-
if (typeof documents === 'undefined') {
110-
throw new AppwriteException('Missing required parameter: "documents"');
110+
if (typeof documentId === 'undefined') {
111+
throw new AppwriteException('Missing required parameter: "documentId"');
111112
}
112-
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId);
113+
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
113114
const payload: Payload = {};
114-
if (typeof documents !== 'undefined') {
115-
payload['documents'] = documents;
115+
if (typeof queries !== 'undefined') {
116+
payload['queries'] = queries;
116117
}
117118
const uri = new URL(this.client.config.endpoint + apiPath);
118119

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

123123
return this.client.call(
124-
'post',
124+
'get',
125125
uri,
126126
apiHeaders,
127127
payload
128128
);
129129
}
130130
/**
131-
* Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
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.
132132
*
133133
* @param {string} databaseId
134134
* @param {string} collectionId
135135
* @param {string} documentId
136-
* @param {string[]} queries
136+
* @param {object} data
137+
* @param {string[]} permissions
137138
* @throws {AppwriteException}
138139
* @returns {Promise<Document>}
139140
*/
140-
getDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, queries?: string[]): Promise<Document> {
141+
upsertDocument<Document extends Models.Document>(databaseId: string, collectionId: string, documentId: string, data: object, permissions?: string[]): Promise<Document> {
141142
if (typeof databaseId === 'undefined') {
142143
throw new AppwriteException('Missing required parameter: "databaseId"');
143144
}
@@ -147,18 +148,25 @@ export class Databases {
147148
if (typeof documentId === 'undefined') {
148149
throw new AppwriteException('Missing required parameter: "documentId"');
149150
}
151+
if (typeof data === 'undefined') {
152+
throw new AppwriteException('Missing required parameter: "data"');
153+
}
150154
const apiPath = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId);
151155
const payload: Payload = {};
152-
if (typeof queries !== 'undefined') {
153-
payload['queries'] = queries;
156+
if (typeof data !== 'undefined') {
157+
payload['data'] = data;
158+
}
159+
if (typeof permissions !== 'undefined') {
160+
payload['permissions'] = permissions;
154161
}
155162
const uri = new URL(this.client.config.endpoint + apiPath);
156163

157164
const apiHeaders: { [header: string]: string } = {
165+
'content-type': 'application/json',
158166
}
159167

160168
return this.client.call(
161-
'get',
169+
'put',
162170
uri,
163171
apiHeaders,
164172
payload

0 commit comments

Comments
 (0)