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

Commit f4d19e2

Browse files
committed
server-esm: Solve some more imports
1 parent 4ed88d2 commit f4d19e2

File tree

11 files changed

+62
-51
lines changed

11 files changed

+62
-51
lines changed

spec/search/search.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import BBranch from "../../src/becca/entities/bbranch.js";
44
import SearchContext from "../../src/services/search/search_context.js";
55
import dateUtils from "../../src/services/date_utils.js";
66
import becca from "../../src/becca/becca.js";
7-
// const { NoteBuilder, findNoteByTitle, note } = require('./becca_mocking');
87
import becca_mocking from "./becca_mocking.js";
98

109
describe('Search', () => {

src/becca/becca_loader.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import cls from "../services/cls.js";
1414
import entityConstructor from "../becca/entity_constructor.js";
1515
import { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow } from './entities/rows';
1616
import AbstractBeccaEntity from "./entities/abstract_becca_entity.js";
17+
import options_init from "../services/options_init.js";
18+
import ws from "../services/ws.js";
1719

1820
const beccaLoaded = new Promise<void>((res, rej) => {
1921
sqlInit.dbReady.then(() => {
2022
cls.init(() => {
2123
load();
2224

23-
require('../services/options_init').initStartupOptions();
25+
options_init.initStartupOptions();
2426

2527
res();
2628
});
@@ -73,7 +75,7 @@ function load() {
7375
function reload(reason: string) {
7476
load();
7577

76-
require('../services/ws').reloadFrontend(reason || "becca reloaded");
78+
ws.reloadFrontend(reason || "becca reloaded");
7779
}
7880

7981
eventService.subscribeBeccaLoader([eventService.ENTITY_CHANGE_SYNCED], ({ entityName, entityRow }) => {

src/becca/entities/battachment.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import log from "../../services/log.js";
99
import { AttachmentRow } from './rows';
1010
import BNote from "./bnote.js";
1111
import BBranch from "./bbranch.js";
12+
import noteService from "../../services/notes.js";
1213

1314
const attachmentRoleToNoteTypeMapping = {
1415
'image': 'image',
@@ -157,8 +158,6 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
157158
throw new Error(`Cannot convert protected attachment outside of protected session`);
158159
}
159160

160-
const noteService = require('../../services/notes');
161-
162161
const { note, branch } = noteService.createNewNote({
163162
parentNoteId: this.ownerId,
164163
title: this.title,

src/becca/entities/bnote.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import TaskContext from "../../services/task_context.js";
1212
import dayjs from "dayjs";
1313
import utc from "dayjs/plugin/utc";
1414
import eventService from "../../services/events.js";
15-
import { AttachmentRow, NoteRow, NoteType, RevisionRow } from './rows';
15+
import { AttachmentRow, AttributeType, NoteRow, NoteType, RevisionRow } from './rows';
1616
import BBranch from "./bbranch.js";
1717
import BAttribute from "./battribute.js";
1818
import { NotePojo } from '../becca-interface';
19+
import searchService from "../../services/search/services/search.js";
20+
import cloningService, { CloneResponse } from "../../services/cloning.js";
21+
import noteService from "../../services/notes.js";
22+
import handlers from "../../services/handlers.js";
1923
dayjs.extend(utc);
2024

2125
const LABEL = 'label';
@@ -890,11 +894,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
890894
}
891895

892896
try {
893-
const searchService = require('../../services/search/services/search');
894-
const {searchResultNoteIds} = searchService.searchFromNote(this);
895-
897+
const result = searchService.searchFromNote(this);
896898
const becca = this.becca;
897-
return (searchResultNoteIds as string[]) // TODO: remove cast once search is converted
899+
return (result.searchResultNoteIds)
898900
.map(resultNoteId => becca.notes[resultNoteId])
899901
.filter(note => !!note);
900902
}
@@ -1261,7 +1263,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
12611263
* @param name - attribute name
12621264
* @param value - attribute value (optional)
12631265
*/
1264-
setAttribute(type: string, name: string, value?: string) {
1266+
setAttribute(type: AttributeType, name: string, value?: string) {
12651267
const attributes = this.getOwnedAttributes();
12661268
const attr = attributes.find(attr => attr.type === type && attr.name === name);
12671269

@@ -1274,8 +1276,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
12741276
}
12751277
}
12761278
else {
1277-
const BAttribute = require('./battribute');
1278-
12791279
new BAttribute({
12801280
noteId: this.noteId,
12811281
type: type,
@@ -1310,9 +1310,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
13101310
* @param name - name of the attribute, not including the leading ~/#
13111311
* @param value - value of the attribute - text for labels, target note ID for relations; optional.
13121312
*/
1313-
addAttribute(type: string, name: string, value: string = "", isInheritable: boolean = false, position: number | null = null): BAttribute {
1314-
const BAttribute = require('./battribute');
1315-
1313+
addAttribute(type: AttributeType, name: string, value: string = "", isInheritable: boolean = false, position: number | null = null): BAttribute {
13161314
return new BAttribute({
13171315
noteId: this.noteId,
13181316
type: type,
@@ -1351,7 +1349,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
13511349
* @param name - attribute name
13521350
* @param value - attribute value (optional)
13531351
*/
1354-
toggleAttribute(type: string, enabled: boolean, name: string, value?: string) {
1352+
toggleAttribute(type: AttributeType, enabled: boolean, name: string, value?: string) {
13551353
if (enabled) {
13561354
this.setAttribute(type, name, value);
13571355
}
@@ -1423,21 +1421,23 @@ class BNote extends AbstractBeccaEntity<BNote> {
14231421
}
14241422

14251423
searchNotesInSubtree(searchString: string) {
1426-
const searchService = require('../../services/search/services/search');
1427-
14281424
return searchService.searchNotes(searchString) as BNote[];
14291425
}
14301426

14311427
searchNoteInSubtree(searchString: string) {
14321428
return this.searchNotesInSubtree(searchString)[0];
14331429
}
14341430

1435-
cloneTo(parentNoteId: string) {
1436-
const cloningService = require('../../services/cloning');
1437-
1431+
cloneTo(parentNoteId: string): CloneResponse {
14381432
const branch = this.becca.getNote(parentNoteId)?.getParentBranches()[0];
1433+
if (!branch?.branchId) {
1434+
return {
1435+
success: false,
1436+
message: "Unable to find the branch ID to clone."
1437+
};
1438+
}
14391439

1440-
return cloningService.cloneNoteToBranch(this.noteId, branch?.branchId);
1440+
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
14411441
}
14421442

14431443
isEligibleForConversionToAttachment(opts: ConvertOpts = { autoConversion: false }) {
@@ -1508,7 +1508,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
15081508

15091509
parentNote.setContent(fixedContent);
15101510

1511-
const noteService = require('../../services/notes');
15121511
noteService.asyncPostProcessContent(parentNote, fixedContent); // to mark an unused attachment for deletion
15131512

15141513
this.deleteNote();
@@ -1535,7 +1534,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
15351534
}
15361535

15371536
// needs to be run before branches and attributes are deleted and thus attached relations disappear
1538-
const handlers = require('../../services/handlers');
15391537
handlers.runAttachedRelations(this, 'runOnNoteDeletion', this);
15401538
taskContext.noteDeletionHandlerTriggered = true;
15411539

src/becca/entities/rows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export interface AttributeRow {
6969
noteId?: string;
7070
type: AttributeType;
7171
name: string;
72-
position?: number;
72+
position?: number | null;
7373
value?: string;
7474
isInheritable?: boolean;
7575
utcDateModified?: string;

src/routes/api/search.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { Request } from "express";
44

55
import becca from "../../becca/becca.js";
66
import SearchContext from "../../services/search/search_context.js";
7-
import searchService from "../../services/search/services/search.js";
7+
import searchService, { EMPTY_RESULT, SearchNoteResult } from "../../services/search/services/search.js";
88
import bulkActionService from "../../services/bulk_actions.js";
99
import cls from "../../services/cls.js";
1010
import attributeFormatter from "../../services/attribute_formatter.js";
1111
import ValidationError from "../../errors/validation_error.js";
1212
import SearchResult from "../../services/search/search_result.js";
1313

14-
function searchFromNote(req: Request) {
14+
function searchFromNote(req: Request): SearchNoteResult {
1515
const note = becca.getNoteOrThrow(req.params.noteId);
1616

1717
if (!note) {
1818
// this can be triggered from recent changes, and it's harmless to return an empty list rather than fail
19-
return [];
19+
return EMPTY_RESULT;
2020
}
2121

2222
if (note.type !== 'search') {

src/services/cloning.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BBranch from '../becca/entities/bbranch';
77
import becca from '../becca/becca';
88
import log from './log';
99

10-
interface CloneResponse {
10+
export interface CloneResponse {
1111
success: boolean;
1212
message?: string;
1313
branchId?: string;
@@ -53,7 +53,7 @@ function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: str
5353
};
5454
}
5555

56-
function cloneNoteToBranch(noteId: string, parentBranchId: string, prefix: string) {
56+
function cloneNoteToBranch(noteId: string, parentBranchId: string, prefix?: string) {
5757
const parentBranch = becca.getBranch(parentBranchId);
5858

5959
if (!parentBranch) {

src/services/import/enex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import sanitizeAttributeName from "../sanitize_attribute_name.js";
1212
import TaskContext from "../task_context.js";
1313
import BNote from "../../becca/entities/bnote.js";
1414
import { File } from "./common";
15+
import { AttributeType } from "../../becca/entities/rows.js";
1516

1617
/**
1718
* date format is e.g. 20181121T193703Z or 2013-04-14T16:19:00.000Z (Mac evernote, see #3496)
@@ -29,7 +30,7 @@ function parseDate(text: string) {
2930
}
3031

3132
interface Attribute {
32-
type: string;
33+
type: AttributeType;
3334
name: string;
3435
value: string;
3536
}

src/services/log.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function checkDate(millisSinceMidnight: number) {
5050
return millisSinceMidnight;
5151
}
5252

53-
function log(str: string) {
53+
function log(str: string | Error) {
5454
const bundleNoteId = cls.get("bundleNoteId");
5555

5656
if (bundleNoteId) {
@@ -66,11 +66,11 @@ function log(str: string) {
6666
console.log(str);
6767
}
6868

69-
function info(message: string) {
69+
function info(message: string | Error) {
7070
log(message);
7171
}
7272

73-
function error(message: string) {
73+
function error(message: string | Error) {
7474
log(`ERROR: ${message}`);
7575
}
7676

src/services/search/services/search.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ import Expression from "../expressions/expression.js";
1818
import sql from "../../sql.js";
1919
import scriptService from "../../script.js";
2020

21-
function searchFromNote(note: BNote) {
21+
export interface SearchNoteResult {
22+
searchResultNoteIds: string[];
23+
highlightedTokens: string[];
24+
error: string | null;
25+
}
26+
27+
export const EMPTY_RESULT: SearchNoteResult = {
28+
searchResultNoteIds: [],
29+
highlightedTokens: [],
30+
error: null
31+
};
32+
33+
function searchFromNote(note: BNote): SearchNoteResult {
2234
let searchResultNoteIds;
2335
let highlightedTokens: string[];
2436

0 commit comments

Comments
 (0)