InAppDatabase

INITIALIZATIONS:

final _kLimitations = {
  "users": const InAppWriteLimitation(5),
  "posts": const InAppWriteLimitation(10),
  "users/user_id/posts": const InAppWriteLimitation(10),
};

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final db = await SharedPreferences.getInstance();
  // Map<String, dynamic> db = {};
  InAppDatabase.init(
    limiter: (key) async {
      return _kLimitations[key]; // OPTIONAL
    },
    reader: (String key) async {
      return db.getString(key);
      // final x = db[key];
      // return x;
    },
    writer: (String key, String? value) async {
      if (value != null) {
        return db.setString(key, value);
        // db[key] = value;
        // return true;
      } else {
        return db.remove(key);
        // db.remove(key);
        // return true;
      }
    },
  );
  runApp(const MyApp());
}

USE CASES:

Add collection document

Future<InAppDocumentSnapshot?> addCollectionDocument() {
  return InAppDatabase.i.collection("users").add({
    "username": UserFaker.username,
    "email": UserFaker.email,
    "age": UserFaker.age,
    "country": UserFaker.country,
    "photoUrl": UserFaker.photoUrl,
  });
}

Set new document

Future<InAppDocumentSnapshot?> setDocument() {
  return InAppDatabase.i.collection("users").doc("1").set({
    "username": "This is a username",
    "email": "This is a user email",
    "age": 24,
    "country": "Bangladesh",
    "photoUrl": "null",
    "hobbies": ['coding', 'gaming', 'sleeping'],
    "skills": ['flutter', 'android', 'node.js', 'spring boot', 'etc'],
  });
}

Update specific document

Future<InAppDocumentSnapshot?> updateSpecificDocument() {
  return InAppDatabase.i.collection("users").doc("1").update({
    'username': "This is a updated username",
    'email': "This is a updated user email",
    'age': InAppFieldValue.increment(2),
    'balance': InAppFieldValue.increment(-10.2),
    'hobbies': InAppFieldValue.arrayUnion(['swimming']),
    'skills': InAppFieldValue.arrayRemove(['node.js', 'spring boot']),
    'timestamp': InAppFieldValue.serverTimestamp(),
    'photoUrl': InAppFieldValue.delete(),
  });
}

Delete specific document

Future<bool> deleteSpecificDocument() {
  return InAppDatabase.i.collection("users").doc("1").delete();
}

Get specific document

Future<InAppDocumentSnapshot?> getSpecificDocument() {
  return InAppDatabase.i.collection("users").doc("1").get();
}

Get all documents from collection

Future<InAppQuerySnapshot> getAllDocuments() {
  return InAppDatabase.i.collection("users").get();
}

Get specific documents by simple query

Future<InAppQuerySnapshot> getSpecificDocumentsByQuery() {
  return InAppDatabase.i
      .collection("users")
      .where("username", isEqualTo: "emma_smith")
      .get();
}

Get specific documents by complex query

Future<InAppQuerySnapshot> getSpecificDocumentsByQuery() {
  return InAppDatabase.i
      .collection("users")
      .where(InAppFilter.or([
        InAppFilter("username", isEqualTo: "emma_smith"),
        InAppFilter("age", isGreaterThanOrEqualTo: 50),
      ]))
      .where("age", isLessThanOrEqualTo: 60)
      .orderBy("age", descending: false)
      .orderBy("email", descending: false)
      .limit(10)
      .get();
}

Get all documents

Stream<InAppQuerySnapshot> getCollectionSnapshots() {
  return InAppDatabase.i.collection("users").snapshots();
}

Get specific documents by simple query

Stream<InAppQuerySnapshot> getSpecificDocumentsByQuerySnapshots() {
  return InAppDatabase.i
      .collection("users")
      .where("username", isEqualTo: "emma_smith")
      .snapshots();
}

Get specific documents by complex query

Stream<InAppQuerySnapshot> getComplexQuerySnapshots() {
  return InAppDatabase.i
      .collection("users")
      .where(InAppFilter.or([
        InAppFilter("username", isEqualTo: "emma_smith"),
        InAppFilter("age", isGreaterThanOrEqualTo: 50),
      ]))
      .where("age", isLessThanOrEqualTo: 60)
      .orderBy("age", descending: false)
      .orderBy("email", descending: false)
      .limit(10)
      .snapshots();
}