Version 0.6.2  
Docs

Database API

Client integration with     code examples.


The Database service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access permissions.

All the data in the database service is stored in structured JSON documents. The Appwrite database service also allows you to nest child documents in parent documents and use deep filters to both search and query your data.

Each database document structure in your project is defined using the Appwrite collection rules. The collections rules help you ensure all your user-submitted data is validated and stored according to the collection structure.

Using Appwrite permissions architecture, you can assign read or write access to each document in your project for either a specific user, team, user role, or even grant it with public access (*). You can learn more about how Appwrite handles permissions and access control.

Create Document

POST/database/collections/{collectionId}/documents

Create a new Document. Before using this route, you should create a new collection resource using either a server integration API or directly from your database console.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

data required object

Document data as JSON object.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. learn more about permissions and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

parentDocument optional string

Parent document unique ID. Use when you want your new document to be a child of a parent document.

parentProperty optional string

Parent document property name. Use when you want your new document to be a child of a parent document.

parentPropertyType optional string

Parent document property connection type. You can set this value to assign, append or prepend, default value is assign. Use when you want your new document to be a child of a parent document.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.database.createDocument('[COLLECTION_ID]', {}, [], []);
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.createDocument(
        collectionId: '[COLLECTION_ID]',
        data: {},
        read: [],
        write: [],
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.createDocument(
        collectionId: '[COLLECTION_ID]',
        data: {},
        read: [],
        write: [],
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }

List Documents

GET/database/collections/{collectionId}/documents

Get a list of all the user documents. You can use the query params to filter your results. On admin mode, this endpoint will return a list of all of the project documents. Learn more about different API modes.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

filters optional array

Array of filter strings. Each filter is constructed from a key name, comparison operator (=, !=, >, <, <=, >=) and a value. You can also use a dot (.) separator in attribute names to filter by child document attributes. Examples: 'name=John Doe' or 'category.$id>=5bed2d152c362'.

offset optional integer

Offset value. Use this value to manage pagination.

limit optional integer

Maximum number of documents to return in response. Use this value to manage pagination.

orderField optional string

Document field that results will be sorted by.

orderType optional string

Order direction. Possible values are DESC for descending order, or ASC for ascending order.

orderCast optional string

Order field type casting. Possible values are int, string, date, time or datetime. The database will attempt to cast the order field to the value you pass here. The default value is a string.

search optional string

Search query. Enter any free text search. The database will try to find a match against all document attributes and children.

first optional integer

Return only the first document. Pass 1 for true or 0 for false. The default value is 0.

last optional integer

Return only the last document. Pass 1 for true or 0 for false. The default value is 0.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.database.listDocuments('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.listDocuments(
        collectionId: '[COLLECTION_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.listDocuments(
        collectionId: '[COLLECTION_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }

Get Document

GET/database/collections/{collectionId}/documents/{documentId}

Get document by its unique ID. This endpoint response returns a JSON object with the document data.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.database.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.getDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.getDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }

Update Document

PATCH/database/collections/{collectionId}/documents/{documentId}

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

data required object

Document data as JSON object.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. learn more about permissions and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.database.updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', {}, [], []);
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.updateDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
        data: {},
        read: [],
        write: [],
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.updateDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
        data: {},
        read: [],
        write: [],
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }

Delete Document

DELETE/database/collections/{collectionId}/documents/{documentId}

Delete document by its unique ID. This endpoint deletes only the parent documents, his attributes and relations to other documents. Child documents will not be deleted.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
    ;
    
    let promise = sdk.database.deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response); // Success
    }, function (error) {
        console.log(error); // Failure
    });
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.deleteDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }
  • import 'package:appwrite/appwrite.dart';
    
    void main() { // Init SDK
      Client client = Client();
      Database database = Database(client);
    
      client
        .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
        .setProject('5df5acd0d48c2') // Your project ID
      ;
    
      Future result = database.deleteDocument(
        collectionId: '[COLLECTION_ID]',
        documentId: '[DOCUMENT_ID]',
      );
    
      result
        .then((response) {
          print(response);
        }).catchError((error) {
          print(error.response);
      });
    }