@tryghost/mg-context 0.1.27 → 0.3.0
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/README.md +574 -156
- package/build/index.d.ts +1 -0
- package/build/lib/AuthorContext.d.ts +3 -0
- package/build/lib/AuthorContext.js +40 -0
- package/build/lib/AuthorContext.js.map +1 -1
- package/build/lib/MigrateBase.d.ts +2 -0
- package/build/lib/MigrateBase.js +7 -0
- package/build/lib/MigrateBase.js.map +1 -1
- package/build/lib/MigrateContext.d.ts +74 -9
- package/build/lib/MigrateContext.js +504 -102
- package/build/lib/MigrateContext.js.map +1 -1
- package/build/lib/PostContext.d.ts +28 -5
- package/build/lib/PostContext.js +263 -19
- package/build/lib/PostContext.js.map +1 -1
- package/build/lib/TagContext.d.ts +3 -0
- package/build/lib/TagContext.js +40 -0
- package/build/lib/TagContext.js.map +1 -1
- package/build/lib/database.d.ts +63 -0
- package/build/lib/database.js +123 -0
- package/build/lib/database.js.map +1 -0
- package/build/lib/db-helpers.d.ts +25 -0
- package/build/lib/db-helpers.js +110 -0
- package/build/lib/db-helpers.js.map +1 -0
- package/build/test/migrate-context.test.js +1942 -107
- package/build/test/migrate-context.test.js.map +1 -1
- package/build/test/post-context.test.js +284 -4
- package/build/test/post-context.test.js.map +1 -1
- package/build/test/tasks.test.js +23 -5
- package/build/test/tasks.test.js.map +1 -1
- package/package.json +20 -13
|
@@ -1,167 +1,569 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { stat, mkdir, open, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { EventEmitter } from 'node:events';
|
|
4
|
+
import errors from '@tryghost/errors';
|
|
5
|
+
import mobiledocConverter from '@tryghost/html-to-mobiledoc';
|
|
6
|
+
import lexicalConverter from '@tryghost/kg-html-to-lexical';
|
|
2
7
|
import MigrateBase from './MigrateBase.js';
|
|
3
8
|
import PostContext from './PostContext.js';
|
|
9
|
+
import TagContext from './TagContext.js';
|
|
10
|
+
import AuthorContext from './AuthorContext.js';
|
|
11
|
+
import { createDatabase } from './database.js';
|
|
12
|
+
import { withTransaction, findByIds, findByColumn, buildDateWhere, buildFullWhere, countWhere, findPostsWhere, findPostIdColumnsWhere, findAllPostIdsWhere, findPostsByIds } from './db-helpers.js';
|
|
4
13
|
export default class MigrateContext extends MigrateBase {
|
|
5
|
-
#posts;
|
|
6
14
|
#contentFormat;
|
|
7
|
-
|
|
15
|
+
#dbPath;
|
|
16
|
+
#ephemeral;
|
|
17
|
+
#emitEvents;
|
|
18
|
+
#warnOnLookupKeyDuplicate;
|
|
19
|
+
#db = null;
|
|
20
|
+
#slugsDeduped = false;
|
|
21
|
+
#duplicateSlugs = [];
|
|
22
|
+
#emitter = new EventEmitter();
|
|
23
|
+
constructor({ contentFormat = 'lexical', dbPath, ephemeral, emitEvents = true, warnOnLookupKeyDuplicate = false } = {}) {
|
|
8
24
|
super();
|
|
9
|
-
this.#posts = [];
|
|
10
25
|
this.#contentFormat = contentFormat;
|
|
26
|
+
this.#emitEvents = emitEvents;
|
|
27
|
+
this.#warnOnLookupKeyDuplicate = warnOnLookupKeyDuplicate;
|
|
28
|
+
if (dbPath) {
|
|
29
|
+
this.#dbPath = dbPath;
|
|
30
|
+
this.#ephemeral = ephemeral ?? false;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this.#dbPath = ':memory:';
|
|
34
|
+
this.#ephemeral = ephemeral ?? true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
get db() {
|
|
38
|
+
if (!this.#db) {
|
|
39
|
+
throw new errors.InternalServerError({ message: 'Database not initialized. Call init() first.' });
|
|
40
|
+
}
|
|
41
|
+
return this.#db;
|
|
42
|
+
}
|
|
43
|
+
async init() {
|
|
44
|
+
if (this.#db) {
|
|
45
|
+
throw new errors.InternalServerError({ message: 'MigrateContext is already initialized. Call close() before reinitializing.' });
|
|
46
|
+
}
|
|
47
|
+
const storage = this.#ephemeral ? ':memory:' : this.#dbPath;
|
|
48
|
+
this.#db = createDatabase(storage);
|
|
49
|
+
}
|
|
50
|
+
async close() {
|
|
51
|
+
if (this.#db) {
|
|
52
|
+
this.#db.db.close();
|
|
53
|
+
this.#db = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// eslint-disable-next-line no-unused-vars
|
|
57
|
+
on(event, listener) {
|
|
58
|
+
this.#emitter.on(event, listener);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
// eslint-disable-next-line no-unused-vars
|
|
62
|
+
off(event, listener) {
|
|
63
|
+
this.#emitter.off(event, listener);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
get duplicateSlugs() {
|
|
67
|
+
return this.#duplicateSlugs;
|
|
68
|
+
}
|
|
69
|
+
async deduplicateSlugs() {
|
|
70
|
+
if (this.#slugsDeduped) {
|
|
71
|
+
return this.#duplicateSlugs;
|
|
72
|
+
}
|
|
73
|
+
this.#slugsDeduped = true;
|
|
74
|
+
// Slug deduplication happens at insert time in PostContext.save().
|
|
75
|
+
// Build grouped result from the collected renames: for each original slug
|
|
76
|
+
// that was renamed, include the retained post (oldest) first, then the renamed ones.
|
|
77
|
+
const renames = this.db.slugRenames;
|
|
78
|
+
if (renames.length === 0) {
|
|
79
|
+
this.#duplicateSlugs = [];
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
const slugOrder = [];
|
|
83
|
+
const groups = new Map();
|
|
84
|
+
for (const entry of renames) {
|
|
85
|
+
if (!groups.has(entry.oldSlug)) {
|
|
86
|
+
slugOrder.push(entry.oldSlug);
|
|
87
|
+
// Find the retained post (the one that kept the original slug)
|
|
88
|
+
const retainedRow = this.db.stmts.findPostsBySlug.all(entry.oldSlug);
|
|
89
|
+
let retainedUrl = '';
|
|
90
|
+
if (retainedRow.length > 0) {
|
|
91
|
+
const source = JSON.parse(retainedRow[0].source);
|
|
92
|
+
const data = JSON.parse(retainedRow[0].data);
|
|
93
|
+
retainedUrl = source.url || data.canonical_url || '';
|
|
94
|
+
}
|
|
95
|
+
groups.set(entry.oldSlug, [{
|
|
96
|
+
oldSlug: entry.oldSlug,
|
|
97
|
+
newSlug: entry.oldSlug,
|
|
98
|
+
url: retainedUrl
|
|
99
|
+
}]);
|
|
100
|
+
}
|
|
101
|
+
groups.get(entry.oldSlug).push(entry);
|
|
102
|
+
}
|
|
103
|
+
const result = [];
|
|
104
|
+
for (const slug of slugOrder) {
|
|
105
|
+
result.push(...groups.get(slug));
|
|
106
|
+
}
|
|
107
|
+
this.#duplicateSlugs = result;
|
|
108
|
+
return result;
|
|
11
109
|
}
|
|
12
|
-
async
|
|
13
|
-
|
|
14
|
-
|
|
110
|
+
async prepareForExport({ batchSize = 200 } = {}) {
|
|
111
|
+
await this.deduplicateSlugs();
|
|
112
|
+
const total = this.db.stmts.countPosts.get().count;
|
|
113
|
+
let processed = 0;
|
|
114
|
+
for (let offset = 0; offset < total; offset += batchSize) {
|
|
115
|
+
const rows = this.db.stmts.findPostsForConversion.all(batchSize, offset);
|
|
116
|
+
this.db.db.exec('BEGIN');
|
|
117
|
+
try {
|
|
118
|
+
for (const row of rows) {
|
|
119
|
+
const cf = row.content_format;
|
|
120
|
+
if (cf !== 'lexical' && cf !== 'mobiledoc') {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const data = JSON.parse(row.data);
|
|
124
|
+
if (cf === 'lexical' && !data.lexical && data.html) {
|
|
125
|
+
data.lexical = JSON.stringify(lexicalConverter.htmlToLexical(data.html));
|
|
126
|
+
this.db.stmts.updatePostData.run(JSON.stringify(data), row.id);
|
|
127
|
+
}
|
|
128
|
+
else if (cf === 'mobiledoc' && !data.mobiledoc && data.html) {
|
|
129
|
+
data.mobiledoc = JSON.stringify(mobiledocConverter.toMobiledoc(data.html));
|
|
130
|
+
this.db.stmts.updatePostData.run(JSON.stringify(data), row.id);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
this.db.db.exec('COMMIT');
|
|
134
|
+
/* c8 ignore next 4 */
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
this.db.db.exec('ROLLBACK');
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
processed += rows.length;
|
|
141
|
+
if (this.#emitEvents) {
|
|
142
|
+
this.#emitter.emit('progress', 'prepareForExport', processed, total);
|
|
143
|
+
}
|
|
144
|
+
// Yield to the event loop between batches so V8 can garbage-collect
|
|
145
|
+
// the intermediate DOM/lexical trees created by the converter
|
|
146
|
+
await new Promise((r) => {
|
|
147
|
+
setImmediate(r);
|
|
148
|
+
});
|
|
15
149
|
}
|
|
16
150
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
callback(
|
|
151
|
+
async transaction(callback) {
|
|
152
|
+
if (this.db.inTransaction) {
|
|
153
|
+
return await callback();
|
|
154
|
+
}
|
|
155
|
+
this.db.db.exec('BEGIN');
|
|
156
|
+
this.db.inTransaction = true;
|
|
157
|
+
try {
|
|
158
|
+
const result = await callback();
|
|
159
|
+
this.db.db.exec('COMMIT');
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
this.db.db.exec('ROLLBACK');
|
|
164
|
+
throw err;
|
|
165
|
+
/* c8 ignore next */ }
|
|
166
|
+
finally {
|
|
167
|
+
this.db.inTransaction = false;
|
|
20
168
|
}
|
|
21
169
|
}
|
|
22
|
-
|
|
170
|
+
#resolveFilterPostIds(filter) {
|
|
171
|
+
if (!filter || (!filter.tag && !filter.author)) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
let tagPostIds = null;
|
|
175
|
+
let authorPostIds = null;
|
|
176
|
+
if (filter.tag) {
|
|
177
|
+
let tags = [];
|
|
178
|
+
if (filter.tag.slug) {
|
|
179
|
+
tags = this.db.stmts.findTagsBySlug.all(filter.tag.slug);
|
|
180
|
+
}
|
|
181
|
+
else if (filter.tag.name) {
|
|
182
|
+
tags = this.db.stmts.findTagsByName.all(filter.tag.name);
|
|
183
|
+
}
|
|
184
|
+
tagPostIds = new Set();
|
|
185
|
+
for (const tag of tags) {
|
|
186
|
+
const postTags = this.db.stmts.findPostTagsByTagId.all(tag.id);
|
|
187
|
+
for (const pt of postTags) {
|
|
188
|
+
tagPostIds.add(pt.post_id);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (filter.author) {
|
|
193
|
+
let authors = [];
|
|
194
|
+
if (filter.author.slug) {
|
|
195
|
+
authors = this.db.stmts.findAuthorsBySlug.all(filter.author.slug);
|
|
196
|
+
}
|
|
197
|
+
else if (filter.author.name) {
|
|
198
|
+
authors = this.db.stmts.findAuthorsByName.all(filter.author.name);
|
|
199
|
+
}
|
|
200
|
+
else if (filter.author.email) {
|
|
201
|
+
authors = this.db.stmts.findAuthorsByEmail.all(filter.author.email);
|
|
202
|
+
}
|
|
203
|
+
authorPostIds = new Set();
|
|
204
|
+
for (const author of authors) {
|
|
205
|
+
const postAuthors = this.db.stmts.findPostAuthorsByAuthorId.all(author.id);
|
|
206
|
+
for (const pa of postAuthors) {
|
|
207
|
+
authorPostIds.add(pa.post_id);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
let ids;
|
|
212
|
+
if (tagPostIds && authorPostIds) {
|
|
213
|
+
ids = [...tagPostIds].filter(id => authorPostIds.has(id));
|
|
214
|
+
}
|
|
215
|
+
else if (tagPostIds) {
|
|
216
|
+
ids = [...tagPostIds];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
ids = [...authorPostIds];
|
|
220
|
+
}
|
|
221
|
+
return ids.sort((a, b) => a - b);
|
|
222
|
+
}
|
|
223
|
+
#buildFilterWhere(filter) {
|
|
224
|
+
const postIds = this.#resolveFilterPostIds(filter);
|
|
225
|
+
const dateWhere = buildDateWhere(filter);
|
|
226
|
+
return buildFullWhere(dateWhere, postIds);
|
|
227
|
+
}
|
|
228
|
+
// eslint-disable-next-line no-unused-vars
|
|
229
|
+
async forEachPost(callback, { batchSize = 100, filter } = {}) {
|
|
230
|
+
const where = this.#buildFilterWhere(filter);
|
|
231
|
+
// Snapshot matching IDs up front so mutations during iteration
|
|
232
|
+
// cannot shift pagination (e.g. changing a date or tag used in the filter).
|
|
233
|
+
const allIds = findAllPostIdsWhere(this.db, where);
|
|
234
|
+
const total = allIds.length;
|
|
235
|
+
let processed = 0;
|
|
236
|
+
for (let offset = 0; offset < total; offset += batchSize) {
|
|
237
|
+
const batchIds = allIds.slice(offset, offset + batchSize);
|
|
238
|
+
const rows = findPostsByIds(this.db, batchIds);
|
|
239
|
+
for (const row of rows) {
|
|
240
|
+
const post = PostContext.fromRow(row, this.db);
|
|
241
|
+
await callback(post);
|
|
242
|
+
withTransaction(this.db, () => {
|
|
243
|
+
post.save(this.db);
|
|
244
|
+
});
|
|
245
|
+
processed += 1;
|
|
246
|
+
}
|
|
247
|
+
if (this.#emitEvents) {
|
|
248
|
+
this.#emitter.emit('progress', 'forEachPost', processed, total);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async addPost(post) {
|
|
253
|
+
let newPost;
|
|
23
254
|
if (post && post instanceof PostContext) {
|
|
24
|
-
|
|
25
|
-
|
|
255
|
+
newPost = post;
|
|
256
|
+
newPost.warnOnLookupKeyDuplicate = this.#warnOnLookupKeyDuplicate;
|
|
257
|
+
newPost.save(this.db);
|
|
26
258
|
}
|
|
27
259
|
else if (post && typeof post === 'object') {
|
|
28
|
-
|
|
29
|
-
this.#
|
|
30
|
-
return emptyPost;
|
|
260
|
+
newPost = new PostContext({ ...post, contentFormat: this.#contentFormat });
|
|
261
|
+
newPost.warnOnLookupKeyDuplicate = this.#warnOnLookupKeyDuplicate;
|
|
31
262
|
}
|
|
32
263
|
else {
|
|
33
|
-
|
|
34
|
-
this.#
|
|
35
|
-
return emptyPost;
|
|
264
|
+
newPost = new PostContext({ contentFormat: this.#contentFormat });
|
|
265
|
+
newPost.warnOnLookupKeyDuplicate = this.#warnOnLookupKeyDuplicate;
|
|
36
266
|
}
|
|
267
|
+
return newPost;
|
|
37
268
|
}
|
|
38
|
-
|
|
269
|
+
async getAllPosts() {
|
|
270
|
+
const rows = this.db.stmts.findAllPostsOrdered.all();
|
|
271
|
+
return rows.map((row) => PostContext.fromRow(row, this.db));
|
|
272
|
+
}
|
|
273
|
+
async findPosts({ slug, title, sourceAttr, tagSlug, tagName, authorSlug, authorName, authorEmail } = {}) {
|
|
39
274
|
if (slug) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
275
|
+
const rows = this.db.stmts.findPostsBySlug.all(slug);
|
|
276
|
+
return rows.map((row) => PostContext.fromRow(row, this.db));
|
|
43
277
|
}
|
|
44
278
|
else if (title) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
279
|
+
const rows = this.db.stmts.findAllPostsOrdered.all();
|
|
280
|
+
const results = [];
|
|
281
|
+
for (const row of rows) {
|
|
282
|
+
const data = JSON.parse(row.data);
|
|
283
|
+
if (data.title === title) {
|
|
284
|
+
results.push(PostContext.fromRow(row, this.db));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return results;
|
|
48
288
|
}
|
|
49
289
|
else if (sourceAttr && sourceAttr.key && sourceAttr.value) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
290
|
+
const rows = this.db.stmts.findAllPostsOrdered.all();
|
|
291
|
+
const results = [];
|
|
292
|
+
for (const row of rows) {
|
|
293
|
+
const source = JSON.parse(row.source);
|
|
294
|
+
if (source[sourceAttr.key] === sourceAttr.value) {
|
|
295
|
+
results.push(PostContext.fromRow(row, this.db));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return results;
|
|
53
299
|
}
|
|
54
300
|
else if (tagSlug) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
301
|
+
const tag = this.db.stmts.findTagBySlug.get(tagSlug);
|
|
302
|
+
if (!tag) {
|
|
303
|
+
return [];
|
|
304
|
+
}
|
|
305
|
+
const postTags = this.db.stmts.findPostTagsByTagId.all(tag.id);
|
|
306
|
+
const results = [];
|
|
307
|
+
for (const pt of postTags) {
|
|
308
|
+
const postRow = this.db.stmts.findPostById.get(pt.post_id);
|
|
309
|
+
if (postRow) {
|
|
310
|
+
results.push(PostContext.fromRow(postRow, this.db));
|
|
59
311
|
}
|
|
60
|
-
}
|
|
61
|
-
return
|
|
312
|
+
}
|
|
313
|
+
return results;
|
|
62
314
|
}
|
|
63
315
|
else if (tagName) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
316
|
+
const tags = this.db.stmts.findTagsByName.all(tagName);
|
|
317
|
+
const postIds = new Set();
|
|
318
|
+
for (const t of tags) {
|
|
319
|
+
const postTags = this.db.stmts.findPostTagsByTagId.all(t.id);
|
|
320
|
+
for (const pt of postTags) {
|
|
321
|
+
postIds.add(pt.post_id);
|
|
68
322
|
}
|
|
69
|
-
}
|
|
70
|
-
|
|
323
|
+
}
|
|
324
|
+
const results = [];
|
|
325
|
+
for (const postId of postIds) {
|
|
326
|
+
const postRow = this.db.stmts.findPostById.get(postId);
|
|
327
|
+
if (postRow) {
|
|
328
|
+
results.push(PostContext.fromRow(postRow, this.db));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return results;
|
|
71
332
|
}
|
|
72
333
|
else if (authorSlug) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
334
|
+
const author = this.db.stmts.findAuthorBySlug.get(authorSlug);
|
|
335
|
+
if (!author) {
|
|
336
|
+
return [];
|
|
337
|
+
}
|
|
338
|
+
const postAuthors = this.db.stmts.findPostAuthorsByAuthorId.all(author.id);
|
|
339
|
+
const results = [];
|
|
340
|
+
for (const pa of postAuthors) {
|
|
341
|
+
const postRow = this.db.stmts.findPostById.get(pa.post_id);
|
|
342
|
+
if (postRow) {
|
|
343
|
+
results.push(PostContext.fromRow(postRow, this.db));
|
|
77
344
|
}
|
|
78
|
-
}
|
|
79
|
-
return
|
|
345
|
+
}
|
|
346
|
+
return results;
|
|
80
347
|
}
|
|
81
348
|
else if (authorName) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
349
|
+
const authors = this.db.stmts.findAuthorsByName.all(authorName);
|
|
350
|
+
const postIds = new Set();
|
|
351
|
+
for (const a of authors) {
|
|
352
|
+
const postAuthors = this.db.stmts.findPostAuthorsByAuthorId.all(a.id);
|
|
353
|
+
for (const pa of postAuthors) {
|
|
354
|
+
postIds.add(pa.post_id);
|
|
86
355
|
}
|
|
87
|
-
}
|
|
88
|
-
|
|
356
|
+
}
|
|
357
|
+
const results = [];
|
|
358
|
+
for (const postId of postIds) {
|
|
359
|
+
const postRow = this.db.stmts.findPostById.get(postId);
|
|
360
|
+
if (postRow) {
|
|
361
|
+
results.push(PostContext.fromRow(postRow, this.db));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return results;
|
|
89
365
|
}
|
|
90
366
|
else if (authorEmail) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
367
|
+
const author = this.db.stmts.findAuthorByEmail.get(authorEmail);
|
|
368
|
+
if (!author) {
|
|
369
|
+
return [];
|
|
370
|
+
}
|
|
371
|
+
const postAuthors = this.db.stmts.findPostAuthorsByAuthorId.all(author.id);
|
|
372
|
+
const results = [];
|
|
373
|
+
for (const pa of postAuthors) {
|
|
374
|
+
const postRow = this.db.stmts.findPostById.get(pa.post_id);
|
|
375
|
+
if (postRow) {
|
|
376
|
+
results.push(PostContext.fromRow(postRow, this.db));
|
|
95
377
|
}
|
|
96
|
-
}
|
|
97
|
-
return
|
|
378
|
+
}
|
|
379
|
+
return results;
|
|
98
380
|
}
|
|
99
381
|
else {
|
|
100
382
|
return null;
|
|
101
383
|
}
|
|
102
384
|
}
|
|
103
|
-
findTags({ slug, name } = {}) {
|
|
104
|
-
// Get all tags from all posts
|
|
105
|
-
let allTags = [];
|
|
106
|
-
this.#posts.forEach((post) => {
|
|
107
|
-
const postTags = post.get('tags');
|
|
108
|
-
postTags.forEach((tag) => {
|
|
109
|
-
allTags.push(tag);
|
|
110
|
-
});
|
|
111
|
-
});
|
|
385
|
+
async findTags({ slug, name } = {}) {
|
|
112
386
|
if (slug) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
});
|
|
387
|
+
const rows = this.db.stmts.findTagsBySlug.all(slug);
|
|
388
|
+
return rows.map((row) => TagContext.fromRow(row));
|
|
116
389
|
}
|
|
117
390
|
else if (name) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
});
|
|
391
|
+
const rows = this.db.stmts.findTagsByName.all(name);
|
|
392
|
+
return rows.map((row) => TagContext.fromRow(row));
|
|
121
393
|
}
|
|
122
394
|
else {
|
|
123
395
|
return null;
|
|
124
396
|
}
|
|
125
397
|
}
|
|
126
|
-
findAuthors({ slug, name, email } = {}) {
|
|
127
|
-
// Get all authors from all posts
|
|
128
|
-
let allAuthors = [];
|
|
129
|
-
this.#posts.forEach((post) => {
|
|
130
|
-
const postTags = post.get('authors');
|
|
131
|
-
postTags.forEach((author) => {
|
|
132
|
-
allAuthors.push(author);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
398
|
+
async findAuthors({ slug, name, email } = {}) {
|
|
135
399
|
if (slug) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
});
|
|
400
|
+
const rows = this.db.stmts.findAuthorsBySlug.all(slug);
|
|
401
|
+
return rows.map((row) => AuthorContext.fromRow(row));
|
|
139
402
|
}
|
|
140
403
|
else if (name) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
});
|
|
404
|
+
const rows = this.db.stmts.findAuthorsByName.all(name);
|
|
405
|
+
return rows.map((row) => AuthorContext.fromRow(row));
|
|
144
406
|
}
|
|
145
407
|
else if (email) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
});
|
|
408
|
+
const rows = this.db.stmts.findAuthorsByEmail.all(email);
|
|
409
|
+
return rows.map((row) => AuthorContext.fromRow(row));
|
|
149
410
|
}
|
|
150
411
|
else {
|
|
151
412
|
return null;
|
|
152
413
|
}
|
|
153
414
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
415
|
+
// eslint-disable-next-line no-unused-vars
|
|
416
|
+
async forEachGhostPost(callback, { batchSize = 100, filter } = {}) {
|
|
417
|
+
const where = this.#buildFilterWhere(filter);
|
|
418
|
+
const total = countWhere(this.db, where);
|
|
419
|
+
let processed = 0;
|
|
420
|
+
for (let offset = 0; offset < total; offset += batchSize) {
|
|
421
|
+
const rows = findPostsWhere(this.db, where, batchSize, offset);
|
|
422
|
+
for (const row of rows) {
|
|
423
|
+
const post = PostContext.fromRow(row, this.db);
|
|
424
|
+
const postFinal = post.getFinal;
|
|
425
|
+
const postData = { ...postFinal.data };
|
|
426
|
+
postData.tags = postData.tags.map((t) => t.data);
|
|
427
|
+
postData.authors = postData.authors.map((a) => a.data);
|
|
428
|
+
await callback(postData, post);
|
|
429
|
+
processed += 1;
|
|
430
|
+
}
|
|
431
|
+
if (this.#emitEvents) {
|
|
432
|
+
this.#emitter.emit('progress', 'forEachGhostPost', processed, total);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// eslint-disable-next-line no-unused-vars
|
|
437
|
+
async writeGhostJson(outputDir, { batchSize = 5000, filename: rawFilename = 'posts', filter, onWrite } = {}) {
|
|
438
|
+
const filename = rawFilename.replace(/\.json$/i, '');
|
|
439
|
+
await mkdir(outputDir, { recursive: true });
|
|
440
|
+
const where = this.#buildFilterWhere(filter);
|
|
441
|
+
const count = countWhere(this.db, where);
|
|
442
|
+
const totalBatches = Math.max(1, Math.ceil(count / batchSize));
|
|
443
|
+
const writtenFiles = [];
|
|
444
|
+
const subBatchSize = 200;
|
|
445
|
+
for (let batch = 0; batch < totalBatches; batch++) {
|
|
446
|
+
const batchOffset = batch * batchSize;
|
|
447
|
+
const batchLimit = Math.min(batchSize, count - batchOffset);
|
|
448
|
+
// Query post IDs and ghost IDs for this file batch
|
|
449
|
+
const postIdRows = findPostIdColumnsWhere(this.db, where, batchLimit, batchOffset);
|
|
450
|
+
const postIds = postIdRows.map((r) => r.id);
|
|
451
|
+
const ghostIdMap = new Map(postIdRows.map((r) => [r.id, r.ghost_id]));
|
|
452
|
+
// Load junction + entity data from DB (small — tags/authors are few)
|
|
453
|
+
const postTagRows = findByColumn(this.db, 'PostTags', 'post_id', postIds, 'post_id ASC, sort_order ASC');
|
|
454
|
+
const postAuthorRows = findByColumn(this.db, 'PostAuthors', 'post_id', postIds, 'post_id ASC, sort_order ASC');
|
|
455
|
+
const tagDbIds = [...new Set(postTagRows.map((r) => r.tag_id))];
|
|
456
|
+
const authorDbIds = [...new Set(postAuthorRows.map((r) => r.author_id))];
|
|
457
|
+
const tagRows = findByIds(this.db, 'Tags', tagDbIds);
|
|
458
|
+
const authorRows = findByIds(this.db, 'Authors', authorDbIds);
|
|
459
|
+
// Build small Ghost JSON arrays
|
|
460
|
+
const tagGhostIds = new Map();
|
|
461
|
+
const tags = tagRows.map((r) => {
|
|
462
|
+
const data = JSON.parse(r.data);
|
|
463
|
+
tagGhostIds.set(r.id, r.ghost_id);
|
|
464
|
+
return { id: r.ghost_id, ...data };
|
|
465
|
+
});
|
|
466
|
+
const authorGhostIds = new Map();
|
|
467
|
+
const users = authorRows.map((r) => {
|
|
468
|
+
const data = JSON.parse(r.data);
|
|
469
|
+
authorGhostIds.set(r.id, r.ghost_id);
|
|
470
|
+
return { id: r.ghost_id, ...data };
|
|
471
|
+
});
|
|
472
|
+
const postsTags = postTagRows.map((r) => ({
|
|
473
|
+
post_id: ghostIdMap.get(r.post_id),
|
|
474
|
+
tag_id: tagGhostIds.get(r.tag_id),
|
|
475
|
+
sort_order: r.sort_order
|
|
476
|
+
}));
|
|
477
|
+
const postsAuthors = postAuthorRows.map((r) => ({
|
|
478
|
+
post_id: ghostIdMap.get(r.post_id),
|
|
479
|
+
author_id: authorGhostIds.get(r.author_id),
|
|
480
|
+
sort_order: r.sort_order
|
|
481
|
+
}));
|
|
482
|
+
// Stream file: posts are loaded and written in sub-batches
|
|
483
|
+
const fileName = totalBatches === 1
|
|
484
|
+
? `${filename}.json`
|
|
485
|
+
: `${filename}-${batch + 1}.json`;
|
|
486
|
+
const outputPath = join(outputDir, fileName);
|
|
487
|
+
const fh = await open(outputPath, 'w');
|
|
488
|
+
const postsMeta = [];
|
|
489
|
+
let postCount = 0;
|
|
490
|
+
// Indent a JSON.stringify block so all lines after the first are at the given depth
|
|
491
|
+
const pretty = (obj, depth) => {
|
|
492
|
+
const pad = ' '.repeat(depth);
|
|
493
|
+
return JSON.stringify(obj, null, 2).replace(/\n/g, `\n${pad}`);
|
|
494
|
+
};
|
|
495
|
+
try {
|
|
496
|
+
await fh.write('{\n');
|
|
497
|
+
await fh.write(` "meta": ${pretty({ exported_on: Date.now(), version: '2.0.0' }, 2)},\n`);
|
|
498
|
+
await fh.write(' "data": {\n');
|
|
499
|
+
await fh.write(' "posts": [\n');
|
|
500
|
+
// Stream posts in sub-batches
|
|
501
|
+
for (let sub = 0; sub < batchLimit; sub += subBatchSize) {
|
|
502
|
+
const rows = findPostsWhere(this.db, where, Math.min(subBatchSize, batchLimit - sub), batchOffset + sub);
|
|
503
|
+
for (const row of rows) {
|
|
504
|
+
const { post, meta } = PostContext.toGhostPost(row);
|
|
505
|
+
if (postCount > 0) {
|
|
506
|
+
await fh.write(',\n');
|
|
507
|
+
}
|
|
508
|
+
await fh.write(` ${pretty(post, 6)}`);
|
|
509
|
+
if (meta) {
|
|
510
|
+
postsMeta.push(meta);
|
|
511
|
+
}
|
|
512
|
+
postCount += 1;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
await fh.write('\n ],\n');
|
|
516
|
+
await fh.write(` "users": ${pretty(users, 4)},\n`);
|
|
517
|
+
await fh.write(` "tags": ${pretty(tags, 4)},\n`);
|
|
518
|
+
await fh.write(` "posts_authors": ${pretty(postsAuthors, 4)},\n`);
|
|
519
|
+
await fh.write(` "posts_tags": ${pretty(postsTags, 4)},\n`);
|
|
520
|
+
await fh.write(` "posts_meta": ${pretty(postsMeta, 4)}\n`);
|
|
521
|
+
await fh.write(' }\n}\n');
|
|
522
|
+
}
|
|
523
|
+
finally {
|
|
524
|
+
await fh.close();
|
|
525
|
+
}
|
|
526
|
+
const fileStats = await stat(outputPath);
|
|
527
|
+
const written = {
|
|
528
|
+
path: resolve(outputPath),
|
|
529
|
+
name: fileName,
|
|
530
|
+
size: fileStats.size,
|
|
531
|
+
posts: postCount
|
|
532
|
+
};
|
|
533
|
+
writtenFiles.push(written);
|
|
534
|
+
if (onWrite) {
|
|
535
|
+
onWrite(written);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return writtenFiles;
|
|
539
|
+
}
|
|
540
|
+
async writeGhostTagsJson(outputDir, { filename = 'tags' } = {}) {
|
|
541
|
+
const name = filename.replace(/\.json$/i, '') + '.json';
|
|
542
|
+
await mkdir(outputDir, { recursive: true });
|
|
543
|
+
const rows = this.db.stmts.findUsedTags.all();
|
|
544
|
+
const tags = rows.map((r) => ({ id: r.ghost_id, ...JSON.parse(r.data) }));
|
|
545
|
+
const outputPath = join(outputDir, name);
|
|
546
|
+
const content = JSON.stringify({
|
|
547
|
+
meta: { exported_on: Date.now(), version: '2.0.0' },
|
|
548
|
+
data: { tags }
|
|
549
|
+
}, null, 2);
|
|
550
|
+
await writeFile(outputPath, content);
|
|
551
|
+
const fileStats = await stat(outputPath);
|
|
552
|
+
return { path: resolve(outputPath), name, size: fileStats.size, posts: 0 };
|
|
159
553
|
}
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
554
|
+
async writeGhostUsersJson(outputDir, { filename = 'users' } = {}) {
|
|
555
|
+
const name = filename.replace(/\.json$/i, '') + '.json';
|
|
556
|
+
await mkdir(outputDir, { recursive: true });
|
|
557
|
+
const rows = this.db.stmts.findUsedAuthors.all();
|
|
558
|
+
const users = rows.map((r) => ({ id: r.ghost_id, ...JSON.parse(r.data) }));
|
|
559
|
+
const outputPath = join(outputDir, name);
|
|
560
|
+
const content = JSON.stringify({
|
|
561
|
+
meta: { exported_on: Date.now(), version: '2.0.0' },
|
|
562
|
+
data: { users }
|
|
563
|
+
}, null, 2);
|
|
564
|
+
await writeFile(outputPath, content);
|
|
565
|
+
const fileStats = await stat(outputPath);
|
|
566
|
+
return { path: resolve(outputPath), name, size: fileStats.size, posts: 0 };
|
|
165
567
|
}
|
|
166
568
|
}
|
|
167
569
|
//# sourceMappingURL=MigrateContext.js.map
|