in_app_database 1.3.3 copy "in_app_database: ^1.3.3" to clipboard
in_app_database: ^1.3.3 copied to clipboard

Efficient in-app database for seamless data storage and retrieval, ensuring optimal performance and user experience.

InAppDatabase #

CREATE DELEGATE #

Map<String, Map<String, dynamic>> databases = {};

class DatabaseDelegate extends InAppDatabaseDelegate {
  @override
  Future<void> init(String dbName) async {
    databases[dbName] = {};
  }

  @override
  Future<Iterable<String>> paths(String dbName) async {
    final x = databases[dbName]!.keys.toList();
    return x;
  }

  @override
  Future<bool> delete(String dbName, String key) async {
    databases[dbName]!.remove(key);
    return true;
  }

  @override
  Future<bool> drop(String dbName) async {
    databases.remove(dbName);
    return true;
  }

  @override
  Future<Object?> read(String dbName, String key) async {
    return databases[dbName]![key];
  }

  @override
  Future<bool> write(String dbName, String key, String? value) async {
    if (value != null) {
      databases[dbName]![key] = value;
      return true;
    } else {
      databases[dbName]!.remove(key);
      return true;
    }
  }

  @override
  Future<InAppWriteLimitation?> limitation(
      String dbName,
      PathDetails details,
      ) async {
    return {
      "users": const InAppWriteLimitation(50),
      "posts": const InAppWriteLimitation(10),
      "users/{user_id}/posts": const InAppWriteLimitation(10),
    }[details.format]; // OPTIONAL
  }
}

INITIALIZATIONS: #

Future<void> main() async {
  InAppDatabase.init(
    delegate: DatabaseDelegate(), // required
    showLogs: true, // optional
    version: InAppDatabaseVersion.v2, // optional
  );
  // ...
}

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),
    // to increment existing value
    'balance': InAppFieldValue.increment(-10.2),
    // to decrement existing value
    'hobbies': InAppFieldValue.arrayFilter((e) => e.isNotEmpty),
    // to filter existing array elements
    'hobbies': InAppFieldValue.arrayUnion(['swimming', 'swimming']),
    // to add new array elements
    'hobbies': InAppFieldValue.arrayUnify(),
    // to remove duplicate
    'skills': InAppFieldValue.arrayRemove(['node.js', 'spring boot']),
    // to remove existing array elements
    'timestamp': InAppFieldValue.timestamp(),
    // to add system timestamp internally
    'photoUrl': InAppFieldValue.delete(),
    // to delete field and value
    'is_verified': InAppFieldValue.toggle(),
    // to change bool status internally by toggle system true/false
  });
}

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();
}

Listen specific documents by simple query #

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

Listen 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();
}
2
likes
150
points
140
downloads

Publisher

unverified uploader

Weekly Downloads

Efficient in-app database for seamless data storage and retrieval, ensuring optimal performance and user experience.

Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

flutter, in_app_query, pluralize

More

Packages that depend on in_app_database