create-erxes 0.1.2 → 0.1.4
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 +1 -1
- package/dist/templates/base/.agents/skills/endpoint/SKILL.md +13 -7
- package/dist/templates/base/AGENTS.md +5 -2
- package/dist/templates/base/api/package.json +3 -2
- package/dist/templates/base/api/src/modules/items/items.ts +6 -8
- package/dist/templates/base/doctor.config.jsonc +20 -0
- package/dist/templates/base/knip.ts +3 -2
- package/dist/templates/base/package.json +2 -1
- package/dist/templates/base/ui/package.json +4 -2
- package/dist/templates/base/vite.config.ts +5 -1
- package/dist/templates/migrate/.agents/skills/plugin-migrate/SKILL.md +13 -1
- package/dist/templates/migrate/migration/checklist.ts +154 -25
- package/dist/templates/migrate/migration/core-map.ts +132 -46
- package/dist/templates/migrate/migration/import-map.ts +8 -2
- package/dist/templates/migrate/scripts/import-mongo.ts +33 -18
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { mkdir, readFile, readdir, symlink, writeFile } from "node:fs/promises";
|
|
|
9
9
|
import { homedir } from "node:os";
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
11
|
// package.json
|
|
12
|
-
var version = "0.1.
|
|
12
|
+
var version = "0.1.4";
|
|
13
13
|
// src/existing-plugins.ts
|
|
14
14
|
/** The old erxes plugins a new-core plugin can be migrated from (`backend/plugins/<name>_api`). */
|
|
15
15
|
const OLD_PLUGINS = [
|
|
@@ -6,15 +6,21 @@ description: Add or change an API procedure. Use when editing api/src/modules, a
|
|
|
6
6
|
1. Pick the permission from `PERMISSIONS` in `api/src/core.ts`. A new one goes
|
|
7
7
|
there and into `manifest.permissions`, named `__id__.<record>.<verb>`.
|
|
8
8
|
2. Write the procedure in `api/src/modules/<module>/<module>.ts` on `reads(action)`
|
|
9
|
-
or `writes(action)` from `api/src/orpc.ts`, with zod `.input()` and `.output()`
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
or `writes(action)` from `api/src/orpc.ts`, with a zod `.input()` and an `.output()` built
|
|
10
|
+
from the table: `createSelectSchema(table).omit({ orgId: true })`, as `items` does
|
|
11
|
+
(`erxes/procedure-output` fails a procedure without one). Query through `context.tx` with no
|
|
12
|
+
org filter: row-level security scopes it. Lists page by cursor, newest first, as
|
|
13
|
+
`items.list` does.
|
|
14
|
+
3. Return the rows as selected: flat, under their column names (`referenceType` and
|
|
15
|
+
`referenceId`, never `reference: { type, id }`), with no `.map` that renames or strips
|
|
16
|
+
fields. A value the row lacks comes from the query, a join or subquery in the `select`, and
|
|
17
|
+
goes on the schema with `.extend()`.
|
|
18
|
+
4. Fail with `ORPCError` (`NOT_FOUND`, `FORBIDDEN`, `CONFLICT`, `BAD_REQUEST`);
|
|
13
19
|
the UI shows its `message`.
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
5. Add the module to `router` in `api/src/router.ts`. The UI's types follow.
|
|
21
|
+
6. In the UI, read with `orpc.<group>.<name>.queryOptions()` and write with
|
|
16
22
|
`mutationOptions()`, invalidating `orpc.<group>.key()` on success.
|
|
17
|
-
|
|
23
|
+
7. Test it in the module's `__tests__`: the happy path, another organisation
|
|
18
24
|
seeing nothing, and a member without the permission getting `FORBIDDEN`.
|
|
19
25
|
|
|
20
26
|
Done when that test passes in `vp run test` and `vp run check` is clean.
|
|
@@ -21,6 +21,7 @@ without line-by-line review.
|
|
|
21
21
|
- Use a maintained library when one does the job. Keep dependencies near their latest release and off deprecated APIs. [typescript/no-deprecated, review]
|
|
22
22
|
- One way to do each job. Search for the existing helper before writing one, merge the repeats you find, delete what nothing uses. [knip, review]
|
|
23
23
|
- Types flow from the database schema to the contract to the UI. Infer them, so each shape is written once. [pnpm check:generated, review]
|
|
24
|
+
- API input, output and events are flat rows, as the table stores them: `referenceType` and `referenceId`, never a nested `{ type, id }` object or a `to…Dto` mapper. A value the row lacks comes from the query. [erxes/no-dto-mapper, review]
|
|
24
25
|
- Parse outside input with zod and narrow it, so the code needs no `any`, cast or `!`. [typescript/no-explicit-any, typescript/no-unsafe-type-assertion, typescript/no-non-null-assertion]
|
|
25
26
|
- Lint and type rules are the spec: when one fires, fix the code. Silencing it is never the fix, whether by a disable comment, `@ts-ignore`, an override, an ignore, a weaker option or a raised limit. A rule that is wrong changes in its own PR. [typescript/ban-ts-comment, review]
|
|
26
27
|
- Split a file or function that outgrows its limit by responsibility (router, service, queries). [max-lines, max-lines-per-function, review]
|
|
@@ -35,8 +36,10 @@ without line-by-line review.
|
|
|
35
36
|
|
|
36
37
|
## This repo
|
|
37
38
|
|
|
38
|
-
- `vp run check` runs the escape guard, format, lint, types,
|
|
39
|
-
and `pnpm check:generated`, which
|
|
39
|
+
- `vp run check` runs the escape guard, format, lint, types, React Doctor on `ui/`
|
|
40
|
+
(`doctor.config.jsonc`), knip, the docs guard and `pnpm check:generated`, which
|
|
41
|
+
compares the migrations with the schema. `pnpm exec react-doctor why <file:line>`,
|
|
42
|
+
run in `ui/`, explains a React Doctor hit.
|
|
40
43
|
- Other gates: `vp run test` on real Postgres via `DATA_TEST_URL`,
|
|
41
44
|
`pnpm check:rls`, `pnpm build`. `vp run fix` formats and fixes lint.
|
|
42
45
|
- Core finds the plugin by these names, so they stay fixed: plugin `__id__`,
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"db:check": "drizzle-kit check"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@erxes/core-contract": "0.1.0-v2.
|
|
25
|
-
"@erxes/plugin-sdk": "0.1.0-v2.
|
|
24
|
+
"@erxes/core-contract": "0.1.0-v2.4",
|
|
25
|
+
"@erxes/plugin-sdk": "0.1.0-v2.4",
|
|
26
26
|
"@opentelemetry/instrumentation-express": "^0.73.0",
|
|
27
27
|
"@opentelemetry/instrumentation-http": "^0.301.1",
|
|
28
28
|
"@opentelemetry/instrumentation-pg": "^0.78.0",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"@orpc/server": "1.16.2",
|
|
32
32
|
"cors": "^2.8.6",
|
|
33
33
|
"drizzle-orm": "0.45.4",
|
|
34
|
+
"drizzle-zod": "0.8.3",
|
|
34
35
|
"express": "^5.3.0",
|
|
35
36
|
"import-in-the-middle": "^3.6.0",
|
|
36
37
|
"pg": "^8.23.1",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ORPCErrorConstructorMap, ORPCError } from '@orpc/server';
|
|
2
2
|
import { and, desc, eq, getTableColumns, ilike, sql } from 'drizzle-orm';
|
|
3
|
+
import { createSelectSchema } from 'drizzle-zod';
|
|
3
4
|
import { z } from 'zod';
|
|
4
5
|
import { PERMISSIONS } from '#/core.ts';
|
|
5
6
|
import { uniqueViolation } from '#/db/client.ts';
|
|
@@ -12,14 +13,11 @@ const { orgId, ...columns } = getTableColumns(items);
|
|
|
12
13
|
const Name = z.string().trim().min(1).max(200);
|
|
13
14
|
const Description = z.string().trim().max(5000);
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
createdAt: z.date(),
|
|
21
|
-
updatedAt: z.date(),
|
|
22
|
-
});
|
|
16
|
+
/**
|
|
17
|
+
* The row as the table stores it, flat and under its column names. A value the row lacks is
|
|
18
|
+
* selected by the query and added with `.extend()`; no mapper reshapes rows.
|
|
19
|
+
*/
|
|
20
|
+
const Item = createSelectSchema(items).omit({ orgId: true });
|
|
23
21
|
|
|
24
22
|
/** The failures a client can branch on: `isDefinedError(error) && error.code === ...`. */
|
|
25
23
|
const itemErrors = {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// React Doctor's rules for erxes React code. `vp run check` runs it and fails on any warning.
|
|
2
|
+
// create-erxes gives every plugin this file, with its ui/ as the one project.
|
|
3
|
+
{
|
|
4
|
+
"$schema": "https://react.doctor/schema/config.json",
|
|
5
|
+
"projects": ["ui"],
|
|
6
|
+
"rules": {
|
|
7
|
+
// How big or branchy a component grows is opinion; max-lines-per-function already caps it.
|
|
8
|
+
"react-doctor/no-high-complexity-react-function": "off",
|
|
9
|
+
"react-doctor/no-giant-component": "off",
|
|
10
|
+
// Mutations refresh through helpers this rule can't see into.
|
|
11
|
+
"react-doctor/query-mutation-missing-invalidation": "off",
|
|
12
|
+
// Micro-optimisations for lists of a few dozen items at most.
|
|
13
|
+
"react-doctor/js-set-map-lookups": "off",
|
|
14
|
+
"react-doctor/js-index-maps": "off",
|
|
15
|
+
"react-doctor/js-hoist-intl": "off",
|
|
16
|
+
// Only costs a full reload in development, and oxlint's react/only-export-components is
|
|
17
|
+
// the place to turn it on.
|
|
18
|
+
"react-doctor/only-export-components": "off",
|
|
19
|
+
},
|
|
20
|
+
}
|
|
@@ -5,8 +5,9 @@ const config: KnipConfig = {
|
|
|
5
5
|
'.': {
|
|
6
6
|
entry: ['{tools,migration,scripts}/*.ts'],
|
|
7
7
|
project: ['*.ts', '{tools,migration,scripts}/**/*.ts'],
|
|
8
|
-
//
|
|
9
|
-
|
|
8
|
+
// The check task runs these: oxlint calls tsrs-lint through OXLINT_TSGOLINT_PATH, and
|
|
9
|
+
// react-doctor reads doctor.config.jsonc.
|
|
10
|
+
ignoreDependencies: ['tsrs', 'react-doctor'],
|
|
10
11
|
},
|
|
11
12
|
api: {
|
|
12
13
|
project: ['src/**/*.ts'],
|
|
@@ -16,9 +16,10 @@
|
|
|
16
16
|
"image:context": "pnpm build && rm -rf .runtime && pnpm --filter @__id__/api deploy --prod .runtime"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@erxes/lint": "0.1.
|
|
19
|
+
"@erxes/lint": "0.1.2",
|
|
20
20
|
"@types/node": "^26.6.6",
|
|
21
21
|
"knip": "^6.45.1",
|
|
22
|
+
"react-doctor": "0.9.23",
|
|
22
23
|
"tsrs": "0.25.0-ts7.1.0-beta",
|
|
23
24
|
"typescript": "7.0.2",
|
|
24
25
|
"vite-plus": "catalog:"
|
|
@@ -8,15 +8,17 @@
|
|
|
8
8
|
"build": "rsbuild build"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@erxes/core-contract": "0.1.0-v2.
|
|
11
|
+
"@erxes/core-contract": "0.1.0-v2.4",
|
|
12
12
|
"@orpc/client": "1.16.2",
|
|
13
13
|
"@orpc/tanstack-query": "1.16.2",
|
|
14
14
|
"@tabler/icons-react": "3.34.0",
|
|
15
15
|
"@tanstack/react-query": "^5.107.1",
|
|
16
|
-
"erxes-ui": "npm:@erxes/ui@4.0.0-v2.
|
|
16
|
+
"erxes-ui": "npm:@erxes/ui@4.0.0-v2.5",
|
|
17
17
|
"jotai": "^3.0.2",
|
|
18
|
+
"nuqs": "^2.10.3",
|
|
18
19
|
"react": "19.3.1",
|
|
19
20
|
"react-dom": "19.3.1",
|
|
21
|
+
"react-hook-form": "^7.92.0",
|
|
20
22
|
"react-i18next": "^18.0.1",
|
|
21
23
|
"react-router-dom": "^7.18.5"
|
|
22
24
|
},
|
|
@@ -6,6 +6,10 @@ import { noSkips } from './tools/test/no-skips.ts';
|
|
|
6
6
|
// The type check is tsrs (TypeScript 7) running as oxlint's tsgolint.
|
|
7
7
|
const tsrs = 'OXLINT_TSGOLINT_PATH=node_modules/.bin/tsrs-lint';
|
|
8
8
|
|
|
9
|
+
// React Doctor on ui/ (doctor.config.jsonc), failing on any warning. Offline: no score upload,
|
|
10
|
+
// share link or crash reports, and no dependency scan.
|
|
11
|
+
const reactDoctor = 'react-doctor --no-score --no-supply-chain --blocking warning';
|
|
12
|
+
|
|
9
13
|
// Vite Task tracks the files each command reads; the lockfile is listed too, so an
|
|
10
14
|
// install that changes a dependency always reruns the task.
|
|
11
15
|
const tracked = { input: [{ auto: true }, 'pnpm-lock.yaml'] };
|
|
@@ -15,7 +19,7 @@ export default defineConfig({
|
|
|
15
19
|
tasks: {
|
|
16
20
|
// A rerun with no edits replays the last result.
|
|
17
21
|
check: {
|
|
18
|
-
command: `erxes-check-escapes && ${tsrs} vp check && knip && erxes-check-docs && pnpm check:generated`,
|
|
22
|
+
command: `erxes-check-escapes && ${tsrs} vp check && ${reactDoctor} && knip && erxes-check-docs && pnpm check:generated`,
|
|
19
23
|
cache: tracked,
|
|
20
24
|
},
|
|
21
25
|
fix: { command: `${tsrs} vp check --fix`, cache: false },
|
|
@@ -34,12 +34,24 @@ data only. The inventory reads code, never a database.
|
|
|
34
34
|
(`tasks.list`, not `getTasks`), and fold operations that only differ by
|
|
35
35
|
filter into one input. A failure the UI acts on gets a typed error. An
|
|
36
36
|
operation the new UI never calls is ticked with no procedure.
|
|
37
|
+
The answer is the table's rows, flat, through the `.output()` the `endpoint` skill builds,
|
|
38
|
+
not the old GraphQL type. Where the old type nested a reference (`contentType` with
|
|
39
|
+
`contentTypeId`, `{ type, id }`), store and return the two columns (`referenceType`,
|
|
40
|
+
`referenceId`). An old field resolver (`createdUser`, `stage`) becomes a join or subquery
|
|
41
|
+
in the `select`, or the UI's own lookup (`MemberInline` for members), never a mapper after
|
|
42
|
+
the query.
|
|
37
43
|
4. Core calls. Do what the arrow says: `sdk` is
|
|
38
44
|
`core.forOrg(orgId).<procedure>` in the API with that scope in
|
|
39
45
|
`manifest.scopes`; `contract` is a call from the UI with the member's own
|
|
40
46
|
session, through `orpc` from `@erxes/core-contract/client` (members through
|
|
41
47
|
erxes-ui's `SelectMember` and `MemberInline`, `ui/AGENTS.md`);
|
|
42
|
-
`plugin-owned` lives in this plugin's tables or code
|
|
48
|
+
`plugin-owned` lives in this plugin's tables or code; `drop` is left out on
|
|
49
|
+
purpose (segments, the client portal, brands): port none of it, and tick it
|
|
50
|
+
once nothing new calls it. Core has no departments or branches: a plugin
|
|
51
|
+
that used them gets its own `__id___teams` and `__id___team_members`,
|
|
52
|
+
drafted with their import from old core's `departments`, `branches` and the
|
|
53
|
+
members' `departmentIds` and `branchIds`. The ids stay the same, so the old
|
|
54
|
+
records' `departmentIds` still match.
|
|
43
55
|
5. Pages. One per UI route, under `ui/src/<module>/`, built from `erxes-ui`
|
|
44
56
|
the way `ui/src/items/` is. The old `ui-modules` components don't exist
|
|
45
57
|
here. Member pickers and names become erxes-ui's `SelectMember` and
|
|
@@ -8,10 +8,16 @@ import { resolve } from 'node:path';
|
|
|
8
8
|
import type * as pg from 'drizzle-orm/pg-core';
|
|
9
9
|
import { z } from 'zod';
|
|
10
10
|
import {
|
|
11
|
+
CLIENT_PORTAL,
|
|
11
12
|
CORE_CALLS,
|
|
13
|
+
WHOLE_MODULES,
|
|
12
14
|
type Equivalent,
|
|
13
15
|
HELPERS,
|
|
16
|
+
isClientPortal,
|
|
17
|
+
isStructureField,
|
|
18
|
+
OWN_TEAMS,
|
|
14
19
|
pluginCall,
|
|
20
|
+
PUBLIC_ROUTE,
|
|
15
21
|
REGISTRATIONS,
|
|
16
22
|
UNMAPPED,
|
|
17
23
|
} from './core-map.ts';
|
|
@@ -98,21 +104,27 @@ const RUNTIME_ENV =
|
|
|
98
104
|
|
|
99
105
|
// Checklist items -------------------------------------------------------------
|
|
100
106
|
|
|
101
|
-
type Item = { section: string; text: string;
|
|
107
|
+
type Item = { section: string; text: string; kind?: Equivalent['kind'] };
|
|
102
108
|
|
|
103
109
|
const items = new Map<string, Item[]>();
|
|
104
110
|
|
|
105
|
-
const add = (module: string, section: string, text: string,
|
|
111
|
+
const add = (module: string, section: string, text: string, kind?: Equivalent['kind']) => {
|
|
106
112
|
const list = items.get(module) ?? [];
|
|
107
113
|
|
|
108
|
-
list.push({ section, text,
|
|
114
|
+
list.push({ section, text, kind });
|
|
109
115
|
items.set(module, list);
|
|
110
116
|
};
|
|
111
117
|
|
|
118
|
+
/** Whether the old plugin used core's departments or branches, by call or by field. */
|
|
119
|
+
let usesTeams = false;
|
|
120
|
+
const TEAMS = `${PLUGIN}_teams`;
|
|
121
|
+
const TEAM_MEMBERS = `${PLUGIN}_team_members`;
|
|
122
|
+
|
|
112
123
|
const describe = (equivalent: Equivalent) =>
|
|
113
|
-
equivalent.kind === 'gap'
|
|
114
|
-
?
|
|
115
|
-
: `${equivalent.kind} \`${equivalent.to
|
|
124
|
+
(equivalent.kind === 'gap' || equivalent.kind === 'drop'
|
|
125
|
+
? `**${equivalent.kind}**: ${equivalent.note ?? ''}`
|
|
126
|
+
: `${equivalent.kind} \`${equivalent.to}\`${equivalent.note ? ` (${equivalent.note})` : ''}`
|
|
127
|
+
).replaceAll('<plugin>', PLUGIN);
|
|
116
128
|
|
|
117
129
|
const camel = (text: string) =>
|
|
118
130
|
text.replaceAll(/[_-](\w)/g, (_match, char: string) => char.toUpperCase());
|
|
@@ -138,10 +150,15 @@ for (const collection of inventory.collections) {
|
|
|
138
150
|
}
|
|
139
151
|
|
|
140
152
|
for (const op of inventory.graphql.operations) {
|
|
153
|
+
const portal = isClientPortal(op);
|
|
154
|
+
|
|
141
155
|
add(
|
|
142
156
|
op.module,
|
|
143
157
|
'Procedures',
|
|
144
|
-
|
|
158
|
+
portal
|
|
159
|
+
? `${op.kind} \`${op.name}\` → ${describe(CLIENT_PORTAL)} ${at(op)}`
|
|
160
|
+
: `${op.kind} \`${op.name}\` → procedure \`${op.module}.${op.name}\` (returns \`${op.returns}\`) ${at(op)}`,
|
|
161
|
+
portal ? 'drop' : undefined,
|
|
145
162
|
);
|
|
146
163
|
}
|
|
147
164
|
|
|
@@ -175,38 +192,66 @@ for (const call of inventory.trpcCalls) {
|
|
|
175
192
|
const target = typeof call.pluginName === 'string' ? call.pluginName : undefined;
|
|
176
193
|
const key = `${show(call.target)}.${show(call.action)}`;
|
|
177
194
|
|
|
178
|
-
const equivalent =
|
|
179
|
-
|
|
195
|
+
const equivalent = isClientPortal(call)
|
|
196
|
+
? CLIENT_PORTAL
|
|
197
|
+
: call.dynamic || !target
|
|
180
198
|
? UNMAPPED
|
|
181
199
|
: target === 'core'
|
|
182
|
-
? (CORE_CALLS[key] ?? UNMAPPED)
|
|
183
|
-
: pluginCall(target, plugin);
|
|
200
|
+
? (CORE_CALLS[key] ?? WHOLE_MODULES[show(call.target)] ?? UNMAPPED)
|
|
201
|
+
: pluginCall(target, plugin, show(call.action));
|
|
184
202
|
|
|
185
203
|
add(
|
|
186
204
|
call.module,
|
|
187
205
|
'Core calls',
|
|
188
206
|
`${target ?? '<dynamic>'} \`${key}\` → ${describe(equivalent)} ${at(call)}`,
|
|
189
|
-
equivalent.kind
|
|
207
|
+
equivalent.kind,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
if (equivalent === OWN_TEAMS) usesTeams = true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Old core's structure: the plugin keeps its own departments and branches, imported with
|
|
214
|
+
// their Mongo ids, so the ids its records hold stay valid.
|
|
215
|
+
for (const schema of inventory.schemas) {
|
|
216
|
+
for (const field of schema.fields.filter((entry) => isStructureField(entry.name))) {
|
|
217
|
+
usesTeams = true;
|
|
218
|
+
add(
|
|
219
|
+
schema.module,
|
|
220
|
+
'Tables',
|
|
221
|
+
`\`${schema.name}.${field.name}\` → plugin-owned: ids in \`${TEAMS}\`, unchanged by the import ${at(schema)}`,
|
|
222
|
+
OWN_TEAMS.kind,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (usesTeams) {
|
|
228
|
+
add(
|
|
229
|
+
'(plugin)',
|
|
230
|
+
'Tables',
|
|
231
|
+
`\`${TEAMS}\` and \`${TEAM_MEMBERS}\` from old core's \`departments\`, \`branches\` and the members' \`departmentIds\` and \`branchIds\` (drafts in migration/schema-draft.ts, imported by scripts/import-mongo.ts): core has no departments or branches`,
|
|
232
|
+
OWN_TEAMS.kind,
|
|
190
233
|
);
|
|
191
234
|
}
|
|
192
235
|
|
|
193
236
|
for (const call of inventory.eventCalls) {
|
|
194
|
-
const equivalent = HELPERS[call.helper] ?? UNMAPPED;
|
|
237
|
+
const equivalent = isClientPortal(call) ? CLIENT_PORTAL : (HELPERS[call.helper] ?? UNMAPPED);
|
|
195
238
|
|
|
196
239
|
add(
|
|
197
240
|
call.module,
|
|
198
241
|
'Events',
|
|
199
242
|
`\`${call.helper}\`${call.type ? ` ${show(call.type)}` : ''} → ${describe(equivalent)} ${at(call)}`,
|
|
200
|
-
equivalent.kind
|
|
243
|
+
equivalent.kind,
|
|
201
244
|
);
|
|
202
245
|
}
|
|
203
246
|
|
|
204
247
|
for (const route of inventory.routes) {
|
|
248
|
+
const equivalent = isClientPortal(route) ? CLIENT_PORTAL : PUBLIC_ROUTE;
|
|
249
|
+
|
|
205
250
|
add(
|
|
206
251
|
route.module,
|
|
207
252
|
'Routes',
|
|
208
|
-
`${route.method} \`${route.path}\`${route.webhook ? ' (webhook)' : ''} →
|
|
209
|
-
|
|
253
|
+
`${route.method} \`${route.path}\`${route.webhook ? ' (webhook)' : ''} → ${describe(equivalent)} ${at(route)}`,
|
|
254
|
+
equivalent.kind,
|
|
210
255
|
);
|
|
211
256
|
}
|
|
212
257
|
|
|
@@ -225,7 +270,7 @@ for (const entry of inventory.registrations) {
|
|
|
225
270
|
'(plugin)',
|
|
226
271
|
'Registrations',
|
|
227
272
|
`${entry.kind} \`${show(entry.id)}\`${entry.group ? ` (${entry.group})` : ''} → ${describe(equivalent)} ${at(entry)}`,
|
|
228
|
-
equivalent.kind
|
|
273
|
+
equivalent.kind,
|
|
229
274
|
);
|
|
230
275
|
}
|
|
231
276
|
|
|
@@ -250,7 +295,8 @@ const ticked = new Map(
|
|
|
250
295
|
);
|
|
251
296
|
|
|
252
297
|
const all = [...items.values()].flat();
|
|
253
|
-
const gaps = all.filter((item) => item.gap).length;
|
|
298
|
+
const gaps = all.filter((item) => item.kind === 'gap').length;
|
|
299
|
+
const drops = all.filter((item) => item.kind === 'drop').length;
|
|
254
300
|
|
|
255
301
|
const order = [
|
|
256
302
|
'(plugin)',
|
|
@@ -275,8 +321,9 @@ const lines = [
|
|
|
275
321
|
`# Migrating ${plugin} to the new core`,
|
|
276
322
|
'',
|
|
277
323
|
`From erxes/erxes \`${inventory.source.ref}\` (${inventory.source.sha.slice(0, 10)}), \`${plugin}_api\` and \`${plugin}_ui\`.`,
|
|
278
|
-
`${all.length} items
|
|
279
|
-
'tick an item once it works on the new core and its old bugs and workarounds are gone,
|
|
324
|
+
`${all.length} items: ${gaps} gaps and ${drops} drops. Follow \`.agents/skills/plugin-migrate\`: one module at a time,`,
|
|
325
|
+
'tick an item once it works on the new core and its old bugs and workarounds are gone, tick a gap once filed, ending its line with `(filed <issue url>)`,',
|
|
326
|
+
'and tick a drop once its code is left out (segments, the client portal and brands are not migrated).',
|
|
280
327
|
'',
|
|
281
328
|
`The old UI imports ${symbols} symbols from \`ui-modules\` in ${inventory.uiModules.files} files; the host has no \`ui-modules\`, so each page rebuilds them from \`erxes-ui\` (members through its \`SelectMember\` and \`MemberInline\`, other records through \`EntityPicker\`). erxes-ui's own old-to-new component table is \`packages/ui/MIGRATION.md\` in erxes (branch \`postgre\`).`,
|
|
282
329
|
`GraphQL types (${inventory.graphql.types.length}) are listed in \`migration/inventory.json\` for reference; procedures replace them.`,
|
|
@@ -343,7 +390,12 @@ for (const collection of inventory.collections) {
|
|
|
343
390
|
|
|
344
391
|
drafted.add(table);
|
|
345
392
|
|
|
346
|
-
|
|
393
|
+
// `timestamps: true` adds createdAt and updatedAt below; a schema that also declares them gets them once.
|
|
394
|
+
const stamped = new Set(schema.timestamps ? ['createdAt', 'updatedAt'] : []);
|
|
395
|
+
|
|
396
|
+
const fields = schema.fields.filter(
|
|
397
|
+
(entry) => entry.type !== 'spread' && entry.name !== '_id' && !stamped.has(entry.name),
|
|
398
|
+
);
|
|
347
399
|
|
|
348
400
|
const columns = fields.map((entry) => {
|
|
349
401
|
const column = `${columnOf(entry.type)}${entry.required ? '.notNull()' : ''}`;
|
|
@@ -369,7 +421,9 @@ for (const collection of inventory.collections) {
|
|
|
369
421
|
' // The Mongo `_id`, kept as text so references survive the import.',
|
|
370
422
|
' id: text().primaryKey(),',
|
|
371
423
|
' orgId: text().notNull(),',
|
|
372
|
-
...spreads.map(
|
|
424
|
+
...spreads.map(
|
|
425
|
+
(entry) => ` // Spread in the old schema, list its fields here: ${entry.name}`,
|
|
426
|
+
),
|
|
373
427
|
...columns,
|
|
374
428
|
...stamps,
|
|
375
429
|
' },',
|
|
@@ -399,6 +453,72 @@ for (const collection of inventory.collections) {
|
|
|
399
453
|
);
|
|
400
454
|
}
|
|
401
455
|
|
|
456
|
+
// The plugin's own departments and branches, in place of old core's structure.
|
|
457
|
+
if (usesTeams) {
|
|
458
|
+
tables.push(
|
|
459
|
+
[
|
|
460
|
+
"/** Old core's departments and branches, now the plugin's. Ids are the Mongo `_id`s. */",
|
|
461
|
+
`export const ${camel(TEAMS)} = table(`,
|
|
462
|
+
" 'teams',",
|
|
463
|
+
' {',
|
|
464
|
+
' id: text().primaryKey(),',
|
|
465
|
+
' orgId: text().notNull(),',
|
|
466
|
+
" kind: text({ enum: ['department', 'branch'] }).notNull(),",
|
|
467
|
+
' name: text(),',
|
|
468
|
+
' code: text(),',
|
|
469
|
+
' parentId: text(),',
|
|
470
|
+
' supervisorId: text(),',
|
|
471
|
+
' createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
|
|
472
|
+
' updatedAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
|
|
473
|
+
' },',
|
|
474
|
+
` (t) => [index('${TEAMS}_org_idx').on(t.orgId)],`,
|
|
475
|
+
');',
|
|
476
|
+
'',
|
|
477
|
+
'/**',
|
|
478
|
+
" * Who is in which team, from old users' departmentIds and branchIds. User ids are core's,",
|
|
479
|
+
' * which keep the Mongo `_id` of members imported from old erxes. Old users may point at',
|
|
480
|
+
' * removed departments: drop those rows before adding a foreign key to the teams.',
|
|
481
|
+
' */',
|
|
482
|
+
`export const ${camel(TEAM_MEMBERS)} = table(`,
|
|
483
|
+
" 'team_members',",
|
|
484
|
+
' {',
|
|
485
|
+
' orgId: text().notNull(),',
|
|
486
|
+
' teamId: text().notNull(),',
|
|
487
|
+
' userId: text().notNull(),',
|
|
488
|
+
' },',
|
|
489
|
+
` (t) => [primaryKey({ columns: [t.teamId, t.userId] }), index('${TEAM_MEMBERS}_user_idx').on(t.userId)],`,
|
|
490
|
+
');',
|
|
491
|
+
].join('\n'),
|
|
492
|
+
);
|
|
493
|
+
|
|
494
|
+
const structure = (collection: string, kind: string) =>
|
|
495
|
+
[
|
|
496
|
+
' {',
|
|
497
|
+
` collection: '${collection}',`,
|
|
498
|
+
` table: '${TEAMS}',`,
|
|
499
|
+
` values: { kind: '${kind}' },`,
|
|
500
|
+
" columns: { _id: 'id', title: 'name', code: 'code', parentId: 'parent_id', supervisorId: 'supervisor_id', createdAt: 'created_at' },",
|
|
501
|
+
' },',
|
|
502
|
+
].join('\n');
|
|
503
|
+
|
|
504
|
+
const members = (field: string) =>
|
|
505
|
+
[
|
|
506
|
+
' {',
|
|
507
|
+
" collection: 'users',",
|
|
508
|
+
` table: '${TEAM_MEMBERS}',`,
|
|
509
|
+
` unwind: '${field}',`,
|
|
510
|
+
` columns: { ${field}: 'team_id', _id: 'user_id' },`,
|
|
511
|
+
' },',
|
|
512
|
+
].join('\n');
|
|
513
|
+
|
|
514
|
+
imports.push(
|
|
515
|
+
structure('departments', 'department'),
|
|
516
|
+
structure('branches', 'branch'),
|
|
517
|
+
members('departmentIds'),
|
|
518
|
+
members('branchIds'),
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
|
|
402
522
|
writeFileSync(
|
|
403
523
|
resolve(here, 'schema-draft.ts'),
|
|
404
524
|
[
|
|
@@ -406,7 +526,7 @@ writeFileSync(
|
|
|
406
526
|
'// schemas. Nothing imports this file: move a table into api/src/db/schema.ts when',
|
|
407
527
|
'// you port its module, pick real types (integer, enums, foreign keys), then run',
|
|
408
528
|
'// `pnpm db:generate` and add its core_enable_tenant_rls line (schema-change skill).',
|
|
409
|
-
|
|
529
|
+
`import { boolean, doublePrecision, index, jsonb, ${usesTeams ? 'primaryKey, ' : ''}text, timestamp } from 'drizzle-orm/pg-core';`,
|
|
410
530
|
"import { table } from '../api/src/db/table.ts';",
|
|
411
531
|
'',
|
|
412
532
|
tables.join('\n\n'),
|
|
@@ -419,11 +539,20 @@ writeFileSync(
|
|
|
419
539
|
[
|
|
420
540
|
'// Which Mongo field goes to which column, per collection, for scripts/import-mongo.ts.',
|
|
421
541
|
'// Written by migration/checklist.ts; edit it as the tables in api/src/db/schema.ts change.',
|
|
422
|
-
'
|
|
542
|
+
'// `values` are columns every row gets; `unwind` makes one row per item of that array field.',
|
|
543
|
+
'export const IMPORT_MAP: {',
|
|
544
|
+
' collection: string;',
|
|
545
|
+
' table: string;',
|
|
546
|
+
' columns: Record<string, string>;',
|
|
547
|
+
' values?: Record<string, string>;',
|
|
548
|
+
' unwind?: string;',
|
|
549
|
+
'}[] = [',
|
|
423
550
|
...imports,
|
|
424
551
|
'];',
|
|
425
552
|
'',
|
|
426
553
|
].join('\n'),
|
|
427
554
|
);
|
|
428
555
|
|
|
429
|
-
console.info(
|
|
556
|
+
console.info(
|
|
557
|
+
`MIGRATION.md: ${all.length} items, ${gaps} gaps, ${drops} drops; ${tables.length} draft tables`,
|
|
558
|
+
);
|
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
// through orpc from @erxes/core-contract/client
|
|
8
8
|
// plugin-owned the plugin keeps it in its own tables or code
|
|
9
9
|
// gap core offers no equivalent yet: file it, don't work around it
|
|
10
|
+
// drop left out on purpose (segments, the client portal, brands): remove the code
|
|
10
11
|
|
|
11
12
|
export type Equivalent = {
|
|
12
|
-
kind: 'sdk' | 'contract' | 'plugin-owned' | 'gap';
|
|
13
|
+
kind: 'sdk' | 'contract' | 'plugin-owned' | 'gap' | 'drop';
|
|
13
14
|
/** The procedure, function or place it moves to. */
|
|
14
15
|
to: string;
|
|
15
16
|
note?: string;
|
|
@@ -19,37 +20,114 @@ const sdk = (to: string, note?: string): Equivalent => ({ kind: 'sdk', to, note
|
|
|
19
20
|
const contract = (to: string, note?: string): Equivalent => ({ kind: 'contract', to, note });
|
|
20
21
|
const owned = (to: string, note?: string): Equivalent => ({ kind: 'plugin-owned', to, note });
|
|
21
22
|
const gap = (note: string): Equivalent => ({ kind: 'gap', to: '', note });
|
|
23
|
+
const drop = (note: string): Equivalent => ({ kind: 'drop', to: '', note });
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Old core's structure (departments, branches, units, positions): core has none. A plugin that
|
|
27
|
+
* used it keeps its own teams; migration/checklist.ts drafts the tables and the import.
|
|
28
|
+
*/
|
|
29
|
+
export const OWN_TEAMS = owned(
|
|
30
|
+
'<plugin>_teams',
|
|
31
|
+
"the plugin's departments and branches; members in <plugin>_team_members, names from users.list (users:read)",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/** A field holding old structure ids or switches: `departmentIds`, `branchId`, `isCheckBranch`. */
|
|
35
|
+
export const isStructureField = (name: string) =>
|
|
36
|
+
/^(department|branch|unit|position)Ids?$|^isCheck(Department|Branch)$/.test(name);
|
|
37
|
+
|
|
38
|
+
const NO_SEGMENTS = drop('the new core has no segments; the plugin filters its own records');
|
|
39
|
+
|
|
40
|
+
/** What old code serving the client portal becomes: nothing, the new core has none. */
|
|
41
|
+
export const CLIENT_PORTAL = drop('the new core has no client portal');
|
|
42
|
+
|
|
43
|
+
/** Old core modules mapped as a whole, whatever the action: left out, or the plugin's own. */
|
|
44
|
+
export const WHOLE_MODULES: Record<string, Equivalent> = {
|
|
45
|
+
segment: NO_SEGMENTS,
|
|
46
|
+
segments: NO_SEGMENTS,
|
|
47
|
+
branches: OWN_TEAMS,
|
|
48
|
+
departments: OWN_TEAMS,
|
|
49
|
+
units: OWN_TEAMS,
|
|
50
|
+
positions: OWN_TEAMS,
|
|
51
|
+
brands: drop('the new core has no brands; leave brand ids and names out'),
|
|
52
|
+
clientPortal: CLIENT_PORTAL,
|
|
53
|
+
clientPortals: CLIENT_PORTAL,
|
|
54
|
+
clientPortalUsers: CLIENT_PORTAL,
|
|
55
|
+
cpUsers: CLIENT_PORTAL,
|
|
56
|
+
cpNotifications: CLIENT_PORTAL,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** Starting automations for a record: they start from the events a plugin publishes. */
|
|
60
|
+
const START_AUTOMATIONS = sdk(
|
|
61
|
+
'core_publish_event() in the same transaction',
|
|
62
|
+
'declare the type in manifest.events; automations start from events',
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
/** Reading automations: plugins can't, so what the plugin needed from them is its own setting. */
|
|
66
|
+
const OWN_AUTOMATION = owned(
|
|
67
|
+
'<plugin>_settings',
|
|
68
|
+
"what the plugin read from an automation (a chatbot's menu, its get-started reply) is its own setting; automations act on its events",
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* An old public Express route (webhooks, OAuth callbacks): the plugin serves it and works out
|
|
73
|
+
* the organisation itself, before any `withOrg`.
|
|
74
|
+
*/
|
|
75
|
+
export const PUBLIC_ROUTE = owned(
|
|
76
|
+
'a route on the plugin API',
|
|
77
|
+
"organisation from the path (an id or key the plugin issued) or, for one address shared by every organisation (Meta's webhook), a lookup table listed in tools/no-org-id.sql; verify the sender's signature first",
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/** Old code that served the client portal: files named after it, `cp*` GraphQL operations. */
|
|
81
|
+
export const isClientPortal = (entry: { file: string; name?: string }) =>
|
|
82
|
+
/client-?portal|cp-?notifications?/i.test(entry.file) || /^cp[A-Z]/.test(entry.name ?? '');
|
|
22
83
|
|
|
23
84
|
/** `sendTRPCMessage({ pluginName: 'core', module, action })`, keyed `module.action`. */
|
|
24
85
|
export const CORE_CALLS: Record<string, Equivalent> = {
|
|
25
86
|
'customers.findOne': sdk('customers.get', 'scope customers:read; by id only'),
|
|
26
|
-
'customers.find':
|
|
87
|
+
'customers.find': sdk(
|
|
27
88
|
'customers.list',
|
|
28
|
-
'
|
|
89
|
+
'scope customers:read; search, ids, state, tagIds, companyId, filter',
|
|
90
|
+
),
|
|
91
|
+
'customers.findActiveCustomers': sdk('customers.list', 'scope customers:read'),
|
|
92
|
+
'customers.removeCustomers': contract(
|
|
93
|
+
'customers.remove',
|
|
94
|
+
'members with contacts.delete only; plugins never delete contacts',
|
|
29
95
|
),
|
|
30
96
|
'customers.createCustomer': sdk(
|
|
31
97
|
'customers.findOrCreate',
|
|
32
|
-
|
|
98
|
+
"scope customers:write; by email or phone, else customers.create (state: 'lead')",
|
|
99
|
+
),
|
|
100
|
+
'customers.createMessengerCustomer': sdk(
|
|
101
|
+
'customers.findOrCreate',
|
|
102
|
+
"scope customers:write; state: 'lead', source: 'chat'",
|
|
33
103
|
),
|
|
34
|
-
'customers.createMessengerCustomer': sdk('customers.findOrCreate', 'scope customers:write'),
|
|
35
104
|
'customers.getWidgetCustomer': sdk(
|
|
36
105
|
'customers.findOrCreate',
|
|
37
106
|
'scope customers:write; by email or phone',
|
|
38
107
|
),
|
|
39
|
-
'customers.updateCustomer':
|
|
40
|
-
'customers.updateOne':
|
|
41
|
-
'customers.updateMessengerCustomer':
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'customers.
|
|
108
|
+
'customers.updateCustomer': sdk('customers.update', 'scope customers:write'),
|
|
109
|
+
'customers.updateOne': sdk('customers.update', 'scope customers:write'),
|
|
110
|
+
'customers.updateMessengerCustomer': sdk(
|
|
111
|
+
'customers.update',
|
|
112
|
+
'scope customers:write; messenger session data (last seen, online, location) stays in the plugin',
|
|
113
|
+
),
|
|
114
|
+
'customers.updateLocation': owned(
|
|
115
|
+
'<plugin>_customers',
|
|
116
|
+
"the visitor's location is messenger session data",
|
|
117
|
+
),
|
|
118
|
+
'customers.updateSession': owned('<plugin>_customers', 'messenger session data'),
|
|
119
|
+
'customers.markCustomerAsActive': owned('<plugin>_customers', 'messenger session data'),
|
|
46
120
|
'companies.findOne': sdk('companies.get', 'scope customers:read; by id only'),
|
|
47
|
-
'companies.find':
|
|
48
|
-
'companies.createCompany':
|
|
49
|
-
'companies.updateCompany':
|
|
50
|
-
'
|
|
51
|
-
|
|
52
|
-
|
|
121
|
+
'companies.find': sdk('companies.list', 'scope customers:read'),
|
|
122
|
+
'companies.createCompany': sdk('companies.create', 'scope customers:write'),
|
|
123
|
+
'companies.updateCompany': sdk('companies.update', 'scope customers:write'),
|
|
124
|
+
'conformity.savedConformity': owned(
|
|
125
|
+
"customerId / companyId columns (or a join table) on the plugin's record",
|
|
126
|
+
'customer ↔ company links: customers.linkCompany (customers:write)',
|
|
127
|
+
),
|
|
128
|
+
'users.findOne': sdk('users.get', 'scope users:read'),
|
|
129
|
+
'users.find': sdk('users.list', 'scope users:read; search, ids, isActive, role'),
|
|
130
|
+
'users.getIds': sdk('users.list', 'scope users:read'),
|
|
53
131
|
'tags.find': owned('<plugin>_tags', "core's tags are for customers and companies (decision 9)"),
|
|
54
132
|
'tags.findOne': owned(
|
|
55
133
|
'<plugin>_tags',
|
|
@@ -82,21 +160,22 @@ export const CORE_CALLS: Record<string, Equivalent> = {
|
|
|
82
160
|
'scope activities:write; customer or company targets, else manifest timelines',
|
|
83
161
|
),
|
|
84
162
|
'notifications.send': sdk('notifications.create', 'scope notifications:write; team members only'),
|
|
85
|
-
'core.sendEmail':
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
'
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
'
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
'
|
|
99
|
-
'
|
|
163
|
+
'core.sendEmail': sdk(
|
|
164
|
+
'emails.send',
|
|
165
|
+
'scope emails:write; plain text, up to 50 addresses, replyTo; team members: notifications.create',
|
|
166
|
+
),
|
|
167
|
+
'emailDeliveries.create': owned(
|
|
168
|
+
'<plugin>_email_deliveries',
|
|
169
|
+
"mail sent from the organisation's own mailbox is the plugin's to log",
|
|
170
|
+
),
|
|
171
|
+
'emailDeliveries.recordHandoff': owned('<plugin>_email_deliveries'),
|
|
172
|
+
'emailSuppression.blocked': owned(
|
|
173
|
+
'<plugin>_email_suppressions',
|
|
174
|
+
'bounces and opt-outs the plugin hears from its mailbox',
|
|
175
|
+
),
|
|
176
|
+
'automation.find': OWN_AUTOMATION,
|
|
177
|
+
'branches.findOne': OWN_TEAMS,
|
|
178
|
+
'departments.findWithChild': OWN_TEAMS,
|
|
100
179
|
'organization.identity': owned('nothing', 'the organisation id is in every token (orgId)'),
|
|
101
180
|
};
|
|
102
181
|
|
|
@@ -115,12 +194,10 @@ export const HELPERS: Record<string, Equivalent> = {
|
|
|
115
194
|
'scope notifications:write; kind in manifest.notificationKinds',
|
|
116
195
|
),
|
|
117
196
|
sendNotifications: sdk('notifications.create', 'scope notifications:write'),
|
|
118
|
-
sendAutomationTrigger:
|
|
119
|
-
|
|
120
|
-
'
|
|
121
|
-
|
|
122
|
-
sendAutomationDeferredCompletion: gap(
|
|
123
|
-
'an automation action answers in its HTTP response; core has no deferred completion',
|
|
197
|
+
sendAutomationTrigger: START_AUTOMATIONS,
|
|
198
|
+
sendAutomationDeferredCompletion: owned(
|
|
199
|
+
'the action answers at once ({ queued: true, jobId })',
|
|
200
|
+
'the plugin keeps the final result on its own job row, and on the timeline with activities.create',
|
|
124
201
|
),
|
|
125
202
|
};
|
|
126
203
|
|
|
@@ -136,15 +213,24 @@ export const REGISTRATIONS: Record<string, Equivalent> = {
|
|
|
136
213
|
'manifest.actions',
|
|
137
214
|
'POST <api>/automations/actions/<type>, verifyCoreCall()',
|
|
138
215
|
),
|
|
139
|
-
'default role':
|
|
140
|
-
|
|
216
|
+
'default role': sdk(
|
|
217
|
+
'manifest.permissionSets',
|
|
218
|
+
'a set of its permissions an owner adds to any role; members keep one role',
|
|
219
|
+
),
|
|
220
|
+
segment: NO_SEGMENTS,
|
|
141
221
|
'import-export': gap("core's import and export cover contacts only"),
|
|
142
222
|
};
|
|
143
223
|
|
|
144
|
-
/**
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
224
|
+
/**
|
|
225
|
+
* A call to another plugin, or to this one through tRPC. The old automations plugin is core's
|
|
226
|
+
* automations now: its `trigger` is an event, and what a plugin read from it is its own setting.
|
|
227
|
+
*/
|
|
228
|
+
export const pluginCall = (target: string, self: string, action: string): Equivalent => {
|
|
229
|
+
if (target === self) return owned('a direct function call');
|
|
230
|
+
|
|
231
|
+
if (target === 'automations') return action === 'trigger' ? START_AUTOMATIONS : OWN_AUTOMATION;
|
|
232
|
+
|
|
233
|
+
return gap(`plugins cannot call ${target}; listen to its events instead`);
|
|
234
|
+
};
|
|
149
235
|
|
|
150
236
|
export const UNMAPPED = gap('not in migration/core-map.ts yet');
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
// Which Mongo field goes to which column, per collection, for scripts/import-mongo.ts.
|
|
2
2
|
// Written by migration/checklist.ts; edit it as the tables in api/src/db/schema.ts change.
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
// `values` are columns every row gets; `unwind` makes one row per item of that array field.
|
|
4
|
+
export const IMPORT_MAP: {
|
|
5
|
+
collection: string;
|
|
6
|
+
table: string;
|
|
7
|
+
columns: Record<string, string>;
|
|
8
|
+
values?: Record<string, string>;
|
|
9
|
+
unwind?: string;
|
|
10
|
+
}[] = [];
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
// pnpm migrate:import [-- --only <collection>]
|
|
6
6
|
//
|
|
7
7
|
// Rows keep the Mongo `_id` as `id`, so running it again skips what it already
|
|
8
|
-
// copied
|
|
9
|
-
// and listed at the end.
|
|
8
|
+
// copied (rows that hit any unique key are skipped). Tables not in the database
|
|
9
|
+
// yet, and fields with no column, are skipped and listed at the end.
|
|
10
10
|
import { parseArgs } from 'node:util';
|
|
11
11
|
import { MongoClient, ObjectId } from 'mongodb';
|
|
12
12
|
import { Pool } from 'pg';
|
|
@@ -54,7 +54,7 @@ const pool = new Pool({ connectionString: DATA_URL });
|
|
|
54
54
|
const skipped: string[] = [];
|
|
55
55
|
|
|
56
56
|
try {
|
|
57
|
-
for (const { collection, table, columns } of IMPORT_MAP) {
|
|
57
|
+
for (const { collection, table, columns, values = {}, unwind } of IMPORT_MAP) {
|
|
58
58
|
if (args.only && args.only !== collection) continue;
|
|
59
59
|
|
|
60
60
|
const { rows: described } = await pool.query<{ column_name: string; data_type: string }>(
|
|
@@ -81,7 +81,8 @@ try {
|
|
|
81
81
|
return false;
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
-
const
|
|
84
|
+
const constants = Object.entries(values);
|
|
85
|
+
const names = ['org_id', ...constants.map(([name]) => name), ...fields.map(([, name]) => name)];
|
|
85
86
|
const cursor = mongo.db().collection(collection).find({}, { batchSize: BATCH });
|
|
86
87
|
let copied = 0;
|
|
87
88
|
let seen = 0;
|
|
@@ -98,7 +99,7 @@ try {
|
|
|
98
99
|
await client.query("SELECT set_config('app.org_id', $1, true)", [ORG_ID]);
|
|
99
100
|
|
|
100
101
|
const result = await client.query(
|
|
101
|
-
`INSERT INTO "${table}" (${names.map((name) => `"${name}"`).join(', ')}) VALUES ${placeholders.join(', ')} ON CONFLICT
|
|
102
|
+
`INSERT INTO "${table}" (${names.map((name) => `"${name}"`).join(', ')}) VALUES ${placeholders.join(', ')} ON CONFLICT DO NOTHING`,
|
|
102
103
|
batch.flat(),
|
|
103
104
|
);
|
|
104
105
|
|
|
@@ -116,24 +117,38 @@ try {
|
|
|
116
117
|
|
|
117
118
|
for await (const document of cursor) {
|
|
118
119
|
seen += 1;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
120
|
+
|
|
121
|
+
// `unwind` turns one document into a row per item of that array field (a user's departmentIds).
|
|
122
|
+
const listed: unknown = unwind ? document[unwind] : undefined;
|
|
123
|
+
|
|
124
|
+
const copies = unwind
|
|
125
|
+
? [listed]
|
|
126
|
+
.flat()
|
|
127
|
+
.filter((item) => item !== undefined && item !== null)
|
|
128
|
+
.map((item) => ({ ...document, [unwind]: item }))
|
|
129
|
+
: [document];
|
|
130
|
+
|
|
131
|
+
for (const copy of copies) {
|
|
132
|
+
batch.push([
|
|
133
|
+
ORG_ID,
|
|
134
|
+
...constants.map(([, value]) => value),
|
|
135
|
+
...fields.map(([field, name]) => {
|
|
136
|
+
const column = known.get(name) ?? { array: false, json: false };
|
|
137
|
+
|
|
138
|
+
return cell(copy[field], column);
|
|
139
|
+
}),
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
if (batch.length === BATCH) {
|
|
143
|
+
await insert(batch);
|
|
144
|
+
batch = [];
|
|
145
|
+
}
|
|
131
146
|
}
|
|
132
147
|
}
|
|
133
148
|
|
|
134
149
|
if (batch.length > 0) await insert(batch);
|
|
135
150
|
|
|
136
|
-
console.info(`${collection} → ${table}: ${copied}
|
|
151
|
+
console.info(`${collection} → ${table}: ${copied} rows copied from ${seen} documents`);
|
|
137
152
|
}
|
|
138
153
|
} finally {
|
|
139
154
|
await mongo.close();
|