Notes/_regroup/bin/generate_document.ts

96 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-12-01 09:19:16 +01:00
/**
* Usage: tsx ./generate_document.ts 1000
2023-06-30 11:18:34 +02:00
* will create 1000 new notes and some clones into the current document.db
2019-12-01 09:19:16 +01:00
*/
import sqlInit from "../src/services/sql_init.js";
import noteService from "../src/services/notes.js";
import attributeService from "../src/services/attributes.js";
import cls from "../src/services/cls.js";
import cloningService from "../src/services/cloning.js";
2025-01-09 18:07:02 +02:00
import loremIpsum from "lorem-ipsum";
import "../src/becca/entity_constructor.js";
2019-12-01 09:19:16 +01:00
const noteCount = parseInt(process.argv[2]);
2019-12-01 09:19:16 +01:00
if (!noteCount) {
console.error(`Please enter number of notes as program parameter.`);
process.exit(1);
}
2025-01-09 18:07:02 +02:00
const notes = ["root"];
function getRandomNoteId() {
const index = Math.floor(Math.random() * notes.length);
return notes[index];
}
async function start() {
for (let i = 0; i < noteCount; i++) {
2024-08-10 18:23:49 +02:00
const title = loremIpsum.loremIpsum({
count: 1,
2025-01-09 18:07:02 +02:00
units: "sentences",
sentenceLowerBound: 1,
sentenceUpperBound: 10
});
const paragraphCount = Math.floor(Math.random() * Math.random() * 100);
2024-08-10 18:23:49 +02:00
const content = loremIpsum.loremIpsum({
count: paragraphCount,
2025-01-09 18:07:02 +02:00
units: "paragraphs",
sentenceLowerBound: 1,
sentenceUpperBound: 15,
paragraphLowerBound: 3,
paragraphUpperBound: 10,
2025-01-09 18:07:02 +02:00
format: "html"
});
2024-08-10 18:23:49 +02:00
const { note } = noteService.createNewNote({
parentNoteId: getRandomNoteId(),
2019-11-16 11:09:52 +01:00
title,
content,
2025-01-09 18:07:02 +02:00
type: "text"
2019-11-16 11:09:52 +01:00
});
console.log(`Created note ${i}: ${title}`);
if (Math.random() < 0.04) {
const noteIdToClone = note.noteId;
const parentNoteId = getRandomNoteId();
2025-01-09 18:07:02 +02:00
const prefix = Math.random() > 0.8 ? "prefix" : "";
const result = await cloningService.cloneNoteToBranch(noteIdToClone, parentNoteId, prefix);
console.log(`Cloning ${i}:`, result.success ? "succeeded" : "FAILED");
}
// does not have to be for the current note
await attributeService.createAttribute({
noteId: getRandomNoteId(),
2025-01-09 18:07:02 +02:00
type: "label",
name: "label",
value: "value",
isInheritable: Math.random() > 0.1 // 10% are inheritable
});
await attributeService.createAttribute({
noteId: getRandomNoteId(),
2025-01-09 18:07:02 +02:00
type: "relation",
name: "relation",
value: getRandomNoteId(),
isInheritable: Math.random() > 0.1 // 10% are inheritable
});
note.saveRevision();
notes.push(note.noteId);
}
process.exit(0);
}
// @TriliumNextTODO sqlInit.dbReady never seems to resolve so program hangs
// see https://github.com/TriliumNext/Notes/issues/1020
sqlInit.dbReady.then(cls.wrap(start)).catch((err) => console.error(err));