Last active
November 8, 2024 23:12
-
-
Save Luckey-Elijah/78701a650e5585525d222c0a94e2583c to your computer and use it in GitHub Desktop.
A collection class that is helpful for mapping pocketbase collections to dart code repositories.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:http/http.dart' as http; | |
import 'package:meta/meta.dart'; | |
import 'package:pocketbase/pocketbase.dart' as pb; | |
mixin ToJson implements pb.Jsonable { | |
String get id; | |
DateTime get created; | |
DateTime get updated; | |
pb.RecordModel get recordModel; | |
} | |
abstract class Collection<T extends ToJson> { | |
const Collection(this.collection); | |
final pb.RecordService collection; | |
Future<T> create( | |
T object, { | |
List<String> removeKeys = const ['id'], | |
Map<String, dynamic> query = const {}, | |
List<http.MultipartFile> files = const [], | |
Map<String, String> headers = const {}, | |
String? expand, | |
String? fields, | |
}) async { | |
final body = object.toJson(); | |
removeKeys.forEach(body.remove); | |
final model = await collection.create( | |
body: body, | |
expand: expand, | |
fields: fields, | |
files: files, | |
headers: headers, | |
query: query, | |
); | |
return fromJson(recordModelToMap(model)); | |
} | |
Future<void> delete( | |
String id, { | |
Map<String, dynamic> body = const {}, | |
Map<String, dynamic> query = const {}, | |
Map<String, String> headers = const {}, | |
}) async { | |
await collection.delete( | |
id, | |
body: body, | |
query: query, | |
headers: headers, | |
); | |
} | |
Future<T> getFirstListItem( | |
String filter, { | |
String? expand, | |
String? fields, | |
Map<String, dynamic> query = const {}, | |
Map<String, String> headers = const {}, | |
}) async { | |
final model = await collection.getFirstListItem( | |
filter, | |
expand: expand, | |
fields: fields, | |
query: query, | |
headers: headers, | |
); | |
return fromJson(recordModelToMap(model)); | |
} | |
Future<List<T>> getFullList({ | |
int batch = 500, | |
String? expand, | |
String? filter, | |
String? sort, | |
String? fields, | |
Map<String, dynamic> query = const {}, | |
Map<String, String> headers = const {}, | |
}) async { | |
final results = await collection.getFullList( | |
batch: batch, | |
expand: expand, | |
filter: filter, | |
sort: sort, | |
fields: fields, | |
query: query, | |
headers: headers, | |
); | |
return [...results.map(recordModelToMap).map(fromJson)]; | |
} | |
Future<pb.ResultList<T>> getList({ | |
int page = 1, | |
int perPage = 30, | |
bool skipTotal = false, | |
String? expand, | |
String? filter, | |
String? sort, | |
String? fields, | |
Map<String, dynamic> query = const {}, | |
Map<String, String> headers = const {}, | |
}) async { | |
final results = await collection.getList( | |
page: page, | |
perPage: perPage, | |
skipTotal: skipTotal, | |
expand: expand, | |
filter: filter, | |
sort: sort, | |
fields: fields, | |
query: query, | |
headers: headers, | |
); | |
return pb.ResultList<T>( | |
items: [...results.items.map(recordModelToMap).map(fromJson)], | |
page: results.page, | |
perPage: results.perPage, | |
totalItems: results.totalItems, | |
totalPages: results.totalPages, | |
); | |
} | |
Future<T> getOne( | |
String id, { | |
String? expand, | |
String? fields, | |
Map<String, dynamic> query = const {}, | |
Map<String, String> headers = const {}, | |
}) async { | |
final model = await collection.getOne( | |
id, | |
expand: expand, | |
fields: fields, | |
query: query, | |
headers: headers, | |
); | |
return fromJson(recordModelToMap(model)); | |
} | |
Future<T> update( | |
String id, { | |
Map<String, dynamic> body = const {}, | |
Map<String, dynamic> query = const {}, | |
List<http.MultipartFile> files = const [], | |
Map<String, String> headers = const {}, | |
String? expand, | |
String? fields, | |
}) async { | |
final model = await collection.update( | |
id, | |
body: body, | |
query: query, | |
files: files, | |
expand: expand, | |
fields: fields, | |
headers: headers, | |
); | |
return fromJson(recordModelToMap(model)); | |
} | |
@protected | |
Map<String, dynamic> recordModelToMap(pb.RecordModel model) { | |
return { | |
...model.data, | |
'id': model.id, | |
'created': model.created, | |
'updated': model.updated, | |
'recordModel': model.toJson(), | |
if (model.expand.isNotEmpty) | |
...model.expand.map( | |
(k, v) => MapEntry(k, [...v.map(recordModelToMap)]), | |
), | |
}; | |
} | |
@protected | |
T fromJson(Map<String, dynamic> json); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment