Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete only one registrer #291

Closed
santinet28 opened this issue Nov 2, 2021 · 3 comments
Closed

delete only one registrer #291

santinet28 opened this issue Nov 2, 2021 · 3 comments

Comments

@santinet28
Copy link

Hello,

I have created a database as follows

static savePreguntasDipositivo ( String questionType, Map <String, dynamic> question, Database db) async { var store = StoreRef.main (); await store.record (questionType) .put (db, question); }

My problem is that I don't know how to delete only one record that matches the String of the Map.

I know it does not have to be difficult but I have blocked myself and I do not know how to continue.

Thanks

@flutter-painter
Copy link

@santinet28

final oldCount = await StoreDb.myCoolStore.count(_database);
final key = await StoreDb.myCoolStore.findKey(
      _database,
      finder: Finder(filter: Filter.equals('myCoolID', 'coolId')),
    );

final wrongCounter = await StoreDb.myCoolStore.record(key).delete(_database);
final newCount = await StoreDb.myCoolStore.count(_database);

By the way @alextekartik I encounter an issue while unit testing this single deletion on multiple objects,
wrongCounter will stay stuck on 1, while newcount equals 0.
Seems to be linked with async, maybe it is because the count goes down to zero.

@alextekartik
Copy link
Collaborator

@flutter-painter Can you share your unit test so I can reproduce? RecordRef.delete return value is not specified (but in this case, I know it returns the key deleted). Why are you assigning it to a counter (wrongCounter). store.delete return the count of deleted items (newCount).

@flutter-painter
Copy link

Thanks for the answer, so it is normal for behaviour for record(key).delete.
I used first the delete method for a Store, not a record, and assumed that it was the same behaviour.
Doc says it will 'Return the count updated. Delete all if no finder'
However the count returned seems stuck on one.

Here goes the unit test :

class Shop {
  final int id;
  final String name;
  const Shop({this.id, this.name});

  Map<String, dynamic> toMap() {
    return {'id': id, 'name': name};
  }

  factory Shop.fromMap(Map<String, dynamic> map) {
    return Shop(id: map['id'] ?? 0, name: map['name'] ?? '');
  }
  String toJson() => json.encode(toMap());
  factory Shop.fromJson(String source) => Shop.fromMap(json.decode(source));
}

Future main() async {
  final _database = await databaseFactoryMemory.openDatabase(('test.db'));
  final _dbStore = sembast.intMapStoreFactory.store('shops');

  final Shop shop = Shop(id: 1, name: 'epi boujou');
  final Shop shop2 = Shop(id: 2, name: 'tati');
  final Shop shop3 = Shop(id: 3, name: 'toto');

  group('test shops store', () {
    test('create_shop', () async {
      final created = await _dbStore.add(_database, shop.toMap());
      final created2 = await _dbStore.add(_database, shop2.toMap());
      final created3 = await _dbStore.add(_database, shop3.toMap());

      print('key1 $created');
      print('key2 $created2');
      print('key3 $created3');
      final count = await _dbStore.count(_database);
      expect(count, 3);
    });
    test('delete_forever', () async {
      final oldCount = await _dbStore.count(_database);
      final dbCounter = await _dbStore.delete(_database,
          finder: sembast.Finder(filter: sembast.Filter.equals('id', shop.id)));
      final newCount = await _dbStore.count(_database);
      print('oldCount $oldCount');
      print('dbCounter $dbCounter');
      print('newCount $newCount');
      final oldCount2 = await _dbStore.count(_database);
      final dbCounter2 = await _dbStore.delete(_database,
          finder:
              sembast.Finder(filter: sembast.Filter.equals('id', shop2.id)));
      final newCount2 = await _dbStore.count(_database);
      print('oldCount2 $oldCount2');
      print('dbCounter2 $dbCounter2');
      print('newCount2 $newCount2');
      // expect(actual, matcher)
    });
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants