@hot-updater/server 1.0.0-rc.0 → 1.0.0-rc.14

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 (213) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +1 -1
  2. package/dist/adapters/databaseConstraintErrors.cjs +0 -1
  3. package/dist/adapters/databasePluginUtils.cjs +14 -1
  4. package/dist/adapters/databasePluginUtils.mjs +14 -2
  5. package/dist/adapters/drizzle.cjs +9 -4
  6. package/dist/adapters/drizzle.d.cts +0 -1
  7. package/dist/adapters/drizzle.d.mts +0 -1
  8. package/dist/adapters/drizzle.mjs +10 -4
  9. package/dist/adapters/drizzleCrud.cjs +93 -3
  10. package/dist/adapters/drizzleCrud.mjs +93 -5
  11. package/dist/adapters/drizzleLazyDB.cjs +2 -0
  12. package/dist/adapters/drizzleLazyDB.mjs +2 -0
  13. package/dist/adapters/drizzleQuery.cjs +3 -2
  14. package/dist/adapters/drizzleQuery.mjs +3 -1
  15. package/dist/adapters/kysely.cjs +1 -1
  16. package/dist/adapters/kysely.d.cts +0 -1
  17. package/dist/adapters/kysely.d.mts +0 -1
  18. package/dist/adapters/kysely.mjs +2 -1
  19. package/dist/adapters/kyselyCrud.cjs +44 -5
  20. package/dist/adapters/kyselyCrud.mjs +45 -6
  21. package/dist/adapters/kyselyQuery.cjs +2 -2
  22. package/dist/adapters/kyselyQuery.mjs +2 -1
  23. package/dist/adapters/mongodb.cjs +36 -1
  24. package/dist/adapters/mongodb.d.cts +1 -1
  25. package/dist/adapters/mongodb.d.mts +1 -1
  26. package/dist/adapters/mongodb.mjs +36 -0
  27. package/dist/adapters/mongodbCollections.cjs +2 -0
  28. package/dist/adapters/mongodbCollections.mjs +2 -0
  29. package/dist/adapters/mongodbQuery.cjs +17 -4
  30. package/dist/adapters/mongodbQuery.mjs +17 -4
  31. package/dist/adapters/mongodbReads.cjs +88 -30
  32. package/dist/adapters/mongodbReads.mjs +88 -29
  33. package/dist/adapters/mongodbWrites.cjs +1 -2
  34. package/dist/adapters/mongodbWrites.mjs +1 -1
  35. package/dist/adapters/prisma.cjs +44 -7
  36. package/dist/adapters/prisma.d.cts +0 -1
  37. package/dist/adapters/prisma.d.mts +0 -1
  38. package/dist/adapters/prisma.mjs +44 -6
  39. package/dist/adapters/prismaInsights.cjs +99 -0
  40. package/dist/adapters/prismaInsights.mjs +97 -0
  41. package/dist/adapters/prismaQuery.cjs +0 -1
  42. package/dist/adapters/prismaRows.cjs +7 -8
  43. package/dist/adapters/prismaRows.mjs +8 -8
  44. package/dist/apiKeys.cjs +6 -2
  45. package/dist/apiKeys.d.cts +0 -1
  46. package/dist/apiKeys.d.mts +0 -1
  47. package/dist/apiKeys.mjs +3 -3
  48. package/dist/createHotUpdaterCore.cjs +27 -12
  49. package/dist/createHotUpdaterCore.d.cts +8 -8
  50. package/dist/createHotUpdaterCore.d.mts +8 -8
  51. package/dist/createHotUpdaterCore.mjs +26 -12
  52. package/dist/db/createBundleDiff.cjs +2 -2
  53. package/dist/db/createBundleDiff.d.cts +1 -5
  54. package/dist/db/createBundleDiff.d.mts +1 -5
  55. package/dist/db/databasePluginCore.cjs +0 -1
  56. package/dist/db/fixedMigrator.cjs +2 -2
  57. package/dist/db/fixedMigrator.mjs +2 -2
  58. package/dist/db/fixedMigratorMongo.cjs +6 -4
  59. package/dist/db/fixedMigratorMongo.mjs +6 -4
  60. package/dist/db/index.d.cts +0 -1
  61. package/dist/db/index.d.mts +0 -1
  62. package/dist/db/releaseCatalog.cjs +0 -1
  63. package/dist/db/releaseCatalog.d.cts +0 -1
  64. package/dist/db/releaseCatalog.d.mts +0 -1
  65. package/dist/db/schema/insightsCollation.cjs +14 -0
  66. package/dist/db/schema/insightsCollation.mjs +14 -0
  67. package/dist/db/schema/sql.cjs +11 -7
  68. package/dist/db/schema/sql.mjs +8 -8
  69. package/dist/db/schemaGenerators.cjs +29 -9
  70. package/dist/db/schemaGenerators.mjs +29 -9
  71. package/dist/db/schemaReadiness.cjs +2 -0
  72. package/dist/db/schemaReadiness.d.cts +1 -0
  73. package/dist/db/schemaReadiness.d.mts +1 -0
  74. package/dist/db/schemaReadiness.mjs +2 -0
  75. package/dist/db/sqlMigrationExecution.cjs +1 -1
  76. package/dist/db/sqlMigrationExecution.mjs +1 -0
  77. package/dist/db/types.cjs +3 -1
  78. package/dist/db/types.d.cts +3 -6
  79. package/dist/db/types.d.mts +3 -6
  80. package/dist/db/types.mjs +2 -2
  81. package/dist/db/updateArtifacts.cjs +0 -1
  82. package/dist/handler.cjs +5 -5
  83. package/dist/handler.d.cts +1 -2
  84. package/dist/handler.d.mts +1 -2
  85. package/dist/handler.mjs +5 -5
  86. package/dist/handlerReleaseCatalogRoutes.cjs +0 -1
  87. package/dist/handlerReleaseManagementRoutes.cjs +0 -1
  88. package/dist/handlerTypes.d.cts +0 -1
  89. package/dist/handlerTypes.d.mts +0 -1
  90. package/dist/handlerVersionRoutes.d.cts +1 -0
  91. package/dist/handlerVersionRoutes.d.mts +1 -0
  92. package/dist/index.cjs +2 -2
  93. package/dist/index.d.cts +4 -4
  94. package/dist/index.d.mts +4 -4
  95. package/dist/index.mjs +2 -2
  96. package/dist/insights/domain.d.cts +92 -0
  97. package/dist/insights/domain.d.mts +92 -0
  98. package/dist/insights/errors.cjs +15 -0
  99. package/dist/insights/errors.mjs +14 -0
  100. package/dist/{analytics → insights}/eventInput.cjs +61 -16
  101. package/dist/{analytics → insights}/eventInput.mjs +61 -18
  102. package/dist/insights/provider.cjs +332 -0
  103. package/dist/insights/provider.d.cts +6 -0
  104. package/dist/insights/provider.d.mts +6 -0
  105. package/dist/insights/provider.mjs +332 -0
  106. package/dist/insights/queryInput.cjs +93 -0
  107. package/dist/insights/queryInput.mjs +91 -0
  108. package/dist/insights/routes.cjs +58 -0
  109. package/dist/insights/routes.mjs +56 -0
  110. package/dist/insights/types.d.cts +32 -0
  111. package/dist/insights/types.d.mts +32 -0
  112. package/dist/node.d.cts +0 -1
  113. package/dist/node.d.mts +0 -1
  114. package/dist/package.cjs +1 -1
  115. package/dist/package.mjs +1 -1
  116. package/dist/schema/index.cjs +17 -3
  117. package/dist/schema/index.mjs +4 -4
  118. package/dist/schema/v1_0_0.cjs +64 -35
  119. package/dist/schema/v1_0_0.mjs +64 -36
  120. package/dist/storageAccess.cjs +0 -1
  121. package/dist/types/index.d.cts +0 -1
  122. package/dist/types/index.d.mts +0 -1
  123. package/package.json +7 -7
  124. package/src/adapters/databasePluginTestDatabase.ts +15 -5
  125. package/src/adapters/databasePluginUtils.ts +24 -0
  126. package/src/adapters/drizzle.spec.ts +203 -5
  127. package/src/adapters/drizzle.ts +16 -4
  128. package/src/adapters/drizzleCrud.ts +200 -9
  129. package/src/adapters/drizzleLazyDB.ts +33 -6
  130. package/src/adapters/kysely.spec.ts +62 -0
  131. package/src/adapters/kysely.ts +7 -1
  132. package/src/adapters/kyselyCrud.ts +115 -7
  133. package/src/adapters/kyselyInsights.spec.ts +168 -0
  134. package/src/adapters/mongodb.integration.spec.ts +483 -0
  135. package/src/adapters/mongodb.spec.ts +89 -0
  136. package/src/adapters/mongodb.ts +59 -1
  137. package/src/adapters/mongodbCollections.ts +16 -0
  138. package/src/adapters/mongodbQuery.ts +22 -3
  139. package/src/adapters/mongodbReads.ts +78 -2
  140. package/src/adapters/mongodbTestClient.ts +107 -6
  141. package/src/adapters/mongodbTestFilter.ts +5 -1
  142. package/src/adapters/mongodbWrites.ts +1 -0
  143. package/src/adapters/prisma.spec.ts +270 -0
  144. package/src/adapters/prisma.ts +75 -2
  145. package/src/adapters/prismaInsights.ts +190 -0
  146. package/src/adapters/prismaRows.ts +17 -10
  147. package/src/adapters/prismaTestClient.ts +89 -7
  148. package/src/apiKeys.integration.spec.ts +3 -3
  149. package/src/createHotUpdaterCore.ts +30 -16
  150. package/src/db/databasePluginUpdateCheck.bench.ts +10 -0
  151. package/src/db/fixedMigrator.spec.ts +63 -0
  152. package/src/db/fixedMigratorMongo.spec.ts +70 -9
  153. package/src/db/fixedMigratorMongo.ts +10 -10
  154. package/src/db/index.spec.ts +6 -1
  155. package/src/db/schema/insightsCollation.ts +21 -0
  156. package/src/db/schema/sql.ts +8 -5
  157. package/src/db/schemaGenerators.ts +42 -13
  158. package/src/db/types.spec.ts +11 -3
  159. package/src/db/types.ts +13 -7
  160. package/src/handler-standalone.integration.spec.ts +6 -8
  161. package/src/handler.ts +12 -12
  162. package/src/index.ts +3 -3
  163. package/src/insights/domain.ts +106 -0
  164. package/src/insights/errors.ts +11 -0
  165. package/src/{analytics → insights}/eventInput.ts +66 -16
  166. package/src/insights/insights.integration.spec.ts +475 -0
  167. package/src/insights/provider.spec.ts +396 -0
  168. package/src/insights/provider.ts +611 -0
  169. package/src/insights/queryInput.ts +142 -0
  170. package/src/insights/routes.ts +104 -0
  171. package/src/insights/types.ts +55 -0
  172. package/src/schema/dsl-all-versions.spec.ts +1 -1
  173. package/src/schema/v1_0_0.spec.ts +77 -3
  174. package/src/schema/v1_0_0.ts +83 -37
  175. package/dist/analytics/bounded/activeOverview.cjs +0 -87
  176. package/dist/analytics/bounded/activeOverview.mjs +0 -85
  177. package/dist/analytics/bounded/installationSearch.cjs +0 -42
  178. package/dist/analytics/bounded/installationSearch.mjs +0 -42
  179. package/dist/analytics/bounded/persistence.cjs +0 -39
  180. package/dist/analytics/bounded/persistence.mjs +0 -38
  181. package/dist/analytics/bounded/provider.cjs +0 -170
  182. package/dist/analytics/bounded/provider.d.cts +0 -7
  183. package/dist/analytics/bounded/provider.d.mts +0 -7
  184. package/dist/analytics/bounded/provider.mjs +0 -170
  185. package/dist/analytics/bounded/scan.cjs +0 -144
  186. package/dist/analytics/bounded/scan.mjs +0 -138
  187. package/dist/analytics/domain.d.cts +0 -121
  188. package/dist/analytics/domain.d.mts +0 -121
  189. package/dist/analytics/errors.cjs +0 -22
  190. package/dist/analytics/errors.mjs +0 -20
  191. package/dist/analytics/persistence.d.cts +0 -6
  192. package/dist/analytics/persistence.d.mts +0 -6
  193. package/dist/analytics/queryInput.cjs +0 -58
  194. package/dist/analytics/queryInput.mjs +0 -55
  195. package/dist/analytics/routes.cjs +0 -63
  196. package/dist/analytics/routes.mjs +0 -61
  197. package/dist/analytics/types.d.cts +0 -17
  198. package/dist/analytics/types.d.mts +0 -17
  199. package/src/analytics/analytics.integration.spec.ts +0 -144
  200. package/src/analytics/bounded/activeOverview.spec.ts +0 -189
  201. package/src/analytics/bounded/activeOverview.ts +0 -148
  202. package/src/analytics/bounded/installationSearch.spec.ts +0 -130
  203. package/src/analytics/bounded/installationSearch.ts +0 -69
  204. package/src/analytics/bounded/persistence.ts +0 -43
  205. package/src/analytics/bounded/provider.spec.ts +0 -219
  206. package/src/analytics/bounded/provider.ts +0 -229
  207. package/src/analytics/bounded/scan.ts +0 -235
  208. package/src/analytics/domain.ts +0 -139
  209. package/src/analytics/errors.ts +0 -18
  210. package/src/analytics/persistence.ts +0 -10
  211. package/src/analytics/queryInput.ts +0 -102
  212. package/src/analytics/routes.ts +0 -123
  213. package/src/analytics/types.ts +0 -44
@@ -1,8 +1,8 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_databasePluginUtils = require("./databasePluginUtils.cjs");
3
2
  const require_mongodbCollections = require("./mongodbCollections.cjs");
4
3
  const require_mongodbQuery = require("./mongodbQuery.cjs");
5
4
  let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
5
+ let _hot_updater_plugin_core_internal = require("@hot-updater/plugin-core/internal");
6
6
  //#region src/adapters/mongodbReads.ts
7
7
  const findMongoRows = async (collections, input, session) => {
8
8
  if (input.limit === 0) return [];
@@ -15,10 +15,13 @@ const findMongoRows = async (collections, input, session) => {
15
15
  ...require_mongodbCollections.mongoSessionOptions(session)
16
16
  }).skip(input.offset).limit(input.limit);
17
17
  if (rawOrderBy === void 0) return cursor.toArray();
18
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.bundles.find(require_mongodbCollections.activeBundleFilter(require_mongodbQuery.createMongoBundleWhere(input.where)), {
19
- projection: require_mongodbCollections.WITHOUT_INTERNAL_FIELDS,
20
- ...require_mongodbCollections.mongoSessionOptions(session)
21
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
18
+ if (needsInMemoryOrder) {
19
+ const rows = await collections.bundles.find(require_mongodbCollections.activeBundleFilter(require_mongodbQuery.createMongoBundleWhere(input.where)), {
20
+ projection: require_mongodbCollections.WITHOUT_INTERNAL_FIELDS,
21
+ ...require_mongodbCollections.mongoSessionOptions(session)
22
+ }).toArray();
23
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
24
+ }
22
25
  const sort = require_mongodbQuery.createMongoSort(input);
23
26
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
24
27
  }
@@ -28,23 +31,33 @@ const findMongoRows = async (collections, input, session) => {
28
31
  ...require_mongodbCollections.mongoSessionOptions(session)
29
32
  }).skip(input.offset).limit(input.limit);
30
33
  if (rawOrderBy === void 0) return cursor.toArray();
31
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.bundlePatches.find(require_mongodbQuery.createMongoPatchWhere(input.where), {
32
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
33
- ...require_mongodbCollections.mongoSessionOptions(session)
34
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
34
+ if (needsInMemoryOrder) {
35
+ const rows = await collections.bundlePatches.find(require_mongodbQuery.createMongoPatchWhere(input.where), {
36
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
37
+ ...require_mongodbCollections.mongoSessionOptions(session)
38
+ }).toArray();
39
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
40
+ }
35
41
  const sort = require_mongodbQuery.createMongoSort(input);
36
42
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
37
43
  }
38
44
  case "bundle_events": {
39
45
  const cursor = collections.bundleEvents.find(require_mongodbQuery.createMongoEventWhere(input.where), {
40
46
  projection: require_mongodbCollections.WITHOUT_MONGO_ID,
41
- ...require_mongodbCollections.mongoSessionOptions(session)
47
+ ...require_mongodbCollections.mongoSessionOptions(session),
48
+ collation: { locale: "simple" },
49
+ readPreference: "primary"
42
50
  }).skip(input.offset).limit(input.limit);
43
51
  if (rawOrderBy === void 0) return cursor.toArray();
44
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.bundleEvents.find(require_mongodbQuery.createMongoEventWhere(input.where), {
45
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
46
- ...require_mongodbCollections.mongoSessionOptions(session)
47
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
52
+ if (needsInMemoryOrder) {
53
+ const rows = await collections.bundleEvents.find(require_mongodbQuery.createMongoEventWhere(input.where), {
54
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
55
+ ...require_mongodbCollections.mongoSessionOptions(session),
56
+ collation: { locale: "simple" },
57
+ readPreference: "primary"
58
+ }).toArray();
59
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
60
+ }
48
61
  const sort = require_mongodbQuery.createMongoSort(input);
49
62
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
50
63
  }
@@ -54,10 +67,13 @@ const findMongoRows = async (collections, input, session) => {
54
67
  ...require_mongodbCollections.mongoSessionOptions(session)
55
68
  }).skip(input.offset).limit(input.limit);
56
69
  if (rawOrderBy === void 0) return cursor.toArray();
57
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.apiKeys.find(require_mongodbQuery.createMongoApiKeyWhere(input.where), {
58
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
59
- ...require_mongodbCollections.mongoSessionOptions(session)
60
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
70
+ if (needsInMemoryOrder) {
71
+ const rows = await collections.apiKeys.find(require_mongodbQuery.createMongoApiKeyWhere(input.where), {
72
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
73
+ ...require_mongodbCollections.mongoSessionOptions(session)
74
+ }).toArray();
75
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
76
+ }
61
77
  const sort = require_mongodbQuery.createMongoSort(input);
62
78
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
63
79
  }
@@ -67,10 +83,13 @@ const findMongoRows = async (collections, input, session) => {
67
83
  ...require_mongodbCollections.mongoSessionOptions(session)
68
84
  }).skip(input.offset).limit(input.limit);
69
85
  if (rawOrderBy === void 0) return cursor.toArray();
70
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.channels.find(require_mongodbQuery.createMongoChannelWhere(input.where), {
71
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
72
- ...require_mongodbCollections.mongoSessionOptions(session)
73
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
86
+ if (needsInMemoryOrder) {
87
+ const rows = await collections.channels.find(require_mongodbQuery.createMongoChannelWhere(input.where), {
88
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
89
+ ...require_mongodbCollections.mongoSessionOptions(session)
90
+ }).toArray();
91
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
92
+ }
74
93
  const sort = require_mongodbQuery.createMongoSort(input);
75
94
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
76
95
  }
@@ -80,10 +99,13 @@ const findMongoRows = async (collections, input, session) => {
80
99
  ...require_mongodbCollections.mongoSessionOptions(session)
81
100
  }).skip(input.offset).limit(input.limit);
82
101
  if (rawOrderBy === void 0) return cursor.toArray();
83
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.releases.find(require_mongodbQuery.createMongoReleaseWhere(input.where), {
84
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
85
- ...require_mongodbCollections.mongoSessionOptions(session)
86
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
102
+ if (needsInMemoryOrder) {
103
+ const rows = await collections.releases.find(require_mongodbQuery.createMongoReleaseWhere(input.where), {
104
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
105
+ ...require_mongodbCollections.mongoSessionOptions(session)
106
+ }).toArray();
107
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
108
+ }
87
109
  const sort = require_mongodbQuery.createMongoSort(input);
88
110
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
89
111
  }
@@ -93,22 +115,58 @@ const findMongoRows = async (collections, input, session) => {
93
115
  ...require_mongodbCollections.mongoSessionOptions(session)
94
116
  }).skip(input.offset).limit(input.limit);
95
117
  if (rawOrderBy === void 0) return cursor.toArray();
96
- if (needsInMemoryOrder) return require_databasePluginUtils.sortRowsByOrder(await collections.releaseCatalogs.find(require_mongodbQuery.createMongoReleaseCatalogWhere(input.where), {
97
- projection: require_mongodbCollections.WITHOUT_MONGO_ID,
98
- ...require_mongodbCollections.mongoSessionOptions(session)
99
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
118
+ if (needsInMemoryOrder) {
119
+ const rows = await collections.releaseCatalogs.find(require_mongodbQuery.createMongoReleaseCatalogWhere(input.where), {
120
+ projection: require_mongodbCollections.WITHOUT_MONGO_ID,
121
+ ...require_mongodbCollections.mongoSessionOptions(session)
122
+ }).toArray();
123
+ return require_databasePluginUtils.sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
124
+ }
100
125
  const sort = require_mongodbQuery.createMongoSort(input);
101
126
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
102
127
  }
103
128
  }
104
129
  };
105
130
  const createMongoReads = (collections, session) => ({
131
+ async findLatestInsightsEvents(input) {
132
+ return collections.bundleEventHeads.aggregate([
133
+ { $match: require_mongodbQuery.createMongoEventWhere((0, _hot_updater_plugin_core_internal.latestInsightsWhere)(input)) },
134
+ { $sort: { install_id: 1 } },
135
+ { $limit: "installId" in input ? 1 : input.limit },
136
+ { $lookup: {
137
+ from: "bundle_events",
138
+ localField: "id",
139
+ foreignField: "id",
140
+ as: "event"
141
+ } },
142
+ { $unwind: "$event" },
143
+ { $replaceRoot: { newRoot: "$event" } },
144
+ { $project: require_mongodbCollections.WITHOUT_MONGO_ID }
145
+ ], {
146
+ ...require_mongodbCollections.mongoSessionOptions(session),
147
+ collation: { locale: "simple" },
148
+ readPreference: "primary"
149
+ }).toArray();
150
+ },
151
+ async countLatestInsightsEvents(input) {
152
+ return (await collections.bundleEventHeads.aggregate([{ $match: { $or: (0, _hot_updater_plugin_core_internal.latestInsightsCountGroups)(input).map(require_mongodbQuery.createMongoEventWhere) } }, { $count: "count" }], {
153
+ ...require_mongodbCollections.mongoSessionOptions(session),
154
+ collation: { locale: "simple" },
155
+ readPreference: "primary"
156
+ }).toArray())[0]?.count ?? 0;
157
+ },
106
158
  count: async (input) => {
107
159
  if (input.distinct !== void 0) throw new _hot_updater_plugin_core.DatabasePluginInputError("invalid-operation");
108
160
  switch (input.model) {
109
161
  case "bundles": return collections.bundles.countDocuments(require_mongodbCollections.activeBundleFilter(require_mongodbQuery.createMongoBundleWhere(input.where)), require_mongodbCollections.mongoSessionOptions(session));
110
162
  case "bundle_patches": return collections.bundlePatches.countDocuments(require_mongodbQuery.createMongoPatchWhere(input.where), require_mongodbCollections.mongoSessionOptions(session));
111
163
  case "releases": return collections.releases.countDocuments(require_mongodbQuery.createMongoReleaseWhere(input.where), require_mongodbCollections.mongoSessionOptions(session));
164
+ case "bundle_events": return collections.bundleEvents.countDocuments(require_mongodbQuery.createMongoEventWhere(input.where), {
165
+ ...require_mongodbCollections.mongoSessionOptions(session),
166
+ collation: { locale: "simple" },
167
+ readPreference: "primary",
168
+ ...session === void 0 ? { readConcern: { level: "snapshot" } } : {}
169
+ });
112
170
  }
113
171
  },
114
172
  findOne: async (input) => {
@@ -2,6 +2,7 @@ import { hasNullOrderOverrides, sortRowsByOrder } from "./databasePluginUtils.mj
2
2
  import { WITHOUT_INTERNAL_FIELDS, WITHOUT_MONGO_ID, activeBundleFilter, mongoSessionOptions } from "./mongodbCollections.mjs";
3
3
  import { createMongoApiKeyWhere, createMongoBundleWhere, createMongoChannelWhere, createMongoEventWhere, createMongoPatchWhere, createMongoReleaseCatalogWhere, createMongoReleaseWhere, createMongoSort } from "./mongodbQuery.mjs";
4
4
  import { DatabasePluginInputError } from "@hot-updater/plugin-core";
5
+ import { latestInsightsCountGroups, latestInsightsWhere } from "@hot-updater/plugin-core/internal";
5
6
  //#region src/adapters/mongodbReads.ts
6
7
  const findMongoRows = async (collections, input, session) => {
7
8
  if (input.limit === 0) return [];
@@ -14,10 +15,13 @@ const findMongoRows = async (collections, input, session) => {
14
15
  ...mongoSessionOptions(session)
15
16
  }).skip(input.offset).limit(input.limit);
16
17
  if (rawOrderBy === void 0) return cursor.toArray();
17
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.bundles.find(activeBundleFilter(createMongoBundleWhere(input.where)), {
18
- projection: WITHOUT_INTERNAL_FIELDS,
19
- ...mongoSessionOptions(session)
20
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
18
+ if (needsInMemoryOrder) {
19
+ const rows = await collections.bundles.find(activeBundleFilter(createMongoBundleWhere(input.where)), {
20
+ projection: WITHOUT_INTERNAL_FIELDS,
21
+ ...mongoSessionOptions(session)
22
+ }).toArray();
23
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
24
+ }
21
25
  const sort = createMongoSort(input);
22
26
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
23
27
  }
@@ -27,23 +31,33 @@ const findMongoRows = async (collections, input, session) => {
27
31
  ...mongoSessionOptions(session)
28
32
  }).skip(input.offset).limit(input.limit);
29
33
  if (rawOrderBy === void 0) return cursor.toArray();
30
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.bundlePatches.find(createMongoPatchWhere(input.where), {
31
- projection: WITHOUT_MONGO_ID,
32
- ...mongoSessionOptions(session)
33
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
34
+ if (needsInMemoryOrder) {
35
+ const rows = await collections.bundlePatches.find(createMongoPatchWhere(input.where), {
36
+ projection: WITHOUT_MONGO_ID,
37
+ ...mongoSessionOptions(session)
38
+ }).toArray();
39
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
40
+ }
34
41
  const sort = createMongoSort(input);
35
42
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
36
43
  }
37
44
  case "bundle_events": {
38
45
  const cursor = collections.bundleEvents.find(createMongoEventWhere(input.where), {
39
46
  projection: WITHOUT_MONGO_ID,
40
- ...mongoSessionOptions(session)
47
+ ...mongoSessionOptions(session),
48
+ collation: { locale: "simple" },
49
+ readPreference: "primary"
41
50
  }).skip(input.offset).limit(input.limit);
42
51
  if (rawOrderBy === void 0) return cursor.toArray();
43
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.bundleEvents.find(createMongoEventWhere(input.where), {
44
- projection: WITHOUT_MONGO_ID,
45
- ...mongoSessionOptions(session)
46
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
52
+ if (needsInMemoryOrder) {
53
+ const rows = await collections.bundleEvents.find(createMongoEventWhere(input.where), {
54
+ projection: WITHOUT_MONGO_ID,
55
+ ...mongoSessionOptions(session),
56
+ collation: { locale: "simple" },
57
+ readPreference: "primary"
58
+ }).toArray();
59
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
60
+ }
47
61
  const sort = createMongoSort(input);
48
62
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
49
63
  }
@@ -53,10 +67,13 @@ const findMongoRows = async (collections, input, session) => {
53
67
  ...mongoSessionOptions(session)
54
68
  }).skip(input.offset).limit(input.limit);
55
69
  if (rawOrderBy === void 0) return cursor.toArray();
56
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.apiKeys.find(createMongoApiKeyWhere(input.where), {
57
- projection: WITHOUT_MONGO_ID,
58
- ...mongoSessionOptions(session)
59
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
70
+ if (needsInMemoryOrder) {
71
+ const rows = await collections.apiKeys.find(createMongoApiKeyWhere(input.where), {
72
+ projection: WITHOUT_MONGO_ID,
73
+ ...mongoSessionOptions(session)
74
+ }).toArray();
75
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
76
+ }
60
77
  const sort = createMongoSort(input);
61
78
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
62
79
  }
@@ -66,10 +83,13 @@ const findMongoRows = async (collections, input, session) => {
66
83
  ...mongoSessionOptions(session)
67
84
  }).skip(input.offset).limit(input.limit);
68
85
  if (rawOrderBy === void 0) return cursor.toArray();
69
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.channels.find(createMongoChannelWhere(input.where), {
70
- projection: WITHOUT_MONGO_ID,
71
- ...mongoSessionOptions(session)
72
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
86
+ if (needsInMemoryOrder) {
87
+ const rows = await collections.channels.find(createMongoChannelWhere(input.where), {
88
+ projection: WITHOUT_MONGO_ID,
89
+ ...mongoSessionOptions(session)
90
+ }).toArray();
91
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
92
+ }
73
93
  const sort = createMongoSort(input);
74
94
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
75
95
  }
@@ -79,10 +99,13 @@ const findMongoRows = async (collections, input, session) => {
79
99
  ...mongoSessionOptions(session)
80
100
  }).skip(input.offset).limit(input.limit);
81
101
  if (rawOrderBy === void 0) return cursor.toArray();
82
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.releases.find(createMongoReleaseWhere(input.where), {
83
- projection: WITHOUT_MONGO_ID,
84
- ...mongoSessionOptions(session)
85
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
102
+ if (needsInMemoryOrder) {
103
+ const rows = await collections.releases.find(createMongoReleaseWhere(input.where), {
104
+ projection: WITHOUT_MONGO_ID,
105
+ ...mongoSessionOptions(session)
106
+ }).toArray();
107
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
108
+ }
86
109
  const sort = createMongoSort(input);
87
110
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
88
111
  }
@@ -92,22 +115,58 @@ const findMongoRows = async (collections, input, session) => {
92
115
  ...mongoSessionOptions(session)
93
116
  }).skip(input.offset).limit(input.limit);
94
117
  if (rawOrderBy === void 0) return cursor.toArray();
95
- if (needsInMemoryOrder) return sortRowsByOrder(await collections.releaseCatalogs.find(createMongoReleaseCatalogWhere(input.where), {
96
- projection: WITHOUT_MONGO_ID,
97
- ...mongoSessionOptions(session)
98
- }).toArray(), rawOrderBy).slice(input.offset, input.offset + input.limit);
118
+ if (needsInMemoryOrder) {
119
+ const rows = await collections.releaseCatalogs.find(createMongoReleaseCatalogWhere(input.where), {
120
+ projection: WITHOUT_MONGO_ID,
121
+ ...mongoSessionOptions(session)
122
+ }).toArray();
123
+ return sortRowsByOrder(rows, rawOrderBy).slice(input.offset, input.offset + input.limit);
124
+ }
99
125
  const sort = createMongoSort(input);
100
126
  return sort === void 0 ? cursor.toArray() : cursor.sort(sort).toArray();
101
127
  }
102
128
  }
103
129
  };
104
130
  const createMongoReads = (collections, session) => ({
131
+ async findLatestInsightsEvents(input) {
132
+ return collections.bundleEventHeads.aggregate([
133
+ { $match: createMongoEventWhere(latestInsightsWhere(input)) },
134
+ { $sort: { install_id: 1 } },
135
+ { $limit: "installId" in input ? 1 : input.limit },
136
+ { $lookup: {
137
+ from: "bundle_events",
138
+ localField: "id",
139
+ foreignField: "id",
140
+ as: "event"
141
+ } },
142
+ { $unwind: "$event" },
143
+ { $replaceRoot: { newRoot: "$event" } },
144
+ { $project: WITHOUT_MONGO_ID }
145
+ ], {
146
+ ...mongoSessionOptions(session),
147
+ collation: { locale: "simple" },
148
+ readPreference: "primary"
149
+ }).toArray();
150
+ },
151
+ async countLatestInsightsEvents(input) {
152
+ return (await collections.bundleEventHeads.aggregate([{ $match: { $or: latestInsightsCountGroups(input).map(createMongoEventWhere) } }, { $count: "count" }], {
153
+ ...mongoSessionOptions(session),
154
+ collation: { locale: "simple" },
155
+ readPreference: "primary"
156
+ }).toArray())[0]?.count ?? 0;
157
+ },
105
158
  count: async (input) => {
106
159
  if (input.distinct !== void 0) throw new DatabasePluginInputError("invalid-operation");
107
160
  switch (input.model) {
108
161
  case "bundles": return collections.bundles.countDocuments(activeBundleFilter(createMongoBundleWhere(input.where)), mongoSessionOptions(session));
109
162
  case "bundle_patches": return collections.bundlePatches.countDocuments(createMongoPatchWhere(input.where), mongoSessionOptions(session));
110
163
  case "releases": return collections.releases.countDocuments(createMongoReleaseWhere(input.where), mongoSessionOptions(session));
164
+ case "bundle_events": return collections.bundleEvents.countDocuments(createMongoEventWhere(input.where), {
165
+ ...mongoSessionOptions(session),
166
+ collation: { locale: "simple" },
167
+ readPreference: "primary",
168
+ ...session === void 0 ? { readConcern: { level: "snapshot" } } : {}
169
+ });
111
170
  }
112
171
  },
113
172
  findOne: async (input) => {
@@ -1,10 +1,9 @@
1
- require("../_virtual/_rolldown/runtime.cjs");
2
1
  const require_mongodbCollections = require("./mongodbCollections.cjs");
3
2
  const require_mongodbQuery = require("./mongodbQuery.cjs");
4
3
  let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
5
4
  //#region src/adapters/mongodbWrites.ts
6
5
  const assertPatchReferences = async (bundles, patch, session) => {
7
- const ids = Array.from(new Set([patch.bundle_id, patch.base_bundle_id]));
6
+ const ids = Array.from(/* @__PURE__ */ new Set([patch.bundle_id, patch.base_bundle_id]));
8
7
  if (await bundles.countDocuments(require_mongodbCollections.activeBundleFilter({ id: { $in: ids } }), require_mongodbCollections.mongoSessionOptions(session)) !== ids.length) throw new require_mongodbCollections.MongoAdapterConstraintError(`patch "${patch.id}" references a missing bundle`);
9
8
  };
10
9
  const createMongoWrites = (collections, session) => ({
@@ -3,7 +3,7 @@ import { createMongoApiKeyWhere, createMongoBundleWhere, createMongoChannelWhere
3
3
  import { createUUIDv7 } from "@hot-updater/plugin-core";
4
4
  //#region src/adapters/mongodbWrites.ts
5
5
  const assertPatchReferences = async (bundles, patch, session) => {
6
- const ids = Array.from(new Set([patch.bundle_id, patch.base_bundle_id]));
6
+ const ids = Array.from(/* @__PURE__ */ new Set([patch.bundle_id, patch.base_bundle_id]));
7
7
  if (await bundles.countDocuments(activeBundleFilter({ id: { $in: ids } }), mongoSessionOptions(session)) !== ids.length) throw new MongoAdapterConstraintError(`patch "${patch.id}" references a missing bundle`);
8
8
  };
9
9
  const createMongoWrites = (collections, session) => ({
@@ -1,23 +1,23 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- require("../_virtual/_rolldown/runtime.cjs");
3
2
  const require_registry = require("../db/schema/registry.cjs");
4
3
  const require_schemaGenerators = require("../db/schemaGenerators.cjs");
5
4
  const require_databaseConstraintErrors = require("./databaseConstraintErrors.cjs");
6
5
  const require_databasePluginUtils = require("./databasePluginUtils.cjs");
7
- const require_prismaQuery = require("./prismaQuery.cjs");
8
6
  const require_prismaRows = require("./prismaRows.cjs");
7
+ const require_prismaInsights = require("./prismaInsights.cjs");
8
+ const require_prismaQuery = require("./prismaQuery.cjs");
9
9
  let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
10
10
  let _hot_updater_plugin_core_internal = require("@hot-updater/plugin-core/internal");
11
11
  //#region src/adapters/prisma.ts
12
12
  const hasCallbackTransaction = (client) => "$transaction" in client && typeof client.$transaction === "function";
13
- const runPrismaTransaction = (client, isolationLevel, callback) => {
13
+ const runPrismaTransaction = (client, isolationLevel, callback, retryUniqueConflict = false) => {
14
14
  const execute = () => isolationLevel === "serializable" ? client.$transaction(callback, { isolationLevel: "Serializable" }) : client.$transaction(callback);
15
15
  if (isolationLevel === "default") return execute();
16
16
  return (async () => {
17
17
  for (let attempt = 0;; attempt += 1) try {
18
18
  return await execute();
19
19
  } catch (error) {
20
- if (attempt >= 2 || typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2034") throw error;
20
+ if (attempt >= 2 || typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2034" && !(error.code === "P2010" && "meta" in error && typeof error.meta === "object" && error.meta !== null && "code" in error.meta && error.meta.code === "40001") && !(retryUniqueConflict && error.code === "P2002")) throw error;
21
21
  }
22
22
  })();
23
23
  };
@@ -45,7 +45,7 @@ const findMany = async (client, input, provider) => {
45
45
  }
46
46
  };
47
47
  const assertPatchReferences = async (client, bundleId, baseBundleId) => {
48
- const ids = Array.from(new Set([bundleId, baseBundleId]));
48
+ const ids = Array.from(/* @__PURE__ */ new Set([bundleId, baseBundleId]));
49
49
  if (await require_prismaRows.getPrismaDelegate(client, "bundles").count({ where: { id: { in: ids } } }) !== ids.length) throw new require_prismaRows.PrismaAdapterError("patch references a missing bundle");
50
50
  };
51
51
  const createCrudImplementation = (client, provider, relationMode) => ({
@@ -155,7 +155,8 @@ const createCrudImplementation = (client, provider, relationMode) => ({
155
155
  },
156
156
  delete: async (input) => {
157
157
  if (input.model === "bundles" && relationMode === "prisma") {
158
- const ids = require_prismaRows.parsePrismaRows(await require_prismaRows.getPrismaDelegate(client, "bundles").findMany({ where: require_prismaQuery.createPrismaWhere(input.where, provider) }), require_prismaRows.parsePrismaBundleRow).map(({ id }) => id);
158
+ const rows = await require_prismaRows.getPrismaDelegate(client, "bundles").findMany({ where: require_prismaQuery.createPrismaWhere(input.where, provider) });
159
+ const ids = require_prismaRows.parsePrismaRows(rows, require_prismaRows.parsePrismaBundleRow).map(({ id }) => id);
159
160
  if (ids.length === 0) return;
160
161
  await require_prismaRows.getPrismaDelegate(client, "bundle_patches").deleteMany({ where: { OR: [{ bundle_id: { in: ids } }, { base_bundle_id: { in: ids } }] } });
161
162
  }
@@ -188,6 +189,29 @@ const createPrismaImplementation = (client, relationMode, provider) => {
188
189
  const crud = createCrudImplementation(client, provider, relationMode);
189
190
  const implementation = {
190
191
  ...crud,
192
+ async recordInsights({ event }) {
193
+ if (!hasCallbackTransaction(client)) return require_prismaInsights.recordPrismaEventWithCte(client, provider, event);
194
+ const events = require_prismaRows.getPrismaDelegate(client, "bundle_events");
195
+ try {
196
+ await runPrismaTransaction(client, provider === "cockroachdb" ? "serializable" : "default", async (transaction) => {
197
+ await require_prismaRows.getPrismaDelegate(transaction, "bundle_events").create({ data: event });
198
+ await require_prismaInsights.updatePrismaEventHead(transaction, provider, event);
199
+ });
200
+ } catch (error) {
201
+ if (typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2002") throw error;
202
+ if (await events.findFirst({ where: { id: event.id } }) === null) throw error;
203
+ }
204
+ },
205
+ async findLatestInsightsEvents(input) {
206
+ const rows = await require_prismaInsights.queryPrismaLatestEvents(client, provider, input);
207
+ if (typeof rows === "number") throw new require_prismaRows.PrismaAdapterError("invalid latest-event rows");
208
+ return rows;
209
+ },
210
+ async countLatestInsightsEvents(input) {
211
+ const count = await require_prismaInsights.queryPrismaLatestEvents(client, provider, input);
212
+ if (typeof count !== "number") throw new require_prismaRows.PrismaAdapterError("invalid latest-event count");
213
+ return count;
214
+ },
191
215
  deleteChannel: (input) => {
192
216
  if (!hasCallbackTransaction(client)) throw new require_prismaRows.PrismaAdapterError("channel deletion requires callback transactions");
193
217
  return runPrismaTransaction(client, "serializable", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).deleteChannel(input));
@@ -212,9 +236,22 @@ const createPrismaImplementation = (client, relationMode, provider) => {
212
236
  const prismaAdapter = (config) => {
213
237
  assertPrismaProvider(config.provider);
214
238
  const adapter = (0, _hot_updater_plugin_core_internal.createDatabasePluginAdapter)("prisma", createPrismaImplementation(config.prisma, config.relationMode ?? "foreign-keys", config.provider));
239
+ const unsupportedMssqlInsights = async () => {
240
+ throw new require_prismaRows.PrismaAdapterError("SQL Server Insights is unsupported: string keys pad trailing spaces and do not provide the required exact UTF-8 identity ordering");
241
+ };
242
+ const insights = config.provider === "mssql" ? {
243
+ recordEvent: unsupportedMssqlInsights,
244
+ listEvents: unsupportedMssqlInsights,
245
+ findLatestEvents: unsupportedMssqlInsights,
246
+ countLatestEvents: unsupportedMssqlInsights,
247
+ countEvents: unsupportedMssqlInsights
248
+ } : adapter.models.insights;
215
249
  return Object.assign((0, _hot_updater_plugin_core.createDatabasePlugin)({
216
250
  name: "prisma",
217
- models: adapter.models,
251
+ models: {
252
+ ...adapter.models,
253
+ insights
254
+ },
218
255
  commit: adapter.commit
219
256
  }), {
220
257
  adapterName: "prisma",
@@ -1,5 +1,4 @@
1
1
  import { DatabaseAdapterWithCapabilities, ORMSQLProvider } from "../db/types.cjs";
2
-
3
2
  //#region src/adapters/prisma.d.ts
4
3
  type PrismaRelationMode = "prisma" | "foreign-keys";
5
4
  interface PrismaConfig {
@@ -1,5 +1,4 @@
1
1
  import { DatabaseAdapterWithCapabilities, ORMSQLProvider } from "../db/types.mjs";
2
-
3
2
  //#region src/adapters/prisma.d.ts
4
3
  type PrismaRelationMode = "prisma" | "foreign-keys";
5
4
  interface PrismaConfig {
@@ -2,20 +2,21 @@ import { getHotUpdaterSchemaVersion, hotUpdaterSchema } from "../db/schema/regis
2
2
  import { generatePrismaSchema } from "../db/schemaGenerators.mjs";
3
3
  import { isChannelDeleteReferencedError, translateChannelDeleteError } from "./databaseConstraintErrors.mjs";
4
4
  import { hasNullOrderOverrides, sortRowsByOrder } from "./databasePluginUtils.mjs";
5
- import { createPrismaOrderBy, createPrismaWhere } from "./prismaQuery.mjs";
6
5
  import { PrismaAdapterError, getPrismaDelegate, parsePrismaApiKeyRow, parsePrismaBundleEventRow, parsePrismaBundleRow, parsePrismaChannelRow, parsePrismaPatchRow, parsePrismaReleaseCatalogRow, parsePrismaReleaseRow, parsePrismaRows } from "./prismaRows.mjs";
6
+ import { queryPrismaLatestEvents, recordPrismaEventWithCte, updatePrismaEventHead } from "./prismaInsights.mjs";
7
+ import { createPrismaOrderBy, createPrismaWhere } from "./prismaQuery.mjs";
7
8
  import { DatabasePluginInputError, createDatabasePlugin } from "@hot-updater/plugin-core";
8
9
  import { createDatabasePluginAdapter } from "@hot-updater/plugin-core/internal";
9
10
  //#region src/adapters/prisma.ts
10
11
  const hasCallbackTransaction = (client) => "$transaction" in client && typeof client.$transaction === "function";
11
- const runPrismaTransaction = (client, isolationLevel, callback) => {
12
+ const runPrismaTransaction = (client, isolationLevel, callback, retryUniqueConflict = false) => {
12
13
  const execute = () => isolationLevel === "serializable" ? client.$transaction(callback, { isolationLevel: "Serializable" }) : client.$transaction(callback);
13
14
  if (isolationLevel === "default") return execute();
14
15
  return (async () => {
15
16
  for (let attempt = 0;; attempt += 1) try {
16
17
  return await execute();
17
18
  } catch (error) {
18
- if (attempt >= 2 || typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2034") throw error;
19
+ if (attempt >= 2 || typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2034" && !(error.code === "P2010" && "meta" in error && typeof error.meta === "object" && error.meta !== null && "code" in error.meta && error.meta.code === "40001") && !(retryUniqueConflict && error.code === "P2002")) throw error;
19
20
  }
20
21
  })();
21
22
  };
@@ -43,7 +44,7 @@ const findMany = async (client, input, provider) => {
43
44
  }
44
45
  };
45
46
  const assertPatchReferences = async (client, bundleId, baseBundleId) => {
46
- const ids = Array.from(new Set([bundleId, baseBundleId]));
47
+ const ids = Array.from(/* @__PURE__ */ new Set([bundleId, baseBundleId]));
47
48
  if (await getPrismaDelegate(client, "bundles").count({ where: { id: { in: ids } } }) !== ids.length) throw new PrismaAdapterError("patch references a missing bundle");
48
49
  };
49
50
  const createCrudImplementation = (client, provider, relationMode) => ({
@@ -153,7 +154,8 @@ const createCrudImplementation = (client, provider, relationMode) => ({
153
154
  },
154
155
  delete: async (input) => {
155
156
  if (input.model === "bundles" && relationMode === "prisma") {
156
- const ids = parsePrismaRows(await getPrismaDelegate(client, "bundles").findMany({ where: createPrismaWhere(input.where, provider) }), parsePrismaBundleRow).map(({ id }) => id);
157
+ const rows = await getPrismaDelegate(client, "bundles").findMany({ where: createPrismaWhere(input.where, provider) });
158
+ const ids = parsePrismaRows(rows, parsePrismaBundleRow).map(({ id }) => id);
157
159
  if (ids.length === 0) return;
158
160
  await getPrismaDelegate(client, "bundle_patches").deleteMany({ where: { OR: [{ bundle_id: { in: ids } }, { base_bundle_id: { in: ids } }] } });
159
161
  }
@@ -186,6 +188,29 @@ const createPrismaImplementation = (client, relationMode, provider) => {
186
188
  const crud = createCrudImplementation(client, provider, relationMode);
187
189
  const implementation = {
188
190
  ...crud,
191
+ async recordInsights({ event }) {
192
+ if (!hasCallbackTransaction(client)) return recordPrismaEventWithCte(client, provider, event);
193
+ const events = getPrismaDelegate(client, "bundle_events");
194
+ try {
195
+ await runPrismaTransaction(client, provider === "cockroachdb" ? "serializable" : "default", async (transaction) => {
196
+ await getPrismaDelegate(transaction, "bundle_events").create({ data: event });
197
+ await updatePrismaEventHead(transaction, provider, event);
198
+ });
199
+ } catch (error) {
200
+ if (typeof error !== "object" || error === null || !("code" in error) || error.code !== "P2002") throw error;
201
+ if (await events.findFirst({ where: { id: event.id } }) === null) throw error;
202
+ }
203
+ },
204
+ async findLatestInsightsEvents(input) {
205
+ const rows = await queryPrismaLatestEvents(client, provider, input);
206
+ if (typeof rows === "number") throw new PrismaAdapterError("invalid latest-event rows");
207
+ return rows;
208
+ },
209
+ async countLatestInsightsEvents(input) {
210
+ const count = await queryPrismaLatestEvents(client, provider, input);
211
+ if (typeof count !== "number") throw new PrismaAdapterError("invalid latest-event count");
212
+ return count;
213
+ },
189
214
  deleteChannel: (input) => {
190
215
  if (!hasCallbackTransaction(client)) throw new PrismaAdapterError("channel deletion requires callback transactions");
191
216
  return runPrismaTransaction(client, "serializable", (transactionClient) => createCrudImplementation(transactionClient, provider, relationMode).deleteChannel(input));
@@ -210,9 +235,22 @@ const createPrismaImplementation = (client, relationMode, provider) => {
210
235
  const prismaAdapter = (config) => {
211
236
  assertPrismaProvider(config.provider);
212
237
  const adapter = createDatabasePluginAdapter("prisma", createPrismaImplementation(config.prisma, config.relationMode ?? "foreign-keys", config.provider));
238
+ const unsupportedMssqlInsights = async () => {
239
+ throw new PrismaAdapterError("SQL Server Insights is unsupported: string keys pad trailing spaces and do not provide the required exact UTF-8 identity ordering");
240
+ };
241
+ const insights = config.provider === "mssql" ? {
242
+ recordEvent: unsupportedMssqlInsights,
243
+ listEvents: unsupportedMssqlInsights,
244
+ findLatestEvents: unsupportedMssqlInsights,
245
+ countLatestEvents: unsupportedMssqlInsights,
246
+ countEvents: unsupportedMssqlInsights
247
+ } : adapter.models.insights;
213
248
  return Object.assign(createDatabasePlugin({
214
249
  name: "prisma",
215
- models: adapter.models,
250
+ models: {
251
+ ...adapter.models,
252
+ insights
253
+ },
216
254
  commit: adapter.commit
217
255
  }), {
218
256
  adapterName: "prisma",