Compare commits

...

2 Commits

Author SHA1 Message Date
d331866f46 Fix app building 2025-01-17 14:27:01 +01:00
3919eed679 Add support for database 2025-01-17 14:05:30 +01:00
8 changed files with 65 additions and 3 deletions

View File

@@ -7,5 +7,5 @@ buildNpmPackage {
pname = "obsidian-tasks-reminder";
version = "0.0.1";
src = ./.;
npmDepsHash = "sha256-rhwGWuw/X7YGdPwIrgkIGukFNpAwpx/a6H3D/2cVP5Y=";
npmDepsHash = "sha256-d8uZWYmroWoju976WXnCaYX+0uTLK/tc6hS/WgEHv/o=";
}

View File

@@ -0,0 +1,17 @@
import fs from "fs";
import { NotificationDatabase } from "../types/notification";
/**
* Loads and deserializes database from JSON formatted file.
*/
export function loadDatabase(file: string): NotificationDatabase {
const text = fs.readFileSync(file).toString();
return deserializeDatabase(text);
}
/**
* Deserializes database from JSON format.
*/
export function deserializeDatabase(json: string): NotificationDatabase {
return JSON.parse(json) as NotificationDatabase;
}

2
src/database/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export { serializeDatabase } from "./serializer";
export { deserializeDatabase } from "./deserializer";

View File

@@ -0,0 +1,34 @@
import fs from "fs";
import { Task } from "../types/task";
import { Notification, NotificationDatabase } from "../types/notification";
/**
* Applies the mapper for each task from list, groups them by time and dumps the data into JSON formatted file.
*/
export function dumpDatabase(file: string, tasks: Task[], mapper: (task: Task) => Notification[]) {
const data = serializeDatabase(tasks, mapper);
fs.writeFileSync(file, data);
}
/**
* Applies the mapper for each task from list, groups them by time and serializes into JSON format.
*/
export function serializeDatabase(tasks: Task[], mapper: (task: Task) => Notification[]): string {
const output = tasks.flatMap(wrapWithTimeFiller(mapper)).reduce((acc, n) => {
if (n.time) {
(acc[n.time] = (acc[n.time] || [])).push(n);
};
return acc;
}, {} as NotificationDatabase);
return JSON.stringify(output);
}
function wrapWithTimeFiller(mapper: (task: Task) => Notification[]): (task: Task) => Notification[] {
return (task: Task) => mapper(task)
.map(notification => ({
...notification,
time: task.reminder ?? notification.time,
}));
}

View File

@@ -3,7 +3,7 @@ import dayjs from "dayjs";
import { createInterface } from "readline";
import { parse } from "../generated/grammar/task";
import { Task as DefaultTask } from "../model";
import { ParseResult } from "../types/grammar/task";
import { ParseResult } from "../types/grammar";
import { Task, TaskPriority } from "../types/task";
/**

View File

@@ -1,5 +1,5 @@
import dayjs, { Dayjs } from "dayjs";
import { ParsedTask, ParsedTaskDate, ParsedTaskDependency, ParsedTaskMeta } from "../types/grammar/task";
import { ParsedTask, ParsedTaskDate, ParsedTaskDependency, ParsedTaskMeta } from "../types/grammar";
import { Task, TaskPriority } from "../types/task";
export class LazyTask implements Task {

View File

@@ -0,0 +1,9 @@
import dayjs, { Dayjs } from "dayjs"
export type Notification = {
time?: string;
title: string;
text: string;
};
export type NotificationDatabase = Record<string, Notification[]>;