@hot-updater/server 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/adapters/drizzle.cjs +9 -0
- package/dist/adapters/drizzle.d.cts +1 -0
- package/dist/adapters/drizzle.d.ts +1 -0
- package/dist/adapters/drizzle.js +3 -0
- package/dist/adapters/kysely.cjs +9 -0
- package/dist/adapters/kysely.d.cts +1 -0
- package/dist/adapters/kysely.d.ts +1 -0
- package/dist/adapters/kysely.js +3 -0
- package/dist/adapters/mongodb.cjs +9 -0
- package/dist/adapters/mongodb.d.cts +1 -0
- package/dist/adapters/mongodb.d.ts +1 -0
- package/dist/adapters/mongodb.js +3 -0
- package/dist/adapters/prisma.cjs +9 -0
- package/dist/adapters/prisma.d.cts +1 -0
- package/dist/adapters/prisma.d.ts +1 -0
- package/dist/adapters/prisma.js +3 -0
- package/dist/adapters/typeorm.cjs +9 -0
- package/dist/adapters/typeorm.d.cts +1 -0
- package/dist/adapters/typeorm.d.ts +1 -0
- package/dist/adapters/typeorm.js +3 -0
- package/dist/calculatePagination.cjs +27 -0
- package/dist/calculatePagination.js +26 -0
- package/dist/db/index.cjs +289 -0
- package/dist/db/index.d.cts +62 -0
- package/dist/db/index.d.ts +62 -0
- package/dist/db/index.js +284 -0
- package/dist/handler.cjs +141 -0
- package/dist/handler.d.cts +37 -0
- package/dist/handler.d.ts +37 -0
- package/dist/handler.js +140 -0
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/schema/v1.cjs +26 -0
- package/dist/schema/v1.js +24 -0
- package/dist/types/index.d.cts +20 -0
- package/dist/types/index.d.ts +20 -0
- package/package.json +73 -0
- package/src/adapters/drizzle.ts +1 -0
- package/src/adapters/kysely.ts +1 -0
- package/src/adapters/mongodb.ts +1 -0
- package/src/adapters/prisma.ts +1 -0
- package/src/adapters/typeorm.ts +1 -0
- package/src/calculatePagination.ts +34 -0
- package/src/db/index.spec.ts +231 -0
- package/src/db/index.ts +490 -0
- package/src/handler-standalone-integration.spec.ts +341 -0
- package/src/handler.ts +231 -0
- package/src/index.ts +3 -0
- package/src/schema/v1.ts +22 -0
- package/src/types/index.ts +21 -0
package/src/db/index.ts
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppUpdateInfo,
|
|
3
|
+
AppVersionGetBundlesArgs,
|
|
4
|
+
Bundle,
|
|
5
|
+
FingerprintGetBundlesArgs,
|
|
6
|
+
GetBundlesArgs,
|
|
7
|
+
Platform,
|
|
8
|
+
UpdateInfo,
|
|
9
|
+
} from "@hot-updater/core";
|
|
10
|
+
import { NIL_UUID } from "@hot-updater/core";
|
|
11
|
+
import type { StoragePlugin } from "@hot-updater/plugin-core";
|
|
12
|
+
import { filterCompatibleAppVersions } from "@hot-updater/plugin-core";
|
|
13
|
+
import type { InferFumaDB } from "fumadb";
|
|
14
|
+
import { fumadb } from "fumadb";
|
|
15
|
+
import { calculatePagination } from "../calculatePagination";
|
|
16
|
+
import { createHandler } from "../handler";
|
|
17
|
+
import { v1 } from "../schema/v1";
|
|
18
|
+
import type { PaginationInfo } from "../types";
|
|
19
|
+
|
|
20
|
+
export const HotUpdaterDB = fumadb({
|
|
21
|
+
namespace: "hot-updater",
|
|
22
|
+
schemas: [v1],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export type HotUpdaterClient = InferFumaDB<typeof HotUpdaterDB>;
|
|
26
|
+
|
|
27
|
+
export type DatabaseAdapter = Parameters<typeof HotUpdaterDB.client>[0];
|
|
28
|
+
|
|
29
|
+
export interface DatabaseAPI {
|
|
30
|
+
getBundleById(id: string): Promise<Bundle | null>;
|
|
31
|
+
getUpdateInfo(args: GetBundlesArgs): Promise<UpdateInfo | null>;
|
|
32
|
+
getAppUpdateInfo(args: GetBundlesArgs): Promise<AppUpdateInfo | null>;
|
|
33
|
+
getChannels(): Promise<string[]>;
|
|
34
|
+
getBundles(options: {
|
|
35
|
+
where?: { channel?: string; platform?: string };
|
|
36
|
+
limit: number;
|
|
37
|
+
offset: number;
|
|
38
|
+
}): Promise<{ data: Bundle[]; pagination: PaginationInfo }>;
|
|
39
|
+
insertBundle(bundle: Bundle): Promise<void>;
|
|
40
|
+
updateBundleById(bundleId: string, newBundle: Partial<Bundle>): Promise<void>;
|
|
41
|
+
deleteBundleById(bundleId: string): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type HotUpdaterAPI = DatabaseAPI & {
|
|
45
|
+
handler: (request: Request) => Promise<Response>;
|
|
46
|
+
createMigrator: () => ReturnType<HotUpdaterClient["createMigrator"]>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type StoragePluginFactory = (args: { cwd: string }) => StoragePlugin;
|
|
50
|
+
|
|
51
|
+
export interface HotUpdaterOptions {
|
|
52
|
+
database: DatabaseAdapter;
|
|
53
|
+
storagePlugins?: (StoragePlugin | StoragePluginFactory)[];
|
|
54
|
+
basePath?: string;
|
|
55
|
+
cwd?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function hotUpdater(options: HotUpdaterOptions): HotUpdaterAPI {
|
|
59
|
+
const client = HotUpdaterDB.client(options.database);
|
|
60
|
+
const cwd = options.cwd ?? process.cwd();
|
|
61
|
+
|
|
62
|
+
// Initialize storage plugins - call factories if they are functions
|
|
63
|
+
const storagePlugins = (options?.storagePlugins ?? []).map((plugin) =>
|
|
64
|
+
typeof plugin === "function" ? plugin({ cwd }) : plugin,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const resolveFileUrl = async (
|
|
68
|
+
storageUri: string | null,
|
|
69
|
+
): Promise<string | null> => {
|
|
70
|
+
if (!storageUri) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const url = new URL(storageUri);
|
|
74
|
+
const protocol = url.protocol.replace(":", "");
|
|
75
|
+
if (protocol === "http" || protocol === "https") {
|
|
76
|
+
return storageUri;
|
|
77
|
+
}
|
|
78
|
+
const plugin = storagePlugins.find((p) => p.supportedProtocol === protocol);
|
|
79
|
+
|
|
80
|
+
if (!plugin) {
|
|
81
|
+
throw new Error(`No storage plugin for protocol: ${protocol}`);
|
|
82
|
+
}
|
|
83
|
+
const { fileUrl } = await plugin.getDownloadUrl(storageUri);
|
|
84
|
+
if (!fileUrl) {
|
|
85
|
+
throw new Error("Storage plugin returned empty fileUrl");
|
|
86
|
+
}
|
|
87
|
+
return fileUrl;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const api = {
|
|
91
|
+
async getBundleById(id: string): Promise<Bundle | null> {
|
|
92
|
+
const version = await client.version();
|
|
93
|
+
const orm = client.orm(version);
|
|
94
|
+
const result = await orm.findFirst("bundles", {
|
|
95
|
+
select: [
|
|
96
|
+
"id",
|
|
97
|
+
"platform",
|
|
98
|
+
"should_force_update",
|
|
99
|
+
"enabled",
|
|
100
|
+
"file_hash",
|
|
101
|
+
"git_commit_hash",
|
|
102
|
+
"message",
|
|
103
|
+
"channel",
|
|
104
|
+
"storage_uri",
|
|
105
|
+
"target_app_version",
|
|
106
|
+
"fingerprint_hash",
|
|
107
|
+
"metadata",
|
|
108
|
+
],
|
|
109
|
+
where: (b) => b.and(b.isNotNull("id"), b("id", "=", id)),
|
|
110
|
+
});
|
|
111
|
+
if (!result) return null;
|
|
112
|
+
const bundle: Bundle = {
|
|
113
|
+
id: result.id,
|
|
114
|
+
platform: result.platform as Platform,
|
|
115
|
+
shouldForceUpdate: Boolean(result.should_force_update),
|
|
116
|
+
enabled: Boolean(result.enabled),
|
|
117
|
+
fileHash: result.file_hash,
|
|
118
|
+
gitCommitHash: result.git_commit_hash ?? null,
|
|
119
|
+
message: result.message ?? null,
|
|
120
|
+
channel: result.channel,
|
|
121
|
+
storageUri: result.storage_uri,
|
|
122
|
+
targetAppVersion: result.target_app_version ?? null,
|
|
123
|
+
fingerprintHash: result.fingerprint_hash ?? null,
|
|
124
|
+
};
|
|
125
|
+
return bundle;
|
|
126
|
+
},
|
|
127
|
+
async getUpdateInfo(args: GetBundlesArgs): Promise<UpdateInfo | null> {
|
|
128
|
+
const version = await client.version();
|
|
129
|
+
const orm = client.orm(version);
|
|
130
|
+
|
|
131
|
+
type UpdateSelectRow = {
|
|
132
|
+
id: string;
|
|
133
|
+
should_force_update: boolean;
|
|
134
|
+
message: string | null;
|
|
135
|
+
storage_uri: string | null;
|
|
136
|
+
file_hash: string;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const toUpdateInfo = (
|
|
140
|
+
row: UpdateSelectRow,
|
|
141
|
+
status: "UPDATE" | "ROLLBACK",
|
|
142
|
+
): UpdateInfo => ({
|
|
143
|
+
id: row.id,
|
|
144
|
+
shouldForceUpdate:
|
|
145
|
+
status === "ROLLBACK" ? true : Boolean(row.should_force_update),
|
|
146
|
+
message: row.message ?? null,
|
|
147
|
+
status,
|
|
148
|
+
storageUri: row.storage_uri ?? null,
|
|
149
|
+
fileHash: row.file_hash ?? null,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const INIT_BUNDLE_ROLLBACK_UPDATE_INFO: UpdateInfo = {
|
|
153
|
+
id: NIL_UUID,
|
|
154
|
+
message: null,
|
|
155
|
+
shouldForceUpdate: true,
|
|
156
|
+
status: "ROLLBACK",
|
|
157
|
+
storageUri: null,
|
|
158
|
+
fileHash: null,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const appVersionStrategy = async ({
|
|
162
|
+
platform,
|
|
163
|
+
appVersion,
|
|
164
|
+
bundleId,
|
|
165
|
+
minBundleId = NIL_UUID,
|
|
166
|
+
channel = "production",
|
|
167
|
+
}: AppVersionGetBundlesArgs): Promise<UpdateInfo | null> => {
|
|
168
|
+
const versionRows = await orm.findMany("bundles", {
|
|
169
|
+
select: ["target_app_version"],
|
|
170
|
+
where: (b) => b.and(b("platform", "=", platform)),
|
|
171
|
+
});
|
|
172
|
+
const allTargetVersions = Array.from(
|
|
173
|
+
new Set(
|
|
174
|
+
(versionRows ?? [])
|
|
175
|
+
.map((r) => r.target_app_version)
|
|
176
|
+
.filter((v): v is string => Boolean(v)),
|
|
177
|
+
),
|
|
178
|
+
);
|
|
179
|
+
const compatibleVersions = filterCompatibleAppVersions(
|
|
180
|
+
allTargetVersions,
|
|
181
|
+
appVersion,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const baseRows =
|
|
185
|
+
compatibleVersions.length === 0
|
|
186
|
+
? []
|
|
187
|
+
: await orm.findMany("bundles", {
|
|
188
|
+
select: [
|
|
189
|
+
"id",
|
|
190
|
+
"should_force_update",
|
|
191
|
+
"message",
|
|
192
|
+
"storage_uri",
|
|
193
|
+
"file_hash",
|
|
194
|
+
"channel",
|
|
195
|
+
"target_app_version",
|
|
196
|
+
"enabled",
|
|
197
|
+
],
|
|
198
|
+
where: (b) =>
|
|
199
|
+
b.and(
|
|
200
|
+
b("enabled", "=", true),
|
|
201
|
+
b("platform", "=", platform),
|
|
202
|
+
b("id", ">=", minBundleId ?? NIL_UUID),
|
|
203
|
+
b("channel", "=", channel),
|
|
204
|
+
b.isNotNull("target_app_version"),
|
|
205
|
+
),
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const candidates = (baseRows ?? []).filter((r) =>
|
|
209
|
+
r.target_app_version
|
|
210
|
+
? compatibleVersions.includes(r.target_app_version)
|
|
211
|
+
: false,
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const byIdDesc = (a: { id: string }, b: { id: string }) =>
|
|
215
|
+
b.id.localeCompare(a.id);
|
|
216
|
+
const sorted = (candidates ?? []).slice().sort(byIdDesc);
|
|
217
|
+
|
|
218
|
+
const latestCandidate = sorted[0] ?? null;
|
|
219
|
+
const currentBundle = sorted.find((b) => b.id === bundleId);
|
|
220
|
+
const updateCandidate =
|
|
221
|
+
sorted.find((b) => b.id.localeCompare(bundleId) > 0) ?? null;
|
|
222
|
+
const rollbackCandidate =
|
|
223
|
+
sorted.find((b) => b.id.localeCompare(bundleId) < 0) ?? null;
|
|
224
|
+
|
|
225
|
+
if (bundleId === NIL_UUID) {
|
|
226
|
+
if (latestCandidate && latestCandidate.id !== bundleId) {
|
|
227
|
+
return toUpdateInfo(latestCandidate, "UPDATE");
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (currentBundle) {
|
|
233
|
+
if (
|
|
234
|
+
latestCandidate &&
|
|
235
|
+
latestCandidate.id.localeCompare(currentBundle.id) > 0
|
|
236
|
+
) {
|
|
237
|
+
return toUpdateInfo(latestCandidate, "UPDATE");
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (updateCandidate) {
|
|
243
|
+
return toUpdateInfo(updateCandidate, "UPDATE");
|
|
244
|
+
}
|
|
245
|
+
if (rollbackCandidate) {
|
|
246
|
+
return toUpdateInfo(rollbackCandidate, "ROLLBACK");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (minBundleId && bundleId.localeCompare(minBundleId) <= 0) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
return INIT_BUNDLE_ROLLBACK_UPDATE_INFO;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const fingerprintStrategy = async ({
|
|
256
|
+
platform,
|
|
257
|
+
fingerprintHash,
|
|
258
|
+
bundleId,
|
|
259
|
+
minBundleId = NIL_UUID,
|
|
260
|
+
channel = "production",
|
|
261
|
+
}: FingerprintGetBundlesArgs): Promise<UpdateInfo | null> => {
|
|
262
|
+
const candidates = await orm.findMany("bundles", {
|
|
263
|
+
select: [
|
|
264
|
+
"id",
|
|
265
|
+
"should_force_update",
|
|
266
|
+
"message",
|
|
267
|
+
"storage_uri",
|
|
268
|
+
"file_hash",
|
|
269
|
+
"channel",
|
|
270
|
+
"fingerprint_hash",
|
|
271
|
+
"enabled",
|
|
272
|
+
],
|
|
273
|
+
where: (b) =>
|
|
274
|
+
b.and(
|
|
275
|
+
b("enabled", "=", true),
|
|
276
|
+
b("platform", "=", platform),
|
|
277
|
+
b("id", ">=", minBundleId ?? NIL_UUID),
|
|
278
|
+
b("channel", "=", channel),
|
|
279
|
+
b("fingerprint_hash", "=", fingerprintHash),
|
|
280
|
+
),
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const byIdDesc = (a: { id: string }, b: { id: string }) =>
|
|
284
|
+
b.id.localeCompare(a.id);
|
|
285
|
+
const sorted = (candidates ?? []).slice().sort(byIdDesc);
|
|
286
|
+
|
|
287
|
+
const latestCandidate = sorted[0] ?? null;
|
|
288
|
+
const currentBundle = sorted.find((b) => b.id === bundleId);
|
|
289
|
+
const updateCandidate =
|
|
290
|
+
sorted.find((b) => b.id.localeCompare(bundleId) > 0) ?? null;
|
|
291
|
+
const rollbackCandidate =
|
|
292
|
+
sorted.find((b) => b.id.localeCompare(bundleId) < 0) ?? null;
|
|
293
|
+
|
|
294
|
+
if (bundleId === NIL_UUID) {
|
|
295
|
+
if (latestCandidate && latestCandidate.id !== bundleId) {
|
|
296
|
+
return toUpdateInfo(latestCandidate, "UPDATE");
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (currentBundle) {
|
|
302
|
+
if (
|
|
303
|
+
latestCandidate &&
|
|
304
|
+
latestCandidate.id.localeCompare(currentBundle.id) > 0
|
|
305
|
+
) {
|
|
306
|
+
return toUpdateInfo(latestCandidate, "UPDATE");
|
|
307
|
+
}
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (updateCandidate) {
|
|
312
|
+
return toUpdateInfo(updateCandidate, "UPDATE");
|
|
313
|
+
}
|
|
314
|
+
if (rollbackCandidate) {
|
|
315
|
+
return toUpdateInfo(rollbackCandidate, "ROLLBACK");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (minBundleId && bundleId.localeCompare(minBundleId) <= 0) {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
return INIT_BUNDLE_ROLLBACK_UPDATE_INFO;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
if (args._updateStrategy === "appVersion") {
|
|
325
|
+
return appVersionStrategy(args);
|
|
326
|
+
}
|
|
327
|
+
if (args._updateStrategy === "fingerprint") {
|
|
328
|
+
return fingerprintStrategy(args);
|
|
329
|
+
}
|
|
330
|
+
return null;
|
|
331
|
+
},
|
|
332
|
+
|
|
333
|
+
async getAppUpdateInfo(
|
|
334
|
+
args: GetBundlesArgs,
|
|
335
|
+
): Promise<AppUpdateInfo | null> {
|
|
336
|
+
const info = await this.getUpdateInfo(args);
|
|
337
|
+
if (!info) return null;
|
|
338
|
+
const { storageUri, ...rest } = info as UpdateInfo & {
|
|
339
|
+
storageUri: string | null;
|
|
340
|
+
};
|
|
341
|
+
const fileUrl = await resolveFileUrl(storageUri ?? null);
|
|
342
|
+
return { ...rest, fileUrl };
|
|
343
|
+
},
|
|
344
|
+
|
|
345
|
+
async getChannels(): Promise<string[]> {
|
|
346
|
+
const version = await client.version();
|
|
347
|
+
const orm = client.orm(version);
|
|
348
|
+
const rows = await orm.findMany("bundles", {
|
|
349
|
+
select: ["channel"],
|
|
350
|
+
where: (b) => b.isNotNull("channel"),
|
|
351
|
+
});
|
|
352
|
+
const set = new Set(rows?.map((r) => r.channel) ?? []);
|
|
353
|
+
return Array.from(set);
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
async getBundles(options: {
|
|
357
|
+
where?: { channel?: string; platform?: string };
|
|
358
|
+
limit: number;
|
|
359
|
+
offset: number;
|
|
360
|
+
}): Promise<{ data: Bundle[]; pagination: PaginationInfo }> {
|
|
361
|
+
const version = await client.version();
|
|
362
|
+
const orm = client.orm(version);
|
|
363
|
+
const { where, limit, offset } = options;
|
|
364
|
+
|
|
365
|
+
const rows = await orm.findMany("bundles", {
|
|
366
|
+
select: [
|
|
367
|
+
"id",
|
|
368
|
+
"platform",
|
|
369
|
+
"should_force_update",
|
|
370
|
+
"enabled",
|
|
371
|
+
"file_hash",
|
|
372
|
+
"git_commit_hash",
|
|
373
|
+
"message",
|
|
374
|
+
"channel",
|
|
375
|
+
"storage_uri",
|
|
376
|
+
"target_app_version",
|
|
377
|
+
"fingerprint_hash",
|
|
378
|
+
"metadata",
|
|
379
|
+
],
|
|
380
|
+
where: (b) =>
|
|
381
|
+
b.and(
|
|
382
|
+
b.isNotNull("id"),
|
|
383
|
+
where?.channel
|
|
384
|
+
? b("channel", "=", where.channel)
|
|
385
|
+
: b.isNotNull("id"),
|
|
386
|
+
where?.platform
|
|
387
|
+
? b("platform", "=", where.platform)
|
|
388
|
+
: b.isNotNull("id"),
|
|
389
|
+
),
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const all: Bundle[] = rows
|
|
393
|
+
.map(
|
|
394
|
+
(r): Bundle => ({
|
|
395
|
+
id: r.id,
|
|
396
|
+
platform: r.platform as Platform,
|
|
397
|
+
shouldForceUpdate: Boolean(r.should_force_update),
|
|
398
|
+
enabled: Boolean(r.enabled),
|
|
399
|
+
fileHash: r.file_hash,
|
|
400
|
+
gitCommitHash: r.git_commit_hash ?? null,
|
|
401
|
+
message: r.message ?? null,
|
|
402
|
+
channel: r.channel,
|
|
403
|
+
storageUri: r.storage_uri,
|
|
404
|
+
targetAppVersion: r.target_app_version ?? null,
|
|
405
|
+
fingerprintHash: r.fingerprint_hash ?? null,
|
|
406
|
+
}),
|
|
407
|
+
)
|
|
408
|
+
.sort((a, b) => b.id.localeCompare(a.id));
|
|
409
|
+
|
|
410
|
+
const total = all.length;
|
|
411
|
+
const sliced = all.slice(offset, offset + limit);
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
data: sliced,
|
|
415
|
+
pagination: calculatePagination(total, { limit, offset }),
|
|
416
|
+
};
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
async insertBundle(bundle: Bundle): Promise<void> {
|
|
420
|
+
const version = await client.version();
|
|
421
|
+
const orm = client.orm(version);
|
|
422
|
+
const values = {
|
|
423
|
+
id: bundle.id,
|
|
424
|
+
platform: bundle.platform,
|
|
425
|
+
should_force_update: bundle.shouldForceUpdate,
|
|
426
|
+
enabled: bundle.enabled,
|
|
427
|
+
file_hash: bundle.fileHash,
|
|
428
|
+
git_commit_hash: bundle.gitCommitHash,
|
|
429
|
+
message: bundle.message,
|
|
430
|
+
channel: bundle.channel,
|
|
431
|
+
storage_uri: bundle.storageUri,
|
|
432
|
+
target_app_version: bundle.targetAppVersion,
|
|
433
|
+
fingerprint_hash: bundle.fingerprintHash,
|
|
434
|
+
metadata: bundle.metadata ?? {},
|
|
435
|
+
};
|
|
436
|
+
const { id, ...updateValues } = values;
|
|
437
|
+
await orm.upsert("bundles", {
|
|
438
|
+
where: (b) => b("id", "=", id),
|
|
439
|
+
create: values,
|
|
440
|
+
update: updateValues,
|
|
441
|
+
});
|
|
442
|
+
},
|
|
443
|
+
|
|
444
|
+
async updateBundleById(
|
|
445
|
+
bundleId: string,
|
|
446
|
+
newBundle: Partial<Bundle>,
|
|
447
|
+
): Promise<void> {
|
|
448
|
+
const version = await client.version();
|
|
449
|
+
const orm = client.orm(version);
|
|
450
|
+
const current = await this.getBundleById(bundleId);
|
|
451
|
+
if (!current) throw new Error("targetBundleId not found");
|
|
452
|
+
const merged: Bundle = { ...current, ...newBundle };
|
|
453
|
+
const values = {
|
|
454
|
+
id: merged.id,
|
|
455
|
+
platform: merged.platform,
|
|
456
|
+
should_force_update: merged.shouldForceUpdate,
|
|
457
|
+
enabled: merged.enabled,
|
|
458
|
+
file_hash: merged.fileHash,
|
|
459
|
+
git_commit_hash: merged.gitCommitHash,
|
|
460
|
+
message: merged.message,
|
|
461
|
+
channel: merged.channel,
|
|
462
|
+
storage_uri: merged.storageUri,
|
|
463
|
+
target_app_version: merged.targetAppVersion,
|
|
464
|
+
fingerprint_hash: merged.fingerprintHash,
|
|
465
|
+
metadata: merged.metadata ?? {},
|
|
466
|
+
};
|
|
467
|
+
const { id: id2, ...updateValues2 } = values;
|
|
468
|
+
await orm.upsert("bundles", {
|
|
469
|
+
where: (b) => b("id", "=", id2),
|
|
470
|
+
create: values,
|
|
471
|
+
update: updateValues2,
|
|
472
|
+
});
|
|
473
|
+
},
|
|
474
|
+
|
|
475
|
+
async deleteBundleById(bundleId: string): Promise<void> {
|
|
476
|
+
const version = await client.version();
|
|
477
|
+
const orm = client.orm(version);
|
|
478
|
+
await orm.deleteMany("bundles", { where: (b) => b("id", "=", bundleId) });
|
|
479
|
+
},
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
return {
|
|
483
|
+
...api,
|
|
484
|
+
handler: createHandler(
|
|
485
|
+
api,
|
|
486
|
+
options?.basePath ? { basePath: options.basePath } : {},
|
|
487
|
+
),
|
|
488
|
+
createMigrator: () => client.createMigrator(),
|
|
489
|
+
};
|
|
490
|
+
}
|