masamune_model_functions 3.4.6 copy "masamune_model_functions: ^3.4.6" to clipboard
masamune_model_functions: ^3.4.6 copied to clipboard

A package for generating models for interacting with external databases and Firestore using Functions.

Masamune logo

Masamune Model Functions

Follow on GitHub Follow on X Follow on YouTube Maintained with Melos

GitHub Sponsor


[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]


Masamune Model Functions #

Usage #

Installation #

  1. Add the package to your project.
flutter pub add masamune_model_functions

Setup #

  1. Register the adapter with your model configuration. Supply a configured FunctionsAdapter that 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 #

  1. 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 operations
  • collection_model: Handle collection load operations
  • aggregate_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 #

  1. 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 #

  1. 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!

https://github.com/sponsors/mathrunet

0
likes
150
points
20
downloads

Publisher

verified publishermathru.net

Weekly Downloads

A package for generating models for interacting with external databases and Firestore using Functions.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, katana, katana_functions, masamune, meta

More

Packages that depend on masamune_model_functions