| 
									
										
										
										
											2019-12-01 09:19:16 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2025-01-20 22:54:12 +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
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 21:46:25 +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"; | 
					
						
							| 
									
										
										
										
											2025-01-20 21:46:25 +01:00
										 |  |  | import "../src/becca/entity_constructor.js"; | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-01 09:19:16 +01:00
										 |  |  | const noteCount = parseInt(process.argv[2]); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-01 09:19:16 +01:00
										 |  |  | if (!noteCount) { | 
					
						
							|  |  |  |     console.error(`Please enter number of notes as program parameter.`); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | const notes = ["root"]; | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-01 10:20:18 +01:00
										 |  |  | function getRandomNoteId() { | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |     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({ | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |             count: 1, | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             units: "sentences", | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |             sentenceLowerBound: 1, | 
					
						
							|  |  |  |             sentenceUpperBound: 10 | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const paragraphCount = Math.floor(Math.random() * Math.random() * 100); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  |         const content = loremIpsum.loremIpsum({ | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |             count: paragraphCount, | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             units: "paragraphs", | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |             sentenceLowerBound: 1, | 
					
						
							|  |  |  |             sentenceUpperBound: 15, | 
					
						
							|  |  |  |             paragraphLowerBound: 3, | 
					
						
							|  |  |  |             paragraphUpperBound: 10, | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             format: "html" | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  |         const { note } = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2019-12-01 10:32:30 +01:00
										 |  |  |             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
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         console.log(`Created note ${i}: ${title}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |         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" : ""; | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 23:39:46 +01:00
										 |  |  |             const result = await cloningService.cloneNoteToBranch(noteIdToClone, parentNoteId, prefix); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |             console.log(`Cloning ${i}:`, result.success ? "succeeded" : "FAILED"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  |         // does not have to be for the current note
 | 
					
						
							| 
									
										
										
										
											2019-12-01 10:20:18 +01:00
										 |  |  |         await attributeService.createAttribute({ | 
					
						
							|  |  |  |             noteId: getRandomNoteId(), | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             type: "label", | 
					
						
							|  |  |  |             name: "label", | 
					
						
							|  |  |  |             value: "value", | 
					
						
							| 
									
										
										
										
											2019-12-01 10:20:18 +01:00
										 |  |  |             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", | 
					
						
							| 
									
										
										
										
											2019-12-01 10:20:18 +01:00
										 |  |  |             value: getRandomNoteId(), | 
					
						
							|  |  |  |             isInheritable: Math.random() > 0.1 // 10% are inheritable
 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 23:01:40 +02:00
										 |  |  |         note.saveRevision(); | 
					
						
							| 
									
										
										
										
											2019-12-02 19:52:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         notes.push(note.noteId); | 
					
						
							| 
									
										
										
										
											2019-12-01 10:20:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  |     process.exit(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 22:54:12 +01:00
										 |  |  | // @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)); |