Skip to content

Commit 5f2ce0a

Browse files
committed
fix(mockServer): filter nedb metadata in export-db-to-file
1 parent b9ce9d3 commit 5f2ce0a

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

mockServer/scripts/export-db-to-file.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const collectionNamingFields = {
1515
blockCategories: ['name']
1616
}
1717

18+
function isNedbMetadataRecord (doc) {
19+
if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
20+
return false
21+
}
22+
23+
return Object.keys(doc).some((key) => key.startsWith('$$'))
24+
}
25+
1826
function sanitizeFileName (name) {
1927
if (!name) {
2028
return ''
@@ -73,7 +81,7 @@ function resolveFileBaseName (doc, collectionName, usedNames) {
7381

7482
function parseDbFile (dbPath) {
7583
const content = fs.readFileSync(dbPath, 'utf8')
76-
return content
84+
const docs = content
7785
.split('\n')
7886
.map((line) => line.trim())
7987
.filter(Boolean)
@@ -84,6 +92,13 @@ function parseDbFile (dbPath) {
8492
throw new Error(`Failed to parse ${path.basename(dbPath)} line ${index + 1}: ${error.message}`)
8593
}
8694
})
95+
96+
const filteredDocs = docs.filter((doc) => !isNedbMetadataRecord(doc))
97+
98+
return {
99+
docs: filteredDocs,
100+
filtered: docs.length - filteredDocs.length
101+
}
87102
}
88103

89104
function ensureDirectory (dirPath) {
@@ -107,7 +122,7 @@ function exportCollection (dbFile) {
107122
const collectionName = path.basename(dbFile, '.db')
108123
const dbPath = path.join(sourceDir, dbFile)
109124
const collectionPath = path.join(outputDir, collectionName)
110-
const docs = parseDbFile(dbPath)
125+
const { docs, filtered } = parseDbFile(dbPath)
111126

112127
ensureDirectory(collectionPath)
113128
if (forceOverwrite) {
@@ -135,7 +150,13 @@ function exportCollection (dbFile) {
135150
written += 1
136151
}
137152

138-
return { collectionName, total: docs.length, written, skipped }
153+
return {
154+
collectionName,
155+
total: docs.length + filtered,
156+
filtered,
157+
written,
158+
skipped
159+
}
139160
}
140161

141162
function run () {
@@ -157,7 +178,7 @@ function run () {
157178
console.log(`Export complete. Output: ${outputDir}`)
158179
summary.forEach((item) => {
159180
console.log(
160-
`- ${item.collectionName}: total=${item.total}, written=${item.written}, skipped=${item.skipped}`
181+
`- ${item.collectionName}: total=${item.total}, filtered=${item.filtered}, written=${item.written}, skipped=${item.skipped}`
161182
)
162183
})
163184
}

0 commit comments

Comments
 (0)