@vue-skuilder/db 0.2.35 → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +15 -4
- package/build.entries.mjs +12 -0
- package/dist/_virtual/_rolldown/runtime.mjs +8 -0
- package/dist/{contentSource-BteiDpxH.d.ts → contentSource-D_E7JaxR.d.ts} +1 -1
- package/dist/core/UserDBDebugger.mjs +285 -0
- package/dist/core/UserDBDebugger.mjs.map +1 -0
- package/dist/core/bulkImport/cardProcessor.mjs +106 -0
- package/dist/core/bulkImport/cardProcessor.mjs.map +1 -0
- package/dist/core/{index.js → index.cjs} +6438 -7784
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.ts +5 -5
- package/dist/core/index.mjs +20 -11368
- package/dist/core/interfaces/contentSource.mjs +20 -0
- package/dist/core/interfaces/contentSource.mjs.map +1 -0
- package/dist/core/navigators/Pipeline.mjs +723 -0
- package/dist/core/navigators/Pipeline.mjs.map +1 -0
- package/dist/core/navigators/PipelineAssembler.mjs +86 -0
- package/dist/core/navigators/PipelineAssembler.mjs.map +1 -0
- package/dist/core/navigators/PipelineDebugger.mjs +771 -0
- package/dist/core/navigators/PipelineDebugger.mjs.map +1 -0
- package/dist/core/navigators/SrsDebugger.mjs +34 -0
- package/dist/core/navigators/SrsDebugger.mjs.map +1 -0
- package/dist/core/navigators/StrategyPressureDebugger.mjs +19 -0
- package/dist/core/navigators/StrategyPressureDebugger.mjs.map +1 -0
- package/dist/core/navigators/defaults.mjs +96 -0
- package/dist/core/navigators/defaults.mjs.map +1 -0
- package/dist/core/navigators/diversityRerank.mjs +85 -0
- package/dist/core/navigators/diversityRerank.mjs.map +1 -0
- package/dist/core/navigators/filters/WeightedFilter.mjs +81 -0
- package/dist/core/navigators/filters/WeightedFilter.mjs.map +1 -0
- package/dist/core/navigators/filters/eloDistance.mjs +244 -0
- package/dist/core/navigators/filters/eloDistance.mjs.map +1 -0
- package/dist/core/navigators/filters/hierarchyDefinition.mjs +235 -0
- package/dist/core/navigators/filters/hierarchyDefinition.mjs.map +1 -0
- package/dist/core/navigators/filters/interferenceMitigator.mjs +201 -0
- package/dist/core/navigators/filters/interferenceMitigator.mjs.map +1 -0
- package/dist/core/navigators/filters/relativePriority.mjs +135 -0
- package/dist/core/navigators/filters/relativePriority.mjs.map +1 -0
- package/dist/core/navigators/filters/userTagPreference.mjs +101 -0
- package/dist/core/navigators/filters/userTagPreference.mjs.map +1 -0
- package/dist/core/navigators/generators/CompositeGenerator.mjs +203 -0
- package/dist/core/navigators/generators/CompositeGenerator.mjs.map +1 -0
- package/dist/core/navigators/generators/elo.mjs +77 -0
- package/dist/core/navigators/generators/elo.mjs.map +1 -0
- package/dist/core/navigators/generators/prescribed.mjs +676 -0
- package/dist/core/navigators/generators/prescribed.mjs.map +1 -0
- package/dist/core/navigators/generators/srs.mjs +158 -0
- package/dist/core/navigators/generators/srs.mjs.map +1 -0
- package/dist/core/navigators/index.mjs +345 -0
- package/dist/core/navigators/index.mjs.map +1 -0
- package/dist/core/navigators/pipelinePlan.mjs +109 -0
- package/dist/core/navigators/pipelinePlan.mjs.map +1 -0
- package/dist/core/navigators/regulators.mjs +128 -0
- package/dist/core/navigators/regulators.mjs.map +1 -0
- package/dist/core/orchestration/gradient.mjs +101 -0
- package/dist/core/orchestration/gradient.mjs.map +1 -0
- package/dist/core/orchestration/index.mjs +115 -0
- package/dist/core/orchestration/index.mjs.map +1 -0
- package/dist/core/orchestration/learning.mjs +140 -0
- package/dist/core/orchestration/learning.mjs.map +1 -0
- package/dist/core/orchestration/recording.mjs +64 -0
- package/dist/core/orchestration/recording.mjs.map +1 -0
- package/dist/core/orchestration/signal.mjs +40 -0
- package/dist/core/orchestration/signal.mjs.map +1 -0
- package/dist/core/types/contentNavigationStrategy.mjs +10 -0
- package/dist/core/types/contentNavigationStrategy.mjs.map +1 -0
- package/dist/core/types/hydration.mjs +13 -0
- package/dist/core/types/hydration.mjs.map +1 -0
- package/dist/core/types/strategyState.mjs +16 -0
- package/dist/core/types/strategyState.mjs.map +1 -0
- package/dist/core/types/studySession.mjs +14 -0
- package/dist/core/types/studySession.mjs.map +1 -0
- package/dist/core/types/types-legacy.mjs +44 -0
- package/dist/core/types/types-legacy.mjs.map +1 -0
- package/dist/core/util/index.mjs +31 -0
- package/dist/core/util/index.mjs.map +1 -0
- package/dist/core/versionStamps.mjs +46 -0
- package/dist/core/versionStamps.mjs.map +1 -0
- package/dist/courseConfigRegistration.mjs +306 -0
- package/dist/courseConfigRegistration.mjs.map +1 -0
- package/dist/{dataLayerProvider-C_QWzHf-.d.ts → dataLayerProvider-BDshhSp_.d.ts} +1 -1
- package/dist/diagnostics/dataset.mjs +118 -0
- package/dist/diagnostics/dataset.mjs.map +1 -0
- package/dist/diagnostics/derive.mjs +318 -0
- package/dist/diagnostics/derive.mjs.map +1 -0
- package/dist/diagnostics/{index.js → index.cjs} +1 -1
- package/dist/diagnostics/index.d.ts +3 -3
- package/dist/diagnostics/index.mjs +7 -1042
- package/dist/diagnostics/loader.mjs +151 -0
- package/dist/diagnostics/loader.mjs.map +1 -0
- package/dist/diagnostics/pipeline.mjs +161 -0
- package/dist/diagnostics/pipeline.mjs.map +1 -0
- package/dist/diagnostics/tagElo.mjs +177 -0
- package/dist/diagnostics/tagElo.mjs.map +1 -0
- package/dist/diagnostics/text.mjs +174 -0
- package/dist/diagnostics/text.mjs.map +1 -0
- package/dist/factory.mjs +70 -0
- package/dist/factory.mjs.map +1 -0
- package/dist/impl/common/BaseUserDB.mjs +1103 -0
- package/dist/impl/common/BaseUserDB.mjs.map +1 -0
- package/dist/impl/common/userDBHelpers.mjs +49 -0
- package/dist/impl/common/userDBHelpers.mjs.map +1 -0
- package/dist/impl/common/userDBKeys.mjs +45 -0
- package/dist/impl/common/userDBKeys.mjs.map +1 -0
- package/dist/impl/couch/CouchDBSyncStrategy.mjs +272 -0
- package/dist/impl/couch/CouchDBSyncStrategy.mjs.map +1 -0
- package/dist/impl/couch/CourseSyncService.mjs +242 -0
- package/dist/impl/couch/CourseSyncService.mjs.map +1 -0
- package/dist/impl/couch/PouchDataLayerProvider.mjs +74 -0
- package/dist/impl/couch/PouchDataLayerProvider.mjs.map +1 -0
- package/dist/impl/couch/adminDB.mjs +61 -0
- package/dist/impl/couch/adminDB.mjs.map +1 -0
- package/dist/impl/couch/auth.mjs +33 -0
- package/dist/impl/couch/auth.mjs.map +1 -0
- package/dist/impl/couch/classroomDB.mjs +227 -0
- package/dist/impl/couch/classroomDB.mjs.map +1 -0
- package/dist/impl/couch/clientCache.mjs +15 -0
- package/dist/impl/couch/clientCache.mjs.map +1 -0
- package/dist/impl/couch/courseAPI.mjs +183 -0
- package/dist/impl/couch/courseAPI.mjs.map +1 -0
- package/dist/impl/couch/courseDB.mjs +729 -0
- package/dist/impl/couch/courseDB.mjs.map +1 -0
- package/dist/impl/couch/courseLookupDB.mjs +94 -0
- package/dist/impl/couch/courseLookupDB.mjs.map +1 -0
- package/dist/impl/couch/{index.js → index.cjs} +6911 -10367
- package/dist/impl/couch/index.cjs.map +1 -0
- package/dist/impl/couch/index.d.ts +12 -5
- package/dist/impl/couch/index.mjs +134 -12146
- package/dist/impl/couch/index.mjs.map +1 -1
- package/dist/impl/couch/pouchdb-setup.mjs +15 -0
- package/dist/impl/couch/pouchdb-setup.mjs.map +1 -0
- package/dist/impl/couch/updateQueue.mjs +101 -0
- package/dist/impl/couch/updateQueue.mjs.map +1 -0
- package/dist/impl/couch/user-course-relDB.mjs +55 -0
- package/dist/impl/couch/user-course-relDB.mjs.map +1 -0
- package/dist/impl/static/NoOpSyncStrategy.mjs +49 -0
- package/dist/impl/static/NoOpSyncStrategy.mjs.map +1 -0
- package/dist/impl/static/StaticDataLayerProvider.mjs +94 -0
- package/dist/impl/static/StaticDataLayerProvider.mjs.map +1 -0
- package/dist/impl/static/StaticDataUnpacker.mjs +311 -0
- package/dist/impl/static/StaticDataUnpacker.mjs.map +1 -0
- package/dist/impl/static/courseDB.mjs +368 -0
- package/dist/impl/static/courseDB.mjs.map +1 -0
- package/dist/impl/static/coursesDB.mjs +38 -0
- package/dist/impl/static/coursesDB.mjs.map +1 -0
- package/dist/impl/static/{index.js → index.cjs} +2461 -5992
- package/dist/impl/static/index.cjs.map +1 -0
- package/dist/impl/static/index.d.ts +4 -4
- package/dist/impl/static/index.mjs +6 -10741
- package/dist/{index-C745YB_6.d.ts → index-BLDpFYG3.d.ts} +1 -1
- package/dist/{index.js → index.cjs} +14070 -14239
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +12 -23
- package/dist/index.mjs +38 -16937
- package/dist/pouch/{index.js → index.cjs} +5 -1
- package/dist/pouch/index.cjs.map +1 -0
- package/dist/pouch/index.mjs +2 -19
- package/dist/study/ItemQueue.mjs +95 -0
- package/dist/study/ItemQueue.mjs.map +1 -0
- package/dist/study/MixerDebugger.mjs +303 -0
- package/dist/study/MixerDebugger.mjs.map +1 -0
- package/dist/study/SessionController.mjs +1187 -0
- package/dist/study/SessionController.mjs.map +1 -0
- package/dist/study/SessionDebugger.mjs +271 -0
- package/dist/study/SessionDebugger.mjs.map +1 -0
- package/dist/study/SessionOverlay.mjs +407 -0
- package/dist/study/SessionOverlay.mjs.map +1 -0
- package/dist/study/SourceMixer.mjs +45 -0
- package/dist/study/SourceMixer.mjs.map +1 -0
- package/dist/study/SpacedRepetition.mjs +108 -0
- package/dist/study/SpacedRepetition.mjs.map +1 -0
- package/dist/study/TagFilteredContentSource.mjs +136 -0
- package/dist/study/TagFilteredContentSource.mjs.map +1 -0
- package/dist/study/services/CardHydrationService.mjs +172 -0
- package/dist/study/services/CardHydrationService.mjs.map +1 -0
- package/dist/study/services/EloService.mjs +217 -0
- package/dist/study/services/EloService.mjs.map +1 -0
- package/dist/study/services/ResponseProcessor.mjs +190 -0
- package/dist/study/services/ResponseProcessor.mjs.map +1 -0
- package/dist/study/services/SrsService.mjs +49 -0
- package/dist/study/services/SrsService.mjs.map +1 -0
- package/dist/study/services/tagRecent.mjs +65 -0
- package/dist/study/services/tagRecent.mjs.map +1 -0
- package/dist/{types-DF0JuPbs.d.ts → types-DChCz22D.d.ts} +1 -1
- package/dist/{types-legacy-DdVjg81H.d.cts → types-legacy-fT5Y1Wuy.d.ts} +1 -1
- package/dist/userDBKeys-ne1g4Xuq.d.ts +16 -0
- package/dist/util/Loggable.mjs +14 -0
- package/dist/util/Loggable.mjs.map +1 -0
- package/dist/util/dataDirectory.mjs +47 -0
- package/dist/util/dataDirectory.mjs.map +1 -0
- package/dist/util/logger.mjs +86 -0
- package/dist/util/logger.mjs.map +1 -0
- package/dist/util/migrator/FileSystemAdapter.mjs +18 -0
- package/dist/util/migrator/FileSystemAdapter.mjs.map +1 -0
- package/dist/util/migrator/StaticToCouchDBMigrator.mjs +428 -0
- package/dist/util/migrator/StaticToCouchDBMigrator.mjs.map +1 -0
- package/dist/util/migrator/types.mjs +11 -0
- package/dist/util/migrator/types.mjs.map +1 -0
- package/dist/util/migrator/validation.mjs +334 -0
- package/dist/util/migrator/validation.mjs.map +1 -0
- package/dist/util/packer/CouchDBToStaticPacker.mjs +438 -0
- package/dist/util/packer/CouchDBToStaticPacker.mjs.map +1 -0
- package/dist/util/packer/{index.js → index.cjs} +73 -9
- package/dist/util/packer/index.cjs.map +1 -0
- package/dist/util/packer/index.d.ts +3 -3
- package/dist/util/packer/index.mjs +2 -537
- package/eslint.config.mjs +9 -1
- package/package.json +25 -13
- package/rolldown.config.mjs +44 -0
- package/src/core/UserDBDebugger.ts +47 -46
- package/src/core/navigators/Pipeline.ts +10 -10
- package/src/core/navigators/PipelineDebugger.ts +55 -55
- package/src/core/navigators/filters/eloDistance.ts +1 -1
- package/src/core/navigators/generators/elo.ts +5 -1
- package/src/core/navigators/index.ts +9 -3
- package/src/factory.ts +10 -1
- package/src/impl/common/BaseUserDB.ts +1 -3
- package/src/impl/common/index.ts +2 -0
- package/src/impl/common/userDBHelpers.ts +4 -58
- package/src/impl/common/userDBKeys.ts +64 -0
- package/src/impl/couch/courseDB.ts +15 -3
- package/src/impl/couch/index.ts +2 -18
- package/src/impl/couch/pouchdb-setup.ts +8 -0
- package/src/index.ts +1 -1
- package/src/study/MixerDebugger.ts +46 -46
- package/src/study/SessionDebugger.ts +20 -20
- package/src/study/SessionOverlay.ts +4 -4
- package/src/util/logger.ts +112 -13
- package/tests/core/navigators/eloGenerator.test.ts +36 -0
- package/tsup.config.ts +24 -21
- package/dist/SyncStrategy-BXliog1m.d.cts +0 -116
- package/dist/classroomDB-DLTtLZct.d.cts +0 -86
- package/dist/contentSource-FQZcZopC.d.cts +0 -2007
- package/dist/core/index.d.cts +0 -521
- package/dist/core/index.js.map +0 -1
- package/dist/core/index.mjs.map +0 -1
- package/dist/dataLayerProvider-3y21KtF-.d.cts +0 -61
- package/dist/diagnostics/index.d.cts +0 -561
- package/dist/diagnostics/index.mjs.map +0 -1
- package/dist/impl/couch/index.d.cts +0 -620
- package/dist/impl/couch/index.js.map +0 -1
- package/dist/impl/static/index.d.cts +0 -232
- package/dist/impl/static/index.js.map +0 -1
- package/dist/impl/static/index.mjs.map +0 -1
- package/dist/index-BIum9Ue2.d.cts +0 -137
- package/dist/index.d.cts +0 -1663
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/pouch/index.d.cts +0 -1
- package/dist/pouch/index.js.map +0 -1
- package/dist/pouch/index.mjs.map +0 -1
- package/dist/types-BcWVH4ww.d.cts +0 -58
- package/dist/types-legacy-DdVjg81H.d.ts +0 -168
- package/dist/util/packer/index.d.cts +0 -5
- package/dist/util/packer/index.js.map +0 -1
- package/dist/util/packer/index.mjs.map +0 -1
- package/src/study/getCardDataShape.ts +0 -34
- /package/dist/diagnostics/{index.js.map → index.cjs.map} +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
|
|
3
|
+
declare const REVIEW_TIME_FORMAT: string;
|
|
4
|
+
/**
|
|
5
|
+
* Build a ScheduledCard `_id` for the given review time.
|
|
6
|
+
*
|
|
7
|
+
* The id suffix is not a mere uniquifier: `getReviewstoDate` PARSES the id
|
|
8
|
+
* (with {@link REVIEW_TIME_FORMAT}) to decide due-ness, so the id is the
|
|
9
|
+
* datum. Anything that writes or rewrites scheduled-card docs (scheduling,
|
|
10
|
+
* user-state snapshot rebasing) must construct ids through this helper —
|
|
11
|
+
* a hand-rolled formatter will get moment's `kk` token (01-24 hours) wrong.
|
|
12
|
+
*/
|
|
13
|
+
declare function makeScheduledCardId(reviewTime: string | Date | moment.Moment): string;
|
|
14
|
+
declare function hexEncode(str: string): string;
|
|
15
|
+
|
|
16
|
+
export { REVIEW_TIME_FORMAT as R, hexEncode as h, makeScheduledCardId as m };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
//#region src/util/Loggable.ts
|
|
3
|
+
var Loggable = class {
|
|
4
|
+
log(...args) {
|
|
5
|
+
console.log(`LOG-${this._className}@${/* @__PURE__ */ new Date()}:`, ...args);
|
|
6
|
+
}
|
|
7
|
+
error(...args) {
|
|
8
|
+
console.error(`ERROR-${this._className}@${/* @__PURE__ */ new Date()}:`, ...args);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { Loggable };
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=Loggable.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Loggable.mjs","names":[],"sources":["../../src/util/Loggable.ts"],"sourcesContent":["export abstract class Loggable {\n protected abstract readonly _className: string;\n protected log(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.log(`LOG-${this._className}@${new Date()}:`, ...args);\n }\n protected error(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.error(`ERROR-${this._className}@${new Date()}:`, ...args);\n }\n}\n"],"mappings":";;AAAA,IAAsB,WAAtB,MAA+B;CAE7B,IAAc,GAAG,MAAuB;AAEtC,UAAQ,IAAI,OAAO,KAAK,WAAW,mBAAG,IAAI,MAAM,CAAC,IAAI,GAAG,KAAK;;CAE/D,MAAgB,GAAG,MAAuB;AAExC,UAAQ,MAAM,SAAS,KAAK,WAAW,mBAAG,IAAI,MAAM,CAAC,IAAI,GAAG,KAAK"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { logger } from "./logger.mjs";
|
|
3
|
+
import { ENV } from "../factory.mjs";
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import * as os from "os";
|
|
7
|
+
//#region src/util/dataDirectory.ts
|
|
8
|
+
/**
|
|
9
|
+
* Get the application data directory for the current platform
|
|
10
|
+
* Uses ~/.tuilder as requested by user for simplicity
|
|
11
|
+
*/
|
|
12
|
+
function getAppDataDirectory() {
|
|
13
|
+
if (ENV.LOCAL_STORAGE_PREFIX) return path.join(os.homedir(), `.tuilder`, ENV.LOCAL_STORAGE_PREFIX);
|
|
14
|
+
else return path.join(os.homedir(), ".tuilder");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Ensure the application data directory exists
|
|
18
|
+
* Creates directory recursively if it doesn't exist
|
|
19
|
+
*/
|
|
20
|
+
async function ensureAppDataDirectory() {
|
|
21
|
+
const appDataDir = getAppDataDirectory();
|
|
22
|
+
try {
|
|
23
|
+
await fs.promises.mkdir(appDataDir, { recursive: true });
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (err.code !== "EEXIST") throw new Error(`Failed to create app data directory ${appDataDir}: ${err.message}`);
|
|
26
|
+
}
|
|
27
|
+
return appDataDir;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the full path for a PouchDB database file
|
|
31
|
+
* @param dbName - The database name (e.g., 'userdb-Colin')
|
|
32
|
+
*/
|
|
33
|
+
function getDbPath(dbName) {
|
|
34
|
+
return path.join(getAppDataDirectory(), dbName);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Initialize data directory for PouchDB usage
|
|
38
|
+
* Should be called once at application startup
|
|
39
|
+
*/
|
|
40
|
+
async function initializeDataDirectory() {
|
|
41
|
+
await ensureAppDataDirectory();
|
|
42
|
+
logger.info(`PouchDB data directory initialized: ${getAppDataDirectory()}`);
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { ensureAppDataDirectory, getAppDataDirectory, getDbPath, initializeDataDirectory };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=dataDirectory.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataDirectory.mjs","names":[],"sources":["../../src/util/dataDirectory.ts"],"sourcesContent":["// Cross-platform data directory utilities for PouchDB\n// Provides OS-appropriate application data directories\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { logger } from './logger';\nimport { ENV } from '@db/factory';\n\n/**\n * Get the application data directory for the current platform\n * Uses ~/.tuilder as requested by user for simplicity\n */\nexport function getAppDataDirectory(): string {\n if (ENV.LOCAL_STORAGE_PREFIX) {\n return path.join(os.homedir(), `.tuilder`, ENV.LOCAL_STORAGE_PREFIX);\n } else {\n return path.join(os.homedir(), '.tuilder');\n }\n}\n\n/**\n * Ensure the application data directory exists\n * Creates directory recursively if it doesn't exist\n */\nexport async function ensureAppDataDirectory(): Promise<string> {\n const appDataDir = getAppDataDirectory();\n \n try {\n await fs.promises.mkdir(appDataDir, { recursive: true });\n } catch (err: any) {\n if (err.code !== 'EEXIST') {\n throw new Error(`Failed to create app data directory ${appDataDir}: ${err.message}`);\n }\n }\n \n return appDataDir;\n}\n\n/**\n * Get the full path for a PouchDB database file\n * @param dbName - The database name (e.g., 'userdb-Colin')\n */\nexport function getDbPath(dbName: string): string {\n return path.join(getAppDataDirectory(), dbName);\n}\n\n/**\n * Initialize data directory for PouchDB usage\n * Should be called once at application startup\n */\nexport async function initializeDataDirectory(): Promise<void> {\n await ensureAppDataDirectory();\n \n // Log initialization\n logger.info(`PouchDB data directory initialized: ${getAppDataDirectory()}`);\n}"],"mappings":";;;;;;;;;;;AAaA,SAAgB,sBAA8B;AAC5C,KAAI,IAAI,qBACN,QAAO,KAAK,KAAK,GAAG,SAAS,EAAE,YAAY,IAAI,qBAAqB;KAEpE,QAAO,KAAK,KAAK,GAAG,SAAS,EAAE,WAAW;;;;;;AAQ9C,eAAsB,yBAA0C;CAC9D,MAAM,aAAa,qBAAqB;AAExC,KAAI;AACF,QAAM,GAAG,SAAS,MAAM,YAAY,EAAE,WAAW,MAAM,CAAC;UACjD,KAAU;AACjB,MAAI,IAAI,SAAS,SACf,OAAM,IAAI,MAAM,uCAAuC,WAAW,IAAI,IAAI,UAAU;;AAIxF,QAAO;;;;;;AAOT,SAAgB,UAAU,QAAwB;AAChD,QAAO,KAAK,KAAK,qBAAqB,EAAE,OAAO;;;;;;AAOjD,eAAsB,0BAAyC;AAC7D,OAAM,wBAAwB;AAG9B,QAAO,KAAK,uCAAuC,qBAAqB,GAAG"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import "../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
//#region src/util/logger.ts
|
|
3
|
+
const LEVELS = {
|
|
4
|
+
debug: 0,
|
|
5
|
+
info: 1,
|
|
6
|
+
warn: 2,
|
|
7
|
+
error: 3,
|
|
8
|
+
silent: 4
|
|
9
|
+
};
|
|
10
|
+
const STORAGE_KEY = "sk.logLevel";
|
|
11
|
+
const isBrowser = typeof window !== "undefined";
|
|
12
|
+
function isLevel(v) {
|
|
13
|
+
return typeof v === "string" && v in LEVELS;
|
|
14
|
+
}
|
|
15
|
+
function initialLevel() {
|
|
16
|
+
if (isBrowser) {
|
|
17
|
+
try {
|
|
18
|
+
const stored = window.localStorage?.getItem(STORAGE_KEY);
|
|
19
|
+
if (isLevel(stored)) return stored;
|
|
20
|
+
} catch {}
|
|
21
|
+
return "warn";
|
|
22
|
+
}
|
|
23
|
+
if (typeof process !== "undefined") {
|
|
24
|
+
const env = process.env.SK_LOG_LEVEL;
|
|
25
|
+
if (isLevel(env)) return env;
|
|
26
|
+
}
|
|
27
|
+
return "info";
|
|
28
|
+
}
|
|
29
|
+
const shared = globalThis.__skuilderDbLog ??= { level: initialLevel() };
|
|
30
|
+
function getLogLevel() {
|
|
31
|
+
return shared.level;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Set the active log level. In the browser this persists across reloads
|
|
35
|
+
* unless `persist` is false.
|
|
36
|
+
*/
|
|
37
|
+
function setLogLevel(level, persist = true) {
|
|
38
|
+
if (!isLevel(level)) {
|
|
39
|
+
console.warn(`[DB] Unknown log level '${level}'. Use one of: ${Object.keys(LEVELS).join(", ")}`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
shared.level = level;
|
|
43
|
+
if (isBrowser && persist) try {
|
|
44
|
+
window.localStorage?.setItem(STORAGE_KEY, level);
|
|
45
|
+
} catch {}
|
|
46
|
+
}
|
|
47
|
+
const logger = {
|
|
48
|
+
debug: (message, ...args) => {
|
|
49
|
+
if (LEVELS[shared.level] <= LEVELS.debug) console.debug(`[DB:DEBUG] ${message}`, ...args);
|
|
50
|
+
},
|
|
51
|
+
info: (message, ...args) => {
|
|
52
|
+
if (LEVELS[shared.level] <= LEVELS.info) console.info(`[DB:INFO] ${message}`, ...args);
|
|
53
|
+
},
|
|
54
|
+
warn: (message, ...args) => {
|
|
55
|
+
if (LEVELS[shared.level] <= LEVELS.warn) console.warn(`[DB:WARN] ${message}`, ...args);
|
|
56
|
+
},
|
|
57
|
+
error: (message, ...args) => {
|
|
58
|
+
if (LEVELS[shared.level] <= LEVELS.error) console.error(`[DB:ERROR] ${message}`, ...args);
|
|
59
|
+
},
|
|
60
|
+
log: (message, ...args) => {
|
|
61
|
+
if (LEVELS[shared.level] <= LEVELS.debug) console.log(`[DB:LOG] ${message}`, ...args);
|
|
62
|
+
},
|
|
63
|
+
print: (message, ...args) => {
|
|
64
|
+
console.log(message, ...args);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Mount the level control on window.skuilder.logLevel
|
|
69
|
+
*
|
|
70
|
+
* window.skuilder.logLevel() // current level
|
|
71
|
+
* window.skuilder.logLevel('info') // set + persist
|
|
72
|
+
*/
|
|
73
|
+
function mountLogLevelControl() {
|
|
74
|
+
if (!isBrowser) return;
|
|
75
|
+
const win = window;
|
|
76
|
+
win.skuilder = win.skuilder || {};
|
|
77
|
+
win.skuilder.logLevel = (level) => {
|
|
78
|
+
if (level !== void 0) setLogLevel(level);
|
|
79
|
+
return getLogLevel();
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
mountLogLevelControl();
|
|
83
|
+
//#endregion
|
|
84
|
+
export { logger };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=logger.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.mjs","names":[],"sources":["../../src/util/logger.ts"],"sourcesContent":["/**\n * Simple logging utility for @vue-skuilder/db package\n *\n * Level-gated logging with ESLint suppressions to resolve console statement\n * violations while maintaining logging functionality.\n *\n * Level resolution:\n * - Browser: `localStorage['sk.logLevel']`, else `warn`. Change it live (and\n * persistently) with `window.skuilder.logLevel('info')`.\n * - Node: `SK_LOG_LEVEL`, else `debug` when NODE_ENV=development, else `info`\n * (CLI tools report progress through `info`).\n */\n\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';\n\nconst LEVELS: Record<LogLevel, number> = { debug: 0, info: 1, warn: 2, error: 3, silent: 4 };\nconst STORAGE_KEY = 'sk.logLevel';\n\nconst isBrowser = typeof window !== 'undefined';\n\nfunction isLevel(v: unknown): v is LogLevel {\n return typeof v === 'string' && v in LEVELS;\n}\n\nfunction initialLevel(): LogLevel {\n if (isBrowser) {\n try {\n const stored = window.localStorage?.getItem(STORAGE_KEY);\n if (isLevel(stored)) return stored;\n } catch {\n // storage blocked (private mode, sandboxed iframe) - fall through\n }\n return 'warn';\n }\n if (typeof process !== 'undefined') {\n const env = process.env.SK_LOG_LEVEL;\n if (isLevel(env)) return env;\n if (process.env.NODE_ENV === 'development') return 'debug';\n }\n return 'info';\n}\n\n// tsup builds each db entry point without code splitting, so a consumer can\n// hold several copies of this module. Keep the level on a shared global so\n// `window.skuilder.logLevel(...)` reaches all of them.\nconst shared: { level: LogLevel } = ((globalThis as any).__skuilderDbLog ??= {\n level: initialLevel(),\n});\n\nexport function getLogLevel(): LogLevel {\n return shared.level;\n}\n\n/**\n * Set the active log level. In the browser this persists across reloads\n * unless `persist` is false.\n */\nexport function setLogLevel(level: LogLevel, persist = true): void {\n if (!isLevel(level)) {\n // eslint-disable-next-line no-console\n console.warn(`[DB] Unknown log level '${level}'. Use one of: ${Object.keys(LEVELS).join(', ')}`);\n return;\n }\n shared.level = level;\n if (isBrowser && persist) {\n try {\n window.localStorage?.setItem(STORAGE_KEY, level);\n } catch {\n // storage blocked - level still applies for this page load\n }\n }\n}\n\nexport const logger = {\n /**\n * Debug-level logging\n */\n debug: (message: string, ...args: any[]): void => {\n if (LEVELS[shared.level] <= LEVELS.debug) {\n // eslint-disable-next-line no-console\n console.debug(`[DB:DEBUG] ${message}`, ...args);\n }\n },\n\n /**\n * Info-level logging - general information\n */\n info: (message: string, ...args: any[]): void => {\n if (LEVELS[shared.level] <= LEVELS.info) {\n // eslint-disable-next-line no-console\n console.info(`[DB:INFO] ${message}`, ...args);\n }\n },\n\n /**\n * Warning-level logging - potential issues\n */\n warn: (message: string, ...args: any[]): void => {\n if (LEVELS[shared.level] <= LEVELS.warn) {\n // eslint-disable-next-line no-console\n console.warn(`[DB:WARN] ${message}`, ...args);\n }\n },\n\n /**\n * Error-level logging - serious problems\n */\n error: (message: string, ...args: any[]): void => {\n if (LEVELS[shared.level] <= LEVELS.error) {\n // eslint-disable-next-line no-console\n console.error(`[DB:ERROR] ${message}`, ...args);\n }\n },\n\n /**\n * Log function for backward compatibility with existing log() usage.\n * Debug level.\n */\n log: (message: string, ...args: any[]): void => {\n if (LEVELS[shared.level] <= LEVELS.debug) {\n // eslint-disable-next-line no-console\n console.log(`[DB:LOG] ${message}`, ...args);\n }\n },\n\n /**\n * Ungated output for interactive console tools (`window.skuilder.*`). The\n * user asked for it, so it prints regardless of level.\n */\n print: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.log(message, ...args);\n },\n};\n\n/**\n * Mount the level control on window.skuilder.logLevel\n *\n * window.skuilder.logLevel() // current level\n * window.skuilder.logLevel('info') // set + persist\n */\nexport function mountLogLevelControl(): void {\n if (!isBrowser) return;\n\n const win = window as any;\n win.skuilder = win.skuilder || {};\n win.skuilder.logLevel = (level?: LogLevel): LogLevel => {\n if (level !== undefined) setLogLevel(level);\n return getLogLevel();\n };\n}\n\nmountLogLevelControl();\n"],"mappings":";;AAeA,MAAM,SAAmC;CAAE,OAAO;CAAG,MAAM;CAAG,MAAM;CAAG,OAAO;CAAG,QAAQ;CAAG;AAC5F,MAAM,cAAc;AAEpB,MAAM,YAAY,OAAO,WAAW;AAEpC,SAAS,QAAQ,GAA2B;AAC1C,QAAO,OAAO,MAAM,YAAY,KAAK;;AAGvC,SAAS,eAAyB;AAChC,KAAI,WAAW;AACb,MAAI;GACF,MAAM,SAAS,OAAO,cAAc,QAAQ,YAAY;AACxD,OAAI,QAAQ,OAAO,CAAE,QAAO;UACtB;AAGR,SAAO;;AAET,KAAI,OAAO,YAAY,aAAa;EAClC,MAAM,MAAM,QAAQ,IAAI;AACxB,MAAI,QAAQ,IAAI,CAAE,QAAO;;AAG3B,QAAO;;AAMT,MAAM,SAA+B,WAAoB,oBAAoB,EAC3E,OAAO,cAAc,EACtB;AAED,SAAgB,cAAwB;AACtC,QAAO,OAAO;;;;;;AAOhB,SAAgB,YAAY,OAAiB,UAAU,MAAY;AACjE,KAAI,CAAC,QAAQ,MAAM,EAAE;AAEnB,UAAQ,KAAK,2BAA2B,MAAM,iBAAiB,OAAO,KAAK,OAAO,CAAC,KAAK,KAAK,GAAG;AAChG;;AAEF,QAAO,QAAQ;AACf,KAAI,aAAa,QACf,KAAI;AACF,SAAO,cAAc,QAAQ,aAAa,MAAM;SAC1C;;AAMZ,MAAa,SAAS;CAIpB,QAAQ,SAAiB,GAAG,SAAsB;AAChD,MAAI,OAAO,OAAO,UAAU,OAAO,MAEjC,SAAQ,MAAM,cAAc,WAAW,GAAG,KAAK;;CAOnD,OAAO,SAAiB,GAAG,SAAsB;AAC/C,MAAI,OAAO,OAAO,UAAU,OAAO,KAEjC,SAAQ,KAAK,aAAa,WAAW,GAAG,KAAK;;CAOjD,OAAO,SAAiB,GAAG,SAAsB;AAC/C,MAAI,OAAO,OAAO,UAAU,OAAO,KAEjC,SAAQ,KAAK,aAAa,WAAW,GAAG,KAAK;;CAOjD,QAAQ,SAAiB,GAAG,SAAsB;AAChD,MAAI,OAAO,OAAO,UAAU,OAAO,MAEjC,SAAQ,MAAM,cAAc,WAAW,GAAG,KAAK;;CAQnD,MAAM,SAAiB,GAAG,SAAsB;AAC9C,MAAI,OAAO,OAAO,UAAU,OAAO,MAEjC,SAAQ,IAAI,YAAY,WAAW,GAAG,KAAK;;CAQ/C,QAAQ,SAAiB,GAAG,SAAsB;AAEhD,UAAQ,IAAI,SAAS,GAAG,KAAK;;CAEhC;;;;;;;AAQD,SAAgB,uBAA6B;AAC3C,KAAI,CAAC,UAAW;CAEhB,MAAM,MAAM;AACZ,KAAI,WAAW,IAAI,YAAY,EAAE;AACjC,KAAI,SAAS,YAAY,UAA+B;AACtD,MAAI,UAAU,KAAA,EAAW,aAAY,MAAM;AAC3C,SAAO,aAAa;;;AAIxB,sBAAsB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "../../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
//#region src/util/migrator/FileSystemAdapter.ts
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when file system operations fail
|
|
5
|
+
*/
|
|
6
|
+
var FileSystemError = class extends Error {
|
|
7
|
+
constructor(message, operation, filePath, cause) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.operation = operation;
|
|
10
|
+
this.filePath = filePath;
|
|
11
|
+
this.cause = cause;
|
|
12
|
+
this.name = "FileSystemError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { FileSystemError };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=FileSystemAdapter.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileSystemAdapter.mjs","names":[],"sources":["../../../src/util/migrator/FileSystemAdapter.ts"],"sourcesContent":["// packages/db/src/util/migrator/FileSystemAdapter.ts\n\n/**\n * Abstraction for file system operations needed by the migrator.\n * This allows dependency injection of file system operations,\n * avoiding bundling issues with Node.js fs module.\n */\nexport interface FileSystemAdapter {\n /**\n * Read a text file and return its contents as a string\n */\n readFile(filePath: string): Promise<string>;\n\n /**\n * Read a binary file and return its contents as a Buffer\n */\n readBinary(filePath: string): Promise<Buffer>;\n\n /**\n * Check if a file or directory exists\n */\n exists(filePath: string): Promise<boolean>;\n\n /**\n * Get file/directory statistics\n */\n stat(filePath: string): Promise<FileStats>;\n\n /**\n * Write text data to a file\n */\n writeFile(filePath: string, data: string | Buffer): Promise<void>;\n\n /**\n * Write JSON data to a file with formatting\n */\n writeJson(filePath: string, data: any, options?: { spaces?: number }): Promise<void>;\n\n /**\n * Ensure a directory exists, creating it and parent directories if needed\n */\n ensureDir(dirPath: string): Promise<void>;\n\n /**\n * Join path segments into a complete path\n */\n joinPath(...segments: string[]): string;\n\n /**\n * Get the directory name of a path\n */\n dirname(filePath: string): string;\n\n /**\n * Check if a path is absolute\n */\n isAbsolute(filePath: string): boolean;\n}\n\nexport interface FileStats {\n isDirectory(): boolean;\n isFile(): boolean;\n size: number;\n}\n\n/**\n * Error thrown when file system operations fail\n */\nexport class FileSystemError extends Error {\n constructor(\n message: string,\n public readonly operation: string,\n public readonly filePath: string,\n public readonly cause?: Error\n ) {\n super(message);\n this.name = 'FileSystemError';\n }\n}"],"mappings":";;;;;AAoEA,IAAa,kBAAb,cAAqC,MAAM;CACzC,YACE,SACA,WACA,UACA,OACA;AACA,QAAM,QAAQ;AAJE,OAAA,YAAA;AACA,OAAA,WAAA;AACA,OAAA,QAAA;AAGhB,OAAK,OAAO"}
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import { __name } from "../../_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { logger } from "../logger.mjs";
|
|
3
|
+
import { DEFAULT_MIGRATION_OPTIONS } from "./types.mjs";
|
|
4
|
+
import { FileSystemError } from "./FileSystemAdapter.mjs";
|
|
5
|
+
import { validateMigration, validateStaticCourse } from "./validation.mjs";
|
|
6
|
+
//#region src/util/migrator/StaticToCouchDBMigrator.ts
|
|
7
|
+
let nodeFS = null;
|
|
8
|
+
let nodePath = null;
|
|
9
|
+
try {
|
|
10
|
+
if (typeof window === "undefined" && typeof process !== "undefined" && process.versions?.node) {
|
|
11
|
+
nodeFS = eval("require")("fs");
|
|
12
|
+
nodePath = eval("require")("path");
|
|
13
|
+
nodeFS.promises = nodeFS.promises || eval("require")("fs").promises;
|
|
14
|
+
}
|
|
15
|
+
} catch {}
|
|
16
|
+
var StaticToCouchDBMigrator = class {
|
|
17
|
+
options;
|
|
18
|
+
progressCallback;
|
|
19
|
+
fs;
|
|
20
|
+
constructor(options = {}, fileSystemAdapter) {
|
|
21
|
+
this.options = {
|
|
22
|
+
...DEFAULT_MIGRATION_OPTIONS,
|
|
23
|
+
...options
|
|
24
|
+
};
|
|
25
|
+
this.fs = fileSystemAdapter;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Set a progress callback to receive updates during migration
|
|
29
|
+
*/
|
|
30
|
+
setProgressCallback(callback) {
|
|
31
|
+
this.progressCallback = callback;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Migrate a static course to CouchDB
|
|
35
|
+
*/
|
|
36
|
+
async migrateCourse(staticPath, targetDB) {
|
|
37
|
+
const startTime = Date.now();
|
|
38
|
+
const result = {
|
|
39
|
+
success: false,
|
|
40
|
+
documentsRestored: 0,
|
|
41
|
+
attachmentsRestored: 0,
|
|
42
|
+
designDocsRestored: 0,
|
|
43
|
+
courseConfigRestored: 0,
|
|
44
|
+
errors: [],
|
|
45
|
+
warnings: [],
|
|
46
|
+
migrationTime: 0
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
logger.info(`Starting migration from ${staticPath} to CouchDB`);
|
|
50
|
+
this.reportProgress("manifest", 0, 1, "Validating static course...");
|
|
51
|
+
const validation = await validateStaticCourse(staticPath, this.fs);
|
|
52
|
+
if (!validation.valid) {
|
|
53
|
+
result.errors.push(...validation.errors);
|
|
54
|
+
throw new Error(`Static course validation failed: ${validation.errors.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
result.warnings.push(...validation.warnings);
|
|
57
|
+
this.reportProgress("manifest", 1, 1, "Loading course manifest...");
|
|
58
|
+
const manifest = await this.loadManifest(staticPath);
|
|
59
|
+
logger.info(`Loaded manifest for course: ${manifest.courseId} (${manifest.courseName})`);
|
|
60
|
+
this.reportProgress("design_docs", 0, manifest.designDocs.length, "Restoring design documents...");
|
|
61
|
+
const designDocResults = await this.restoreDesignDocuments(manifest.designDocs, targetDB);
|
|
62
|
+
result.designDocsRestored = designDocResults.restored;
|
|
63
|
+
result.errors.push(...designDocResults.errors);
|
|
64
|
+
result.warnings.push(...designDocResults.warnings);
|
|
65
|
+
this.reportProgress("course_config", 0, 1, "Restoring CourseConfig document...");
|
|
66
|
+
const courseConfigResults = await this.restoreCourseConfig(manifest, targetDB);
|
|
67
|
+
result.courseConfigRestored = courseConfigResults.restored;
|
|
68
|
+
result.errors.push(...courseConfigResults.errors);
|
|
69
|
+
result.warnings.push(...courseConfigResults.warnings);
|
|
70
|
+
this.reportProgress("course_config", 1, 1, "CourseConfig document restored");
|
|
71
|
+
const expectedCounts = this.calculateExpectedCounts(manifest);
|
|
72
|
+
this.reportProgress("documents", 0, manifest.documentCount, "Aggregating documents from chunks...");
|
|
73
|
+
const documents = await this.aggregateDocuments(staticPath, manifest);
|
|
74
|
+
const filteredDocuments = documents.filter((doc) => doc._id !== "CourseConfig");
|
|
75
|
+
if (documents.length !== filteredDocuments.length) result.warnings.push(`Filtered out ${documents.length - filteredDocuments.length} CourseConfig document(s) from chunks to prevent conflicts`);
|
|
76
|
+
this.reportProgress("documents", filteredDocuments.length, manifest.documentCount, "Uploading documents to CouchDB...");
|
|
77
|
+
const docResults = await this.uploadDocuments(filteredDocuments, targetDB);
|
|
78
|
+
result.documentsRestored = docResults.restored;
|
|
79
|
+
result.errors.push(...docResults.errors);
|
|
80
|
+
result.warnings.push(...docResults.warnings);
|
|
81
|
+
const docsWithAttachments = documents.filter((doc) => doc._attachments && Object.keys(doc._attachments).length > 0);
|
|
82
|
+
this.reportProgress("attachments", 0, docsWithAttachments.length, "Uploading attachments...");
|
|
83
|
+
const attachmentResults = await this.uploadAttachments(staticPath, docsWithAttachments, targetDB);
|
|
84
|
+
result.attachmentsRestored = attachmentResults.restored;
|
|
85
|
+
result.errors.push(...attachmentResults.errors);
|
|
86
|
+
result.warnings.push(...attachmentResults.warnings);
|
|
87
|
+
if (this.options.validateRoundTrip) {
|
|
88
|
+
this.reportProgress("validation", 0, 1, "Validating migration...");
|
|
89
|
+
const validationResult = await validateMigration(targetDB, expectedCounts, manifest);
|
|
90
|
+
if (!validationResult.valid) {
|
|
91
|
+
result.warnings.push("Migration validation found issues");
|
|
92
|
+
validationResult.issues.forEach((issue) => {
|
|
93
|
+
if (issue.type === "error") result.errors.push(`Validation: ${issue.message}`);
|
|
94
|
+
else result.warnings.push(`Validation: ${issue.message}`);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
this.reportProgress("validation", 1, 1, "Migration validation completed");
|
|
98
|
+
}
|
|
99
|
+
result.success = result.errors.length === 0;
|
|
100
|
+
result.migrationTime = Date.now() - startTime;
|
|
101
|
+
logger.info(`Migration completed in ${result.migrationTime}ms`);
|
|
102
|
+
logger.info(`Documents restored: ${result.documentsRestored}`);
|
|
103
|
+
logger.info(`Attachments restored: ${result.attachmentsRestored}`);
|
|
104
|
+
logger.info(`Design docs restored: ${result.designDocsRestored}`);
|
|
105
|
+
logger.info(`CourseConfig restored: ${result.courseConfigRestored}`);
|
|
106
|
+
if (result.errors.length > 0) logger.error(`Migration completed with ${result.errors.length} errors`);
|
|
107
|
+
if (result.warnings.length > 0) logger.warn(`Migration completed with ${result.warnings.length} warnings`);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
result.success = false;
|
|
110
|
+
result.migrationTime = Date.now() - startTime;
|
|
111
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
112
|
+
result.errors.push(`Migration failed: ${errorMessage}`);
|
|
113
|
+
logger.error("Migration failed:", error);
|
|
114
|
+
if (this.options.cleanupOnFailure) try {
|
|
115
|
+
await this.cleanupFailedMigration(targetDB);
|
|
116
|
+
} catch (cleanupError) {
|
|
117
|
+
logger.error("Failed to cleanup after migration failure:", cleanupError);
|
|
118
|
+
result.warnings.push("Failed to cleanup after migration failure");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Load and parse the manifest file
|
|
125
|
+
*/
|
|
126
|
+
async loadManifest(staticPath) {
|
|
127
|
+
try {
|
|
128
|
+
let manifestContent;
|
|
129
|
+
let manifestPath;
|
|
130
|
+
if (this.fs) {
|
|
131
|
+
manifestPath = this.fs.joinPath(staticPath, "manifest.json");
|
|
132
|
+
manifestContent = await this.fs.readFile(manifestPath);
|
|
133
|
+
} else {
|
|
134
|
+
manifestPath = nodeFS && nodePath ? nodePath.join(staticPath, "manifest.json") : `${staticPath}/manifest.json`;
|
|
135
|
+
if (nodeFS && this.isLocalPath(staticPath)) manifestContent = await nodeFS.promises.readFile(manifestPath, "utf8");
|
|
136
|
+
else {
|
|
137
|
+
const response = await fetch(manifestPath);
|
|
138
|
+
if (!response.ok) throw new Error(`Failed to fetch manifest: ${response.status} ${response.statusText}`);
|
|
139
|
+
manifestContent = await response.text();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const manifest = JSON.parse(manifestContent);
|
|
143
|
+
if (!manifest.version || !manifest.courseId || !manifest.chunks) throw new Error("Invalid manifest structure");
|
|
144
|
+
return manifest;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
const errorMessage = error instanceof FileSystemError ? error.message : `Failed to load manifest: ${error instanceof Error ? error.message : String(error)}`;
|
|
147
|
+
throw new Error(errorMessage);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Restore design documents to CouchDB
|
|
152
|
+
*/
|
|
153
|
+
async restoreDesignDocuments(designDocs, db) {
|
|
154
|
+
const result = {
|
|
155
|
+
restored: 0,
|
|
156
|
+
errors: [],
|
|
157
|
+
warnings: []
|
|
158
|
+
};
|
|
159
|
+
for (let i = 0; i < designDocs.length; i++) {
|
|
160
|
+
const designDoc = designDocs[i];
|
|
161
|
+
this.reportProgress("design_docs", i, designDocs.length, `Restoring ${designDoc._id}...`);
|
|
162
|
+
try {
|
|
163
|
+
let existingDoc;
|
|
164
|
+
try {
|
|
165
|
+
existingDoc = await db.get(designDoc._id);
|
|
166
|
+
} catch {}
|
|
167
|
+
const docToInsert = {
|
|
168
|
+
_id: designDoc._id,
|
|
169
|
+
views: designDoc.views
|
|
170
|
+
};
|
|
171
|
+
if (existingDoc) {
|
|
172
|
+
docToInsert._rev = existingDoc._rev;
|
|
173
|
+
logger.debug(`Updating existing design document: ${designDoc._id}`);
|
|
174
|
+
} else logger.debug(`Creating new design document: ${designDoc._id}`);
|
|
175
|
+
await db.put(docToInsert);
|
|
176
|
+
result.restored++;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
const errorMessage = `Failed to restore design document ${designDoc._id}: ${error instanceof Error ? error.message : String(error)}`;
|
|
179
|
+
result.errors.push(errorMessage);
|
|
180
|
+
logger.error(errorMessage);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
this.reportProgress("design_docs", designDocs.length, designDocs.length, `Restored ${result.restored} design documents`);
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Aggregate documents from all chunks
|
|
188
|
+
*/
|
|
189
|
+
async aggregateDocuments(staticPath, manifest) {
|
|
190
|
+
const allDocuments = [];
|
|
191
|
+
const documentMap = /* @__PURE__ */ new Map();
|
|
192
|
+
for (let i = 0; i < manifest.chunks.length; i++) {
|
|
193
|
+
const chunk = manifest.chunks[i];
|
|
194
|
+
this.reportProgress("documents", allDocuments.length, manifest.documentCount, `Loading chunk ${chunk.id}...`);
|
|
195
|
+
try {
|
|
196
|
+
const documents = await this.loadChunk(staticPath, chunk);
|
|
197
|
+
for (const doc of documents) {
|
|
198
|
+
if (!doc._id) {
|
|
199
|
+
logger.warn(`Document without _id found in chunk ${chunk.id}, skipping`);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (documentMap.has(doc._id)) logger.warn(`Duplicate document ID found: ${doc._id}, using latest version`);
|
|
203
|
+
documentMap.set(doc._id, doc);
|
|
204
|
+
}
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw new Error(`Failed to load chunk ${chunk.id}: ${error instanceof Error ? error.message : String(error)}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
allDocuments.push(...documentMap.values());
|
|
210
|
+
logger.info(`Aggregated ${allDocuments.length} unique documents from ${manifest.chunks.length} chunks`);
|
|
211
|
+
return allDocuments;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Load documents from a single chunk file
|
|
215
|
+
*/
|
|
216
|
+
async loadChunk(staticPath, chunk) {
|
|
217
|
+
try {
|
|
218
|
+
let chunkContent;
|
|
219
|
+
let chunkPath;
|
|
220
|
+
if (this.fs) {
|
|
221
|
+
chunkPath = this.fs.joinPath(staticPath, chunk.path);
|
|
222
|
+
chunkContent = await this.fs.readFile(chunkPath);
|
|
223
|
+
} else {
|
|
224
|
+
chunkPath = nodeFS && nodePath ? nodePath.join(staticPath, chunk.path) : `${staticPath}/${chunk.path}`;
|
|
225
|
+
if (nodeFS && this.isLocalPath(staticPath)) chunkContent = await nodeFS.promises.readFile(chunkPath, "utf8");
|
|
226
|
+
else {
|
|
227
|
+
const response = await fetch(chunkPath);
|
|
228
|
+
if (!response.ok) throw new Error(`Failed to fetch chunk: ${response.status} ${response.statusText}`);
|
|
229
|
+
chunkContent = await response.text();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const documents = JSON.parse(chunkContent);
|
|
233
|
+
if (!Array.isArray(documents)) throw new Error("Chunk file does not contain an array of documents");
|
|
234
|
+
return documents;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
const errorMessage = error instanceof FileSystemError ? error.message : `Failed to load chunk: ${error instanceof Error ? error.message : String(error)}`;
|
|
237
|
+
throw new Error(errorMessage);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Upload documents to CouchDB in batches
|
|
242
|
+
*/
|
|
243
|
+
async uploadDocuments(documents, db) {
|
|
244
|
+
const result = {
|
|
245
|
+
restored: 0,
|
|
246
|
+
errors: [],
|
|
247
|
+
warnings: []
|
|
248
|
+
};
|
|
249
|
+
const batchSize = this.options.chunkBatchSize;
|
|
250
|
+
for (let i = 0; i < documents.length; i += batchSize) {
|
|
251
|
+
const batch = documents.slice(i, i + batchSize);
|
|
252
|
+
this.reportProgress("documents", i, documents.length, `Uploading batch ${Math.floor(i / batchSize) + 1}...`);
|
|
253
|
+
try {
|
|
254
|
+
const docsToInsert = batch.map((doc) => {
|
|
255
|
+
const cleanDoc = { ...doc };
|
|
256
|
+
delete cleanDoc._rev;
|
|
257
|
+
delete cleanDoc._attachments;
|
|
258
|
+
return cleanDoc;
|
|
259
|
+
});
|
|
260
|
+
const bulkResult = await db.bulkDocs(docsToInsert);
|
|
261
|
+
for (let j = 0; j < bulkResult.length; j++) {
|
|
262
|
+
const docResult = bulkResult[j];
|
|
263
|
+
const originalDoc = batch[j];
|
|
264
|
+
if ("error" in docResult) {
|
|
265
|
+
const errorMessage = `Failed to upload document ${originalDoc._id}: ${docResult.error} - ${docResult.reason}`;
|
|
266
|
+
result.errors.push(errorMessage);
|
|
267
|
+
logger.error(errorMessage);
|
|
268
|
+
} else result.restored++;
|
|
269
|
+
}
|
|
270
|
+
} catch (error) {
|
|
271
|
+
let errorMessage;
|
|
272
|
+
if (error instanceof Error) errorMessage = `Failed to upload document batch starting at index ${i}: ${error.message}`;
|
|
273
|
+
else if (error && typeof error === "object" && "message" in error) errorMessage = `Failed to upload document batch starting at index ${i}: ${error.message}`;
|
|
274
|
+
else errorMessage = `Failed to upload document batch starting at index ${i}: ${JSON.stringify(error)}`;
|
|
275
|
+
result.errors.push(errorMessage);
|
|
276
|
+
logger.error(errorMessage);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
this.reportProgress("documents", documents.length, documents.length, `Uploaded ${result.restored} documents`);
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Upload attachments from filesystem to CouchDB
|
|
284
|
+
*/
|
|
285
|
+
async uploadAttachments(staticPath, documents, db) {
|
|
286
|
+
const result = {
|
|
287
|
+
restored: 0,
|
|
288
|
+
errors: [],
|
|
289
|
+
warnings: []
|
|
290
|
+
};
|
|
291
|
+
let processedDocs = 0;
|
|
292
|
+
for (const doc of documents) {
|
|
293
|
+
this.reportProgress("attachments", processedDocs, documents.length, `Processing attachments for ${doc._id}...`);
|
|
294
|
+
processedDocs++;
|
|
295
|
+
if (!doc._attachments) continue;
|
|
296
|
+
for (const [attachmentName, attachmentMeta] of Object.entries(doc._attachments)) try {
|
|
297
|
+
const uploadResult = await this.uploadSingleAttachment(staticPath, doc._id, attachmentName, attachmentMeta, db);
|
|
298
|
+
if (uploadResult.success) result.restored++;
|
|
299
|
+
else result.errors.push(uploadResult.error || "Unknown attachment upload error");
|
|
300
|
+
} catch (error) {
|
|
301
|
+
const errorMessage = `Failed to upload attachment ${doc._id}/${attachmentName}: ${error instanceof Error ? error.message : String(error)}`;
|
|
302
|
+
result.errors.push(errorMessage);
|
|
303
|
+
logger.error(errorMessage);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
this.reportProgress("attachments", documents.length, documents.length, `Uploaded ${result.restored} attachments`);
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Upload a single attachment file
|
|
311
|
+
*/
|
|
312
|
+
async uploadSingleAttachment(staticPath, docId, attachmentName, attachmentMeta, db) {
|
|
313
|
+
const result = {
|
|
314
|
+
success: false,
|
|
315
|
+
attachmentName,
|
|
316
|
+
docId
|
|
317
|
+
};
|
|
318
|
+
try {
|
|
319
|
+
if (!attachmentMeta.path) {
|
|
320
|
+
result.error = "Attachment metadata missing file path";
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
let attachmentData;
|
|
324
|
+
let attachmentPath;
|
|
325
|
+
if (this.fs) {
|
|
326
|
+
attachmentPath = this.fs.joinPath(staticPath, attachmentMeta.path);
|
|
327
|
+
attachmentData = await this.fs.readBinary(attachmentPath);
|
|
328
|
+
} else {
|
|
329
|
+
attachmentPath = nodeFS && nodePath ? nodePath.join(staticPath, attachmentMeta.path) : `${staticPath}/${attachmentMeta.path}`;
|
|
330
|
+
if (nodeFS && this.isLocalPath(staticPath)) attachmentData = await nodeFS.promises.readFile(attachmentPath);
|
|
331
|
+
else {
|
|
332
|
+
const response = await fetch(attachmentPath);
|
|
333
|
+
if (!response.ok) {
|
|
334
|
+
result.error = `Failed to fetch attachment: ${response.status} ${response.statusText}`;
|
|
335
|
+
return result;
|
|
336
|
+
}
|
|
337
|
+
attachmentData = await response.arrayBuffer();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const doc = await db.get(docId);
|
|
341
|
+
await db.putAttachment(docId, attachmentName, doc._rev, attachmentData, attachmentMeta.content_type);
|
|
342
|
+
result.success = true;
|
|
343
|
+
} catch (error) {
|
|
344
|
+
result.error = error instanceof Error ? error.message : String(error);
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Restore CourseConfig document from manifest
|
|
350
|
+
*/
|
|
351
|
+
async restoreCourseConfig(manifest, targetDB) {
|
|
352
|
+
const results = {
|
|
353
|
+
restored: 0,
|
|
354
|
+
errors: [],
|
|
355
|
+
warnings: []
|
|
356
|
+
};
|
|
357
|
+
try {
|
|
358
|
+
if (!manifest.courseConfig) {
|
|
359
|
+
results.warnings.push("No courseConfig found in manifest, skipping CourseConfig document creation");
|
|
360
|
+
return results;
|
|
361
|
+
}
|
|
362
|
+
const courseConfigDoc = {
|
|
363
|
+
_id: "CourseConfig",
|
|
364
|
+
...manifest.courseConfig,
|
|
365
|
+
courseID: manifest.courseId
|
|
366
|
+
};
|
|
367
|
+
delete courseConfigDoc._rev;
|
|
368
|
+
await targetDB.put(courseConfigDoc);
|
|
369
|
+
results.restored = 1;
|
|
370
|
+
logger.info(`CourseConfig document created for course: ${manifest.courseId}`);
|
|
371
|
+
} catch (error) {
|
|
372
|
+
const errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
|
|
373
|
+
results.errors.push(`Failed to restore CourseConfig: ${errorMessage}`);
|
|
374
|
+
logger.error("CourseConfig restoration failed:", error);
|
|
375
|
+
}
|
|
376
|
+
return results;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Calculate expected document counts from manifest
|
|
380
|
+
*/
|
|
381
|
+
calculateExpectedCounts(manifest) {
|
|
382
|
+
const counts = {};
|
|
383
|
+
for (const chunk of manifest.chunks) counts[chunk.docType] = (counts[chunk.docType] || 0) + chunk.documentCount;
|
|
384
|
+
if (manifest.designDocs.length > 0) counts["_design"] = manifest.designDocs.length;
|
|
385
|
+
return counts;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Clean up database after failed migration
|
|
389
|
+
*/
|
|
390
|
+
async cleanupFailedMigration(db) {
|
|
391
|
+
logger.info("Cleaning up failed migration...");
|
|
392
|
+
try {
|
|
393
|
+
const docsToDelete = (await db.allDocs()).rows.map((row) => ({
|
|
394
|
+
_id: row.id,
|
|
395
|
+
_rev: row.value.rev,
|
|
396
|
+
_deleted: true
|
|
397
|
+
}));
|
|
398
|
+
if (docsToDelete.length > 0) {
|
|
399
|
+
await db.bulkDocs(docsToDelete);
|
|
400
|
+
logger.info(`Cleaned up ${docsToDelete.length} documents from failed migration`);
|
|
401
|
+
}
|
|
402
|
+
} catch (error) {
|
|
403
|
+
logger.error("Failed to cleanup documents:", error);
|
|
404
|
+
throw error;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Report progress to callback if available
|
|
409
|
+
*/
|
|
410
|
+
reportProgress(phase, current, total, message) {
|
|
411
|
+
if (this.progressCallback) this.progressCallback({
|
|
412
|
+
phase,
|
|
413
|
+
current,
|
|
414
|
+
total,
|
|
415
|
+
message
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Check if a path is a local file path (vs URL)
|
|
420
|
+
*/
|
|
421
|
+
isLocalPath(path) {
|
|
422
|
+
return !path.startsWith("http://") && !path.startsWith("https://");
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
//#endregion
|
|
426
|
+
export { StaticToCouchDBMigrator };
|
|
427
|
+
|
|
428
|
+
//# sourceMappingURL=StaticToCouchDBMigrator.mjs.map
|