1
0
Fork 0
mirror of synced 2024-08-23 06:02:13 +12:00
appwrite/app/sdks/client-flutter/lib/services/database.dart

121 lines
4.7 KiB
Dart
Raw Normal View History

2020-03-27 02:20:07 +13:00
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
import 'package:dio/dio.dart';
2020-03-27 02:20:07 +13:00
import 'package:meta/meta.dart';
2020-04-06 01:23:15 +12:00
import "../client.dart";
2020-03-27 02:20:07 +13:00
import '../enums.dart';
2020-04-06 01:23:15 +12:00
import "../service.dart";
2020-01-31 05:18:59 +13:00
class Database extends Service {
2020-04-06 01:23:15 +12:00
Database(Client client): super(client);
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
/// List Documents
///
2020-01-31 05:18:59 +13:00
/// 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](/docs/admin).
2020-04-11 22:02:21 +12:00
///
2020-05-02 16:23:56 +12:00
Future<Response> listDocuments({@required String collectionId, List filters = const [], int offset = 0, int limit = 50, String orderField = '\$id', OrderType orderType = OrderType.asc, String orderCast = 'string', String search = '', int first = 0, int last = 0}) {
2020-04-06 01:23:15 +12:00
final String path = '/database/collections/{collectionId}/documents'.replaceAll(RegExp('{collectionId}'), collectionId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'filters': filters,
'offset': offset,
'limit': limit,
2020-05-02 16:23:56 +12:00
'orderField': orderField,
'orderType': orderType.name(),
'orderCast': orderCast,
2020-04-06 01:23:15 +12:00
'search': search,
'first': first,
'last': last,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Document
///
2020-06-17 22:12:45 +12:00
/// Create a new Document. Before using this route, you should create a new
/// collection resource using either a [server
/// integration](/docs/server/database?sdk=nodejs#createCollection) API or
/// directly from your database console.
2020-04-11 22:02:21 +12:00
///
2020-04-06 16:55:38 +12:00
Future<Response> createDocument({@required String collectionId, @required dynamic data, @required List read, @required List write, String parentDocument = '', String parentProperty = '', String parentPropertyType = 'assign'}) {
2020-04-06 01:23:15 +12:00
final String path = '/database/collections/{collectionId}/documents'.replaceAll(RegExp('{collectionId}'), collectionId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'data': data,
'read': read,
'write': write,
'parentDocument': parentDocument,
'parentProperty': parentProperty,
'parentPropertyType': parentPropertyType,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get Document
///
2020-01-31 05:18:59 +13:00
/// Get document by its unique ID. This endpoint response returns a JSON object
/// with the document data.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> getDocument({@required String collectionId, @required String documentId}) {
final String path = '/database/collections/{collectionId}/documents/{documentId}'.replaceAll(RegExp('{collectionId}'), collectionId).replaceAll(RegExp('{documentId}'), documentId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update Document
2020-04-06 01:23:15 +12:00
Future<Response> updateDocument({@required String collectionId, @required String documentId, @required dynamic data, @required List read, @required List write}) {
final String path = '/database/collections/{collectionId}/documents/{documentId}'.replaceAll(RegExp('{collectionId}'), collectionId).replaceAll(RegExp('{documentId}'), documentId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'data': data,
'read': read,
'write': write,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.patch, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Delete Document
///
2020-01-31 05:18:59 +13:00
/// 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.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> deleteDocument({@required String collectionId, @required String documentId}) {
final String path = '/database/collections/{collectionId}/documents/{documentId}'.replaceAll(RegExp('{collectionId}'), collectionId).replaceAll(RegExp('{documentId}'), documentId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.delete, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
}