Masamune Model Functions
[GitHub](https://github.com/mathrunet) | [YouTube](https://www.youtube.com/c/mathrunetchannel) | [Packages](https://pub.flutter-io.cn/publishers/mathru.net/packages) | [X](https://x.com/mathru) | [LinkedIn](https://www.linkedin.com/in/mathrunet/) | [mathru.net](https://mathru.net)
Masamune Model Functions
Usage
Installation
- Add the package to your project.
flutter pub add masamune_model_functions
Setup
- Register the adapter with your model configuration. Supply a configured
FunctionsAdapterthat connects to your backend (e.g., Firebase Functions, Cloud Run).
// lib/main.dart
import 'package:masamune_model_functions/masamune_model_functions.dart';
import 'package:katana_functions_firebase/katana_functions_firebase.dart';
final functions = FirebaseFunctionsAdapter(
options: DefaultFirebaseOptions.currentPlatform,
region: FirebaseRegion.asiaNortheast1, // Your region
);
final modelAdapter = FunctionsModelAdapter(
functionsAdapter: functions,
);
void main() {
runMasamuneApp(
appRef: appRef,
modelAdapter: modelAdapter,
(appRef, _) => MasamuneApp(
appRef: appRef,
home: HomePage(),
),
);
}
Basic Operations
- Execute Masamune model operations as usual. All reads and writes are proxied through your backend Functions.
Load a Collection:
class MyPage extends PageScopedWidget {
@override
Widget build(BuildContext context, PageRef ref) {
final users = ref.app.model(UserModel.collection())..load();
return ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
final user = users[index].value;
return ListTile(
title: Text(user?.name ?? ''),
);
},
);
}
}
Save a Document:
final collection = ref.app.model(UserModel.collection());
final newDoc = collection.create();
await newDoc.save(
UserModel(
name: 'John Doe',
email: 'john@example.com',
),
);
Backend Implementation
Your backend must implement handlers for the following actions:
document_model: Handle document load/save/delete operationscollection_model: Handle collection load operationsaggregate_model: Handle aggregation queries (count, sum, etc.)
Example Cloud Functions Handler:
// Cloud Functions
export const model = functions.https.onCall(async (data, context) => {
const { action, path, query, value } = data;
switch (action) {
case "document_model":
// Handle document operations
return await handleDocument(path, value);
case "collection_model":
// Handle collection queries
return await handleCollection(path, query);
case "aggregate_model":
// Handle aggregations
return await handleAggregate(path, query);
}
});
Custom Action Names
- Customize the action names if your Functions use different identifiers:
final adapter = FunctionsModelAdapter(
functionsAdapter: functions,
documentAction: 'document_model_custom',
collectionAction: 'collection_model_custom',
aggregateAction: 'aggregate_model_custom',
);
Aggregations
- Perform aggregation queries through your backend:
final count = await ref.app.model(
UserModel.collection().limitTo(100),
).aggregate(ModelAggregateQuery.count());
print("Total users: $count");
Ensure your server action returns numeric values that can be cast to the requested type.
GitHub Sponsors
Sponsors are always welcome. Thank you for your support!
Libraries
- masamune_model_functions
- A package for generating models for interacting with external databases and Firestore using Firebase Functions.