Skip to content

Commit

Permalink
simplifying dart files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dokotela committed Nov 22, 2024
1 parent 438af1f commit c923be6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 230 deletions.
9 changes: 3 additions & 6 deletions dart/find.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
95 changes: 57 additions & 38 deletions dart/init_resources.dart
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');
Expand All @@ -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 {
Expand All @@ -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,
};

Expand All @@ -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;
}
76 changes: 0 additions & 76 deletions dart/pb_repository.dart

This file was deleted.

17 changes: 0 additions & 17 deletions dart/pocketbase_id.dart

This file was deleted.

90 changes: 0 additions & 90 deletions dart/upload_account.dart

This file was deleted.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ require (
golang.org/x/crypto/x509roots/fallback v0.0.0-20240507223354-67b13616a595 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/image v0.22.0 // indirect
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.9.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,6 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f h1:23H/YlmTHfmmvpZ+ajKZL0qLz0+IwFOIqQA0mQbmLeM=
golang.org/x/mobile v0.0.0-20241108191957-fa514ef75a0f/go.mod h1:UbSUP4uu/C9hw9R2CkojhXlAxvayHjBdU9aRvE+c1To=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down

0 comments on commit c923be6

Please sign in to comment.