server: switch to @seald/nedb

This commit is contained in:
Jesse Chan
2022-05-01 14:34:42 -07:00
parent 321aa2320c
commit 1af1d24755
7 changed files with 228 additions and 86 deletions

View File

@@ -1,4 +1,4 @@
import Datastore from 'nedb-promises';
import Datastore from '@seald-io/nedb';
import path from 'path';
import type {
@@ -19,7 +19,7 @@ const DEFAULT_QUERY_LIMIT = 20;
class NotificationService extends BaseService<NotificationServiceEvents> {
count: NotificationCount = {read: 0, total: 0, unread: 0};
db = Datastore.create({
db = new Datastore({
autoload: true,
filename: path.join(config.dbPath, this.user._id, 'notifications.db'),
});
@@ -28,7 +28,7 @@ class NotificationService extends BaseService<NotificationServiceEvents> {
super(...args);
(async () => {
const notifications = await this.db.find<Notification>({}).catch(() => undefined);
const notifications = await this.db.findAsync<Notification>({}).catch(() => undefined);
if (notifications != null) {
notifications.forEach((notification) => {
@@ -68,7 +68,7 @@ class NotificationService extends BaseService<NotificationServiceEvents> {
this.count.unread += notifications.length;
await this.db
.insert(
.insertAsync(
notifications.map((notification) => ({
ts,
data: notification.data,
@@ -87,7 +87,7 @@ class NotificationService extends BaseService<NotificationServiceEvents> {
* @return {Promise<void>} - Rejects with error.
*/
async clearNotifications(): Promise<void> {
await this.db.remove({}, {multi: true});
await this.db.removeAsync({}, {multi: true});
this.count = {read: 0, total: 0, unread: 0};
this.emitUpdate();
@@ -100,11 +100,11 @@ class NotificationService extends BaseService<NotificationServiceEvents> {
* @return {NotificationState} - Resolves with notifications and counts or rejects with error.
*/
async getNotifications({allNotifications, start, limit}: NotificationFetchOptions): Promise<NotificationState> {
const sortedNotifications = this.db.find<Notification>({}).sort({ts: -1});
const sortedNotifications = this.db.findAsync<Notification>({}).sort({ts: -1});
if (allNotifications) {
return {
notifications: await sortedNotifications.exec(),
notifications: await sortedNotifications.execAsync(),
count: this.count,
};
} else if (start != null) {
@@ -112,12 +112,12 @@ class NotificationService extends BaseService<NotificationServiceEvents> {
notifications: await sortedNotifications
.skip(Number(start))
.limit(Number(limit) || DEFAULT_QUERY_LIMIT)
.exec(),
.execAsync(),
count: this.count,
};
} else {
return {
notifications: await sortedNotifications.limit(Number(limit) || DEFAULT_QUERY_LIMIT).exec(),
notifications: await sortedNotifications.limit(Number(limit) || DEFAULT_QUERY_LIMIT).execAsync(),
count: this.count,
};
}