fix(stream_chat_persistence): tests
This commit is contained in:
@@ -19,10 +19,10 @@ class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
(select(channels)..where((c) => c.cid.equals(cid))).join([
|
||||
leftOuterJoin(users, channels.createdById.equalsExp(users.id)),
|
||||
]).map((rows) {
|
||||
final channel = rows.readTable(channels);
|
||||
final createdBy = rows.readTable(users);
|
||||
final channel = rows.readTableOrNull(channels);
|
||||
final createdBy = rows.readTableOrNull(users);
|
||||
return channel.toChannelModel(createdBy: createdBy?.toUser());
|
||||
}).getSingle();
|
||||
}).getSingleOrNull();
|
||||
|
||||
/// Delete all channels by matching cid in [cids]
|
||||
///
|
||||
|
||||
@@ -32,7 +32,7 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
Future<void> updateChannelQueries(
|
||||
Map<String, dynamic> filter,
|
||||
List<String> cids, {
|
||||
bool clearQueryCache,
|
||||
bool clearQueryCache = false,
|
||||
}) async =>
|
||||
transaction(() async {
|
||||
final hash = _computeHash(filter);
|
||||
@@ -57,6 +57,14 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
});
|
||||
});
|
||||
|
||||
///
|
||||
Future<List<String>> getCachedChannelCids(Map<String, dynamic> filter) {
|
||||
final hash = _computeHash(filter);
|
||||
return (select(channelQueries)..where((c) => c.queryHash.equals(hash)))
|
||||
.map((c) => c.channelCid)
|
||||
.get();
|
||||
}
|
||||
|
||||
/// Get list of channels by filter, sort and paginationParams
|
||||
Future<List<ChannelModel>> getChannels({
|
||||
Map<String, dynamic> filter,
|
||||
@@ -72,12 +80,7 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
return true;
|
||||
}(), '');
|
||||
|
||||
final hash = _computeHash(filter);
|
||||
final cachedChannelCids = await (select(channelQueries)
|
||||
..where((c) => c.queryHash.equals(hash)))
|
||||
.map((c) => c.channelCid)
|
||||
.get();
|
||||
|
||||
final cachedChannelCids = await getCachedChannelCids(filter);
|
||||
final query = select(channels)..where((c) => c.cid.isIn(cachedChannelCids));
|
||||
|
||||
final cachedChannels = await (query.join([
|
||||
|
||||
@@ -2,14 +2,13 @@ import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:stream_chat_persistence/src/entity/connection_events.dart';
|
||||
import 'package:stream_chat_persistence/src/entity/users.dart';
|
||||
|
||||
import 'package:stream_chat_persistence/src/mapper/mapper.dart';
|
||||
|
||||
part 'connection_event_dao.g.dart';
|
||||
|
||||
/// The Data Access Object for operations in [ConnectionEvents] table.
|
||||
@UseDao(tables: [ConnectionEvents, Users])
|
||||
@UseDao(tables: [ConnectionEvents])
|
||||
class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
with _$ConnectionEventDaoMixin {
|
||||
/// Creates a new connection event dao instance
|
||||
@@ -18,16 +17,16 @@ class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
/// Get the latest stored connection event
|
||||
Future<Event> get connectionEvent => select(connectionEvents)
|
||||
.map((eventEntity) => eventEntity.toEvent())
|
||||
.getSingle();
|
||||
.getSingleOrNull();
|
||||
|
||||
/// Get the latest stored lastSyncAt
|
||||
Future<DateTime> get lastSyncAt =>
|
||||
select(connectionEvents).getSingle().then((r) => r?.lastSyncAt);
|
||||
select(connectionEvents).getSingleOrNull().then((r) => r?.lastSyncAt);
|
||||
|
||||
/// Update stored connection event with latest data
|
||||
Future<void> updateConnectionEvent(Event event) async =>
|
||||
transaction(() async {
|
||||
final connectionInfo = await select(connectionEvents).getSingle();
|
||||
final connectionInfo = await select(connectionEvents).getSingleOrNull();
|
||||
await into(connectionEvents).insert(
|
||||
ConnectionEventEntity(
|
||||
id: 1,
|
||||
|
||||
@@ -9,5 +9,4 @@ part of 'connection_event_dao.dart';
|
||||
mixin _$ConnectionEventDaoMixin on DatabaseAccessor<MoorChatDatabase> {
|
||||
$ConnectionEventsTable get connectionEvents =>
|
||||
attachedDatabase.connectionEvents;
|
||||
$UsersTable get users => attachedDatabase.users;
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
(delete(messages)..where((tbl) => tbl.channelCid.isIn(cids))).go();
|
||||
|
||||
Future<Message> _messageFromJoinRow(TypedResult rows) async {
|
||||
final userEntity = rows.readTable(_users);
|
||||
final pinnedByEntity = rows.readTable(_pinnedByUsers);
|
||||
final msgEntity = rows.readTable(messages);
|
||||
final userEntity = rows.readTableOrNull(_users);
|
||||
final pinnedByEntity = rows.readTableOrNull(_pinnedByUsers);
|
||||
final msgEntity = rows.readTableOrNull(messages);
|
||||
final latestReactions = await _db.reactionDao.getReactions(msgEntity.id);
|
||||
final ownReactions = await _db.reactionDao.getReactionsByUserId(
|
||||
msgEntity.id,
|
||||
@@ -66,13 +66,13 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
])
|
||||
..where(messages.id.equals(id)))
|
||||
.map(_messageFromJoinRow)
|
||||
.getSingle();
|
||||
.getSingleOrNull();
|
||||
|
||||
/// Returns all the messages of a particular thread by matching
|
||||
/// [Messages.channelCid] with [cid]
|
||||
Future<List<Message>> getThreadMessages(String cid) async =>
|
||||
Future.wait(await (select(messages).join([
|
||||
leftOuterJoin(users, messages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(_pinnedByUsers,
|
||||
messages.pinnedByUserId.equalsExp(_pinnedByUsers.id)),
|
||||
])
|
||||
@@ -89,18 +89,36 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
PaginationParams options,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(messages).join([
|
||||
innerJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
innerJoin(
|
||||
leftOuterJoin(_users, messages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(
|
||||
_pinnedByUsers, messages.pinnedByUserId.equalsExp(_pinnedByUsers.id)),
|
||||
])
|
||||
..where(isNotNull(messages.parentId))
|
||||
..where(messages.parentId.equals(parentId))
|
||||
..orderBy([OrderingTerm.asc(messages.createdAt)]))
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (options?.lessThan != null && msgList.isNotEmpty) {
|
||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
@@ -123,24 +141,26 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
(delete(pinnedMessages)..where((tbl) => tbl.channelCid.isIn(cids))).go();
|
||||
|
||||
Future<Message> _messageFromJoinRow(TypedResult rows) async {
|
||||
final userEntity = rows.readTable(users);
|
||||
final pinnedByEntity = rows.readTable(_pinnedByUsers);
|
||||
final msgEntity = rows.readTable(pinnedMessages);
|
||||
final userEntity = rows.readTableOrNull(users);
|
||||
final pinnedByEntity = rows.readTableOrNull(_pinnedByUsers);
|
||||
final msgEntity = rows.readTableOrNull(pinnedMessages);
|
||||
final latestReactions = await _db.reactionDao.getReactions(msgEntity.id);
|
||||
final ownReactions = await _db.reactionDao.getReactionsByUserId(
|
||||
msgEntity.id,
|
||||
@@ -66,7 +66,7 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
])
|
||||
..where(pinnedMessages.id.equals(id)))
|
||||
.map(_messageFromJoinRow)
|
||||
.getSingle();
|
||||
.getSingleOrNull();
|
||||
|
||||
/// Returns all the messages of a particular thread by matching
|
||||
/// [PinnedMessages.channelCid] with [cid]
|
||||
@@ -89,18 +89,36 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
PaginationParams options,
|
||||
}) async {
|
||||
final msgList = await Future.wait(await (select(pinnedMessages).join([
|
||||
innerJoin(_users, pinnedMessages.userId.equalsExp(_users.id)),
|
||||
innerJoin(_pinnedByUsers,
|
||||
leftOuterJoin(_users, pinnedMessages.userId.equalsExp(_users.id)),
|
||||
leftOuterJoin(_pinnedByUsers,
|
||||
pinnedMessages.pinnedByUserId.equalsExp(_pinnedByUsers.id)),
|
||||
])
|
||||
..where(isNotNull(pinnedMessages.parentId))
|
||||
..where(pinnedMessages.parentId.equals(parentId))
|
||||
..orderBy([OrderingTerm.asc(pinnedMessages.createdAt)]))
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere((m) => m.id == options.lessThan);
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (options?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
if (options?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == options.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (options?.limit != null) {
|
||||
return msgList.take(options.limit).toList();
|
||||
}
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
@@ -123,24 +141,26 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
.map(_messageFromJoinRow)
|
||||
.get());
|
||||
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
if (msgList.isNotEmpty) {
|
||||
if (messagePagination?.lessThan != null) {
|
||||
final lessThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.lessThan,
|
||||
);
|
||||
if (lessThanIndex != -1) {
|
||||
msgList.removeRange(lessThanIndex, msgList.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
if (messagePagination?.greaterThanOrEqual != null) {
|
||||
final greaterThanIndex = msgList.indexWhere(
|
||||
(m) => m.id == messagePagination.greaterThanOrEqual,
|
||||
);
|
||||
if (greaterThanIndex != -1) {
|
||||
msgList.removeRange(0, greaterThanIndex);
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
}
|
||||
if (messagePagination?.limit != null) {
|
||||
return msgList.take(messagePagination.limit).toList();
|
||||
}
|
||||
return msgList;
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
|
||||
..where(reactions.messageId.equals(messageId))
|
||||
..orderBy([OrderingTerm.asc(reactions.createdAt)]))
|
||||
.map((rows) {
|
||||
final userEntity = rows.readTable(users);
|
||||
final reactionEntity = rows.readTable(reactions);
|
||||
final userEntity = rows.readTableOrNull(users);
|
||||
final reactionEntity = rows.readTableOrNull(reactions);
|
||||
return reactionEntity.toReaction(user: userEntity?.toUser());
|
||||
}).get();
|
||||
|
||||
|
||||
@@ -20,4 +20,10 @@ class UserDao extends DatabaseAccessor<MoorChatDatabase> with _$UserDaoMixin {
|
||||
mode: InsertMode.insertOrReplace,
|
||||
),
|
||||
);
|
||||
|
||||
/// Returns the list of all the users stored in db
|
||||
Future<List<User>> getUsers() =>
|
||||
(select(users)..orderBy([(u) => OrderingTerm.desc(u.createdAt)]))
|
||||
.map((it) => it.toUser())
|
||||
.get();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:moor/ffi.dart';
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
|
||||
import 'package:stream_chat_persistence/src/converter/converter.dart';
|
||||
import 'package:stream_chat_persistence/src/dao/dao.dart';
|
||||
import 'package:stream_chat_persistence/src/db/shared/shared_db.dart';
|
||||
import 'package:stream_chat_persistence/src/entity/entity.dart';
|
||||
|
||||
part 'moor_chat_database.g.dart';
|
||||
export 'shared/shared_db.dart';
|
||||
|
||||
LazyDatabase _openConnection(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
bool persistOnDisk = true,
|
||||
}) =>
|
||||
LazyDatabase(() async => SharedDB.constructDatabase(
|
||||
userId,
|
||||
logStatements: logStatements,
|
||||
persistOnDisk: persistOnDisk,
|
||||
));
|
||||
part 'moor_chat_database.g.dart';
|
||||
|
||||
/// A chat database implemented using moor
|
||||
@UseMoor(tables: [
|
||||
@@ -43,14 +36,9 @@ LazyDatabase _openConnection(
|
||||
class MoorChatDatabase extends _$MoorChatDatabase {
|
||||
/// Creates a new moor chat database instance
|
||||
MoorChatDatabase(
|
||||
this._userId, {
|
||||
logStatements = false,
|
||||
bool persistOnDisk = true,
|
||||
}) : super(_openConnection(
|
||||
_userId,
|
||||
logStatements: logStatements,
|
||||
persistOnDisk: persistOnDisk,
|
||||
));
|
||||
this._userId,
|
||||
QueryExecutor executor,
|
||||
) : super(executor);
|
||||
|
||||
/// Instantiate a new database instance
|
||||
MoorChatDatabase.connect(
|
||||
@@ -58,6 +46,10 @@ class MoorChatDatabase extends _$MoorChatDatabase {
|
||||
DatabaseConnection connection,
|
||||
) : super.connect(connection);
|
||||
|
||||
/// Custom constructor used only for testing
|
||||
@visibleForTesting
|
||||
MoorChatDatabase.testable(this._userId) : super(VmDatabase.memory());
|
||||
|
||||
final String _userId;
|
||||
|
||||
/// User id to which the database is connected
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
|
||||
@@ -14,27 +15,49 @@ import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
|
||||
/// for native platform applications
|
||||
class SharedDB {
|
||||
/// Returns a new instance of [VmDatabase] created using [userId]
|
||||
/// on a regular isolate.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<VmDatabase> constructDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
bool persistOnDisk = true,
|
||||
}) async {
|
||||
ConnectionMode connectionMode = ConnectionMode.regular,
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
if (persistOnDisk) {
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final path = join(dir.path, '$dbName.sqlite');
|
||||
final file = File(path);
|
||||
return VmDatabase(file, logStatements: logStatements);
|
||||
}
|
||||
if (Platform.isMacOS || Platform.isLinux) {
|
||||
final file = File('$dbName.sqlite');
|
||||
return VmDatabase(file, logStatements: logStatements);
|
||||
}
|
||||
if (connectionMode == ConnectionMode.background) {
|
||||
return MoorChatDatabase.connect(
|
||||
userId,
|
||||
DatabaseConnection.delayed(Future(() async {
|
||||
final isolate = await _createMoorIsolate(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
);
|
||||
return isolate.connect();
|
||||
})),
|
||||
);
|
||||
}
|
||||
return MoorChatDatabase(
|
||||
userId,
|
||||
LazyDatabase(
|
||||
() async => _constructDatabase(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<VmDatabase> _constructDatabase(
|
||||
String dbName, {
|
||||
bool logStatements = false,
|
||||
}) async {
|
||||
if (Platform.isIOS || Platform.isAndroid) {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final path = join(dir.path, '$dbName.sqlite');
|
||||
final file = File(path);
|
||||
return VmDatabase(file, logStatements: logStatements);
|
||||
}
|
||||
if (Platform.isMacOS || Platform.isLinux) {
|
||||
final file = File('$dbName.sqlite');
|
||||
return VmDatabase(file, logStatements: logStatements);
|
||||
}
|
||||
return VmDatabase.memory(logStatements: logStatements);
|
||||
}
|
||||
@@ -69,27 +92,6 @@ class SharedDB {
|
||||
|
||||
return await receivePort.first as MoorIsolate;
|
||||
}
|
||||
|
||||
/// Returns a new instance of [MoorChatDatabase] using the factory constructor
|
||||
/// [MoorChatDatabase.connect] created on a background isolate.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
return MoorChatDatabase.connect(
|
||||
userId,
|
||||
DatabaseConnection.delayed(Future(() async {
|
||||
final isolate = await _createMoorIsolate(
|
||||
dbName,
|
||||
logStatements: logStatements,
|
||||
);
|
||||
return isolate.connect();
|
||||
})),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _IsolateStartRequest {
|
||||
|
||||
@@ -1,29 +1,17 @@
|
||||
import 'package:moor/backends.dart';
|
||||
// coverage:ignore-file
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
|
||||
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase]
|
||||
class SharedDB {
|
||||
/// Returns a new instance of database.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<DelegatedDatabase> constructDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
bool persistOnDisk = true,
|
||||
ConnectionMode connectionMode = ConnectionMode.regular,
|
||||
}) {
|
||||
throw UnsupportedError(
|
||||
'No implementation of the constructDatabase api provided');
|
||||
}
|
||||
|
||||
/// Return a new instance of moor chat database.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
}) {
|
||||
throw UnsupportedError(
|
||||
'No implementation of the constructMoorChatDatabase api provided');
|
||||
'No implementation of the constructDatabase api provided',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor_web.dart';
|
||||
import 'package:stream_chat_persistence/src/stream_chat_persistence_client.dart';
|
||||
|
||||
@@ -6,27 +7,14 @@ import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
/// A Helper class to construct new instances of [MoorChatDatabase] specifically
|
||||
/// for Web applications
|
||||
class SharedDB {
|
||||
/// Returns a new instance of [WebDatabase] created using [userId].
|
||||
///
|
||||
/// Generally used with [ConnectionMode.regular].
|
||||
static Future<WebDatabase> constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
bool persistOnDisk = true, // ignored on web
|
||||
}) async {
|
||||
final dbName = 'db_$userId';
|
||||
return WebDatabase(dbName, logStatements: logStatements);
|
||||
}
|
||||
|
||||
/// Returns a new instance of [MoorChatDatabase] creating using the
|
||||
/// default constructor.
|
||||
///
|
||||
/// Generally used with [ConnectionMode.background].
|
||||
static MoorChatDatabase constructMoorChatDatabase(
|
||||
/// Returns a new instance of [MoorChatDatabase].
|
||||
static MoorChatDatabase constructDatabase(
|
||||
String userId, {
|
||||
bool logStatements = false,
|
||||
ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web
|
||||
}) {
|
||||
final dbName = 'db_$userId';
|
||||
return MoorChatDatabase(dbName, logStatements: logStatements);
|
||||
final queryExecutor = WebDatabase(dbName, logStatements: logStatements);
|
||||
return MoorChatDatabase(userId, queryExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
|
||||
/// Represents a [ChannelQueries] table in [MoorChatDatabase].
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
|
||||
/// Represents a [Members] table in [MoorChatDatabase].
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/list_converter.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
|
||||
import 'package:stream_chat_persistence/src/entity/messages.dart';
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
|
||||
/// Represents a [Reads] table in [MoorChatDatabase].
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// coverage:ignore-file
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:stream_chat_persistence/src/converter/map_converter.dart';
|
||||
|
||||
|
||||
@@ -46,14 +46,14 @@ extension ChannelModelX on ChannelModel {
|
||||
id: id,
|
||||
type: type,
|
||||
cid: cid,
|
||||
config: config.toJson(),
|
||||
config: config?.toJson(),
|
||||
frozen: frozen,
|
||||
lastMessageAt: lastMessageAt,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt,
|
||||
deletedAt: deletedAt,
|
||||
memberCount: memberCount,
|
||||
createdById: createdBy.id,
|
||||
createdById: createdBy?.id,
|
||||
extraData: extraData,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:mutex/mutex.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
|
||||
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
|
||||
import 'package:stream_chat_persistence/src/db/shared/shared_db.dart';
|
||||
|
||||
/// Various connection modes on which [StreamChatPersistenceClient] can work
|
||||
enum ConnectionMode {
|
||||
@@ -15,6 +14,9 @@ enum ConnectionMode {
|
||||
background,
|
||||
}
|
||||
|
||||
/// Signature for a function which provides instance of [MoorChatDatabase]
|
||||
typedef DatabaseProvider = MoorChatDatabase Function(String, ConnectionMode);
|
||||
|
||||
final _levelEmojiMapper = {
|
||||
Level.INFO: 'ℹ️',
|
||||
Level.WARNING: '⚠️',
|
||||
@@ -36,26 +38,6 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
||||
_logger.onRecord.listen(logHandlerFunction ?? _defaultLogHandler);
|
||||
}
|
||||
|
||||
/// A function that has a parameter of type [LogRecord].
|
||||
/// This is called on every new log record.
|
||||
/// By default the client will use the handler returned by
|
||||
/// [_getDefaultLogHandler].
|
||||
/// Setting it you can handle the log messages directly instead of have them
|
||||
/// written to stdout,
|
||||
/// this is very convenient if you use an error tracking tool or if you want
|
||||
/// to centralize your logs into one facility.
|
||||
///
|
||||
/// ```dart
|
||||
/// myLogHandlerFunction = (LogRecord record) {
|
||||
/// // do something with the record (ie. send it to Sentry or Fabric)
|
||||
/// }
|
||||
///
|
||||
/// final client = StreamChatPersistenceClient(
|
||||
/// logHandlerFunction: myLogHandlerFunction,
|
||||
/// );
|
||||
///```
|
||||
LogHandlerFunction logHandlerFunction;
|
||||
|
||||
/// [MoorChatDatabase] instance used by this client.
|
||||
@visibleForTesting
|
||||
MoorChatDatabase db;
|
||||
@@ -84,24 +66,25 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
||||
return ret;
|
||||
}
|
||||
|
||||
MoorChatDatabase _defaultDatabaseProvider(
|
||||
String userId,
|
||||
ConnectionMode mode,
|
||||
) =>
|
||||
SharedDB.constructDatabase(userId, connectionMode: mode);
|
||||
|
||||
@override
|
||||
Future<void> connect(String userId) async {
|
||||
Future<void> connect(
|
||||
String userId, {
|
||||
DatabaseProvider databaseProvider, // Used only for testing
|
||||
}) async {
|
||||
if (db != null) {
|
||||
throw Exception(
|
||||
'An instance of StreamChatDatabase is already connected.\n'
|
||||
'disconnect the previous instance before connecting again.',
|
||||
);
|
||||
}
|
||||
switch (_connectionMode) {
|
||||
case ConnectionMode.regular:
|
||||
_logger.info('Connecting on a regular isolate');
|
||||
db = MoorChatDatabase(userId);
|
||||
return;
|
||||
case ConnectionMode.background:
|
||||
_logger.info('Connecting on background isolate');
|
||||
db = SharedDB.constructMoorChatDatabase(userId);
|
||||
return;
|
||||
}
|
||||
db = databaseProvider?.call(userId, _connectionMode) ??
|
||||
_defaultDatabaseProvider(userId, _connectionMode);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -259,9 +242,9 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
||||
@override
|
||||
Future<void> updateChannelQueries(
|
||||
Map<String, dynamic> filter,
|
||||
List<String> cids,
|
||||
bool clearQueryCache,
|
||||
) =>
|
||||
List<String> cids, {
|
||||
bool clearQueryCache = false,
|
||||
}) =>
|
||||
_readProtected(() async {
|
||||
_logger.info('updateChannelQueries');
|
||||
return db.channelQueryDao.updateChannelQueries(
|
||||
|
||||
Reference in New Issue
Block a user