Changes all documents to have an explicit type
This commit is contained in:
46
pb_migrations/1751082417_updated_documents.js
Normal file
46
pb_migrations/1751082417_updated_documents.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
migrate((app) => {
|
||||
const collection = app.findCollectionByNameOrId("pbc_3332084752")
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
"indexes": [
|
||||
"CREATE INDEX `idx_gxNj5R3hxv` ON `documents` (`type`)"
|
||||
]
|
||||
}, collection)
|
||||
|
||||
// add field
|
||||
collection.fields.addAt(3, new Field({
|
||||
"hidden": false,
|
||||
"id": "select2363381545",
|
||||
"maxSelect": 1,
|
||||
"name": "type",
|
||||
"presentable": false,
|
||||
"required": false,
|
||||
"system": false,
|
||||
"type": "select",
|
||||
"values": [
|
||||
"location",
|
||||
"monster",
|
||||
"npc",
|
||||
"scene",
|
||||
"secret",
|
||||
"session",
|
||||
"treasure"
|
||||
]
|
||||
}))
|
||||
|
||||
return app.save(collection)
|
||||
}, (app) => {
|
||||
const collection = app.findCollectionByNameOrId("pbc_3332084752")
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
"indexes": []
|
||||
}, collection)
|
||||
|
||||
// remove field
|
||||
collection.fields.removeById("select2363381545")
|
||||
|
||||
return app.save(collection)
|
||||
})
|
||||
25
pb_migrations/1751082429_updated_documents.js
Normal file
25
pb_migrations/1751082429_updated_documents.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
migrate((app) => {
|
||||
const collection = app.findCollectionByNameOrId("pbc_3332084752")
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
"indexes": [
|
||||
"CREATE INDEX `idx_gxNj5R3hxv` ON `documents` (`type`)",
|
||||
"CREATE INDEX `idx_KtpMErDe1C` ON `documents` (`campaign`)"
|
||||
]
|
||||
}, collection)
|
||||
|
||||
return app.save(collection)
|
||||
}, (app) => {
|
||||
const collection = app.findCollectionByNameOrId("pbc_3332084752")
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
"indexes": [
|
||||
"CREATE INDEX `idx_gxNj5R3hxv` ON `documents` (`type`)"
|
||||
]
|
||||
}, collection)
|
||||
|
||||
return app.save(collection)
|
||||
})
|
||||
55
pb_migrations/1751082553_extract_document_types.js
Normal file
55
pb_migrations/1751082553_extract_document_types.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const DocType = [
|
||||
"location",
|
||||
"monster",
|
||||
"npc",
|
||||
"scene",
|
||||
"secret",
|
||||
"session",
|
||||
"treasure",
|
||||
];
|
||||
|
||||
function parseJsonB(data) {
|
||||
if (typeof data === "string") {
|
||||
return JSON.parse(data);
|
||||
} else if (data instanceof Array) {
|
||||
return JSON.parse(String.fromCharCode.apply(String, data));
|
||||
}
|
||||
throw new Error("Unsupported data type for JSON parsing");
|
||||
}
|
||||
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
migrate(
|
||||
(app) => {
|
||||
let documents = app.findAllRecords("documents");
|
||||
console.log("Records to parse: ", documents.length);
|
||||
|
||||
documents: for (const doc of documents) {
|
||||
if (!doc) continue;
|
||||
|
||||
let data = parseJsonB(doc.get("data"));
|
||||
|
||||
if (data[""]) {
|
||||
data = data[""];
|
||||
}
|
||||
for (const t of DocType) {
|
||||
if (data[t]) {
|
||||
doc.set("type", t);
|
||||
doc.set("data", data[t]);
|
||||
app.save(doc);
|
||||
continue documents;
|
||||
}
|
||||
}
|
||||
throw new Error(`Unrecognized data: ${JSON.stringify(data)}`);
|
||||
}
|
||||
},
|
||||
(app) => {
|
||||
// add down queries...
|
||||
let documents = app.findAllRecords("documents");
|
||||
|
||||
for (const doc of documents) {
|
||||
if (!doc) continue;
|
||||
doc.set("data", { [doc.get("type")]: doc.get("data") });
|
||||
app.save(doc);
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user