tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
777 stars 63 forks source link

Timestamp not supported? #149

Closed Sam-Guru-In-Training closed 4 years ago

Sam-Guru-In-Training commented 4 years ago

Well I have Timestamps using the Firestore plugin, that stamp each record with an expiration date.

When I try and save them to SEMBAST it throws this:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument: type Timestamp not supported: Instance of 'Timestamp'
E/flutter ( 4910): #0      SembastDatabase._check (package:sembast/src/database_impl.dart:1424:5)
E/flutter ( 4910): #1      SembastDatabase._check (package:sembast/src/database_impl.dart:1416:9)

Following through to the _impl source it's sanitising for List and Maps, but no Timestamp type.

Good chance it's my error. For the moment I'll just process the Timestamp to be saved as a String.

Would be great to have it work brainlessly though, and handle type: Geopoint too, that's Firestore's 2 esoteric datatypes, and would make SEMBAST the perfect cache for Firestore.

alextekartik commented 4 years ago

Timestamp and Geopoint from firestore are unfortunately only defined in a flutter context. Sembast itself does not depend on flutter so that is basically impossible as is. sembast has its own Timestap definition that is easy to map though.

I understand the issue which is not worst nor better than plain json.

That is said, sembast supports custom type so it should be possible to write adapters for firestore specific definition (Geopoint, Timestamp, DocumentReference). I'm not sure everything is public but yes it should be possible to write in a firestore context such adapters. By default only 2 custom types are supported (Blob and Timestamp) (https://pub.dev/documentation/sembast/latest/sembast/sembastDefaultTypeAdapters.html). If you want I can take a look and see what (if any) is missing to write such adapters and could easily write such adapters as a reference.

alextekartik commented 4 years ago

So I implemented my suggestion in an example that you could try to use. You should not have to convert the data anymore according you open the sembast database with a specific codec that supports firestore data.

DatabaseFactory factory;

var db = await factory.openDatabase('db', codec: sembastFirestoreCodec);

Setup

pubspec.yaml:

dependencies:
  sembast_cloud_firestore_type_adapters:
    git:
      url: git://github.com/tekartik/sembast_flutter_more.dart
      path: cloud_firestore_type_adapters
      ref: dart2
    version: '>=0.1.0'

Usage

DatabaseFactory factory;

var db = await factory.openDatabase('db', codec: sembastFirestoreCodec);

// You can then store firestore content data inside sembast
var store = stringMapStoreFactory.store();
var record = store.record('test');
var data = {
  'int': 1,
  'String': 'text',
  'firestoreTimestamp': Timestamp(1234, 5678),
  'firestoreBlob': Blob(Uint8List.fromList([1, 2, 3])),
  'firestoreGeoPoint': const GeoPoint(1.1, 2.2)
};
await record.add(db, data);

I will likely not publish to pub since that is a very specific usage.

The package: https://github.com/tekartik/sembast_flutter_more.dart/tree/master/cloud_firestore_type_adapters

Sam-Guru-In-Training commented 4 years ago

Cheers Alexandre, this is brilliant! I was extracting different fields into Strings, and then rebuilding the original objects from them upon extraction, this looks neater.

Presently grappling with getting filtered records into a Stream for StreamBuilder, but Sembast is all working, storing my stuff, excited, new things I'm always fearful I won't get it working at all.

Thanks so much. x Sam.

On Sat, 25 Apr 2020 at 15:54, Alexandre Roux notifications@github.com wrote:

So I implemented my suggestion in an example that you could try to use. You should not have to convert the data anymore according you open the sembast database with a specific codec that supports firestore data.

DatabaseFactory factory; var db = await factory.openDatabase('db', codec: sembastFirestoreCodec);

Setup

pubspec.yaml:

dependencies: sembast_cloud_firestore_type_adapters: git: url: git://github.com/tekartik/sembast_flutter_more.dart path: cloud_firestore_type_adapters ref: dart2 version: '>=0.1.0'

Usage

DatabaseFactory factory; var db = await factory.openDatabase('db', codec: sembastFirestoreCodec); // You can then store firestore content data inside sembastvar store = stringMapStoreFactory.store();var record = store.record('test');var data = { 'int': 1, 'String': 'text', 'firestoreTimestamp': Timestamp(1234, 5678), 'firestoreBlob': Blob(Uint8List.fromList([1, 2, 3])), 'firestoreGeoPoint': const GeoPoint(1.1, 2.2) };await record.add(db, data);

I will likely not publish to pub since that is a very specific usage.

The package: https://github.com/tekartik/sembast_flutter_more.dart/tree/master/cloud_firestore_type_adapters

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tekartik/sembast.dart/issues/149#issuecomment-619391297, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACEZUNTVHANY2O74Z6JE5BTROL2SXANCNFSM4MQVMYYQ .

alextekartik commented 4 years ago

Presently grappling with getting filtered records into a Stream

I'm not sure it what you need here but https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/new_api.md#listen-to-changes queries and onSnapshot is similar to firestore (you get a stream when content changes).

Sam-Guru-In-Training commented 4 years ago

Oh you're a sweet heart. It's OK, I've figured it out. I had difficulty constructing a Stream, I needed Async* and yield, yield is fussy about where it works. Maybe when the app is finished I'll write a blog about it.

Thanks so much for your attention Alexandre, hope you prosper maximus x Sam.

On Mon, 27 Apr 2020 at 10:00, Alexandre Roux notifications@github.com wrote:

Presently grappling with getting filtered records into a Stream

I'm not sure it what you need here but https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/new_api.md#listen-to-changes queries and onSnapshot is similar to firestore (you get a stream when content changes).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tekartik/sembast.dart/issues/149#issuecomment-619837936, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACEZUNRFMKA32G66VSWQZC3ROVCSDANCNFSM4MQVMYYQ .

zenkog commented 4 years ago

As Sembast and Firestore getting more popular, I believe there are a lot of people who needs to use Sembast as local DB and Firestore as cloud DB. Like me LOL. When working with Timestamp I just convert to microseconds every time and store as int. The adapter would be super helpful. I think publishing it could be really useful for many people as well.. can we vote? :-D