Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 625e9fb

Browse files
committed
Merge branch 'improved_rollup_and_footer'
2 parents ae33aa7 + 8268ce9 commit 625e9fb

File tree

23 files changed

+446
-148
lines changed

23 files changed

+446
-148
lines changed

docs/docs/features/Formulas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The root object `db` has the following functions:
3131

3232
- `js` : execute a javascript function that you previously defined in the `js` folder of your table in the database or plugin global settings. (I.E.: `db.js.myFunction( arg1, arg2)`)
3333
- `dataview`: expose the dataview API. (see [Dataview API](https://github.com/blacksmithgu/obsidian-dataview/blob/master/src/api/plugin-api.ts))
34-
- `rollup`: expose the rollup functions of the dbfolder plugin. (see [Rollup documentation](/features/Relations/#rollups))
34+
- `rollup`: expose the rollup functions of the dbfolder plugin. (see [Rollup documentation](/obsidian-db-folder/features/Relations/#rollups))
3535

3636
#### Javascript file structure
3737
To add a javascript file to the `js` folder, it must be a `.js` file and have the following structure:

src/DatabaseView.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { obtainFormulasFromFolder } from "automations/AutomationsHelper";
21
import { DatabaseColumn } from "cdm/DatabaseModel";
32
import { UpdaterData, ViewEvents } from "cdm/EmitterModel";
43
import {
@@ -38,6 +37,7 @@ import {
3837
Menu,
3938
} from "obsidian";
4039
import { createRoot, Root } from "react-dom/client";
40+
import { DbAutomationService } from "services/AutomationService";
4141
import DatabaseInfo from "services/DatabaseInfo";
4242
import { LOGGER } from "services/Logger";
4343
import { SettingsModal } from "Settings";
@@ -96,6 +96,10 @@ export class DatabaseView extends TextFileView implements HoverParent {
9696
this.plugin.addView(this, data);
9797
}
9898

99+
getIcon() {
100+
return DB_ICONS.NAME;
101+
}
102+
99103
getViewType(): string {
100104
return DatabaseCore.FRONTMATTER_KEY;
101105
}
@@ -132,7 +136,7 @@ export class DatabaseView extends TextFileView implements HoverParent {
132136
.addItem((item) => {
133137
item
134138
.setTitle(t("menu_pane_open_db_settings_action"))
135-
.setIcon(DB_ICONS.NAME)
139+
.setIcon("gear")
136140
.onClick(this.settingsAction.bind(this));
137141
})
138142
.addSeparator();
@@ -168,7 +172,7 @@ export class DatabaseView extends TextFileView implements HoverParent {
168172
);
169173
this.initial = obtainInitialType(this.columns);
170174

171-
this.formulas = await obtainFormulasFromFolder(
175+
this.formulas = await DbAutomationService.buildFns(
172176
this.diskConfig.yaml.config
173177
);
174178
// Define table properties
@@ -196,7 +200,7 @@ export class DatabaseView extends TextFileView implements HoverParent {
196200
private initActions(): void {
197201
// Settings action
198202
this.addAction(
199-
DB_ICONS.NAME,
203+
"gear",
200204
`${t("menu_pane_open_db_settings_action")}`,
201205
this.settingsAction.bind(this)
202206
);

src/automations/AutomationsHelper.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/automations/Footer.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DEFAULT_SETTINGS, FooterType } from "helpers/Constants";
22
import { Literal } from "obsidian-dataview";
33
import { DataviewService } from "services/DataviewService";
44
import { DateTime } from "luxon";
5+
import { DbAutomationService } from "services/AutomationService";
56

67
export default class Footer {
78
constructor(public readonly colValues: Literal[]) { }
@@ -119,10 +120,7 @@ export default class Footer {
119120
* @returns
120121
*/
121122
public sum(): string {
122-
const total = this.colValues
123-
.map((value) => Number(value))
124-
.filter((value) => !isNaN(value))
125-
.reduce((acc: number, value: number) => acc + value, 0);
123+
const total = DbAutomationService.coreFns.numbers.sum(this.colValues);
126124
return `Total: ${total}`;
127125
}
128126

@@ -131,10 +129,7 @@ export default class Footer {
131129
* @returns
132130
*/
133131
public min(): string {
134-
const min = this.colValues
135-
.map((value) => Number(value))
136-
.filter((value) => !isNaN(value))
137-
.reduce((acc: number, value: number) => Math.min(acc, value), Number.MAX_SAFE_INTEGER);
132+
const min = DbAutomationService.coreFns.numbers.min(this.colValues);
138133
return `Min: ${min}`;
139134
}
140135

@@ -143,10 +138,7 @@ export default class Footer {
143138
* @returns
144139
*/
145140
public max(): string {
146-
const max = this.colValues
147-
.map((value) => Number(value))
148-
.filter((value) => !isNaN(value))
149-
.reduce((acc: number, value: number) => Math.max(acc, value), Number.MIN_SAFE_INTEGER);
141+
const max = DbAutomationService.coreFns.numbers.max(this.colValues);
150142
return `Max: ${max}`;
151143
}
152144

@@ -158,10 +150,8 @@ export default class Footer {
158150
* @returns
159151
*/
160152
public earliestDate(): string {
161-
const earliest = this.colValues
162-
.filter((value) => DateTime.isDateTime(value))
163-
.reduce((acc: DateTime, value: DateTime) => acc < value ? acc : value, DateTime.max);
164-
return DateTime.isDateTime(earliest) ?
153+
const earliest = DbAutomationService.coreFns.luxon.earliest(this.colValues);
154+
return earliest.isValid ?
165155
`Earliest: ${earliest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
166156
null;
167157
}
@@ -171,10 +161,8 @@ export default class Footer {
171161
* @returns
172162
*/
173163
public latestDate(): string {
174-
const latest = this.colValues
175-
.filter((value) => DateTime.isDateTime(value))
176-
.reduce((acc: DateTime, value: DateTime) => acc > value ? acc : value, DateTime.min);
177-
return DateTime.isDateTime(latest) ?
164+
const latest = DbAutomationService.coreFns.luxon.latest(this.colValues);
165+
return latest.isValid ?
178166
`Latest: ${latest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
179167
null;
180168
}
@@ -184,15 +172,8 @@ export default class Footer {
184172
* @returns
185173
*/
186174
public rangeDate(): string {
187-
const earliest = this.colValues
188-
.filter((value) => DateTime.isDateTime(value))
189-
.reduce((acc: DateTime, value: DateTime) => acc < value ? acc : value, DateTime.max);
190-
const latest = this.colValues
191-
.filter((value) => DateTime.isDateTime(value))
192-
.reduce((acc: DateTime, value: DateTime) => acc > value ? acc : value, DateTime.min);
193-
return DateTime.isDateTime(earliest) && DateTime.isDateTime(latest) ?
194-
`Range: ${earliest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)} - ${latest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
195-
null;
175+
const range = DbAutomationService.coreFns.luxon.range(this.colValues);
176+
return `Range: ${range} days`
196177
}
197178

198179
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { FormulaFunctions } from "automations/formula_functions/FormulaFunctions";
2-
import { IGenerateObject } from "automations/IGenerateObject";
2+
import { IGenerateObject } from "automations/core/IGenerateObject";
33
import { LocalSettings } from "cdm/SettingsModel";
4-
import { Link } from "obsidian-dataview";
5-
import { DataviewService } from "services/DataviewService";
6-
import Rollup from "automations/Rollup";
74
export class FormulaGenerator implements IGenerateObject {
85
public js_functions: FormulaFunctions;
96

@@ -16,13 +13,8 @@ export class FormulaGenerator implements IGenerateObject {
1613
}
1714

1815
async generate_object(): Promise<Record<string, unknown>> {
19-
const final_object: Record<string, unknown> = {};
20-
Object.assign(final_object, {
16+
return {
2117
js: await this.generate_js_functions(),
22-
dataview: DataviewService.getDataviewAPI(),
23-
rollup: (relation: Link[]) => new Rollup(relation)
24-
}
25-
);
26-
return final_object;
18+
};
2719
}
2820
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { IGenerateObject } from "automations/core/IGenerateObject";
2+
import { DatabaseFnType } from "cdm/ModulesFnModel";
3+
import { DbModule } from "./core/DbModule";
4+
import { LuxonFn } from "./core/modules/LuxonFn";
5+
import { NumbersFn } from "./core/modules/NumbersFn";
6+
export class ModulesGenerator implements IGenerateObject {
7+
private modules_array: Array<DbModule> = [];
8+
9+
constructor() {
10+
this.modules_array.push(new NumbersFn());
11+
this.modules_array.push(new LuxonFn());
12+
}
13+
14+
async init(): Promise<void> {
15+
for (const mod of this.modules_array) {
16+
await mod.init();
17+
}
18+
}
19+
20+
async generate_object(): Promise<DatabaseFnType> {
21+
const internal_functions_object: { [key: string]: unknown } = {};
22+
for (const mod of this.modules_array) {
23+
internal_functions_object[mod.getName()] =
24+
await mod.generate_object();
25+
}
26+
27+
return internal_functions_object as DatabaseFnType;
28+
}
29+
}

0 commit comments

Comments
 (0)