create-erxes 0.0.0-stage → 0.1.1

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 (69) hide show
  1. package/README.md +23 -2
  2. package/dist/index.js +481 -0
  3. package/dist/templates/base/.agents/skills/endpoint/SKILL.md +20 -0
  4. package/dist/templates/base/.agents/skills/schema-change/SKILL.md +28 -0
  5. package/dist/templates/base/.agents/skills/verify/SKILL.md +24 -0
  6. package/dist/templates/base/.github/workflows/ci.yml +44 -0
  7. package/dist/templates/base/AGENTS.md +59 -0
  8. package/dist/templates/base/CLAUDE.md +1 -0
  9. package/dist/templates/base/Dockerfile +13 -0
  10. package/dist/templates/base/_dockerignore +4 -0
  11. package/dist/templates/base/_env.example +15 -0
  12. package/dist/templates/base/_gitignore +11 -0
  13. package/dist/templates/base/_node-version +1 -0
  14. package/dist/templates/base/api/AGENTS.md +22 -0
  15. package/dist/templates/base/api/drizzle/0000_items.sql +13 -0
  16. package/dist/templates/base/api/drizzle/meta/0000_snapshot.json +126 -0
  17. package/dist/templates/base/api/drizzle/meta/_journal.json +13 -0
  18. package/dist/templates/base/api/drizzle.config.ts +16 -0
  19. package/dist/templates/base/api/package.json +47 -0
  20. package/dist/templates/base/api/src/__tests__/server.test.ts +51 -0
  21. package/dist/templates/base/api/src/config.ts +43 -0
  22. package/dist/templates/base/api/src/core.ts +37 -0
  23. package/dist/templates/base/api/src/db/client.ts +30 -0
  24. package/dist/templates/base/api/src/db/migrate.ts +80 -0
  25. package/dist/templates/base/api/src/db/schema.ts +34 -0
  26. package/dist/templates/base/api/src/db/table.ts +8 -0
  27. package/dist/templates/base/api/src/logger.ts +5 -0
  28. package/dist/templates/base/api/src/main.ts +23 -0
  29. package/dist/templates/base/api/src/migrate.ts +9 -0
  30. package/dist/templates/base/api/src/modules/items/__tests__/items.test.ts +92 -0
  31. package/dist/templates/base/api/src/modules/items/items.ts +127 -0
  32. package/dist/templates/base/api/src/orpc.ts +25 -0
  33. package/dist/templates/base/api/src/router.ts +10 -0
  34. package/dist/templates/base/api/src/server.ts +58 -0
  35. package/dist/templates/base/api/src/telemetry.ts +27 -0
  36. package/dist/templates/base/api/src/test/core-functions.sql +18 -0
  37. package/dist/templates/base/api/src/test/setup.ts +68 -0
  38. package/dist/templates/base/api/tsconfig.json +9 -0
  39. package/dist/templates/base/knip.ts +21 -0
  40. package/dist/templates/base/package.json +30 -0
  41. package/dist/templates/base/pnpm-workspace.yaml +20 -0
  42. package/dist/templates/base/tools/check-generated.ts +70 -0
  43. package/dist/templates/base/tools/check-rls.sh +30 -0
  44. package/dist/templates/base/tools/check-rls.sql +109 -0
  45. package/dist/templates/base/tools/no-org-id.sql +5 -0
  46. package/dist/templates/base/tools/test/no-skips.ts +58 -0
  47. package/dist/templates/base/tsconfig.base.json +16 -0
  48. package/dist/templates/base/tsconfig.json +9 -0
  49. package/dist/templates/base/ui/AGENTS.md +34 -0
  50. package/dist/templates/base/ui/package.json +35 -0
  51. package/dist/templates/base/ui/rsbuild.config.ts +39 -0
  52. package/dist/templates/base/ui/src/__Pascal__.tsx +14 -0
  53. package/dist/templates/base/ui/src/config.tsx +11 -0
  54. package/dist/templates/base/ui/src/index.ts +3 -0
  55. package/dist/templates/base/ui/src/items/ItemSheet.tsx +107 -0
  56. package/dist/templates/base/ui/src/items/ItemsPage.tsx +143 -0
  57. package/dist/templates/base/ui/src/items/useItems.ts +79 -0
  58. package/dist/templates/base/ui/src/orpc.ts +32 -0
  59. package/dist/templates/base/ui/src/styles.css +8 -0
  60. package/dist/templates/base/ui/tsconfig.json +11 -0
  61. package/dist/templates/base/vite.config.ts +62 -0
  62. package/dist/templates/migrate/.agents/skills/plugin-migrate/SKILL.md +66 -0
  63. package/dist/templates/migrate/migration/checklist.ts +429 -0
  64. package/dist/templates/migrate/migration/core-map.ts +150 -0
  65. package/dist/templates/migrate/migration/import-map.ts +4 -0
  66. package/dist/templates/migrate/migration/inventory.ts +881 -0
  67. package/dist/templates/migrate/package.json +15 -0
  68. package/dist/templates/migrate/scripts/import-mongo.ts +143 -0
  69. package/package.json +32 -3
@@ -0,0 +1,15 @@
1
+ {
2
+ "scripts": {
3
+ "migrate:inventory": "node migration/inventory.ts",
4
+ "migrate:checklist": "node migration/checklist.ts && vp fmt MIGRATION.md migration",
5
+ "migrate:import": "node --env-file-if-exists=.env scripts/import-mongo.ts"
6
+ },
7
+ "devDependencies": {
8
+ "@ast-grep/napi": "^0.51.2",
9
+ "@types/pg": "^8.23.1",
10
+ "drizzle-orm": "0.45.4",
11
+ "mongodb": "^7.8.0",
12
+ "pg": "^8.23.1",
13
+ "zod": "^4.6.7"
14
+ }
15
+ }
@@ -0,0 +1,143 @@
1
+ // Copies one organisation's documents from the old erxes Mongo into this
2
+ // plugin's tables, following migration/import-map.ts.
3
+ //
4
+ // MONGO_URL=mongodb://… DATA_URL=postgres://… ORG_ID=<core org id> \
5
+ // pnpm migrate:import [-- --only <collection>]
6
+ //
7
+ // Rows keep the Mongo `_id` as `id`, so running it again skips what it already
8
+ // copied. Tables not in the database yet, and fields with no column, are skipped
9
+ // and listed at the end.
10
+ import { parseArgs } from 'node:util';
11
+ import { MongoClient, ObjectId } from 'mongodb';
12
+ import { Pool } from 'pg';
13
+ import { IMPORT_MAP } from '../migration/import-map.ts';
14
+
15
+ const BATCH = 500;
16
+
17
+ const { values: args } = parseArgs({ options: { only: { type: 'string' } } });
18
+ const { MONGO_URL, DATA_URL, ORG_ID } = process.env;
19
+
20
+ if (!MONGO_URL || !DATA_URL || !ORG_ID) {
21
+ console.error('Set MONGO_URL, DATA_URL and ORG_ID.');
22
+ process.exit(1);
23
+ }
24
+
25
+ type Column = { array: boolean; json: boolean };
26
+
27
+ const plain = (value: unknown): unknown => {
28
+ if (value instanceof ObjectId) return value.toHexString();
29
+
30
+ if (Array.isArray(value)) return value.map(plain);
31
+
32
+ if (value instanceof Date || value === null || typeof value !== 'object') return value;
33
+
34
+ return Object.fromEntries(Object.entries(value).map(([key, inner]) => [key, plain(inner)]));
35
+ };
36
+
37
+ const cell = (value: unknown, column: Column) => {
38
+ const flat = plain(value);
39
+
40
+ if (flat === undefined || flat === null) return null;
41
+
42
+ if (column.json) return JSON.stringify(flat);
43
+
44
+ // An array column takes an array as it is and a single value as a one-item array.
45
+ if (column.array) return [flat].flat();
46
+
47
+ if (typeof flat === 'object' && !(flat instanceof Date)) return JSON.stringify(flat);
48
+
49
+ return flat;
50
+ };
51
+
52
+ const mongo = await MongoClient.connect(MONGO_URL);
53
+ const pool = new Pool({ connectionString: DATA_URL });
54
+ const skipped: string[] = [];
55
+
56
+ try {
57
+ for (const { collection, table, columns } of IMPORT_MAP) {
58
+ if (args.only && args.only !== collection) continue;
59
+
60
+ const { rows: described } = await pool.query<{ column_name: string; data_type: string }>(
61
+ 'SELECT column_name, data_type FROM information_schema.columns WHERE table_name = $1',
62
+ [table],
63
+ );
64
+
65
+ if (described.length === 0) {
66
+ skipped.push(`${collection}: no table ${table} yet`);
67
+ continue;
68
+ }
69
+
70
+ const known = new Map(
71
+ described.map(({ column_name, data_type }) => [
72
+ column_name,
73
+ { array: data_type === 'ARRAY', json: data_type === 'jsonb' || data_type === 'json' },
74
+ ]),
75
+ );
76
+
77
+ const fields = Object.entries(columns).filter(([field, name]) => {
78
+ if (known.has(name)) return true;
79
+ skipped.push(`${collection}.${field}: no column ${table}.${name}`);
80
+
81
+ return false;
82
+ });
83
+
84
+ const names = ['org_id', ...fields.map(([, name]) => name)];
85
+ const cursor = mongo.db().collection(collection).find({}, { batchSize: BATCH });
86
+ let copied = 0;
87
+ let seen = 0;
88
+
89
+ const insert = async (batch: unknown[][]) => {
90
+ const placeholders = batch.map(
91
+ (_, row) => `(${names.map((__, col) => `$${row * names.length + col + 1}`).join(', ')})`,
92
+ );
93
+
94
+ const client = await pool.connect();
95
+
96
+ try {
97
+ await client.query('BEGIN');
98
+ await client.query("SELECT set_config('app.org_id', $1, true)", [ORG_ID]);
99
+
100
+ const result = await client.query(
101
+ `INSERT INTO "${table}" (${names.map((name) => `"${name}"`).join(', ')}) VALUES ${placeholders.join(', ')} ON CONFLICT (id) DO NOTHING`,
102
+ batch.flat(),
103
+ );
104
+
105
+ await client.query('COMMIT');
106
+ copied += result.rowCount ?? 0;
107
+ } catch (error) {
108
+ await client.query('ROLLBACK');
109
+ throw error;
110
+ } finally {
111
+ client.release();
112
+ }
113
+ };
114
+
115
+ let batch: unknown[][] = [];
116
+
117
+ for await (const document of cursor) {
118
+ seen += 1;
119
+ batch.push([
120
+ ORG_ID,
121
+ ...fields.map(([field, name]) => {
122
+ const column = known.get(name) ?? { array: false, json: false };
123
+
124
+ return cell(document[field], column);
125
+ }),
126
+ ]);
127
+
128
+ if (batch.length === BATCH) {
129
+ await insert(batch);
130
+ batch = [];
131
+ }
132
+ }
133
+
134
+ if (batch.length > 0) await insert(batch);
135
+
136
+ console.info(`${collection} → ${table}: ${copied} of ${seen} copied`);
137
+ }
138
+ } finally {
139
+ await mongo.close();
140
+ await pool.end();
141
+ }
142
+
143
+ if (skipped.length > 0) console.info(`Skipped:\n ${skipped.join('\n ')}`);
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "create-erxes",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "description": "Create an erxes plugin for the new core, or migrate an old erxes plugin onto it: pnpm create erxes",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/erxes/erxes.git",
10
+ "directory": "packages/create-erxes"
11
+ },
12
+ "bin": {
13
+ "create-erxes": "./dist/index.js"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "type": "module",
19
+ "publishConfig": {
20
+ "access": "public",
21
+ "provenance": true
22
+ },
23
+ "dependencies": {
24
+ "@clack/prompts": "^1.9.0",
25
+ "commander": "^15.1.0",
26
+ "tinyexec": "^1.3.1",
27
+ "zod": "^4.6.7"
28
+ },
29
+ "engines": {
30
+ "node": ">=26"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown"
34
+ }
6
35
  }