@terreno/api 57.3.0 → 57.4.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.
Files changed (293) hide show
  1. package/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
  2. package/README.md +6 -0
  3. package/dist/adminTypes.d.ts +7 -0
  4. package/dist/adminTypes.js +6 -1
  5. package/dist/api.d.ts +10 -0
  6. package/dist/api.js +99 -36
  7. package/dist/audit/auditApp.d.ts +30 -0
  8. package/dist/audit/auditApp.js +155 -0
  9. package/dist/audit/auditEventModel.d.ts +29 -0
  10. package/dist/audit/auditEventModel.js +102 -0
  11. package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
  12. package/dist/audit/cloudTasksEnqueue.js +162 -0
  13. package/dist/audit/diff.d.ts +10 -0
  14. package/dist/audit/diff.js +230 -0
  15. package/dist/audit/processQueue.d.ts +6 -0
  16. package/dist/audit/processQueue.js +101 -0
  17. package/dist/audit/rbacSink.d.ts +2 -0
  18. package/dist/audit/rbacSink.js +82 -0
  19. package/dist/audit/record.d.ts +56 -0
  20. package/dist/audit/record.js +373 -0
  21. package/dist/betterAuth.d.ts +6 -0
  22. package/dist/betterAuthSetup.js +1 -1
  23. package/dist/collectionRegistry.d.ts +3 -1
  24. package/dist/collectionRegistry.js +25 -0
  25. package/dist/index.d.ts +26 -1
  26. package/dist/index.js +48 -2
  27. package/dist/mcp/query.d.ts +0 -13
  28. package/dist/mcp/query.js +8 -8
  29. package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
  30. package/dist/migrations/fixtures/all-down/20260910120000-one.js +49 -0
  31. package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
  32. package/dist/migrations/fixtures/all-down/20260910120001-two.js +49 -0
  33. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
  34. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +45 -0
  35. package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
  36. package/dist/migrations/fixtures/bad-name/not-a-migration.js +45 -0
  37. package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
  38. package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
  39. package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
  40. package/dist/migrations/fixtures/valid/20260910120000-alpha.js +45 -0
  41. package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
  42. package/dist/migrations/fixtures/valid/20260910120001-beta.js +49 -0
  43. package/dist/migrations/generate.d.ts +17 -0
  44. package/dist/migrations/generate.js +277 -0
  45. package/dist/migrations/load.d.ts +20 -0
  46. package/dist/migrations/load.js +236 -0
  47. package/dist/migrations/lock.d.ts +13 -0
  48. package/dist/migrations/lock.js +145 -0
  49. package/dist/migrations/runner.d.ts +42 -0
  50. package/dist/migrations/runner.js +503 -0
  51. package/dist/migrations/schemaCatalog.d.ts +44 -0
  52. package/dist/migrations/schemaCatalog.js +326 -0
  53. package/dist/migrations/types.d.ts +87 -0
  54. package/dist/migrations/types.js +7 -0
  55. package/dist/models/notification.d.ts +2 -0
  56. package/dist/models/notification.js +62 -0
  57. package/dist/models/notificationPreference.d.ts +2 -0
  58. package/dist/models/notificationPreference.js +49 -0
  59. package/dist/notifications/notificationService.d.ts +40 -0
  60. package/dist/notifications/notificationService.js +323 -0
  61. package/dist/notifications/notificationsApp.d.ts +18 -0
  62. package/dist/notifications/notificationsApp.js +250 -0
  63. package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
  64. package/dist/notifications/notificationsBeforeSend.js +77 -0
  65. package/dist/openApi.d.ts +1 -7
  66. package/dist/openApi.js +7 -6
  67. package/dist/openApiBuilder.d.ts +14 -3
  68. package/dist/openApiValidator.d.ts +13 -1
  69. package/dist/openApiValidator.js +200 -36
  70. package/dist/orgs/orgContext.d.ts +23 -0
  71. package/dist/orgs/orgContext.js +198 -0
  72. package/dist/orgs/orgPermissions.d.ts +4 -0
  73. package/dist/orgs/orgPermissions.js +77 -0
  74. package/dist/orgs/orgPlugin.d.ts +13 -0
  75. package/dist/orgs/orgPlugin.js +84 -0
  76. package/dist/orgs/organizationModel.d.ts +13 -0
  77. package/dist/orgs/organizationModel.js +301 -0
  78. package/dist/orgs/organizationSettings.d.ts +19 -0
  79. package/dist/orgs/organizationSettings.js +120 -0
  80. package/dist/orgs/orgsApp.d.ts +28 -0
  81. package/dist/orgs/orgsApp.js +678 -0
  82. package/dist/permissions.d.ts +7 -13
  83. package/dist/permissions.js +8 -31
  84. package/dist/rateLimit/middleware.d.ts +0 -8
  85. package/dist/rateLimit/middleware.js +2 -3
  86. package/dist/rateLimit/mongoStore.d.ts +1 -0
  87. package/dist/rateLimit/mongoStore.js +1 -0
  88. package/dist/rateLimit/policies.d.ts +0 -4
  89. package/dist/rateLimit/policies.js +5 -7
  90. package/dist/rateLimit/redisStore.d.ts +1 -0
  91. package/dist/rateLimit/redisStore.js +1 -0
  92. package/dist/rbac/access.js +30 -13
  93. package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
  94. package/dist/rbac/membershipOrgAdminSource.js +122 -0
  95. package/dist/rbac/modelRouterAccess.d.ts +3 -0
  96. package/dist/rbac/modelRouterAccess.js +3 -0
  97. package/dist/rbac/resolve.js +60 -31
  98. package/dist/rbac/roleModel.d.ts +2 -5
  99. package/dist/rbac/roleModel.js +18 -4
  100. package/dist/rbac/statements.d.ts +10 -1
  101. package/dist/rbac/statements.js +19 -2
  102. package/dist/rbac/types.d.ts +5 -0
  103. package/dist/realtime/changeStreamWatcher.d.ts +10 -4
  104. package/dist/realtime/changeStreamWatcher.js +110 -64
  105. package/dist/realtime/deprecation.d.ts +1 -0
  106. package/dist/realtime/deprecation.js +1 -0
  107. package/dist/realtime/realtimeApp.d.ts +1 -0
  108. package/dist/realtime/realtimeApp.js +1 -0
  109. package/dist/schemaMetadata.js +16 -5
  110. package/dist/sync/adminBroadcastScope.d.ts +40 -0
  111. package/dist/sync/adminBroadcastScope.js +194 -0
  112. package/dist/sync/adminWindowAccess.d.ts +13 -0
  113. package/dist/sync/adminWindowAccess.js +96 -0
  114. package/dist/sync/adminWindowMutation.d.ts +88 -0
  115. package/dist/sync/adminWindowMutation.js +247 -0
  116. package/dist/sync/executors.d.ts +9 -3
  117. package/dist/sync/executors.js +190 -159
  118. package/dist/sync/mutationHandler.d.ts +5 -2
  119. package/dist/sync/mutationHandler.js +191 -71
  120. package/dist/sync/registry.js +4 -0
  121. package/dist/sync/routes.d.ts +13 -0
  122. package/dist/sync/routes.js +113 -56
  123. package/dist/sync/socketHandlers.d.ts +8 -2
  124. package/dist/sync/socketHandlers.js +144 -50
  125. package/dist/sync/streams.d.ts +2 -0
  126. package/dist/sync/streams.js +7 -1
  127. package/dist/sync/syncApp.d.ts +2 -1
  128. package/dist/sync/syncApp.js +2 -1
  129. package/dist/sync/syncBatch.d.ts +5 -2
  130. package/dist/sync/syncBatch.js +4 -4
  131. package/dist/sync/types.d.ts +18 -0
  132. package/dist/terrenoApp.d.ts +29 -0
  133. package/dist/terrenoApp.js +91 -54
  134. package/dist/tests/createTestData.d.ts +1 -3
  135. package/dist/tests/createTestData.js +3 -5
  136. package/dist/tests/models.d.ts +1 -0
  137. package/dist/tests/models.js +9 -2
  138. package/dist/tests/testHelper.d.ts +0 -5
  139. package/dist/tests/testHelper.js +2 -9
  140. package/dist/types/authToken.d.ts +3 -3
  141. package/dist/types/mcpServiceToken.d.ts +2 -2
  142. package/dist/types/membership.d.ts +23 -0
  143. package/dist/types/membership.js +2 -0
  144. package/dist/types/notification.d.ts +28 -0
  145. package/dist/types/notification.js +2 -0
  146. package/dist/types/notificationPreference.d.ts +20 -0
  147. package/dist/types/notificationPreference.js +2 -0
  148. package/dist/types/organization.d.ts +36 -0
  149. package/dist/types/organization.js +2 -0
  150. package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
  151. package/dist/webhooks/idempotency/webhookReceipt.js +7 -7
  152. package/dist/webhooks/rawBody.d.ts +1 -0
  153. package/dist/webhooks/rawBody.js +1 -0
  154. package/package.json +31 -25
  155. package/src/__snapshots__/openApi.test.ts.snap +192 -54
  156. package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
  157. package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
  158. package/src/adminTypes.ts +8 -0
  159. package/src/api.test.ts +39 -2
  160. package/src/api.ts +68 -0
  161. package/src/audit/auditApp.test.ts +243 -0
  162. package/src/audit/auditApp.ts +139 -0
  163. package/src/audit/auditEventModel.ts +156 -0
  164. package/src/audit/auditRouter.test.ts +456 -0
  165. package/src/audit/cloudTasksEnqueue.test.ts +124 -0
  166. package/src/audit/cloudTasksEnqueue.ts +104 -0
  167. package/src/audit/diff.ts +163 -0
  168. package/src/audit/processQueue.ts +69 -0
  169. package/src/audit/rbacSink.test.ts +86 -0
  170. package/src/audit/rbacSink.ts +42 -0
  171. package/src/audit/record.test.ts +402 -0
  172. package/src/audit/record.ts +328 -0
  173. package/src/auth.test.ts +1 -1
  174. package/src/betterAuth.ts +7 -0
  175. package/src/betterAuthSetup.test.ts +27 -0
  176. package/src/betterAuthSetup.ts +1 -0
  177. package/src/collectionRegistry.test.ts +8 -9
  178. package/src/collectionRegistry.ts +21 -1
  179. package/src/express.d.ts +2 -0
  180. package/src/expressServer.test.ts +50 -2
  181. package/src/index.ts +69 -0
  182. package/src/mcp/handlers.test.ts +275 -0
  183. package/src/mcp/query.ts +3 -3
  184. package/src/mcp/toolGenerator.test.ts +81 -0
  185. package/src/migrations/cli.test.ts +376 -0
  186. package/src/migrations/cli.ts +283 -0
  187. package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
  188. package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
  189. package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
  190. package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
  191. package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
  192. package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
  193. package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
  194. package/src/migrations/generate.test.ts +333 -0
  195. package/src/migrations/generate.ts +216 -0
  196. package/src/migrations/load.test.ts +103 -0
  197. package/src/migrations/load.ts +124 -0
  198. package/src/migrations/lock.test.ts +133 -0
  199. package/src/migrations/lock.ts +118 -0
  200. package/src/migrations/runner.test.ts +504 -0
  201. package/src/migrations/runner.ts +358 -0
  202. package/src/migrations/schemaCatalog.test.ts +159 -0
  203. package/src/migrations/schemaCatalog.ts +262 -0
  204. package/src/migrations/types.ts +94 -0
  205. package/src/models/notification.ts +68 -0
  206. package/src/models/notificationPreference.ts +62 -0
  207. package/src/notifications/notificationService.ts +254 -0
  208. package/src/notifications/notificationsApp.test.ts +505 -0
  209. package/src/notifications/notificationsApp.ts +200 -0
  210. package/src/notifications/notificationsBeforeSend.ts +51 -0
  211. package/src/openApi.test.ts +0 -1
  212. package/src/openApi.ts +2 -1
  213. package/src/openApiBuilder.ts +14 -3
  214. package/src/openApiValidator.test.ts +221 -2
  215. package/src/openApiValidator.ts +218 -33
  216. package/src/orgs/orgAccessOptIn.test.ts +87 -0
  217. package/src/orgs/orgContext.test.ts +259 -0
  218. package/src/orgs/orgContext.ts +151 -0
  219. package/src/orgs/orgPermissions.test.ts +105 -0
  220. package/src/orgs/orgPermissions.ts +40 -0
  221. package/src/orgs/orgPlugin.test.ts +130 -0
  222. package/src/orgs/orgPlugin.ts +70 -0
  223. package/src/orgs/orgRbac.test.ts +267 -0
  224. package/src/orgs/orgSecurityDefects.test.ts +415 -0
  225. package/src/orgs/organizationModel.test.ts +205 -0
  226. package/src/orgs/organizationModel.ts +257 -0
  227. package/src/orgs/organizationSettings.test.ts +113 -0
  228. package/src/orgs/organizationSettings.ts +126 -0
  229. package/src/orgs/orgsApp.test.ts +382 -0
  230. package/src/orgs/orgsApp.ts +563 -0
  231. package/src/permissions.test.ts +62 -36
  232. package/src/permissions.ts +8 -30
  233. package/src/rateLimit/middleware.ts +1 -1
  234. package/src/rateLimit/mongoStore.ts +1 -0
  235. package/src/rateLimit/policies.ts +2 -2
  236. package/src/rateLimit/redisStore.ts +1 -0
  237. package/src/rbac/access.ts +11 -3
  238. package/src/rbac/membershipOrgAdminSource.ts +59 -0
  239. package/src/rbac/modelRouterAccess.ts +3 -0
  240. package/src/rbac/permissionUtils.test.ts +26 -5
  241. package/src/rbac/resolve.test.ts +34 -2
  242. package/src/rbac/resolve.ts +26 -7
  243. package/src/rbac/roleModel.test.ts +34 -1
  244. package/src/rbac/roleModel.ts +17 -1
  245. package/src/rbac/statements.test.ts +19 -0
  246. package/src/rbac/statements.ts +19 -1
  247. package/src/rbac/types.ts +5 -0
  248. package/src/realtime/changeStreamWatcher.ts +73 -33
  249. package/src/realtime/deprecation.ts +1 -0
  250. package/src/realtime/realtime.test.ts +357 -185
  251. package/src/realtime/realtimeApp.ts +1 -0
  252. package/src/schemaMetadata.test.ts +14 -0
  253. package/src/schemaMetadata.ts +5 -5
  254. package/src/sync/adminBroadcastConfig.test.ts +96 -0
  255. package/src/sync/adminBroadcastScope.test.ts +155 -0
  256. package/src/sync/adminBroadcastScope.ts +123 -0
  257. package/src/sync/adminWindowAccess.test.ts +57 -0
  258. package/src/sync/adminWindowAccess.ts +32 -0
  259. package/src/sync/adminWindowMutation.test.ts +450 -0
  260. package/src/sync/adminWindowMutation.ts +283 -0
  261. package/src/sync/executors.ts +33 -6
  262. package/src/sync/mutationHandler.test.ts +277 -1
  263. package/src/sync/mutationHandler.ts +171 -29
  264. package/src/sync/registry.ts +4 -0
  265. package/src/sync/routes.ts +52 -8
  266. package/src/sync/socketHandlers.ts +154 -58
  267. package/src/sync/streams.ts +6 -0
  268. package/src/sync/sync.test.ts +3 -2
  269. package/src/sync/syncApp.ts +2 -1
  270. package/src/sync/syncBatch.ts +7 -2
  271. package/src/sync/syncRoutes.test.ts +297 -3
  272. package/src/sync/syncSocket.test.ts +229 -0
  273. package/src/sync/types.ts +20 -0
  274. package/src/terrenoApp.migrations.test.ts +114 -0
  275. package/src/terrenoApp.test.ts +41 -0
  276. package/src/terrenoApp.ts +94 -31
  277. package/src/tests/createTestData.ts +2 -2
  278. package/src/tests/models.ts +10 -2
  279. package/src/tests/testHelper.ts +1 -7
  280. package/src/types/authToken.ts +2 -4
  281. package/src/types/mcpServiceToken.ts +1 -7
  282. package/src/types/membership.ts +48 -0
  283. package/src/types/notification.ts +34 -0
  284. package/src/types/notificationPreference.ts +28 -0
  285. package/src/types/organization.ts +49 -0
  286. package/src/webhooks/idempotency/webhookReceipt.ts +4 -7
  287. package/src/webhooks/rawBody.test.ts +33 -0
  288. package/src/webhooks/rawBody.ts +1 -0
  289. package/tsconfig.json +1 -1
  290. package/tsconfig.typedoc.json +2 -1
  291. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
  292. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
  293. package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
@@ -0,0 +1,326 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
34
+ }
35
+ }
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.diffSchemaCatalog = exports.buildSchemaCatalog = void 0;
40
+ var skipPaths = new Set(["_id", "__v"]);
41
+ var indexKey = function (index) {
42
+ return "".concat(JSON.stringify(index.keys), ":").concat(index.unique ? "u" : "n");
43
+ };
44
+ var fieldMap = function (fields) {
45
+ return new Map(fields.map(function (field) { return [field.path, field]; }));
46
+ };
47
+ var buildSchemaCatalog = function (_a) {
48
+ var e_1, _b, e_2, _c;
49
+ var _d;
50
+ var models = _a.models;
51
+ var catalog = { models: {} };
52
+ try {
53
+ for (var models_1 = __values(models), models_1_1 = models_1.next(); !models_1_1.done; models_1_1 = models_1.next()) {
54
+ var model = models_1_1.value;
55
+ var fields = [];
56
+ var paths = model.schema.paths;
57
+ try {
58
+ for (var _e = (e_2 = void 0, __values(Object.keys(paths))), _f = _e.next(); !_f.done; _f = _e.next()) {
59
+ var path = _f.value;
60
+ if (skipPaths.has(path)) {
61
+ continue;
62
+ }
63
+ var schemaType = paths[path];
64
+ var required = Boolean(schemaType.isRequired);
65
+ var unique = Boolean((_d = schemaType.options) === null || _d === void 0 ? void 0 : _d.unique);
66
+ fields.push({
67
+ instance: schemaType.instance || "Mixed",
68
+ path: path,
69
+ required: required,
70
+ unique: unique,
71
+ });
72
+ }
73
+ }
74
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
75
+ finally {
76
+ try {
77
+ if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
78
+ }
79
+ finally { if (e_2) throw e_2.error; }
80
+ }
81
+ fields.sort(function (a, b) { return a.path.localeCompare(b.path); });
82
+ var indexes = model.schema
83
+ .indexes()
84
+ .map(function (entry) {
85
+ var _a = __read(entry, 2), keys = _a[0], options = _a[1];
86
+ return {
87
+ keys: keys,
88
+ unique: Boolean(options === null || options === void 0 ? void 0 : options.unique),
89
+ };
90
+ })
91
+ .sort(function (a, b) { return indexKey(a).localeCompare(indexKey(b)); });
92
+ catalog.models[model.modelName] = {
93
+ collection: model.collection.collectionName,
94
+ fields: fields,
95
+ indexes: indexes,
96
+ modelName: model.modelName,
97
+ };
98
+ }
99
+ }
100
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
101
+ finally {
102
+ try {
103
+ if (models_1_1 && !models_1_1.done && (_b = models_1.return)) _b.call(models_1);
104
+ }
105
+ finally { if (e_1) throw e_1.error; }
106
+ }
107
+ return catalog;
108
+ };
109
+ exports.buildSchemaCatalog = buildSchemaCatalog;
110
+ var diffSchemaCatalog = function (_a) {
111
+ var e_3, _b;
112
+ var _c, _d, _e, _f;
113
+ var after = _a.after, before = _a.before;
114
+ var ops = [];
115
+ var modelNames = new Set(__spreadArray(__spreadArray([], __read(Object.keys(before.models)), false), __read(Object.keys(after.models)), false));
116
+ var _loop_1 = function (modelName) {
117
+ var e_4, _j, e_5, _k, e_6, _l, e_7, _m, e_8, _o, e_9, _p;
118
+ var beforeModel = (_c = before.models[modelName]) !== null && _c !== void 0 ? _c : {
119
+ collection: (_e = (_d = after.models[modelName]) === null || _d === void 0 ? void 0 : _d.collection) !== null && _e !== void 0 ? _e : modelName,
120
+ fields: [],
121
+ indexes: [],
122
+ modelName: modelName,
123
+ };
124
+ var afterModel = (_f = after.models[modelName]) !== null && _f !== void 0 ? _f : {
125
+ collection: beforeModel.collection,
126
+ fields: [],
127
+ indexes: [],
128
+ modelName: modelName,
129
+ };
130
+ var beforeFields = fieldMap(beforeModel.fields);
131
+ var afterFields = fieldMap(afterModel.fields);
132
+ var removed = beforeModel.fields.filter(function (field) { return !afterFields.has(field.path); });
133
+ var added = afterModel.fields.filter(function (field) { return !beforeFields.has(field.path); });
134
+ var usedRemoved = new Set();
135
+ var usedAdded = new Set();
136
+ var _loop_2 = function (removedField) {
137
+ var match = added.find(function (addedField) {
138
+ return addedField.instance === removedField.instance && !usedAdded.has(addedField.path);
139
+ });
140
+ if (!match) {
141
+ return "continue";
142
+ }
143
+ usedRemoved.add(removedField.path);
144
+ usedAdded.add(match.path);
145
+ ops.push({
146
+ fromPath: removedField.path,
147
+ kind: "renameField",
148
+ modelName: modelName,
149
+ safe: false,
150
+ toPath: match.path,
151
+ });
152
+ if (match.required) {
153
+ ops.push({
154
+ kind: "addRequiredField",
155
+ modelName: modelName,
156
+ path: match.path,
157
+ safe: false,
158
+ });
159
+ }
160
+ };
161
+ try {
162
+ for (var removed_1 = (e_4 = void 0, __values(removed)), removed_1_1 = removed_1.next(); !removed_1_1.done; removed_1_1 = removed_1.next()) {
163
+ var removedField = removed_1_1.value;
164
+ _loop_2(removedField);
165
+ }
166
+ }
167
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
168
+ finally {
169
+ try {
170
+ if (removed_1_1 && !removed_1_1.done && (_j = removed_1.return)) _j.call(removed_1);
171
+ }
172
+ finally { if (e_4) throw e_4.error; }
173
+ }
174
+ try {
175
+ for (var added_1 = (e_5 = void 0, __values(added)), added_1_1 = added_1.next(); !added_1_1.done; added_1_1 = added_1.next()) {
176
+ var field = added_1_1.value;
177
+ if (usedAdded.has(field.path)) {
178
+ continue;
179
+ }
180
+ if (field.required) {
181
+ ops.push({
182
+ kind: "addRequiredField",
183
+ modelName: modelName,
184
+ path: field.path,
185
+ safe: false,
186
+ });
187
+ continue;
188
+ }
189
+ ops.push({
190
+ kind: "addOptionalField",
191
+ modelName: modelName,
192
+ path: field.path,
193
+ safe: true,
194
+ });
195
+ }
196
+ }
197
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
198
+ finally {
199
+ try {
200
+ if (added_1_1 && !added_1_1.done && (_k = added_1.return)) _k.call(added_1);
201
+ }
202
+ finally { if (e_5) throw e_5.error; }
203
+ }
204
+ try {
205
+ for (var removed_2 = (e_6 = void 0, __values(removed)), removed_2_1 = removed_2.next(); !removed_2_1.done; removed_2_1 = removed_2.next()) {
206
+ var field = removed_2_1.value;
207
+ if (usedRemoved.has(field.path)) {
208
+ continue;
209
+ }
210
+ ops.push({
211
+ kind: "removeField",
212
+ modelName: modelName,
213
+ path: field.path,
214
+ safe: false,
215
+ });
216
+ }
217
+ }
218
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
219
+ finally {
220
+ try {
221
+ if (removed_2_1 && !removed_2_1.done && (_l = removed_2.return)) _l.call(removed_2);
222
+ }
223
+ finally { if (e_6) throw e_6.error; }
224
+ }
225
+ try {
226
+ for (var _q = (e_7 = void 0, __values(afterModel.fields)), _r = _q.next(); !_r.done; _r = _q.next()) {
227
+ var field = _r.value;
228
+ var previous = beforeFields.get(field.path);
229
+ if (!previous) {
230
+ continue;
231
+ }
232
+ if (previous.instance !== field.instance) {
233
+ ops.push({
234
+ kind: "changeFieldType",
235
+ modelName: modelName,
236
+ path: field.path,
237
+ safe: false,
238
+ });
239
+ }
240
+ if (!previous.required && field.required) {
241
+ ops.push({
242
+ kind: "addRequiredField",
243
+ modelName: modelName,
244
+ path: field.path,
245
+ safe: false,
246
+ });
247
+ }
248
+ }
249
+ }
250
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
251
+ finally {
252
+ try {
253
+ if (_r && !_r.done && (_m = _q.return)) _m.call(_q);
254
+ }
255
+ finally { if (e_7) throw e_7.error; }
256
+ }
257
+ var beforeIndexes = new Set(beforeModel.indexes.map(indexKey));
258
+ var afterIndexes = new Set(afterModel.indexes.map(indexKey));
259
+ try {
260
+ for (var _s = (e_8 = void 0, __values(afterModel.indexes)), _t = _s.next(); !_t.done; _t = _s.next()) {
261
+ var index = _t.value;
262
+ if (beforeIndexes.has(indexKey(index))) {
263
+ continue;
264
+ }
265
+ if (index.unique) {
266
+ ops.push({
267
+ keys: index.keys,
268
+ kind: "addUniqueIndex",
269
+ modelName: modelName,
270
+ safe: false,
271
+ });
272
+ continue;
273
+ }
274
+ ops.push({
275
+ keys: index.keys,
276
+ kind: "addIndex",
277
+ modelName: modelName,
278
+ safe: true,
279
+ });
280
+ }
281
+ }
282
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
283
+ finally {
284
+ try {
285
+ if (_t && !_t.done && (_o = _s.return)) _o.call(_s);
286
+ }
287
+ finally { if (e_8) throw e_8.error; }
288
+ }
289
+ try {
290
+ for (var _u = (e_9 = void 0, __values(beforeModel.indexes)), _v = _u.next(); !_v.done; _v = _u.next()) {
291
+ var index = _v.value;
292
+ if (afterIndexes.has(indexKey(index))) {
293
+ continue;
294
+ }
295
+ ops.push({
296
+ keys: index.keys,
297
+ kind: "dropIndex",
298
+ modelName: modelName,
299
+ safe: true,
300
+ });
301
+ }
302
+ }
303
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
304
+ finally {
305
+ try {
306
+ if (_v && !_v.done && (_p = _u.return)) _p.call(_u);
307
+ }
308
+ finally { if (e_9) throw e_9.error; }
309
+ }
310
+ };
311
+ try {
312
+ for (var _g = __values(__spreadArray([], __read(modelNames), false).sort()), _h = _g.next(); !_h.done; _h = _g.next()) {
313
+ var modelName = _h.value;
314
+ _loop_1(modelName);
315
+ }
316
+ }
317
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
318
+ finally {
319
+ try {
320
+ if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
321
+ }
322
+ finally { if (e_3) throw e_3.error; }
323
+ }
324
+ return { ops: ops };
325
+ };
326
+ exports.diffSchemaCatalog = diffSchemaCatalog;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Shared types and constants for Terreno MongoDB migrations. History and the
3
+ * migrate lock both live in the native `terreno_migrations` collection.
4
+ */
5
+ import type mongoose from "mongoose";
6
+ /** Mongo collection that stores applied migration rows and the lock document. */
7
+ export declare const MIGRATIONS_COLLECTION = "terreno_migrations";
8
+ /** Reserved `_id` for the migrate lock row (Task 1.2). */
9
+ export declare const MIGRATION_LOCK_ID = "_lock";
10
+ /** Native collection documents in `terreno_migrations` (string `_id`, not ObjectId). */
11
+ export interface MigrationStoreDoc {
12
+ _id: string;
13
+ id?: string;
14
+ checksum?: string;
15
+ appliedAt?: Date;
16
+ holder?: string;
17
+ heartbeatAt?: Date;
18
+ expiresAt?: Date;
19
+ }
20
+ export interface MigrationLogger {
21
+ debug: (message: string, ...meta: unknown[]) => void;
22
+ info: (message: string, ...meta: unknown[]) => void;
23
+ warn: (message: string, ...meta: unknown[]) => void;
24
+ error: (message: string, ...meta: unknown[]) => void;
25
+ }
26
+ export interface MigrationContext {
27
+ dryRun: boolean;
28
+ mongoose: typeof mongoose;
29
+ logger: MigrationLogger;
30
+ addLog?: (level: "info" | "warn" | "error", message: string) => Promise<void>;
31
+ checkCancellation?: () => Promise<void>;
32
+ }
33
+ export interface MigrationModule {
34
+ id: string;
35
+ up: (ctx: MigrationContext) => Promise<void>;
36
+ down?: (ctx: MigrationContext) => Promise<void>;
37
+ schemaAfter?: unknown;
38
+ }
39
+ export interface LoadedMigration extends MigrationModule {
40
+ checksum: string;
41
+ }
42
+ export interface AppliedMigrationRecord {
43
+ id: string;
44
+ checksum: string;
45
+ appliedAt: Date;
46
+ }
47
+ export interface RunMigrationsOptions {
48
+ migrations: LoadedMigration[];
49
+ dryRun: boolean;
50
+ connection: mongoose.Connection;
51
+ mongoose: typeof mongoose;
52
+ logger?: MigrationLogger;
53
+ lockTtlMs?: number;
54
+ lockPollMs?: number;
55
+ checkCancellation?: () => Promise<void>;
56
+ addLog?: MigrationContext["addLog"];
57
+ }
58
+ export interface RunMigrationsResult {
59
+ applied: string[];
60
+ skipped: string[];
61
+ dryRun: boolean;
62
+ }
63
+ export interface RunDownMigrationsOptions {
64
+ migrations: LoadedMigration[];
65
+ dryRun: boolean;
66
+ connection: mongoose.Connection;
67
+ mongoose: typeof mongoose;
68
+ logger?: MigrationLogger;
69
+ lockTtlMs?: number;
70
+ lockPollMs?: number;
71
+ steps: number;
72
+ }
73
+ export interface RunDownMigrationsResult {
74
+ reversed: string[];
75
+ dryRun: boolean;
76
+ }
77
+ export interface MigrationStatus {
78
+ applied: AppliedMigrationRecord[];
79
+ pending: Array<{
80
+ id: string;
81
+ checksum: string;
82
+ }>;
83
+ lock: {
84
+ holder: string;
85
+ expiresAt: Date;
86
+ } | null;
87
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MIGRATION_LOCK_ID = exports.MIGRATIONS_COLLECTION = void 0;
4
+ /** Mongo collection that stores applied migration rows and the lock document. */
5
+ exports.MIGRATIONS_COLLECTION = "terreno_migrations";
6
+ /** Reserved `_id` for the migrate lock row (Task 1.2). */
7
+ exports.MIGRATION_LOCK_ID = "_lock";
@@ -0,0 +1,2 @@
1
+ import type { NotificationModel } from "../types/notification";
2
+ export declare const Notification: NotificationModel;
@@ -0,0 +1,62 @@
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.Notification = void 0;
7
+ var mongoose_1 = __importDefault(require("mongoose"));
8
+ var plugins_1 = require("../plugins");
9
+ var syncSeqPlugin_1 = require("../sync/syncSeqPlugin");
10
+ var notificationSchema = new mongoose_1.default.Schema({
11
+ _id: {
12
+ default: function () { return new mongoose_1.default.Types.ObjectId().toHexString(); },
13
+ description: "The document id (string so offline sync clients can mint ids)",
14
+ type: String,
15
+ },
16
+ archivedAt: {
17
+ default: null,
18
+ description: "When the owner archived (dismissed) this notification; null means it is still in the active inbox",
19
+ type: Date,
20
+ },
21
+ body: {
22
+ description: "Main notification message text shown in the inbox",
23
+ required: true,
24
+ trim: true,
25
+ type: String,
26
+ },
27
+ href: {
28
+ description: "Optional deep link or in-app route opened when the user taps the notification",
29
+ trim: true,
30
+ type: String,
31
+ },
32
+ kind: {
33
+ description: "Optional category string used for icons or grouping in the UI",
34
+ trim: true,
35
+ type: String,
36
+ },
37
+ ownerId: {
38
+ description: "The user who owns this inbox row",
39
+ ref: "User",
40
+ required: true,
41
+ type: mongoose_1.default.Schema.Types.ObjectId,
42
+ },
43
+ readAt: {
44
+ default: null,
45
+ description: "When the owner marked this notification as read; null means unread",
46
+ type: Date,
47
+ },
48
+ title: {
49
+ description: "Short headline shown in the inbox list",
50
+ required: true,
51
+ trim: true,
52
+ type: String,
53
+ },
54
+ }, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
55
+ // biome-ignore assist/source/useSortedKeys: ownerId must lead the compound index for owner-scoped inbox queries
56
+ notificationSchema.index({ ownerId: 1, created: -1 });
57
+ notificationSchema.plugin(plugins_1.createdUpdatedPlugin);
58
+ notificationSchema.plugin(plugins_1.isDeletedPlugin);
59
+ notificationSchema.plugin(plugins_1.findExactlyOne);
60
+ notificationSchema.plugin(plugins_1.findOneOrNone);
61
+ notificationSchema.plugin(syncSeqPlugin_1.syncPlugin);
62
+ exports.Notification = mongoose_1.default.model("Notification", notificationSchema);
@@ -0,0 +1,2 @@
1
+ import type { NotificationPreferenceModel } from "../types/notificationPreference";
2
+ export declare const NotificationPreference: NotificationPreferenceModel;
@@ -0,0 +1,49 @@
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.NotificationPreference = void 0;
7
+ var mongoose_1 = __importDefault(require("mongoose"));
8
+ var plugins_1 = require("../plugins");
9
+ var syncSeqPlugin_1 = require("../sync/syncSeqPlugin");
10
+ var notificationPreferenceSchema = new mongoose_1.default.Schema({
11
+ _id: {
12
+ default: function () { return new mongoose_1.default.Types.ObjectId().toHexString(); },
13
+ description: "The document id (string so offline sync clients can mint ids)",
14
+ type: String,
15
+ },
16
+ inapp: {
17
+ default: true,
18
+ description: "Whether in-app inbox rows are created for this user",
19
+ type: Boolean,
20
+ },
21
+ mail: {
22
+ default: true,
23
+ description: "Whether outbound email is sent for notifications",
24
+ type: Boolean,
25
+ },
26
+ ownerId: {
27
+ description: "The user these preferences belong to",
28
+ ref: "User",
29
+ required: true,
30
+ type: mongoose_1.default.Schema.Types.ObjectId,
31
+ },
32
+ push: {
33
+ default: true,
34
+ description: "Whether push notifications are sent for this user",
35
+ type: Boolean,
36
+ },
37
+ sms: {
38
+ default: true,
39
+ description: "Whether SMS messages are sent for this user",
40
+ type: Boolean,
41
+ },
42
+ }, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
43
+ notificationPreferenceSchema.plugin(plugins_1.createdUpdatedPlugin);
44
+ notificationPreferenceSchema.plugin(plugins_1.isDeletedPlugin);
45
+ notificationPreferenceSchema.plugin(plugins_1.findExactlyOne);
46
+ notificationPreferenceSchema.plugin(plugins_1.findOneOrNone);
47
+ notificationPreferenceSchema.plugin(syncSeqPlugin_1.syncPlugin);
48
+ notificationPreferenceSchema.index({ ownerId: 1 }, { partialFilterExpression: { deleted: false }, unique: true });
49
+ exports.NotificationPreference = mongoose_1.default.model("NotificationPreference", notificationPreferenceSchema);
@@ -0,0 +1,40 @@
1
+ import type { Model } from "mongoose";
2
+ import type { NotifyInput } from "../types/notification";
3
+ export interface NotificationsCommsService {
4
+ sendMail: (message: {
5
+ html?: string;
6
+ subject: string;
7
+ text?: string;
8
+ to: string;
9
+ }, options?: {
10
+ userId?: string;
11
+ }) => Promise<unknown>;
12
+ sendPushToUser: (message: {
13
+ body: string;
14
+ title: string;
15
+ userId: string;
16
+ }) => Promise<unknown[]>;
17
+ sendSms: (message: {
18
+ body: string;
19
+ to: string;
20
+ }, options?: {
21
+ userId?: string;
22
+ }) => Promise<unknown>;
23
+ }
24
+ export interface NotificationServiceOptions {
25
+ getComms?: () => NotificationsCommsService;
26
+ retainDays?: number;
27
+ userModel?: Model<{
28
+ email?: string;
29
+ phone?: string;
30
+ }>;
31
+ }
32
+ export interface NotificationService {
33
+ notify: (input: NotifyInput) => Promise<string>;
34
+ sweepExpired: () => Promise<number>;
35
+ }
36
+ export declare const assertValidNotificationReadAt: (readAt: unknown) => void;
37
+ export declare const assertValidNotificationArchivedAt: (archivedAt: unknown) => void;
38
+ export declare const createNotificationService: (options?: NotificationServiceOptions) => NotificationService;
39
+ export declare const configureNotificationService: (options: NotificationServiceOptions) => void;
40
+ export declare const getNotificationService: () => NotificationService;