@openinc/parse-server-opendash 4.17.1 → 4.18.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/dist/features/changelog/index.d.ts +2 -0
- package/dist/features/changelog/index.js +7 -0
- package/dist/features/changelog/services/deriveSearchFields.d.ts +39 -0
- package/dist/features/changelog/services/deriveSearchFields.js +176 -0
- package/dist/features/changelog/services/ensureChangelogIndexes.d.ts +17 -0
- package/dist/features/changelog/services/ensureChangelogIndexes.js +78 -0
- package/dist/hooks/_ChangeLog.js +31 -11
- package/dist/scripts/backfill-changelog-search.d.ts +1 -0
- package/dist/scripts/backfill-changelog-search.js +112 -0
- package/dist/types/Changelog.d.ts +9 -0
- package/dist/types/Changelog.js +18 -0
- package/package.json +2 -1
- package/schema/Changelog.json +12 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureChangelogIndexes = exports.deriveSearchFields = void 0;
|
|
4
|
+
var deriveSearchFields_js_1 = require("./services/deriveSearchFields.js");
|
|
5
|
+
Object.defineProperty(exports, "deriveSearchFields", { enumerable: true, get: function () { return deriveSearchFields_js_1.deriveSearchFields; } });
|
|
6
|
+
var ensureChangelogIndexes_js_1 = require("./services/ensureChangelogIndexes.js");
|
|
7
|
+
Object.defineProperty(exports, "ensureChangelogIndexes", { enumerable: true, get: function () { return ensureChangelogIndexes_js_1.ensureChangelogIndexes; } });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The search fields of an `OD3_Changelog` entry, derived from its snapshots.
|
|
3
|
+
*
|
|
4
|
+
* The snapshots (`original` / `value`) are whole `toJSON()` dumps stored as
|
|
5
|
+
* Object fields — Parse can neither say which fields differ between them nor
|
|
6
|
+
* search text inside them. These three columns make an entry findable by what
|
|
7
|
+
* a person actually asks:
|
|
8
|
+
*
|
|
9
|
+
* - "who changed the status?" → `changedFields`
|
|
10
|
+
* - "what happened to equipment X / when was it set to *done*?" → `refs`
|
|
11
|
+
* - "what happened to *Pumpe 3* / ticket 4711?" → `searchText`
|
|
12
|
+
*/
|
|
13
|
+
export interface ChangelogSearchFields {
|
|
14
|
+
/**
|
|
15
|
+
* Top-level fields whose value differs between `original` and `value`. A
|
|
16
|
+
* create lists every field it set, a delete every field the object had.
|
|
17
|
+
*/
|
|
18
|
+
changedFields: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Every pointer in either snapshot, as `field:objectId` (arrays of pointers
|
|
21
|
+
* contribute one per element). Both snapshots, so an entry that moved a
|
|
22
|
+
* ticket from one machine to another belongs to both machines.
|
|
23
|
+
*/
|
|
24
|
+
refs: string[];
|
|
25
|
+
/**
|
|
26
|
+
* The text and number values of both snapshots, lower-cased and
|
|
27
|
+
* whitespace-normalised, for a case-insensitive substring search. One value
|
|
28
|
+
* per line, so a phrase cannot match across two fields.
|
|
29
|
+
*/
|
|
30
|
+
searchText: string;
|
|
31
|
+
}
|
|
32
|
+
type Snapshot = Record<string, unknown> | null | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Derive the search fields of a changelog entry from its snapshots. Pure and
|
|
35
|
+
* deterministic, so the beforeSave hook and the backfill produce the same
|
|
36
|
+
* result for the same entry.
|
|
37
|
+
*/
|
|
38
|
+
export declare function deriveSearchFields(original: Snapshot, value: Snapshot): ChangelogSearchFields;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deriveSearchFields = deriveSearchFields;
|
|
4
|
+
const fast_equals_1 = require("fast-equals");
|
|
5
|
+
/** Bookkeeping Parse adds to every object — never a change anyone made. */
|
|
6
|
+
const TECHNICAL_FIELDS = new Set(["objectId", "createdAt", "updatedAt", "ACL"]);
|
|
7
|
+
/** Credentials a `_User` snapshot can carry; they stay out of the search text. */
|
|
8
|
+
const SECRET_FIELDS = new Set(["sessionToken", "authData"]);
|
|
9
|
+
/**
|
|
10
|
+
* Upper bound for `searchText`. Values are joined shortest first, so a long
|
|
11
|
+
* description or an embedded JSON blob is what gets cut — never the name,
|
|
12
|
+
* number or title next to it.
|
|
13
|
+
*/
|
|
14
|
+
const MAX_SEARCH_TEXT_LENGTH = 10_000;
|
|
15
|
+
/** Nesting depth followed into JSON fields, for both comparison and text. */
|
|
16
|
+
const MAX_DEPTH = 5;
|
|
17
|
+
/**
|
|
18
|
+
* The objectId of a reference, in either shape it reaches us: wire JSON
|
|
19
|
+
* (`{__type: "Pointer" | "Object"}`, as `toJSON()` wrote it) or a decoded
|
|
20
|
+
* `Parse.Object` (the SDK decodes nested pointers when it reads an Object
|
|
21
|
+
* field). Duck-typed rather than `instanceof`, so a second copy of the SDK does
|
|
22
|
+
* not make every pointer invisible.
|
|
23
|
+
*/
|
|
24
|
+
function pointerId(value) {
|
|
25
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const v = value;
|
|
29
|
+
if (typeof v.toPointer === "function" && typeof v.id === "string") {
|
|
30
|
+
return v.id;
|
|
31
|
+
}
|
|
32
|
+
if ((v.__type === "Pointer" || v.__type === "Object") &&
|
|
33
|
+
typeof v.objectId === "string") {
|
|
34
|
+
return v.objectId;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
function isPlainObject(value) {
|
|
39
|
+
if (typeof value !== "object" || value === null)
|
|
40
|
+
return false;
|
|
41
|
+
const proto = Object.getPrototypeOf(value);
|
|
42
|
+
return proto === Object.prototype || proto === null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A value as it should be compared: references reduced to their identity. An
|
|
46
|
+
* included object's other fields are not part of this record, and two decoded
|
|
47
|
+
* `Parse.Object`s of the same pointer differ in internal counters.
|
|
48
|
+
*/
|
|
49
|
+
function comparable(value, depth = 0) {
|
|
50
|
+
const id = pointerId(value);
|
|
51
|
+
if (id !== undefined)
|
|
52
|
+
return { objectId: id };
|
|
53
|
+
if (depth >= MAX_DEPTH)
|
|
54
|
+
return value;
|
|
55
|
+
if (Array.isArray(value)) {
|
|
56
|
+
return value.map((item) => comparable(item, depth + 1));
|
|
57
|
+
}
|
|
58
|
+
if (isPlainObject(value)) {
|
|
59
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
60
|
+
key,
|
|
61
|
+
comparable(item, depth + 1),
|
|
62
|
+
]));
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
function isEmpty(value) {
|
|
67
|
+
return value === null || value === undefined;
|
|
68
|
+
}
|
|
69
|
+
function sameValue(before, after) {
|
|
70
|
+
// `null` and absent both mean "not set" — clearing a field that was never
|
|
71
|
+
// set is no change.
|
|
72
|
+
if (isEmpty(before) && isEmpty(after))
|
|
73
|
+
return true;
|
|
74
|
+
return (0, fast_equals_1.deepEqual)(comparable(before), comparable(after));
|
|
75
|
+
}
|
|
76
|
+
function changedFieldsOf(original, value) {
|
|
77
|
+
const keys = new Set([
|
|
78
|
+
...Object.keys(original ?? {}),
|
|
79
|
+
...Object.keys(value ?? {}),
|
|
80
|
+
]);
|
|
81
|
+
return [...keys]
|
|
82
|
+
.filter((key) => !TECHNICAL_FIELDS.has(key))
|
|
83
|
+
.filter((key) => !sameValue(original?.[key], value?.[key]))
|
|
84
|
+
.sort();
|
|
85
|
+
}
|
|
86
|
+
function refsOf(snapshots) {
|
|
87
|
+
const refs = new Set();
|
|
88
|
+
for (const snapshot of snapshots) {
|
|
89
|
+
for (const [key, value] of Object.entries(snapshot ?? {})) {
|
|
90
|
+
if (TECHNICAL_FIELDS.has(key))
|
|
91
|
+
continue;
|
|
92
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
93
|
+
const id = pointerId(item);
|
|
94
|
+
if (id !== undefined)
|
|
95
|
+
refs.add(`${key}:${id}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return [...refs].sort();
|
|
100
|
+
}
|
|
101
|
+
function normalizeText(text) {
|
|
102
|
+
return text.replace(/\s+/g, " ").trim().toLowerCase();
|
|
103
|
+
}
|
|
104
|
+
/** The name of a file, as wire JSON or as a decoded `Parse.File`. */
|
|
105
|
+
function fileNameOf(value) {
|
|
106
|
+
if (value.__type === "File" && typeof value.name === "string") {
|
|
107
|
+
return value.name;
|
|
108
|
+
}
|
|
109
|
+
if (typeof value.name === "function" && typeof value.url === "function") {
|
|
110
|
+
const name = value.name();
|
|
111
|
+
return typeof name === "string" ? name : undefined;
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
function collectText(value, out, depth) {
|
|
116
|
+
if (isEmpty(value) || depth > MAX_DEPTH)
|
|
117
|
+
return;
|
|
118
|
+
if (typeof value === "string") {
|
|
119
|
+
const text = normalizeText(value);
|
|
120
|
+
if (text)
|
|
121
|
+
out.add(text);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (typeof value === "number") {
|
|
125
|
+
if (Number.isFinite(value))
|
|
126
|
+
out.add(String(value));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
for (const item of value)
|
|
131
|
+
collectText(item, out, depth + 1);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (typeof value !== "object")
|
|
135
|
+
return;
|
|
136
|
+
const v = value;
|
|
137
|
+
const fileName = fileNameOf(v);
|
|
138
|
+
if (fileName) {
|
|
139
|
+
collectText(fileName, out, depth);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// References, dates, geo points, … carry ids and coordinates, not words. Their
|
|
143
|
+
// wire form is a plain object too, so it has to be caught by `__type` before
|
|
144
|
+
// the generic walk below would index `className` and `objectId`.
|
|
145
|
+
if (typeof v.__type === "string" || !isPlainObject(v))
|
|
146
|
+
return;
|
|
147
|
+
for (const item of Object.values(v))
|
|
148
|
+
collectText(item, out, depth + 1);
|
|
149
|
+
}
|
|
150
|
+
function searchTextOf(snapshots) {
|
|
151
|
+
const values = new Set();
|
|
152
|
+
for (const snapshot of snapshots) {
|
|
153
|
+
for (const [key, value] of Object.entries(snapshot ?? {})) {
|
|
154
|
+
if (TECHNICAL_FIELDS.has(key) || SECRET_FIELDS.has(key))
|
|
155
|
+
continue;
|
|
156
|
+
collectText(value, values, 1);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return [...values]
|
|
160
|
+
.sort((a, b) => a.length - b.length)
|
|
161
|
+
.join("\n")
|
|
162
|
+
.slice(0, MAX_SEARCH_TEXT_LENGTH);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Derive the search fields of a changelog entry from its snapshots. Pure and
|
|
166
|
+
* deterministic, so the beforeSave hook and the backfill produce the same
|
|
167
|
+
* result for the same entry.
|
|
168
|
+
*/
|
|
169
|
+
function deriveSearchFields(original, value) {
|
|
170
|
+
const snapshots = [original, value];
|
|
171
|
+
return {
|
|
172
|
+
changedFields: changedFieldsOf(original, value),
|
|
173
|
+
refs: refsOf(snapshots),
|
|
174
|
+
searchText: searchTextOf(snapshots),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create the changelog indexes if they are missing.
|
|
3
|
+
*
|
|
4
|
+
* Not through the class schema: the schema tool has no `indexes` support, and
|
|
5
|
+
* Parse's schema API hands index keys to Mongo untranslated — an index on
|
|
6
|
+
* `createdAt` would index a field that does not exist (Mongo stores it as
|
|
7
|
+
* `_created_at`, a pointer as `_p_<field>`). The adapter's `ensureIndex` does
|
|
8
|
+
* that translation.
|
|
9
|
+
*
|
|
10
|
+
* Idempotent: Mongo ignores a `createIndex` for an index that already exists
|
|
11
|
+
* with the same keys and name. Building them on a large collection takes a
|
|
12
|
+
* while, so the caller should not wait for it.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ensureChangelogIndexes(schema: {
|
|
15
|
+
className: string;
|
|
16
|
+
fields: Record<string, unknown>;
|
|
17
|
+
} | undefined): Promise<void>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ensureChangelogIndexes = ensureChangelogIndexes;
|
|
7
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const index_js_1 = require("../../../types/index.js");
|
|
9
|
+
const LOG_PREFIX = "[@openinc/parse-server-opendash]";
|
|
10
|
+
/**
|
|
11
|
+
* Indexes of `OD3_Changelog`, by name. Every changelog query sorts newest
|
|
12
|
+
* first, so each filter is paired with `createdAt` — Mongo then walks the index
|
|
13
|
+
* backwards and stops at the page size instead of sorting every match in
|
|
14
|
+
* memory. `searchText` gets none: a substring regex cannot use one, it is
|
|
15
|
+
* evaluated on the rows the other filters (class, date range) narrowed down.
|
|
16
|
+
*/
|
|
17
|
+
const INDEXES = {
|
|
18
|
+
od_changelog_created: ["createdAt"],
|
|
19
|
+
od_changelog_class_created: ["nameOfClass", "createdAt"],
|
|
20
|
+
od_changelog_object_created: ["changedObject", "createdAt"],
|
|
21
|
+
od_changelog_user_created: ["actingUser", "createdAt"],
|
|
22
|
+
od_changelog_fields_created: ["changedFields", "createdAt"],
|
|
23
|
+
od_changelog_refs_created: ["refs", "createdAt"],
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The storage adapter of the running Parse Server, or `undefined` when it is
|
|
27
|
+
* out of reach — a second copy of `parse-server` (whose config cache is empty)
|
|
28
|
+
* or an adapter without `ensureIndex`.
|
|
29
|
+
*/
|
|
30
|
+
function storageAdapter() {
|
|
31
|
+
try {
|
|
32
|
+
const Config = require("parse-server/lib/Config");
|
|
33
|
+
const adapter = Config.get(node_1.default.applicationId)?.database?.adapter;
|
|
34
|
+
return typeof adapter?.ensureIndex === "function" ? adapter : undefined;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create the changelog indexes if they are missing.
|
|
42
|
+
*
|
|
43
|
+
* Not through the class schema: the schema tool has no `indexes` support, and
|
|
44
|
+
* Parse's schema API hands index keys to Mongo untranslated — an index on
|
|
45
|
+
* `createdAt` would index a field that does not exist (Mongo stores it as
|
|
46
|
+
* `_created_at`, a pointer as `_p_<field>`). The adapter's `ensureIndex` does
|
|
47
|
+
* that translation.
|
|
48
|
+
*
|
|
49
|
+
* Idempotent: Mongo ignores a `createIndex` for an index that already exists
|
|
50
|
+
* with the same keys and name. Building them on a large collection takes a
|
|
51
|
+
* while, so the caller should not wait for it.
|
|
52
|
+
*/
|
|
53
|
+
async function ensureChangelogIndexes(schema) {
|
|
54
|
+
const adapter = storageAdapter();
|
|
55
|
+
if (!adapter || !schema) {
|
|
56
|
+
console.warn(`${LOG_PREFIX} Cannot reach the storage adapter — ${index_js_1.Changelog.className} ` +
|
|
57
|
+
`indexes are not ensured. Create them by hand: ` +
|
|
58
|
+
Object.values(INDEXES)
|
|
59
|
+
.map((fields) => `{ ${fields.join(", ")} }`)
|
|
60
|
+
.join(", "));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const ensured = [];
|
|
64
|
+
for (const [name, fields] of Object.entries(INDEXES)) {
|
|
65
|
+
try {
|
|
66
|
+
// `ensureIndex` strips `_rperm`/`_wperm` from the schema's fields in
|
|
67
|
+
// place, hence the copy. Not sparse: Mongo would refuse a sparse index
|
|
68
|
+
// for a sort over all entries, the admin table's default view.
|
|
69
|
+
await adapter.ensureIndex(index_js_1.Changelog.className, { className: schema.className, fields: { ...schema.fields } }, fields, name, false, { sparse: false });
|
|
70
|
+
ensured.push(name);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.warn(`${LOG_PREFIX} Could not ensure index ${name} on ${index_js_1.Changelog.className}:`, error?.message ?? error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
console.log(`${LOG_PREFIX} ${index_js_1.Changelog.className} indexes ensured: ` +
|
|
77
|
+
(ensured.join(", ") || "none"));
|
|
78
|
+
}
|
package/dist/hooks/_ChangeLog.js
CHANGED
|
@@ -7,11 +7,12 @@ exports.init = init;
|
|
|
7
7
|
const node_1 = __importDefault(require("parse/node"));
|
|
8
8
|
const AMQPBusConnection_js_1 = require("../features/openware/services/AMQPBusConnection.js");
|
|
9
9
|
const index_js_1 = require("../features/schema/index.js");
|
|
10
|
-
const index_js_2 = require("../features/
|
|
11
|
-
const index_js_3 = require("../
|
|
10
|
+
const index_js_2 = require("../features/changelog/index.js");
|
|
11
|
+
const index_js_3 = require("../features/config/index.js");
|
|
12
|
+
const index_js_4 = require("../types/index.js");
|
|
12
13
|
async function init() {
|
|
13
14
|
const schema = await node_1.default.Schema.all();
|
|
14
|
-
const config =
|
|
15
|
+
const config = index_js_3.ConfigInstance.getInstance();
|
|
15
16
|
console.log(`ChangeLog publishing enabled: ${config.getBoolean("CHANGELOG_PUBLISH_ENABLE")}`);
|
|
16
17
|
const blacklistedClasses = [
|
|
17
18
|
"_Session",
|
|
@@ -22,9 +23,24 @@ async function init() {
|
|
|
22
23
|
.filter((c) => c.length > 0),
|
|
23
24
|
];
|
|
24
25
|
console.log(`ChangeLog blacklisted classes: ${blacklistedClasses.join(", ") || "None"}`);
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
// Index building on a large collection takes a while; the server does not
|
|
27
|
+
// have to wait for it.
|
|
28
|
+
void (0, index_js_2.ensureChangelogIndexes)(schema.find((s) => s.className === index_js_4.Changelog.className));
|
|
29
|
+
(0, index_js_1.beforeSaveHook)(index_js_4.Changelog.className, async (request) => {
|
|
30
|
+
const { object, user, original } = request;
|
|
27
31
|
await (0, index_js_1.defaultHandler)(request);
|
|
32
|
+
// Re-derived on every save, so they always match the snapshots and cannot
|
|
33
|
+
// be set by hand. See features/changelog/services/deriveSearchFields.ts.
|
|
34
|
+
const search = (0, index_js_2.deriveSearchFields)(object.get("original"), object.get("value"));
|
|
35
|
+
object.set("changedFields", search.changedFields);
|
|
36
|
+
object.set("refs", search.refs);
|
|
37
|
+
object.set("searchText", search.searchText);
|
|
38
|
+
// The ACL is settled when the entry is written. A later save — the search
|
|
39
|
+
// backfill, an admin edit — must not re-derive it: entries from before the
|
|
40
|
+
// mirroring below were locked to od-admin, and re-mirroring would quietly
|
|
41
|
+
// widen who can read them.
|
|
42
|
+
if (original)
|
|
43
|
+
return;
|
|
28
44
|
// Mirror the ACL of the changed object instead of locking the changelog to
|
|
29
45
|
// the od-admin role: whoever can see the underlying object can see its
|
|
30
46
|
// history. The object snapshot lives in `value` for create/update and in
|
|
@@ -62,8 +78,12 @@ async function init() {
|
|
|
62
78
|
console.log(`Establishing Connection to ${url} for ChangeLog publishing...`);
|
|
63
79
|
await connection.connect();
|
|
64
80
|
console.log(`Connected to AMQPBus at ${url} for ChangeLog publishing.`);
|
|
65
|
-
(0, index_js_1.afterSaveHook)(
|
|
66
|
-
const { object: changelog } = request;
|
|
81
|
+
(0, index_js_1.afterSaveHook)(index_js_4.Changelog.className, async (request) => {
|
|
82
|
+
const { object: changelog, original } = request;
|
|
83
|
+
// Only a new entry reports a change. Saving an existing one again (the
|
|
84
|
+
// search backfill) would otherwise replay the whole history on the bus.
|
|
85
|
+
if (original)
|
|
86
|
+
return;
|
|
67
87
|
const msg = JSON.stringify(changelog.toJSON());
|
|
68
88
|
const topic = `${params.topicPrefix && params.topicPrefix.length > 0
|
|
69
89
|
? params.topicPrefix + "."
|
|
@@ -78,8 +98,8 @@ async function init() {
|
|
|
78
98
|
}
|
|
79
99
|
schema.forEach((classSchema) => {
|
|
80
100
|
if (!classSchema.className ||
|
|
81
|
-
classSchema.className ===
|
|
82
|
-
classSchema.className ===
|
|
101
|
+
classSchema.className === index_js_4.Changelog.className ||
|
|
102
|
+
classSchema.className === index_js_4.Permission.className) {
|
|
83
103
|
console.log(`Skipping ChangeLog hook for class ${classSchema.className}`);
|
|
84
104
|
return;
|
|
85
105
|
}
|
|
@@ -103,7 +123,7 @@ async function init() {
|
|
|
103
123
|
console.log(`Skipping ChangeLog entry for class ${classSchema.className} and object ${object.id} due to ${context?.skipChangelog ? "context flag" : "blacklisted class"}.`);
|
|
104
124
|
return;
|
|
105
125
|
}
|
|
106
|
-
const changelog = new
|
|
126
|
+
const changelog = new index_js_4.Changelog();
|
|
107
127
|
changelog.set("nameOfClass", classSchema.className);
|
|
108
128
|
changelog.set("changedObject", object.id);
|
|
109
129
|
changelog.set("operation", "delete");
|
|
@@ -131,7 +151,7 @@ async function init() {
|
|
|
131
151
|
console.log(`Skipping ChangeLog entry for class ${classSchema.className} and object ${object.id} due to ${context.skipChangelog ? "context flag" : "blacklisted class"}.`);
|
|
132
152
|
return;
|
|
133
153
|
}
|
|
134
|
-
const changelog = new
|
|
154
|
+
const changelog = new index_js_4.Changelog();
|
|
135
155
|
changelog.set("nameOfClass", classSchema.className);
|
|
136
156
|
changelog.set("changedObject", object.id);
|
|
137
157
|
changelog.set("operation", original ? "update" : "create");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fast_equals_1 = require("fast-equals");
|
|
7
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const index_js_1 = require("../features/changelog/index.js");
|
|
9
|
+
const index_js_2 = require("../types/index.js");
|
|
10
|
+
/**
|
|
11
|
+
* One-off backfill for the search fields of `OD3_Changelog` (`changedFields`,
|
|
12
|
+
* `refs`, `searchText`).
|
|
13
|
+
*
|
|
14
|
+
* From here on `beforeSave(OD3_Changelog)` derives them for every new entry,
|
|
15
|
+
* but everything already in the database predates that — and so does every
|
|
16
|
+
* entry the v1 migration writes, because migration mode registers no
|
|
17
|
+
* beforeSave at all. Until this ran, those entries are invisible to the search
|
|
18
|
+
* and the field filter (`references` sources still find them client-side).
|
|
19
|
+
*
|
|
20
|
+
* PARSE_URL=… PARSE_APP_ID=… PARSE_MASTER_KEY=… \
|
|
21
|
+
* node dist/scripts/backfill-changelog-search.js [--dry-run]
|
|
22
|
+
*
|
|
23
|
+
* Safe to re-run: an entry is only saved when its stored fields differ from
|
|
24
|
+
* what its snapshots derive to — which also makes this the way to bring old
|
|
25
|
+
* entries up to date after the derivation changed. The save itself leaves the
|
|
26
|
+
* entry's ACL alone and publishes nothing; both only happen for new entries.
|
|
27
|
+
*/
|
|
28
|
+
const BATCH_SIZE = 500;
|
|
29
|
+
function requireEnv(name) {
|
|
30
|
+
const value = process.env[name];
|
|
31
|
+
if (!value) {
|
|
32
|
+
console.error(`Missing required environment variable ${name}.`);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Iterate the changelog in `(createdAt, objectId)` order, page by page. The
|
|
39
|
+
* objectId breaks ties: entries can share a timestamp (the migration writes
|
|
40
|
+
* reconstructed history with historical ones), and a cursor on `createdAt`
|
|
41
|
+
* alone would skip whatever shares the last timestamp of a page.
|
|
42
|
+
*/
|
|
43
|
+
async function* pages() {
|
|
44
|
+
let cursor;
|
|
45
|
+
for (;;) {
|
|
46
|
+
let query = new node_1.default.Query(index_js_2.Changelog);
|
|
47
|
+
if (cursor) {
|
|
48
|
+
query = node_1.default.Query.or(new node_1.default.Query(index_js_2.Changelog).greaterThan("createdAt", cursor.createdAt), new node_1.default.Query(index_js_2.Changelog)
|
|
49
|
+
.equalTo("createdAt", cursor.createdAt)
|
|
50
|
+
.greaterThan("objectId", cursor.id));
|
|
51
|
+
}
|
|
52
|
+
const results = await query
|
|
53
|
+
.ascending("createdAt")
|
|
54
|
+
.addAscending("objectId")
|
|
55
|
+
.limit(BATCH_SIZE)
|
|
56
|
+
.find({ useMasterKey: true });
|
|
57
|
+
if (results.length === 0)
|
|
58
|
+
return;
|
|
59
|
+
yield results;
|
|
60
|
+
// Fetched rows always carry both; the SDK types them optional for unsaved ones.
|
|
61
|
+
const last = results[results.length - 1];
|
|
62
|
+
cursor = { createdAt: last.createdAt, id: last.id };
|
|
63
|
+
if (results.length < BATCH_SIZE)
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function main() {
|
|
68
|
+
const dryRun = process.argv.includes("--dry-run");
|
|
69
|
+
node_1.default.initialize(requireEnv("PARSE_APP_ID"), undefined, requireEnv("PARSE_MASTER_KEY"));
|
|
70
|
+
node_1.default.serverURL =
|
|
71
|
+
process.env.PARSE_URL ?? "http://localhost:1337/parse";
|
|
72
|
+
let scanned = 0;
|
|
73
|
+
let updated = 0;
|
|
74
|
+
let failed = 0;
|
|
75
|
+
for await (const page of pages()) {
|
|
76
|
+
const stale = [];
|
|
77
|
+
for (const entry of page) {
|
|
78
|
+
scanned++;
|
|
79
|
+
const fields = (0, index_js_1.deriveSearchFields)(entry.original, entry.value);
|
|
80
|
+
const current = {
|
|
81
|
+
changedFields: entry.changedFields,
|
|
82
|
+
refs: entry.refs,
|
|
83
|
+
searchText: entry.searchText,
|
|
84
|
+
};
|
|
85
|
+
if ((0, fast_equals_1.deepEqual)(current, fields))
|
|
86
|
+
continue;
|
|
87
|
+
entry.changedFields = fields.changedFields;
|
|
88
|
+
entry.refs = fields.refs;
|
|
89
|
+
entry.searchText = fields.searchText;
|
|
90
|
+
stale.push(entry);
|
|
91
|
+
}
|
|
92
|
+
if (!dryRun && stale.length > 0) {
|
|
93
|
+
try {
|
|
94
|
+
await node_1.default.Object.saveAll(stale, { useMasterKey: true });
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
failed += stale.length;
|
|
98
|
+
console.error(`Saving ${stale.length} entries failed (${stale[0].id} … ${stale[stale.length - 1].id}):`, error?.message ?? error);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
updated += stale.length;
|
|
103
|
+
console.log(`${dryRun ? "[dry run] " : ""}${scanned} scanned, ${updated} ${dryRun ? "to update" : "updated"}`);
|
|
104
|
+
}
|
|
105
|
+
console.log(`${dryRun ? "[dry run] " : ""}done: ${scanned} scanned, ${updated} ${dryRun ? "to update" : "updated"}, ${failed} failed`);
|
|
106
|
+
if (failed > 0)
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
main().catch((error) => {
|
|
110
|
+
console.error(error);
|
|
111
|
+
process.exit(1);
|
|
112
|
+
});
|
|
@@ -6,12 +6,15 @@ export interface ChangelogAttributes {
|
|
|
6
6
|
createdAt: Date;
|
|
7
7
|
updatedAt: Date;
|
|
8
8
|
actingUser?: _User | undefined;
|
|
9
|
+
changedFields?: any[] | undefined;
|
|
9
10
|
changedObject: string;
|
|
10
11
|
context?: any | undefined;
|
|
11
12
|
masterkey: boolean;
|
|
12
13
|
nameOfClass: string;
|
|
13
14
|
operation: string;
|
|
14
15
|
original?: any | undefined;
|
|
16
|
+
refs?: any[] | undefined;
|
|
17
|
+
searchText?: string | undefined;
|
|
15
18
|
value?: any | undefined;
|
|
16
19
|
}
|
|
17
20
|
export declare class Changelog extends Parse.Object<ChangelogAttributes> {
|
|
@@ -19,6 +22,8 @@ export declare class Changelog extends Parse.Object<ChangelogAttributes> {
|
|
|
19
22
|
constructor(data?: Partial<ChangelogAttributes>);
|
|
20
23
|
get actingUser(): _User | undefined;
|
|
21
24
|
set actingUser(value: _User | undefined);
|
|
25
|
+
get changedFields(): any[] | undefined;
|
|
26
|
+
set changedFields(value: any[] | undefined);
|
|
22
27
|
get changedObject(): string;
|
|
23
28
|
set changedObject(value: string);
|
|
24
29
|
get context(): any | undefined;
|
|
@@ -31,6 +36,10 @@ export declare class Changelog extends Parse.Object<ChangelogAttributes> {
|
|
|
31
36
|
set operation(value: string);
|
|
32
37
|
get original(): any | undefined;
|
|
33
38
|
set original(value: any | undefined);
|
|
39
|
+
get refs(): any[] | undefined;
|
|
40
|
+
set refs(value: any[] | undefined);
|
|
41
|
+
get searchText(): string | undefined;
|
|
42
|
+
set searchText(value: string | undefined);
|
|
34
43
|
get value(): any | undefined;
|
|
35
44
|
set value(value: any | undefined);
|
|
36
45
|
}
|
package/dist/types/Changelog.js
CHANGED
|
@@ -16,6 +16,12 @@ class Changelog extends node_1.default.Object {
|
|
|
16
16
|
set actingUser(value) {
|
|
17
17
|
super.set("actingUser", value);
|
|
18
18
|
}
|
|
19
|
+
get changedFields() {
|
|
20
|
+
return super.get("changedFields");
|
|
21
|
+
}
|
|
22
|
+
set changedFields(value) {
|
|
23
|
+
super.set("changedFields", value);
|
|
24
|
+
}
|
|
19
25
|
get changedObject() {
|
|
20
26
|
return super.get("changedObject");
|
|
21
27
|
}
|
|
@@ -52,6 +58,18 @@ class Changelog extends node_1.default.Object {
|
|
|
52
58
|
set original(value) {
|
|
53
59
|
super.set("original", value);
|
|
54
60
|
}
|
|
61
|
+
get refs() {
|
|
62
|
+
return super.get("refs");
|
|
63
|
+
}
|
|
64
|
+
set refs(value) {
|
|
65
|
+
super.set("refs", value);
|
|
66
|
+
}
|
|
67
|
+
get searchText() {
|
|
68
|
+
return super.get("searchText");
|
|
69
|
+
}
|
|
70
|
+
set searchText(value) {
|
|
71
|
+
super.set("searchText", value);
|
|
72
|
+
}
|
|
55
73
|
get value() {
|
|
56
74
|
return super.get("value");
|
|
57
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openinc/parse-server-opendash",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.18.0",
|
|
4
4
|
"description": "Parse Server Cloud Code for open.INC Stack.",
|
|
5
5
|
"packageManager": "pnpm@12.8.1",
|
|
6
6
|
"keywords": [
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"migrate": "node dist/scripts/migration/run.js",
|
|
67
67
|
"migrate:verify": "node dist/scripts/migration/verify.js",
|
|
68
68
|
"backfill:meta-contexts": "node dist/scripts/backfill-meta-contexts.js",
|
|
69
|
+
"backfill:changelog-search": "node dist/scripts/backfill-changelog-search.js",
|
|
69
70
|
"seed:v1": "node dist/scripts/seed-v1-testdata.js"
|
|
70
71
|
},
|
|
71
72
|
"dependencies": {
|
package/schema/Changelog.json
CHANGED
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
"targetClass": "_User",
|
|
6
6
|
"required": false
|
|
7
7
|
},
|
|
8
|
+
"changedFields": {
|
|
9
|
+
"type": "Array",
|
|
10
|
+
"required": false
|
|
11
|
+
},
|
|
8
12
|
"changedObject": {
|
|
9
13
|
"type": "String",
|
|
10
14
|
"required": true
|
|
@@ -30,6 +34,14 @@
|
|
|
30
34
|
"type": "Object",
|
|
31
35
|
"required": false
|
|
32
36
|
},
|
|
37
|
+
"refs": {
|
|
38
|
+
"type": "Array",
|
|
39
|
+
"required": false
|
|
40
|
+
},
|
|
41
|
+
"searchText": {
|
|
42
|
+
"type": "String",
|
|
43
|
+
"required": false
|
|
44
|
+
},
|
|
33
45
|
"value": {
|
|
34
46
|
"type": "Object",
|
|
35
47
|
"required": false
|