Code Monkey home page Code Monkey logo

Comments (8)

alextekartik avatar alextekartik commented on September 28, 2024

It is not possible at the moment. But it could be implemented efficiently in a transaction (using a transaction is the key to make it fast, especially when the changes are commited) by finding records and updating them 1 by one.

The API does not support 'patching' a record neither to update a single field.

Let's keep this issue open as I think they both deserve solution that I need to think about. Thanks!

from sembast.dart.

fvisticot avatar fvisticot commented on September 28, 2024

If update is not available... how to update fields (even in a transaction) ?
Do I need to make a get -> to get the record, delete it, and ad it again ?

from sembast.dart.

alextekartik avatar alextekartik commented on September 28, 2024

As i said you cannot update a single field (I'm currently looking at that). You can update the whole record however using:

put({'macAddress': '00:11:22:33:44:55'}, key)

To update a single field for now, the solution is:

await db.transaction((txn) async {
  // read
  record = await txn.getRecord(key);
  // update one field
  record['macAddress'] = '00:11:22:33:44:55';
  // write
  await txn.putRecord(record);
});

from sembast.dart.

alextekartik avatar alextekartik commented on September 28, 2024

I have publish 1.10.0 that allows updating only some fields of a record, similarly to firestore.set. Some documentation here: https://github.com/tekartik/sembast.dart/blob/master/doc/writes.md#updating-fields

store.update({'macAddress': '00:11:22:33:44:55}, key);

Bulk update is not supported yet. You still have to query first (you can query keys only) then update each record one by one (which is very fast in a transaction)

from sembast.dart.

fvisticot avatar fvisticot commented on September 28, 2024

Using 1.10.0
I have made some tests to update a record field :
This code is working:
await store.update({'flushed': true}, 89);

This code is working:

await _db.transaction((txn) async {
        var store = txn.getStore('beacons');
        store.update({'flushed': true}, 148);
      });

When I try to loop over multiple records it does not work:

await _db.transaction((txn) async {
        var store=txn.getStore('beacons');
        List<Future> futures = List();
        recordsIds.forEach((key) => futures.add(store.update({'flushed': true}, key)));
        await Future.wait(futures);
      });

I get this error in the logs:

flutter: FINE: 2018-11-06 23:56:53.030405: txn 2 get null key 9
flutter: FINE: 2018-11-06 23:56:53.031026: txn 2 get null key 10
flutter: FINE: 2018-11-06 23:56:53.032141: txn 2 get null key 11
flutter: FINE: 2018-11-06 23:56:53.032805: txn 2 get null key 12
flutter: FINE: 2018-11-06 23:56:53.033639: txn 2 get null key 13

How to update multiple records in a transaction coming from a list of keys ?

from sembast.dart.

alextekartik avatar alextekartik commented on September 28, 2024

Thanks for the report. I could not reproduce the issue. I tried it here and it worked as expected:

var recordsIds = [key2, key3];
await db.transaction((txn) async {
var store = txn.getStore(beaconsStoreName);
List<Future> futures = [];
recordsIds.forEach(
(key) => futures.add(store.update({'flushed': true}, key)));
await Future.wait(futures);
});
var store = db.getStore(beaconsStoreName);
var records = await store.findRecords(null);
expect(getRecordsValues(records), [
{'name': 'beacon1'},
{'name': 'beacon2', 'flushed': true},
{'name': 'beacon3', 'flushed': true}
]);

Do you have a unit test that you could easily extract?

from sembast.dart.

fvisticot avatar fvisticot commented on September 28, 2024

Sorry, it was an issue on my side.. I was using a key of type int for storing and requesting with a key of type String ...
It works now !!

from sembast.dart.

alextekartik avatar alextekartik commented on September 28, 2024

I added in 1.10.1 the utility function updateRecords although it is only a few lines of code. See https://github.com/tekartik/sembast.dart/blob/master/doc/writes.md#bulk-update

from sembast.dart.

Related Issues (20)

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.