Last updated:
0 purchases
realm repo
Realm Repo #
A flutter project to make realm easy to work with by declaring a database, online or offline, and repositories for single objects and collections
This package just exposes the necessary CRUD functions
Web support is not yet supported as realm doesn't support web
Entities #
To create a realm model annotate the class with @RealmModel and the name of the class should start
with an underscore '_' and a primary key annotation @PrimaryKey
For
Example
import 'package:realm/realm.dart';
part 'realm_model_example.g.dart';
@RealmModel()
class _MyRealmObject {
@PrimaryKey()
@MapTo('_id')
late ObjectId id;
late String name;
}
copied to clipboard
To Generate build classes use this command and make sure to import part '$name.g.dart'
flutter pub run realm generate
copied to clipboard
Realm Repositories #
RealmRepositories are collections self-contained as a type in the repositories
Just add this import
import 'package:realm_repo/realm_repo.dart';
copied to clipboard
OnlineValue
To store a single object in cloud atlas
FOR OnlineValueRealmRepository YOU SHOULD USE @MapTo('
Be careful
Example
late final OnlineValueRealmRepository<MyRealmObject> myUserDataRepository =
OnlineValueRealmRepository(appId: appId, user: user, schemas: [MyRealmObject.schema]);
copied to clipboard
OfflineValue
To store a single object in offline database
Example
final OfflineValueRealmRepository<MyRealmObject> myUserDataRepository =
OfflineValueRealmRepository(schemas: [MyRealmObject.schema]);
copied to clipboard
OnlineCollection
To store a collection of object in online database
Example
late final OnlineCollectionRealmRepository<MyRealmObject> allUserDataRepository =
OnlineCollectionRealmRepository(appId: appId, user: user, schemas: [MyRealmObject.schema]);
copied to clipboard
OfflineCollection
To store a collection of objects in offline database
Example
final OfflineCollectionRealmRepository<MyRealmObject>allUserDataRepository =
OfflineCollectionRealmRepository(schemas: [MyRealmObject.schema]);
copied to clipboard
Realm Databases #
To implement a realm database you can extend the RealmOnlineDatabase or RealmOfflineDatabase in any class and declare
the respective repositories. This gives an advantage as it can make your database flexible in times of dilemma
Online:
class MyOnlineDatabase extends RealmOnlineDatabase {
MyOnlineDatabase({required super.appId});
late final OnlineValueRealmRepository<MyRealmObject>
myUserDataRepository = OnlineValueRealmRepository(
appId: appId, user: user, schemas: [MyRealmObject.schema]);
late final OnlineCollectionRealmRepository<MyRealmObject>
allUserDataRepository = OnlineCollectionRealmRepository(
appId: appId, user: user, schemas: [MyRealmObject.schema]);
}
Future<void> main () async {
/// Use online database to store collections and single value objects
MyOnlineDatabase onlineDb = MyOnlineDatabase(appId: 'Insert APP ID HERE');
/// Initialize the database
await onlineDb.init();
}
copied to clipboard
Offline:
/// Same as described in MyOnlineDatabase but without appId
class MyOfflineDatabase extends RealmOfflineDatabase {
MyOfflineDatabase();
final OfflineValueRealmRepository<MyRealmObject> myUserDataRepository =
OfflineValueRealmRepository(schemas: [MyRealmObject.schema]);
final OfflineCollectionRealmRepository<MyRealmObject>
allUserDataRepository =
OfflineCollectionRealmRepository(schemas: [MyRealmObject.schema]);
}
Future<void> main () async {
/// Use offline database to store collections and single value objects
MyOfflineDatabase offlineDb = MyOfflineDatabase();
}
copied to clipboard
Schema
@RealmModel()
class _MyRealmObject {
@PrimaryKey()
@MapTo('_id')
late ObjectId id;
late String name;
@override
int get hashCode => Object.hash(id, name);
@override
bool operator ==(Object other) {
return super.hashCode == other.hashCode;
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.