@openpeepshq/core 0.1.32 → 0.1.34
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/accessTokens/helpers.d.ts +2 -2
- package/dist/accessTokens/tokens.js +14 -7
- package/dist/accounts/mutations.d.ts +2 -2
- package/dist/accounts/mutations.js +33 -35
- package/dist/analytics/cache.js +12 -7
- package/dist/analytics/compile.js +16 -10
- package/dist/analytics/pdfReport.d.ts +1 -0
- package/dist/analytics/pdfReport.js +5 -4
- package/dist/analytics/privacy.d.ts +18 -0
- package/dist/analytics/privacy.js +10 -0
- package/dist/analytics/read.js +75 -19
- package/dist/analytics/report.d.ts +21 -1
- package/dist/analytics/report.js +9 -2
- package/dist/backups/index.js +78 -15
- package/dist/backups/metadata.d.ts +8 -2
- package/dist/backups/metadata.js +6 -2
- package/dist/config/defaults/capabilities.js +13 -2
- package/dist/config/defaults/community.js +7 -9
- package/dist/config/defaults/core.d.ts +1 -1
- package/dist/config/defaults/core.js +3 -0
- package/dist/config/defaults/db.d.ts +0 -2
- package/dist/config/defaults/db.js +0 -2
- package/dist/config/index.d.ts +25 -3
- package/dist/config/index.js +7 -1
- package/dist/db/helpers.d.ts +7 -28
- package/dist/db/helpers.js +30 -152
- package/dist/db/index.d.ts +3 -1
- package/dist/db/index.js +5 -0
- package/dist/db/init.js +2 -2
- package/dist/db/migration/importCollections.d.ts +15 -10
- package/dist/db/migration/importCollections.js +121 -155
- package/dist/db/migration/shared.d.ts +2 -37
- package/dist/db/migration/shared.js +2 -59
- package/dist/db/pg/client.js +4 -0
- package/dist/db/pg/collections.d.ts +2 -2
- package/dist/db/pg/collections.js +2 -2
- package/dist/db/pg/filters/edges.d.ts +1 -1
- package/dist/db/pg/filters/edges.js +1 -1
- package/dist/db/pg/filters/posts.d.ts +11 -0
- package/dist/db/pg/filters/posts.js +37 -3
- package/dist/db/pg/filters/profiles.d.ts +1 -0
- package/dist/db/pg/filters/profiles.js +2 -1
- package/dist/db/pg/map/filters.js +37 -2
- package/dist/db/pg/map/mutations.js +13 -8
- package/dist/db/pg/map/registry.d.ts +5 -0
- package/dist/db/pg/map/registry.js +64 -24
- package/dist/db/pg/map/relations.d.ts +1 -1
- package/dist/db/pg/map/relations.js +98 -46
- package/dist/db/pg/migrate.d.ts +22 -0
- package/dist/db/pg/migrate.js +88 -2
- package/dist/db/pg/queries/activity.js +3 -3
- package/dist/db/pg/queries/sorts.d.ts +1 -0
- package/dist/db/pg/queries/sorts.js +4 -0
- package/dist/db/pg/schema/documents.d.ts +422 -41
- package/dist/db/pg/schema/documents.js +43 -9
- package/dist/db/pg/schema/edges.d.ts +117 -7
- package/dist/db/pg/schema/edges.js +4 -2
- package/dist/db/pg/schema/index.d.ts +505 -15
- package/dist/db/pg/sql/0007_shallow_oracle.sql +3 -0
- package/dist/db/pg/sql/0008_group_owner_roles.sql +31 -0
- package/dist/db/pg/sql/0009_event_occurrences.sql +26 -0
- package/dist/db/pg/sql/0010_user_group_membership_roles.sql +16 -0
- package/dist/db/pg/sql/0011_drop_data_migrations.sql +1 -0
- package/dist/db/pg/sql/0012_posts_last_activity_at.sql +5 -0
- package/dist/db/pg/sql/0013_add_blocks_edge.sql +12 -0
- package/dist/db/pg/sql/0014_federation_datamodel.sql +59 -0
- package/dist/db/pg/sql/0015_profile_hashtags.sql +12 -0
- package/dist/db/pg/sql/meta/{0006_snapshot.json → 0015_snapshot.json} +393 -39
- package/dist/db/pg/sql/meta/_journal.json +64 -1
- package/dist/db/replaceOrigin.js +6 -0
- package/dist/db/types.d.ts +0 -4
- package/dist/email/render.js +11 -3
- package/dist/events/index.d.ts +6 -1
- package/dist/events/index.js +55 -33
- package/dist/federation/activities.d.ts +17 -0
- package/dist/federation/activities.js +26 -0
- package/dist/federation/identity.d.ts +16 -0
- package/dist/federation/identity.js +52 -0
- package/dist/federation/index.d.ts +4 -0
- package/dist/federation/index.js +3 -0
- package/dist/federation/profiles.d.ts +3 -0
- package/dist/federation/profiles.js +14 -0
- package/dist/groups/finders.d.ts +3 -2
- package/dist/groups/helpers.d.ts +24 -6
- package/dist/groups/mapping.d.ts +6 -4
- package/dist/groups/mapping.js +2 -2
- package/dist/groups/mutations.js +1 -1
- package/dist/hashtags/finders.d.ts +6 -3
- package/dist/hashtags/finders.js +2 -0
- package/dist/hashtags/helpers.d.ts +8 -0
- package/dist/hashtags/helpers.js +4 -0
- package/dist/hashtags/index.d.ts +2 -0
- package/dist/hashtags/index.js +2 -0
- package/dist/hashtags/mutations.d.ts +3 -0
- package/dist/hashtags/mutations.js +13 -1
- package/dist/i18n/index.d.ts +1 -1
- package/dist/i18n/index.js +3 -1
- package/dist/i18n/pluginLocales.d.ts +6 -0
- package/dist/i18n/pluginLocales.js +15 -0
- package/dist/inviteLinks/helpers.d.ts +4 -4
- package/dist/jams/cache.d.ts +1 -1
- package/dist/jams/cache.js +2 -1
- package/dist/jams/export.d.ts +19 -0
- package/dist/jams/export.js +60 -0
- package/dist/jams/finders.d.ts +2 -1
- package/dist/jams/finders.js +18 -7
- package/dist/jams/health.d.ts +8 -0
- package/dist/jams/health.js +46 -0
- package/dist/jams/helpers.d.ts +6 -2
- package/dist/jams/index.d.ts +6 -3
- package/dist/jams/index.js +41 -27
- package/dist/jams/jobs.js +1 -1
- package/dist/jams/livekit.d.ts +93 -10
- package/dist/jams/livekit.js +151 -52
- package/dist/jams/mapping.d.ts +17 -7
- package/dist/jams/mutations.d.ts +3 -1
- package/dist/jams/recording.d.ts +10 -0
- package/dist/jams/recording.js +32 -0
- package/dist/jams/rtmp.d.ts +5 -0
- package/dist/jams/rtmp.js +8 -0
- package/dist/jams/token.d.ts +15 -3
- package/dist/jams/token.js +30 -16
- package/dist/jams/waitingRoom.d.ts +6 -6
- package/dist/jams/waitingRoom.js +24 -23
- package/dist/jobs/index.d.ts +1 -0
- package/dist/jobs/index.js +6 -1
- package/dist/jobs/jobDetail.js +1 -6
- package/dist/media/index.d.ts +1 -1
- package/dist/media/index.js +23 -8
- package/dist/media/processing.js +18 -7
- package/dist/notifications/defaultNotifications/follow/index.js +2 -2
- package/dist/notifications/defaultNotifications/groupMemberJoined/index.js +3 -1
- package/dist/notifications/defaultNotifications/groupMemberLeft/index.js +3 -1
- package/dist/notifications/defaultNotifications/index.js +4 -0
- package/dist/notifications/defaultNotifications/newGroupPost/index.js +12 -6
- package/dist/notifications/defaultNotifications/pollEnded/index.d.ts +19 -0
- package/dist/notifications/defaultNotifications/pollEnded/index.js +39 -0
- package/dist/notifications/defaultNotifications/pollEnded/recipients.d.ts +3 -0
- package/dist/notifications/defaultNotifications/pollEnded/recipients.js +18 -0
- package/dist/notifications/defaultNotifications/rsvp/index.d.ts +6 -1
- package/dist/notifications/defaultNotifications/rsvp/index.js +7 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.d.ts +24 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/index.js +75 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.d.ts +4 -0
- package/dist/notifications/defaultNotifications/rsvpCanceled/recipients.js +11 -0
- package/dist/notifications/helpers.js +6 -2
- package/dist/notifications/jobs.js +3 -1
- package/dist/notifications/mutations.js +4 -0
- package/dist/notifications/push.js +11 -4
- package/dist/plugins/helpers.d.ts +1 -0
- package/dist/plugins/helpers.js +1 -8
- package/dist/plugins/index.d.ts +5 -0
- package/dist/plugins/index.js +78 -5
- package/dist/plugins/install.d.ts +39 -0
- package/dist/plugins/install.js +455 -0
- package/dist/plugins/pluginAuth.d.ts +1 -1
- package/dist/plugins/pluginAuth.js +10 -6
- package/dist/plugins/state.d.ts +4 -0
- package/dist/plugins/state.js +29 -0
- package/dist/posts/activity.d.ts +9 -0
- package/dist/posts/activity.js +35 -0
- package/dist/posts/contextClosure.d.ts +1 -9
- package/dist/posts/contextClosure.js +15 -44
- package/dist/posts/conversationQueries.d.ts +7 -0
- package/dist/posts/conversationQueries.js +76 -2
- package/dist/posts/eventAgenda.d.ts +166 -0
- package/dist/posts/eventAgenda.js +126 -0
- package/dist/posts/eventOccurrenceJobs.d.ts +4 -0
- package/dist/posts/eventOccurrenceJobs.js +28 -0
- package/dist/posts/eventOccurrences.d.ts +10 -0
- package/dist/posts/eventOccurrences.js +61 -0
- package/dist/posts/finders.d.ts +47 -16
- package/dist/posts/finders.js +93 -62
- package/dist/posts/helpers/filters.js +14 -9
- package/dist/posts/helpers/index.d.ts +129 -30
- package/dist/posts/helpers/index.js +66 -33
- package/dist/posts/index.d.ts +7 -1
- package/dist/posts/index.js +7 -1
- package/dist/posts/mapping.d.ts +491 -13
- package/dist/posts/mapping.js +45 -10
- package/dist/posts/mutations.d.ts +441 -4
- package/dist/posts/mutations.js +177 -43
- package/dist/posts/pollEnd.d.ts +3 -0
- package/dist/posts/pollEnd.js +7 -0
- package/dist/posts/pollEndJobs.d.ts +8 -0
- package/dist/posts/pollEndJobs.js +49 -0
- package/dist/posts/replyClosure.d.ts +10 -0
- package/dist/posts/replyClosure.js +43 -0
- package/dist/posts/rsvpConfirmationEmail.js +14 -15
- package/dist/posts/threadPreview.d.ts +8 -0
- package/dist/posts/threadPreview.js +44 -0
- package/dist/posts/unseenCounts.d.ts +2 -0
- package/dist/posts/unseenCounts.js +28 -8
- package/dist/profileSettings/cache.js +22 -5
- package/dist/profileSettings/mapping.d.ts +2 -0
- package/dist/profiles/activityQueries.d.ts +5 -0
- package/dist/profiles/activityQueries.js +85 -0
- package/dist/profiles/activitySummary.d.ts +3 -0
- package/dist/profiles/activitySummary.js +87 -0
- package/dist/profiles/blocks.d.ts +12 -0
- package/dist/profiles/blocks.js +73 -0
- package/dist/profiles/cache.d.ts +4 -1
- package/dist/profiles/cache.js +28 -7
- package/dist/profiles/finders.d.ts +2 -1
- package/dist/profiles/finders.js +8 -3
- package/dist/profiles/helpers.d.ts +7 -5
- package/dist/profiles/index.d.ts +3 -1
- package/dist/profiles/index.js +3 -1
- package/dist/profiles/mapping.d.ts +10 -5
- package/dist/profiles/mapping.js +13 -1
- package/dist/profiles/mutations.d.ts +8 -4
- package/dist/profiles/mutations.js +42 -15
- package/dist/pushSubscriptions/helpers.d.ts +2 -2
- package/dist/pushSubscriptions/mutations.js +13 -1
- package/dist/redis/connection.d.ts +2 -0
- package/dist/redis/connection.js +25 -6
- package/dist/reports/helpers.d.ts +6 -6
- package/dist/roles/index.d.ts +1 -1
- package/dist/roles/index.js +1 -1
- package/dist/roles/mapping.d.ts +1 -0
- package/dist/roles/mutations.js +86 -3
- package/dist/search/mapping.d.ts +1 -1
- package/dist/search/mapping.js +6 -2
- package/dist/server/index.d.ts +19 -1
- package/dist/server/index.js +37 -18
- package/dist/server/stablePublicServerInfo.d.ts +17 -0
- package/dist/server/stablePublicServerInfo.js +37 -0
- package/dist/server/status.d.ts +25 -0
- package/dist/server/status.js +98 -0
- package/dist/stats/finders.d.ts +1 -16131
- package/dist/stats/finders.js +9 -5
- package/dist/stats/mapping.d.ts +21 -1
- package/dist/stats/mapping.js +0 -3
- package/package.json +6 -12
- package/dist/db/arango.d.ts +0 -2
- package/dist/db/arango.js +0 -110
- package/dist/db/migration/autoMigrate.d.ts +0 -2
- package/dist/db/migration/autoMigrate.js +0 -111
- package/dist/db/migration/cli.d.ts +0 -1
- package/dist/db/migration/cli.js +0 -56
- package/dist/db/migration/detect.d.ts +0 -6
- package/dist/db/migration/detect.js +0 -87
- package/dist/db/migration/exportArango.d.ts +0 -4
- package/dist/db/migration/exportArango.js +0 -80
- package/dist/db/migration/importPostgres.d.ts +0 -3
- package/dist/db/migration/importPostgres.js +0 -18
- package/dist/db/migration/transform.d.ts +0 -18
- package/dist/db/migration/transform.js +0 -209
- package/dist/db/migration/validate.d.ts +0 -18
- package/dist/db/migration/validate.js +0 -69
- package/dist/db/pg/sql/meta/0000_snapshot.json +0 -3380
- package/dist/db/pg/sql/meta/0004_snapshot.json +0 -4114
- package/dist/db/pg/sql/meta/0005_snapshot.json +0 -4179
- package/dist/mastodon/index.d.ts +0 -4
- package/dist/mastodon/index.js +0 -51
- package/dist/mastodon/types.d.ts +0 -7579
- package/dist/mastodon/types.js +0 -523
- package/dist/mastodon/typescript.d.ts +0 -1578
- package/dist/mastodon/typescript.js +0 -1
- package/dist/roles/defaults.d.ts +0 -2
- package/dist/roles/defaults.js +0 -94
|
@@ -18,7 +18,7 @@ export declare const executeCount: (db: PgDb, mapData: MapData<object, object>)
|
|
|
18
18
|
export declare const executeFirst: (db: PgDb, mapData: MapData<object, object>) => Promise<Doc | undefined>;
|
|
19
19
|
export declare const relationsFrom: (db: PgDb, start: {
|
|
20
20
|
id: string;
|
|
21
|
-
},
|
|
21
|
+
}, _parentCollection: string, relation: Relation & {
|
|
22
22
|
mapping: MapData<object, object>;
|
|
23
23
|
}) => Promise<Doc[]>;
|
|
24
24
|
export declare const evaluateDerived: (db: PgDb, doc: Doc, derived: DerivedProperty) => Promise<unknown>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { and, eq, getTableColumns, inArray, sql, } from 'drizzle-orm';
|
|
2
2
|
import { applyDateRangeToEdgeSql, applyLimit, applyPostFilters, applySqlLimit, applySort, partitionFilters, sortToSqlOrderBy, getEdgeTable, } from './filters.js';
|
|
3
3
|
import { isSqlFilter } from '../filters/index.js';
|
|
4
|
-
import { asTable, edgeRegistry, getTableForCollection, rowToDocument, } from './registry.js';
|
|
4
|
+
import { asTable, documentIdColumn, edgeRegistry, getTableForCollection, rowToDocument, } from './registry.js';
|
|
5
5
|
import { normalizeComputedDatetime } from '../mappers.js';
|
|
6
6
|
const buildComputedSelect = (mapData, tableRef) => {
|
|
7
7
|
var _a;
|
|
@@ -40,7 +40,9 @@ export const fetchRowsByIds = async (db, collection, ids, softDelete) => {
|
|
|
40
40
|
if (!ids.length)
|
|
41
41
|
return [];
|
|
42
42
|
const table = asTable(getTableForCollection(collection));
|
|
43
|
-
const conditions = [
|
|
43
|
+
const conditions = [
|
|
44
|
+
inArray(documentIdColumn(collection, table), ids),
|
|
45
|
+
];
|
|
44
46
|
if (softDelete !== false && table.deletedAt) {
|
|
45
47
|
conditions.push(sql `${table.deletedAt} IS NULL`);
|
|
46
48
|
}
|
|
@@ -61,7 +63,9 @@ export const fetchFilteredRowsByIds = async (db, mapData, ids) => {
|
|
|
61
63
|
const tableRef = getTableForCollection(mapData.collection);
|
|
62
64
|
const table = asTable(tableRef);
|
|
63
65
|
const { sqlWhere, postFilters } = partitionFilters(mapData.collection, table, mapData.filters, mapData.defaultFilter, mapData.softDelete);
|
|
64
|
-
const conditions = [
|
|
66
|
+
const conditions = [
|
|
67
|
+
inArray(documentIdColumn(mapData.collection, table), ids),
|
|
68
|
+
];
|
|
65
69
|
if (sqlWhere) {
|
|
66
70
|
conditions.push(sqlWhere);
|
|
67
71
|
}
|
|
@@ -130,8 +134,8 @@ const attachForeignKeyRelation = async (db, docs, fkr) => Promise.all(docs.map(a
|
|
|
130
134
|
const loaded = (await Promise.all(ids.map((id) => executeFind(db, fkr.mapping.collection, fkr.mapping, id)))).filter(Boolean);
|
|
131
135
|
return Object.assign(Object.assign({}, doc), { [fkr.alias]: fkr.cardinality === 'many' ? loaded : ((_b = loaded[0]) !== null && _b !== void 0 ? _b : undefined) });
|
|
132
136
|
}));
|
|
133
|
-
const attachRelation = async (db,
|
|
134
|
-
var _a;
|
|
137
|
+
const attachRelation = async (db, _parentCollection, docs, relation) => {
|
|
138
|
+
var _a, _b;
|
|
135
139
|
if (relation.maxDepth && relation.maxDepth > 1) {
|
|
136
140
|
return Promise.all(docs.map(async (doc) => (Object.assign(Object.assign({}, doc), { [relation.alias]: await traverseRelation(db, doc.id, relation) }))));
|
|
137
141
|
}
|
|
@@ -160,51 +164,96 @@ const attachRelation = async (db, parentCollection, docs, relation) => {
|
|
|
160
164
|
list.push(edge);
|
|
161
165
|
grouped.set(parentId, list);
|
|
162
166
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
return Object.assign(Object.assign({}, doc), { [relation.alias]: count });
|
|
167
|
+
const vertexCol = vertexEdgeColumn(relation);
|
|
168
|
+
const edgesFor = (doc) => { var _a; return (_a = grouped.get(doc.id)) !== null && _a !== void 0 ? _a : []; };
|
|
169
|
+
const edgeVertexIdsFor = (doc) => edgesFor(doc).map((edge) => edge[vertexCol]);
|
|
170
|
+
// Vertices are fetched and hydrated once for the whole batch. Doing it per
|
|
171
|
+
// parent row cost a query per row per relation, so one feed page ran
|
|
172
|
+
// hundreds of round trips.
|
|
173
|
+
const batchIdsFrom = (idsFor) => [
|
|
174
|
+
...new Set(docs.flatMap(idsFor)),
|
|
175
|
+
];
|
|
176
|
+
if (relation.count) {
|
|
177
|
+
if (!((_b = relation.mapping) === null || _b === void 0 ? void 0 : _b.collection)) {
|
|
178
|
+
return docs.map((doc) => (Object.assign(Object.assign({}, doc), { [relation.alias]: edgesFor(doc).length })));
|
|
176
179
|
}
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
+
const rows = await fetchRowsByIds(db, relation.mapping.collection, batchIdsFrom(edgeVertexIdsFor), relation.mapping.softDelete);
|
|
181
|
+
const counted = new Set(applyPostFilters(rows, relation.mapping.filters, relation.mapping.defaultFilter).map((row) => row.id));
|
|
182
|
+
return docs.map((doc) => (Object.assign(Object.assign({}, doc), { [relation.alias]: [...new Set(edgeVertexIdsFor(doc))].filter((id) => counted.has(id)).length })));
|
|
183
|
+
}
|
|
184
|
+
const vertexIdsFor = (doc) => capVertexIds(edgeVertexIdsFor(doc), relation);
|
|
185
|
+
const vertexCollection = vertexCollectionFor(relation);
|
|
186
|
+
const lookup = vertexCollection
|
|
187
|
+
? await fetchVertexLookup(db, vertexCollection, batchIdsFrom(vertexIdsFor), relation)
|
|
188
|
+
: emptyVertexLookup;
|
|
189
|
+
return docs.map((doc) => (Object.assign(Object.assign({}, doc), { [relation.alias]: relationValueFrom(edgesFor(doc), relation, vertexIdsFor(doc), lookup) })));
|
|
180
190
|
};
|
|
181
|
-
const
|
|
191
|
+
const nodeCapFromLimit = (limit) => {
|
|
192
|
+
if (limit == null)
|
|
193
|
+
return undefined;
|
|
194
|
+
return typeof limit === 'number' ? limit : limit[1];
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Cap before fetch when mapping.limit is set so hot inbound lists (e.g.
|
|
198
|
+
* embedded reposters) do not hydrate every vertex. uuidv7 ids are
|
|
199
|
+
* time-ordered, so newest-first approximates `createdAt DESC` before we have
|
|
200
|
+
* row data to sort on.
|
|
201
|
+
*/
|
|
202
|
+
const capVertexIds = (vertexIds, relation) => {
|
|
182
203
|
var _a;
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (!vertexCollection) {
|
|
187
|
-
const items = edgeRows.map((edge) => {
|
|
188
|
-
var _a;
|
|
189
|
-
return rowToDocument(edgeCollectionName(relation.edgeCollection), edge, (_a = relation.mapping) === null || _a === void 0 ? void 0 : _a.keepMetadata);
|
|
190
|
-
});
|
|
191
|
-
return relation.cardinality === 'one' ? items[0] : items;
|
|
204
|
+
const nodeCap = nodeCapFromLimit((_a = relation.mapping) === null || _a === void 0 ? void 0 : _a.limit);
|
|
205
|
+
if (nodeCap === undefined || relation.cardinality !== 'many') {
|
|
206
|
+
return vertexIds;
|
|
192
207
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
208
|
+
return [...new Set(vertexIds)]
|
|
209
|
+
.sort((a, b) => (a < b ? 1 : a > b ? -1 : 0))
|
|
210
|
+
.slice(0, nodeCap);
|
|
211
|
+
};
|
|
212
|
+
const emptyVertexLookup = {
|
|
213
|
+
get: () => undefined,
|
|
214
|
+
rank: () => 0,
|
|
215
|
+
};
|
|
216
|
+
const fetchVertexLookup = async (db, vertexCollection, vertexIds, relation) => {
|
|
217
|
+
var _a;
|
|
218
|
+
const rows = await fetchRowsByIds(db, vertexCollection, vertexIds, (_a = relation.mapping) === null || _a === void 0 ? void 0 : _a.softDelete);
|
|
219
|
+
const vertices = relation.mapping
|
|
220
|
+
? await hydrateMapData(db, relation.mapping, rows)
|
|
221
|
+
: rows;
|
|
222
|
+
const byId = new Map(vertices.map((vertex) => [vertex.id, vertex]));
|
|
223
|
+
const ranks = new Map(vertices.map((vertex, index) => [vertex.id, index]));
|
|
224
|
+
return {
|
|
225
|
+
get: (id) => byId.get(id),
|
|
226
|
+
rank: (id) => { var _a; return (_a = ranks.get(id)) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER; },
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
const relationValueFrom = (edgeRows, relation, vertexIds, lookup) => {
|
|
230
|
+
var _a, _b;
|
|
231
|
+
const toEdgeDoc = (edge) => {
|
|
232
|
+
var _a;
|
|
233
|
+
return rowToDocument(edgeCollectionName(relation.edgeCollection), edge, (_a = relation.mapping) === null || _a === void 0 ? void 0 : _a.keepMetadata);
|
|
234
|
+
};
|
|
235
|
+
if (!vertexCollectionFor(relation)) {
|
|
236
|
+
const items = edgeRows.map(toEdgeDoc);
|
|
237
|
+
return relation.cardinality === 'one' ? items[0] : items;
|
|
196
238
|
}
|
|
197
239
|
if (relation.skipEdge) {
|
|
198
|
-
|
|
240
|
+
const vertices = [...new Set(vertexIds)]
|
|
241
|
+
.sort((a, b) => lookup.rank(a) - lookup.rank(b))
|
|
242
|
+
.map((id) => lookup.get(id))
|
|
243
|
+
.filter((vertex) => !!vertex);
|
|
244
|
+
if (relation.cardinality === 'one')
|
|
245
|
+
return vertices[0];
|
|
246
|
+
return applyLimit(applySort(vertices, (_a = relation.mapping) === null || _a === void 0 ? void 0 : _a.sort), (_b = relation.mapping) === null || _b === void 0 ? void 0 : _b.limit);
|
|
199
247
|
}
|
|
248
|
+
const vertexCol = vertexEdgeColumn(relation);
|
|
249
|
+
const capped = new Set(vertexIds);
|
|
200
250
|
const items = edgeRows
|
|
201
251
|
.map((edge) => {
|
|
202
|
-
var _a;
|
|
203
252
|
const vertexId = edge[vertexCol];
|
|
204
|
-
const vertex =
|
|
253
|
+
const vertex = capped.has(vertexId) ? lookup.get(vertexId) : undefined;
|
|
205
254
|
if (!vertex)
|
|
206
255
|
return undefined;
|
|
207
|
-
const edgeDoc =
|
|
256
|
+
const edgeDoc = toEdgeDoc(edge);
|
|
208
257
|
if (relation.vertexAlias) {
|
|
209
258
|
return Object.assign(Object.assign({}, edgeDoc), { [relation.vertexAlias]: vertex });
|
|
210
259
|
}
|
|
@@ -213,10 +262,13 @@ const resolveRelationValue = async (db, edgeRows, relation, _parentCollection) =
|
|
|
213
262
|
.filter(Boolean);
|
|
214
263
|
return relation.cardinality === 'one' ? items[0] : items;
|
|
215
264
|
};
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
265
|
+
const resolveRelationValue = async (db, edgeRows, relation) => {
|
|
266
|
+
const vertexCollection = vertexCollectionFor(relation);
|
|
267
|
+
const vertexIds = capVertexIds(edgeRows.map((edge) => edge[vertexEdgeColumn(relation)]), relation);
|
|
268
|
+
const lookup = vertexCollection
|
|
269
|
+
? await fetchVertexLookup(db, vertexCollection, vertexIds, relation)
|
|
270
|
+
: emptyVertexLookup;
|
|
271
|
+
return relationValueFrom(edgeRows, relation, vertexIds, lookup);
|
|
220
272
|
};
|
|
221
273
|
const traverseRelation = async (db, startId, relation) => {
|
|
222
274
|
var _a, _b, _c, _d, _e;
|
|
@@ -272,7 +324,7 @@ export const executeFind = async (db, collection, mapData, id, ignoreSoftDelete
|
|
|
272
324
|
const table = asTable(tableRef);
|
|
273
325
|
const columns = getTableColumns(tableRef);
|
|
274
326
|
const computedSelect = buildComputedSelect(mapData, tableRef);
|
|
275
|
-
const conditions = [eq(table
|
|
327
|
+
const conditions = [eq(documentIdColumn(collection, table), id)];
|
|
276
328
|
if (!ignoreSoftDelete && mapData.softDelete !== false && table.deletedAt) {
|
|
277
329
|
conditions.push(sql `${table.deletedAt} IS NULL`);
|
|
278
330
|
}
|
|
@@ -338,7 +390,7 @@ export const executeFirst = async (db, mapData) => {
|
|
|
338
390
|
const limited = applyLimit(await executeAll(db, mapData), [0, 1]);
|
|
339
391
|
return limited[0];
|
|
340
392
|
};
|
|
341
|
-
export const relationsFrom = async (db, start,
|
|
393
|
+
export const relationsFrom = async (db, start, _parentCollection, relation) => {
|
|
342
394
|
var _a;
|
|
343
395
|
// Match attachRelation: honor maxDepth via BFS traversal. Without this,
|
|
344
396
|
// callers like getConversationByStart only see direct (depth-1) replies.
|
|
@@ -367,7 +419,7 @@ export const relationsFrom = async (db, start, parentCollection, relation) => {
|
|
|
367
419
|
}
|
|
368
420
|
// Keep edge fields (e.g. roles) and attach the related vertex under
|
|
369
421
|
// vertexAlias — same shape as hydrateDocuments/attachRelation.
|
|
370
|
-
const value = await resolveRelationValue(db, edgeRows, relation
|
|
422
|
+
const value = await resolveRelationValue(db, edgeRows, relation);
|
|
371
423
|
if (relation.cardinality === 'one') {
|
|
372
424
|
return value ? [value] : [];
|
|
373
425
|
}
|
|
@@ -390,7 +442,7 @@ export const deleteRelationsFor = async (db, startId, relation) => {
|
|
|
390
442
|
const vertexTable = asTable(vertexTableRef);
|
|
391
443
|
await db
|
|
392
444
|
.delete(vertexTableRef)
|
|
393
|
-
.where(eq(
|
|
445
|
+
.where(eq(documentIdColumn(relation.mapping.collection, vertexTable), vertexId));
|
|
394
446
|
}
|
|
395
447
|
await db
|
|
396
448
|
.delete(edgeTableRef)
|
package/dist/db/pg/migrate.d.ts
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
/** Drizzle journal tags in apply order (e.g. `0000_bizarre_chat`). */
|
|
2
|
+
export declare const listSchemaVersions: () => string[];
|
|
3
|
+
export declare const getFirstSchemaVersion: () => string;
|
|
4
|
+
export declare const getLatestSchemaVersion: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* Postgres backups created before `schemaVersion` was stamped assumed this
|
|
7
|
+
* journal tag (see `sql/meta/0007_snapshot.json`).
|
|
8
|
+
*/
|
|
9
|
+
export declare const LEGACY_POSTGRES_BACKUP_SCHEMA_VERSION = "0007_shallow_oracle";
|
|
10
|
+
/**
|
|
11
|
+
* Schema to apply before JSONL import on restore.
|
|
12
|
+
* - Postgres with `schemaVersion` → that tag (must still exist in this binary).
|
|
13
|
+
* - Postgres without → `0007_shallow_oracle` (pre-stamping baseline).
|
|
14
|
+
*/
|
|
15
|
+
export declare const resolveRestoreSchemaVersion: (schemaVersion?: string) => string;
|
|
16
|
+
/** Apply Drizzle migrations only through `tag` (inclusive). */
|
|
17
|
+
export declare const migrateToSchemaVersion: (tag: string) => Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Wipe public + drizzle schemas, then migrate through `tag`.
|
|
20
|
+
* Used by backup restore so import lands on a known schema version.
|
|
21
|
+
*/
|
|
22
|
+
export declare const resetAndMigrateToSchemaVersion: (tag: string) => Promise<void>;
|
|
1
23
|
/** Fail if app tables are missing (used when RUN_DB_MIGRATE_ON_BOOT=false). */
|
|
2
24
|
export declare const assertSchemaReady: () => Promise<void>;
|
|
3
25
|
/**
|
package/dist/db/pg/migrate.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
1
|
+
import { copyFileSync, existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
2
4
|
import { join, dirname } from 'node:path';
|
|
3
5
|
import { fileURLToPath } from 'node:url';
|
|
4
6
|
import { getTableName, sql } from 'drizzle-orm';
|
|
@@ -17,6 +19,86 @@ const resolveMigrationsFolder = () => {
|
|
|
17
19
|
return join(moduleDir, '..', '..', '..', 'src', 'db', 'pg', 'sql');
|
|
18
20
|
};
|
|
19
21
|
const migrationsFolder = resolveMigrationsFolder();
|
|
22
|
+
const readMigrationJournal = () => {
|
|
23
|
+
const journalPath = join(migrationsFolder, 'meta', '_journal.json');
|
|
24
|
+
return JSON.parse(readFileSync(journalPath, 'utf8'));
|
|
25
|
+
};
|
|
26
|
+
/** Drizzle journal tags in apply order (e.g. `0000_bizarre_chat`). */
|
|
27
|
+
export const listSchemaVersions = () => readMigrationJournal().entries.map((entry) => entry.tag);
|
|
28
|
+
export const getFirstSchemaVersion = () => {
|
|
29
|
+
const tags = listSchemaVersions();
|
|
30
|
+
const first = tags[0];
|
|
31
|
+
if (!first) {
|
|
32
|
+
throw new Error('No Postgres schema migrations found');
|
|
33
|
+
}
|
|
34
|
+
return first;
|
|
35
|
+
};
|
|
36
|
+
export const getLatestSchemaVersion = () => {
|
|
37
|
+
const tags = listSchemaVersions();
|
|
38
|
+
const latest = tags[tags.length - 1];
|
|
39
|
+
if (!latest) {
|
|
40
|
+
throw new Error('No Postgres schema migrations found');
|
|
41
|
+
}
|
|
42
|
+
return latest;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Postgres backups created before `schemaVersion` was stamped assumed this
|
|
46
|
+
* journal tag (see `sql/meta/0007_snapshot.json`).
|
|
47
|
+
*/
|
|
48
|
+
export const LEGACY_POSTGRES_BACKUP_SCHEMA_VERSION = '0007_shallow_oracle';
|
|
49
|
+
const assertKnownSchemaVersion = (tag) => {
|
|
50
|
+
if (!listSchemaVersions().includes(tag)) {
|
|
51
|
+
throw new Error(`Unknown Postgres schema version "${tag}"; known: ${listSchemaVersions().join(', ')}`);
|
|
52
|
+
}
|
|
53
|
+
return tag;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Schema to apply before JSONL import on restore.
|
|
57
|
+
* - Postgres with `schemaVersion` → that tag (must still exist in this binary).
|
|
58
|
+
* - Postgres without → `0007_shallow_oracle` (pre-stamping baseline).
|
|
59
|
+
*/
|
|
60
|
+
export const resolveRestoreSchemaVersion = (schemaVersion) => {
|
|
61
|
+
if (schemaVersion) {
|
|
62
|
+
return assertKnownSchemaVersion(schemaVersion);
|
|
63
|
+
}
|
|
64
|
+
return assertKnownSchemaVersion(LEGACY_POSTGRES_BACKUP_SCHEMA_VERSION);
|
|
65
|
+
};
|
|
66
|
+
const buildMigrationsFolderUpTo = async (tag) => {
|
|
67
|
+
const journal = readMigrationJournal();
|
|
68
|
+
const endIdx = journal.entries.findIndex((entry) => entry.tag === tag);
|
|
69
|
+
if (endIdx < 0) {
|
|
70
|
+
throw new Error(`Unknown Postgres schema version: ${tag}`);
|
|
71
|
+
}
|
|
72
|
+
const entries = journal.entries.slice(0, endIdx + 1);
|
|
73
|
+
const folder = await mkdtemp(join(tmpdir(), 'op-migrate-'));
|
|
74
|
+
await mkdir(join(folder, 'meta'), { recursive: true });
|
|
75
|
+
await writeFile(join(folder, 'meta', '_journal.json'), `${JSON.stringify(Object.assign(Object.assign({}, journal), { entries }), null, 2)}\n`);
|
|
76
|
+
for (const entry of entries) {
|
|
77
|
+
copyFileSync(join(migrationsFolder, `${entry.tag}.sql`), join(folder, `${entry.tag}.sql`));
|
|
78
|
+
}
|
|
79
|
+
return folder;
|
|
80
|
+
};
|
|
81
|
+
/** Apply Drizzle migrations only through `tag` (inclusive). */
|
|
82
|
+
export const migrateToSchemaVersion = async (tag) => {
|
|
83
|
+
const folder = await buildMigrationsFolderUpTo(tag);
|
|
84
|
+
try {
|
|
85
|
+
await ensurePublicSchema();
|
|
86
|
+
await migrate(pgDb(), { migrationsFolder: folder });
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
await rm(folder, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Wipe public + drizzle schemas, then migrate through `tag`.
|
|
94
|
+
* Used by backup restore so import lands on a known schema version.
|
|
95
|
+
*/
|
|
96
|
+
export const resetAndMigrateToSchemaVersion = async (tag) => {
|
|
97
|
+
log.info('Resetting Postgres schemas and migrating to %s', tag);
|
|
98
|
+
await resetPostgresSchemas();
|
|
99
|
+
await migrateToSchemaVersion(tag);
|
|
100
|
+
log.info('Postgres schema ready at %s', tag);
|
|
101
|
+
};
|
|
20
102
|
const APP_TABLE_NAMES = [
|
|
21
103
|
...DOCUMENT_IMPORT_ORDER.map((collection) => getTableName(getTableForCollection(collection))),
|
|
22
104
|
...EDGE_IMPORT_ORDER.map((collection) => getTableName(getTableForCollection(collection))),
|
|
@@ -58,8 +140,12 @@ const isSchemaConflictError = (err) => {
|
|
|
58
140
|
const code = candidate.code;
|
|
59
141
|
// 23505 unique_violation (e.g. orphan composite type in pg_type)
|
|
60
142
|
// 42P07 duplicate_table
|
|
143
|
+
// 42701 duplicate_column (partial migrate left a column, journal did not)
|
|
61
144
|
// 3F000 invalid_schema_name (stale search_path after DROP public)
|
|
62
|
-
return code === '23505' ||
|
|
145
|
+
return (code === '23505' ||
|
|
146
|
+
code === '42P07' ||
|
|
147
|
+
code === '42701' ||
|
|
148
|
+
code === '3F000');
|
|
63
149
|
});
|
|
64
150
|
};
|
|
65
151
|
const applyMigrations = async () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { and, count, eq, exists, gte, isNull, lt, sql, } from 'drizzle-orm';
|
|
2
2
|
import { alias, QueryBuilder } from 'drizzle-orm/pg-core';
|
|
3
3
|
import { posts } from '../schema/documents.js';
|
|
4
|
-
import { entries, follows, postGroups,
|
|
4
|
+
import { entries, follows, postGroups, repost, replyTo } from '../schema/edges.js';
|
|
5
5
|
import { asTable } from '../map/registry.js';
|
|
6
6
|
const queryBuilder = new QueryBuilder();
|
|
7
7
|
const outerIdText = (table) => sql `${asTable(table).id}::text`;
|
|
@@ -17,10 +17,10 @@ const edgeOutboundCount = (edgeTable, outerTable, window) => {
|
|
|
17
17
|
}
|
|
18
18
|
return sql `(SELECT count(*)::int FROM ${edgeTable} WHERE ${and(...conditions)})`;
|
|
19
19
|
};
|
|
20
|
-
export const profileActivityScoreExpr = (outerTable, window) => sql `COALESCE(${edgeOutboundCount(entries, outerTable, window)}, 0) + COALESCE(${edgeOutboundCount(
|
|
20
|
+
export const profileActivityScoreExpr = (outerTable, window) => sql `COALESCE(${edgeOutboundCount(entries, outerTable, window)}, 0) + COALESCE(${edgeOutboundCount(follows, outerTable, window)}, 0)`;
|
|
21
21
|
export const postActivityScoreExpr = (outerTable) => {
|
|
22
22
|
const postId = outerIdText(outerTable);
|
|
23
|
-
return sql `(SELECT count(*)::int FROM ${
|
|
23
|
+
return sql `(SELECT count(*)::int FROM ${entries} WHERE ${entries.toId} = ${postId}) + (SELECT count(*)::int FROM ${replyTo} rt INNER JOIN ${posts} p ON p.id::text = rt.from_id WHERE rt.to_id = ${postId} AND p.deleted_at IS NULL) + (SELECT count(*)::int FROM ${repost} WHERE ${repost.toId} = ${postId})`;
|
|
24
24
|
};
|
|
25
25
|
export const postReplyCountExpr = (outerTable) => {
|
|
26
26
|
const replyPosts = alias(posts, 'reply_posts');
|