mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-27 10:32:58 +02:00
Chore: Desktop: Add more ways to automatically populate the database
This commit is contained in:
parent
0cd2fd660d
commit
fa7d48a3bd
@ -559,7 +559,12 @@ class Application extends BaseApplication {
|
||||
|
||||
await SpellCheckerService.instance().initialize(new SpellCheckerServiceDriverNative());
|
||||
|
||||
// await populateDatabase(reg.db());
|
||||
// await populateDatabase(reg.db(), {
|
||||
// clearDatabase: true,
|
||||
// folderCount: 1000,
|
||||
// rootFolderCount: 1,
|
||||
// subFolderDepth: 1,
|
||||
// });
|
||||
|
||||
// setTimeout(() => {
|
||||
// console.info(CommandService.instance().commandsToMarkdownTable(this.store().getState()));
|
||||
|
@ -9,6 +9,8 @@ export interface Options {
|
||||
tagsPerNote?: number;
|
||||
silent?: number;
|
||||
clearDatabase?: boolean;
|
||||
rootFolderCount?: number;
|
||||
subFolderDepth?: number;
|
||||
}
|
||||
|
||||
function randomIndex(array: any[]): number {
|
||||
@ -47,6 +49,8 @@ export default async function populateDatabase(db: any, options: Options = null)
|
||||
tagCount: 0,
|
||||
tagsPerNote: 0,
|
||||
clearDatabase: false,
|
||||
rootFolderCount: 0,
|
||||
subFolderDepth: 0,
|
||||
...options,
|
||||
};
|
||||
|
||||
@ -55,21 +59,45 @@ export default async function populateDatabase(db: any, options: Options = null)
|
||||
const createdFolderIds: string[] = [];
|
||||
const createdNoteIds: string[] = [];
|
||||
const createdTagIds: string[] = [];
|
||||
const createdFolderDepths: Record<string, number> = {};
|
||||
const folderDepthToId: Record<number, string[]> = {};
|
||||
let rootFolderCount: number = 0;
|
||||
|
||||
for (let i = 0; i < options.folderCount; i++) {
|
||||
const folder: any = {
|
||||
title: `folder${i}`,
|
||||
};
|
||||
|
||||
const isRoot = Math.random() <= 0.1 || i === 0;
|
||||
let isRoot = Math.random() <= 0.1 || i === 0;
|
||||
|
||||
if (options.rootFolderCount && rootFolderCount >= options.rootFolderCount) isRoot = false;
|
||||
|
||||
let depth: number = 0;
|
||||
|
||||
if (!isRoot) {
|
||||
const parentIndex = randomIndex(createdFolderIds);
|
||||
folder.parent_id = createdFolderIds[parentIndex];
|
||||
let possibleFolderIds: string[] = [];
|
||||
if (options.subFolderDepth) {
|
||||
for (let i = 0; i < options.subFolderDepth; i++) {
|
||||
if (folderDepthToId[i]) possibleFolderIds = possibleFolderIds.concat(folderDepthToId[i]);
|
||||
}
|
||||
} else {
|
||||
possibleFolderIds = createdFolderIds;
|
||||
}
|
||||
|
||||
const parentIndex = randomIndex(possibleFolderIds);
|
||||
const parentId = possibleFolderIds[parentIndex];
|
||||
folder.parent_id = parentId;
|
||||
depth = createdFolderDepths[parentId] + 1;
|
||||
} else {
|
||||
rootFolderCount++;
|
||||
}
|
||||
|
||||
const savedFolder = await Folder.save(folder);
|
||||
createdFolderIds.push(savedFolder.id);
|
||||
createdFolderDepths[savedFolder.id] = depth;
|
||||
|
||||
if (!folderDepthToId[depth]) folderDepthToId[depth] = [];
|
||||
folderDepthToId[depth].push(savedFolder.id);
|
||||
|
||||
if (!options.silent) console.info(`Folders: ${i} / ${options.folderCount}`);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user