@coffer-org/server 1.12.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ import { registerMcpHttp } from "./mcp-http.js";
24
24
  import { registerOAuthApi } from "./oauth-api.js";
25
25
  import { baseUrl } from "./public-url.js";
26
26
  import { discoverPluginAssets, discoverRuntime } from "./plugin-discovery.js";
27
- import { checkLatestVersion, resolveUpdateTarget, resolveAllUpdateTargets, resolveRuntimeTarget, runNpmInstall, } from "./plugin-updates.js";
27
+ import { checkLatestVersion, resolveUpdateTarget, resolveAllUpdateTargets, resolveBaseTargets, resolveRuntimeTarget, runNpmInstall, } from "./plugin-updates.js";
28
28
  import { buildClientSchema } from "./schema-api.js";
29
29
  import { recordList, recordGet, recordCreate, recordUpdate, recordDelete, rowMatch, UnknownTypeError } from "./records-api.js";
30
30
  import { maskTree, preserveTree } from "./field-masking.js";
@@ -294,6 +294,26 @@ app.post('/api/plugins/update-all', async (req, reply) => {
294
294
  reply.send({ ok: true, updated });
295
295
  setTimeout(() => process.exit(0), 500);
296
296
  });
297
+ app.post('/api/system/update', async (req, reply) => {
298
+ if (!requireAdmin(req, reply))
299
+ return;
300
+ const assets = await discoverPluginAssets();
301
+ const runtime = await discoverRuntime();
302
+ const core = assets.find((a) => a.id === 'core');
303
+ const [coreLatest, runtimeLatest] = await Promise.all([
304
+ core && !core.local ? checkLatestVersion(core.packageName) : Promise.resolve(null),
305
+ runtime && !runtime.local ? checkLatestVersion(runtime.packageName) : Promise.resolve(null),
306
+ ]);
307
+ const targets = resolveBaseTargets(assets, runtime, coreLatest, runtimeLatest);
308
+ if (targets.length === 0)
309
+ return reply.code(400).send({ error: 'no_update_available' });
310
+ const result = await runNpmInstall(targets.map((t) => `${t.packageName}@${t.to}`), process.cwd());
311
+ if (!result.ok) {
312
+ return reply.code(500).send({ error: 'install_failed', stderr: result.stderr });
313
+ }
314
+ reply.send({ ok: true, updated: targets.map(({ id, from, to }) => ({ id, from, to })) });
315
+ setTimeout(() => process.exit(0), 500);
316
+ });
297
317
  app.get('/api/plugins/:id/settings', async (req, reply) => {
298
318
  if (!requireAdmin(req, reply))
299
319
  return;
@@ -33,7 +33,9 @@ export async function introspectTable(em, table) {
33
33
  return {
34
34
  exists: !!t,
35
35
  columns: new Set(t ? t.getColumns().map((c) => c.name) : []),
36
- indexes: t ? t.getIndexes().map((i) => ({ columnNames: i.columnNames ?? [], unique: !!i.unique, primary: !!i.primary })) : [],
36
+ indexes: t
37
+ ? t.getIndexes().map((i) => ({ columnNames: i.columnNames ?? [], unique: !!i.unique, primary: !!i.primary }))
38
+ : [],
37
39
  };
38
40
  }
39
41
  export function makeTable(em, table) {
@@ -87,7 +89,9 @@ export function makeTable(em, table) {
87
89
  const info = (await conn.execute(`PRAGMA index_info(${q(idx.name)})`));
88
90
  if (!info.some((c) => c.name === column))
89
91
  continue;
90
- const meta = (await conn.execute(`SELECT sql FROM sqlite_master WHERE type='index' AND name=?`, [idx.name]));
92
+ const meta = (await conn.execute(`SELECT sql FROM sqlite_master WHERE type='index' AND name=?`, [
93
+ idx.name,
94
+ ]));
91
95
  const sql = meta[0]?.sql;
92
96
  if (sql == null)
93
97
  continue;
@@ -132,6 +136,49 @@ export function makeTable(em, table) {
132
136
  throw e;
133
137
  }
134
138
  },
139
+ async convert(c) {
140
+ const info = await introspectTable(em, table);
141
+ if (!info.exists)
142
+ return;
143
+ if (!c.from.some((f) => info.columns.has(f)))
144
+ return;
145
+ const sources = new Set(c.from);
146
+ for (const t of c.to) {
147
+ if (info.columns.has(t.name) && !sources.has(t.name)) {
148
+ throw new Error(`[migrations] ${table}.${t.name}: цільова колонка convert вже існує і не є джерелом — використай changeType/renameColumn`);
149
+ }
150
+ }
151
+ const sqlite = dialectOf(em) === 'sqlite';
152
+ const idCol = sqlite ? 'rowid' : 'id';
153
+ const colType = sqlite ? sqlType : pgType;
154
+ const present = c.from.filter((f) => info.columns.has(f));
155
+ await conn.execute(`BEGIN`);
156
+ try {
157
+ for (const t of c.to) {
158
+ if (!info.columns.has(t.name))
159
+ await conn.execute(`ALTER TABLE ${T} ADD COLUMN ${q(t.name)} ${colType(t.type)}`);
160
+ }
161
+ const select = present.map((f) => q(f)).join(', ');
162
+ const rows = (await conn.execute(`SELECT ${idCol} AS __rid, ${select} FROM ${T}`));
163
+ const setSql = c.to.map((t) => `${q(t.name)} = ?`).join(', ');
164
+ for (const r of rows) {
165
+ const src = {};
166
+ for (const f of c.from)
167
+ src[f] = r[f] ?? null;
168
+ const dst = c.map(src);
169
+ const params = c.to.map((t) => {
170
+ const v = dst[t.name];
171
+ return v === undefined || (typeof v === 'number' && Number.isNaN(v)) ? null : v;
172
+ });
173
+ await conn.execute(`UPDATE ${T} SET ${setSql} WHERE ${idCol} = ?`, [...params, r['__rid']]);
174
+ }
175
+ await conn.execute(`COMMIT`);
176
+ }
177
+ catch (e) {
178
+ await conn.execute(`ROLLBACK`);
179
+ throw e;
180
+ }
181
+ },
135
182
  };
136
183
  }
137
184
  export function validateMigrations(pluginId, list) {
@@ -1,6 +1,6 @@
1
1
  import type { EntityManager } from '@mikro-orm/core';
2
2
  import type { z } from 'zod';
3
- import type { ColumnType } from '@coffer-org/sdk/fields';
3
+ import type { ColumnType, ColumnConversion } from '@coffer-org/sdk/fields';
4
4
  import { type Logger } from '@coffer-org/sdk/logger';
5
5
  export type { BackgroundTask } from './background-scheduler.ts';
6
6
  export interface TableOps {
@@ -12,6 +12,7 @@ export interface TableOps {
12
12
  default?: string | number | boolean | null;
13
13
  transform?: (old: unknown) => unknown;
14
14
  }): Promise<void>;
15
+ convert(c: ColumnConversion): Promise<void>;
15
16
  }
16
17
  export interface MigrationCtx {
17
18
  table(name: string): TableOps;
@@ -16,6 +16,11 @@ export interface AllUpdateTarget {
16
16
  to: string;
17
17
  }
18
18
  export declare function resolveAllUpdateTargets(assets: PluginAssetRecord[], latestById: Map<string, string | null>): AllUpdateTarget[];
19
+ export declare function resolveBaseTargets(assets: PluginAssetRecord[], runtime: {
20
+ packageName: string;
21
+ installedVersion: string;
22
+ local: boolean;
23
+ } | null, coreLatest: string | null, runtimeLatest: string | null): AllUpdateTarget[];
19
24
  export interface RuntimeUpdateTarget {
20
25
  packageName: string;
21
26
  from: string;
@@ -49,6 +49,16 @@ export function resolveAllUpdateTargets(assets, latestById) {
49
49
  return [{ id: rec.id, packageName: rec.packageName, from: rec.version, to: latest }];
50
50
  });
51
51
  }
52
+ export function resolveBaseTargets(assets, runtime, coreLatest, runtimeLatest) {
53
+ const core = assets.find((a) => a.id === 'core');
54
+ const rt = resolveRuntimeTarget(runtime, runtimeLatest);
55
+ return [
56
+ ...(core && !core.local && coreLatest && coreLatest !== core.version
57
+ ? [{ id: 'core', packageName: core.packageName, from: core.version, to: coreLatest }]
58
+ : []),
59
+ ...(rt ? [{ id: 'runtime', packageName: rt.packageName, from: rt.from, to: rt.to }] : []),
60
+ ];
61
+ }
52
62
  export function resolveRuntimeTarget(runtime, latestVersion) {
53
63
  if (!runtime || runtime.local)
54
64
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffer-org/server",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@coffer-org/core": "^1.4.0",
28
- "@coffer-org/sdk": "^1.7.0",
28
+ "@coffer-org/sdk": "^1.8.0",
29
29
  "@extractus/oembed-extractor": "^4.1.0",
30
30
  "@fastify/cors": "^11.2.0",
31
31
  "@fastify/multipart": "^10.0.0",