-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
60 additions
and
230 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,6 @@ Future<void> main() async { | |
// Authenticate | ||
await pb.admins.authWithPassword('[email protected]', '01 password'); | ||
|
||
final resultList = await pb.collection('patient').getList( | ||
page: 1, | ||
perPage: 50, | ||
); | ||
resultList.items.forEach((record) => print(record.data['resource']['id'])); | ||
} | ||
final resultList = await pb.collection('Observation').getOne('8dazsc6sbge4tgh'); | ||
print(resultList.data); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,57 @@ | ||
// Function to create or update a record | ||
import 'dart:io'; | ||
|
||
import 'package:fhir_r4/fhir_r4.dart'; | ||
import 'package:pocketbase/pocketbase.dart'; | ||
|
||
class HttpOverridesImpl extends HttpOverrides { | ||
@override | ||
HttpClient createHttpClient(SecurityContext? context) { | ||
return super.createHttpClient(context) | ||
..badCertificateCallback = | ||
(X509Certificate cert, String host, int port) => true; | ||
} | ||
} | ||
|
||
Future<void> main() async { | ||
HttpOverrides.global = HttpOverridesImpl(); | ||
|
||
final PocketBase pb = PocketBase('http://127.0.0.1:8090'); | ||
|
||
try { | ||
await pb.admins.authWithPassword('[email protected]', '01 password'); | ||
createOrUpdateRecord(pb, patient); | ||
|
||
// final String patientFile = File('assets/Patient.ndjson').readAsStringSync(); | ||
// final List<String> patientStrings = patientFile.split('\n'); | ||
// patientStrings.removeWhere((e) => e == ''); | ||
// final List<dynamic> patients = | ||
// patientStrings.map((e) => jsonDecode(e)).toList(); | ||
// for (final dynamic patient in patients) { | ||
// if (patient is Map<String, dynamic>) { | ||
// createOrUpdateRecord(pb, patient); | ||
// } | ||
// } | ||
await uploadPatients(pb); | ||
} catch (e) { | ||
print(e); | ||
} | ||
} | ||
|
||
// Function to create or update a record | ||
Future<void> uploadPatients(PocketBase pb) async { | ||
final dir = Directory('assets/mimic_iv'); | ||
final List<FileSystemEntity> files = dir.listSync(); | ||
for (final FileSystemEntity file in files) { | ||
if (file is File) { | ||
final List<String> lines = file.readAsLinesSync(); | ||
for (final String line in lines) { | ||
final Resource resource = Resource.fromJsonString(line); | ||
await createOrUpdateResource(pb, resource); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class HttpOverridesImpl extends HttpOverrides { | ||
@override | ||
HttpClient createHttpClient(SecurityContext? context) { | ||
return super.createHttpClient(context) | ||
..badCertificateCallback = | ||
(X509Certificate cert, String host, int port) => true; | ||
} | ||
} | ||
|
||
Future<Resource> createOrUpdateResource( | ||
PocketBase pb, Resource resource) async => | ||
Resource.fromJson( | ||
await createOrUpdateRecord(pb, resource.prepareForUpload.toJson())); | ||
|
||
Future<Map<String, dynamic>> createOrUpdateRecord( | ||
PocketBase pb, Map<String, dynamic> resourceMap) async { | ||
final Map<String, dynamic> resource = resourceMapToBody(resourceMap); | ||
final Map<String, dynamic> body = resourceMapToBody(resourceMap); | ||
final String resourceType = resourceMap['resourceType']; | ||
try { | ||
final RecordModel recordModel = await pb | ||
.collection( | ||
resource['resource']['resourceType'].toString().toLowerCase()) | ||
.create(body: resource); | ||
final RecordModel recordModel = | ||
await pb.collection(resourceType.toLowerCase()).create(body: body); | ||
print(recordModel); | ||
if (recordModel.data['resource'] != null) { | ||
print('resource returned'); | ||
|
@@ -54,10 +62,8 @@ Future<Map<String, dynamic>> createOrUpdateRecord( | |
} catch (e) { | ||
print('Error creating or updating record: $e'); | ||
if (e is ClientException && e.statusCode == 404) { | ||
final RecordModel recordModel = await pb | ||
.collection( | ||
resource['resource']['resourceType'].toString().toLowerCase()) | ||
.create(body: resource); | ||
final RecordModel recordModel = | ||
await pb.collection(resourceType.toLowerCase()).create(body: body); | ||
if (recordModel.data['resource'] != null) { | ||
return recordModel.data['resource']; | ||
} else { | ||
|
@@ -69,14 +75,8 @@ Future<Map<String, dynamic>> createOrUpdateRecord( | |
} | ||
} | ||
|
||
Map<String, dynamic> patient = { | ||
'resourceType': 'Patient', | ||
'birthDate': '1981-09-18' | ||
}; | ||
|
||
Map<String, dynamic> resourceMapToBody(Map<String, dynamic> resourceMap) => { | ||
"resourceType": resourceMap['resourceType'], | ||
"versionId": 1, | ||
"lastUpdated": resourceMap['meta']['lastUpdated'], | ||
"resource": resourceMap, | ||
}; | ||
|
||
|
@@ -90,3 +90,22 @@ Map<String, dynamic> _operationOutcomes(String error) => <String, dynamic>{ | |
} | ||
] | ||
}; | ||
|
||
extension PocketbaseId on Resource { | ||
static String generate() => | ||
generateNewUuidString().replaceAll('-', '').substring(12, 27); | ||
|
||
Resource get newPocketbaseId { | ||
final Map<String, dynamic> json = this.toJson(); | ||
json['id'] = generate(); | ||
return Resource.fromJson(json); | ||
} | ||
|
||
Resource get newPocketbaseIdIfNoId => | ||
this.id != null && this.id.toString().length == 15 | ||
? this | ||
: newPocketbaseId; | ||
|
||
Resource get prepareForUpload => | ||
updateVersion(versionIdAsTime: true).newPocketbaseIdIfNoId; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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