Giter Site home page Giter Site logo

streamqflite's Introduction

streamqflite

Pub

A reactive stream wrapper around sqflite inspired by sqlbrite.

Getting Started

In your flutter project, add the dependency to your pubspec.yaml

dependencies:
  ...
  streamqflite: ^1.0.0

Usage

Import streamqflite.dart

import 'package:streamqflite/streamqflite.dart';

Wrap your database in a StreamDatabase.

var streamDb = StreamDatabase(db);

You can then listen to a query

// Emits a single row, doesn't emit if the row dosen't exist.
Stream<MyEntry> singleQuery = streamDb.createQuery("MyTable", where: 'id = ?', whereArgs: [id])
    .mapToOne((row) => MyEntry(row));

// Emits a single row, or the given default value if the row doesn't exist.
Stream<MyEntry> singleOrDefaultQuery = streamDb.createQuery("MyTable", where: 'id = ?', whereArgs: [id])
    .mapToOneOrDefault((row) => MyEntry(row), MyEntry.empty());

// Emits a list of rows.
Stream<List<MyEntry>> listQuery = streamDb.createQuery("MyTable", where: 'name LIKE ?', whereArgs: [query])
    .mapToList((row) => MyEntry(row));

var flexibleQuery = streamDb.createQuery("MyTable", where: 'name LIKE ?', whereArgs: [query])
    .asyncMap((query) => {
        // query is lazy, this lets you not even execute it if you don't need to.
        if (condition) {
            return query();
        } else {
            return Stream.empty();
        }
    }).map((rows) {
        // Do something with all the rows.
        return ...;
    });

These queries will run once to get the current data, then again whenever the given table is modified though the StreamDatabase.

streamqflite's People

Contributors

evant avatar pattelanius avatar

Stargazers

Alexander Mateo avatar Saurav  avatar yourgame avatar alan kaiser avatar Renan Delfanti avatar  avatar Rohit Karadkar avatar Altan Özlü avatar Edgar Santiago avatar Njuguna Mureithi avatar Anthony Iacono avatar  avatar  avatar Jonathan Younger avatar Dinesh avatar  avatar József Barát avatar Javier Benitez avatar Midori Kochiya avatar Sergey Chuvashev avatar  avatar  avatar Nicolás Caorsi avatar Rodrigo Franco avatar Dhruv Chandrani avatar TOYAMA Yosaku avatar İhsan Işık avatar  avatar Dimitri Krivoj avatar Gihwan Oh avatar plavelo avatar Fredrik S avatar Bálint Böde avatar Lukasz Piliszczuk avatar  avatar Sacha Arbonel avatar Vitus avatar Andrejs Agejevs avatar Kashif Minhaj avatar Mark Han avatar

Watchers

 avatar James Cloos avatar  avatar

streamqflite's Issues

Testing - How can I mock it?

First at all, thx for the amazing lib...
I would like to know how can I stub to test my functions, I'm trying to mock it but I don't know how to do it
First I wanna to test this call

  Stream<List<Customer>> fetchCustomers() => db.createQuery('customers')
    .mapToList(_mapCustomer);

I'm trying this:

      test("it returns a list with fake customer", () async {
        when(mockDatabase.createQuery('customers')
          .mapToList((row) => Customer.fromDB(row)))
          .thenAnswer((_) => Stream.fromIterable([fakeCustomer].cast()));

        Stream<List<Customer>> subject = CustomersCache(mockDatabase)
          .fetchCustomers();

        expect(subject, emits([fakeCustomer]));
      });

I think that I need to mock createQuery but how can I do?

Provide generic map function

Is there a generic map function I can use with this library?

My app uses BuiltList instead of List so I want to do something like this

  Stream<BuiltList<T>> mapToBuiltList<T>(T mapper(Map<String, dynamic> row)) {
    return _source.asyncMap((query) => query()).map((rows) {
      var result = BuiltList<T>().toBuilder();

      for (final row in rows) {
        result.add(mapper(row));
      }

      return result.build();
    });
  }

I think the library should expose a generic map function so that any data structure can be used.

_source.asyncMap((query) => query()) is the part that could be exposed.

Still interested in maintaining this project?

Hey,

I just wanted to know whether you're still interested in maintaining this project.

I had the same idea for a library like this this weekend but saw that yours is existing. The only thing missing is Dart 2 support.

If you're still interested, I would try to submit a PR where all libraries are updated and Dart 2 is supported.

Performance question

Hi @evant

Thank you for making this library.

Just wondering when I make a query then subscribe to the stream, then I cancel the subscription is the query then no longer observed for changes.

Just wanted to make sure that my SQL queries aren't accumulating and then all being watched for changes even when there are no subscribers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.