@ea-utilities/db-sync
v1.0.21
Published
Add a local database using IndexedDb, user can syncronize changes telegram (Or create new implementation following ISyncClient interface)
Downloads
233
Maintainers
Readme
db-sync
Add a local database using IndexedDb, user can syncronize changes telegram (Or create new implementation following ISyncClient interface)
Dependencies
- GramJs when telegram chat is used as db
Installation
Run in your terminal
npm i @ea-utilities/db-sync
Usage (LocalDb)
export interface UserModel {
id: string;
name: string;
lastName: string;
birthday: Date;
}
DB
const indexedDb = new IndexedDbClient("localDb", ["users", ...]);
indexedDb.syncAll();
Collections
const usersCollection: DbInstace<UserModel> = new DbInstace(indexedDb, "users");
const filesCollection = new DbFileInstance(indexedDb);
Get data
const users = await usersCollection.get();
Save or update
usersCollection.addOrUpdate([...UserModel]);
usersCollection.add([...UserModel]);
usersCollection.update([...UserModel]);
Delete
usersCollection.delete(UserModel.id);
Listen for changes
usersCollection.subscribe(() => {
users = await usersCollection.get();
});
Usage (Telegram sync)
const telegramInstance = new TelegramSyncClient(isConnected => {
if (isConnected) {
indexedDb?.syncAll();
}
});
telegramInstance.auth({
chatName: string;
appId: number;
appHash: string;
session?: string;
phone: string;
});
const indexedDb = new IndexedDbClient("localDb", ["users", ...], telegramInstance);
indexedDb?.syncAll();