@lenne.tech/nest-server 11.3.0 → 11.4.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 (35) hide show
  1. package/bin/migrate.js +40 -0
  2. package/dist/core/modules/migrate/cli/migrate-cli.d.ts +3 -0
  3. package/dist/core/modules/migrate/cli/migrate-cli.js +221 -0
  4. package/dist/core/modules/migrate/cli/migrate-cli.js.map +1 -0
  5. package/dist/core/modules/migrate/helpers/migration.helper.d.ts +12 -0
  6. package/dist/core/modules/migrate/helpers/migration.helper.js +57 -0
  7. package/dist/core/modules/migrate/helpers/migration.helper.js.map +1 -0
  8. package/dist/core/modules/migrate/helpers/ts-compiler.d.ts +2 -0
  9. package/dist/core/modules/migrate/helpers/ts-compiler.js +3 -0
  10. package/dist/core/modules/migrate/helpers/ts-compiler.js.map +1 -0
  11. package/dist/core/modules/migrate/index.d.ts +4 -0
  12. package/dist/core/modules/migrate/index.js +21 -0
  13. package/dist/core/modules/migrate/index.js.map +1 -0
  14. package/dist/core/modules/migrate/migration-runner.d.ts +26 -0
  15. package/dist/core/modules/migrate/migration-runner.js +124 -0
  16. package/dist/core/modules/migrate/migration-runner.js.map +1 -0
  17. package/dist/core/modules/migrate/mongo-state-store.d.ts +30 -0
  18. package/dist/core/modules/migrate/mongo-state-store.js +105 -0
  19. package/dist/core/modules/migrate/mongo-state-store.js.map +1 -0
  20. package/dist/core/modules/migrate/templates/migration-project.template.ts +53 -0
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +1 -0
  23. package/dist/index.js.map +1 -1
  24. package/dist/tsconfig.build.tsbuildinfo +1 -1
  25. package/package.json +9 -3
  26. package/src/core/modules/migrate/MIGRATION_FROM_NODEPIT.md +298 -0
  27. package/src/core/modules/migrate/README.md +453 -0
  28. package/src/core/modules/migrate/cli/migrate-cli.ts +319 -0
  29. package/src/core/modules/migrate/helpers/migration.helper.ts +117 -0
  30. package/src/core/modules/migrate/helpers/ts-compiler.js +14 -0
  31. package/src/core/modules/migrate/index.ts +41 -0
  32. package/src/core/modules/migrate/migration-runner.ts +230 -0
  33. package/src/core/modules/migrate/mongo-state-store.ts +283 -0
  34. package/src/core/modules/migrate/templates/migration-project.template.ts +53 -0
  35. package/src/index.ts +9 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "11.3.0",
3
+ "version": "11.4.1",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -14,8 +14,9 @@
14
14
  "homepage": "https://github.com/lenneTech/nest-server",
15
15
  "license": "MIT",
16
16
  "scripts": {
17
- "build": "rimraf dist && nest build && npm run build:copy-types && npm run build:add-type-references",
17
+ "build": "rimraf dist && nest build && npm run build:copy-types && npm run build:copy-templates && npm run build:add-type-references",
18
18
  "build:copy-types": "mkdir -p dist/types && cp src/types/*.d.ts dist/types/",
19
+ "build:copy-templates": "mkdir -p dist/core/modules/migrate/templates && cp src/core/modules/migrate/templates/migration-project.template.ts dist/core/modules/migrate/templates/",
19
20
  "build:add-type-references": "node scripts/add-type-references.js",
20
21
  "build:pack": "npm pack && echo 'use file:/ROOT_PATH_TO_TGZ_FILE to integrate the package'",
21
22
  "build:dev": "npm run build && yalc push --private",
@@ -182,9 +183,14 @@
182
183
  },
183
184
  "main": "dist/index.js",
184
185
  "types": "dist/index.d.ts",
186
+ "bin": {
187
+ "nest-migrate": "./bin/migrate.js",
188
+ "migrate": "./bin/migrate.js"
189
+ },
185
190
  "files": [
186
191
  "dist/**/*",
187
- "src/**/*"
192
+ "src/**/*",
193
+ "bin/**/*"
188
194
  ],
189
195
  "watch": {
190
196
  "build:dev": "src"
@@ -0,0 +1,298 @@
1
+ # Migration from @nodepit/migrate-state-store-mongodb
2
+
3
+ This guide provides step-by-step instructions to migrate from `@nodepit/migrate-state-store-mongodb` to the built-in nest-server migration system.
4
+
5
+ ## Prerequisites
6
+
7
+ Current state of nest-server-starter projects:
8
+ - Using `@nodepit/migrate-state-store-mongodb` for state storage
9
+ - Using external `migrate` package for CLI
10
+ - Custom migration utilities in `migrations-utils/`
11
+
12
+ ## Migration Steps
13
+
14
+ ### Step 1: Update Dependencies
15
+
16
+ **File:** `package.json`
17
+
18
+ Remove old migration packages:
19
+ ```bash
20
+ npm uninstall migrate @nodepit/migrate-state-store-mongodb ts-migrate-mongoose
21
+ ```
22
+
23
+ Ensure latest nest-server is installed:
24
+ ```bash
25
+ npm install @lenne.tech/nest-server@latest
26
+ ```
27
+
28
+ The `migrate` CLI is now provided by `@lenne.tech/nest-server` - no external package needed!
29
+
30
+ ### Step 2: Update Migration State Store
31
+
32
+ **File:** `migrations-utils/migrate.js`
33
+
34
+ **Before:**
35
+ ```javascript
36
+ import config from '../src/config.env';
37
+ const migrate = require('migrate');
38
+ const { MongoStateStore } = require('@nodepit/migrate-state-store-mongodb');
39
+
40
+ const MONGO_URL = config.mongoose.uri;
41
+ const COLLECTION_NAME = 'migrations';
42
+
43
+ module.exports = class MyMongoStateStore extends MongoStateStore {
44
+ constructor() {
45
+ super({ uri: MONGO_URL, collectionName: COLLECTION_NAME });
46
+ }
47
+ };
48
+ ```
49
+
50
+ **After:**
51
+ ```javascript
52
+ const { createMigrationStore } = require('@lenne.tech/nest-server');
53
+ const config = require('../src/config.env');
54
+
55
+ module.exports = createMigrationStore(
56
+ config.default.mongoose.uri,
57
+ 'migrations' // optional, default is 'migrations'
58
+ );
59
+ ```
60
+
61
+ ### Step 3: Update or Remove Utility Files
62
+
63
+ **The following files should be updated or deleted:**
64
+
65
+ #### 🔄 Update `migrations-utils/db.ts` to Proxy (Recommended for backwards compatibility)
66
+
67
+ **Option A: Create a simple re-export proxy** (Recommended)
68
+
69
+ This keeps old migrations working without modifications:
70
+
71
+ ```typescript
72
+ /**
73
+ * Legacy compatibility layer for old migrations
74
+ * Re-exports database and migration helpers from @lenne.tech/nest-server
75
+ */
76
+ export { createMigrationStore, getDb, uploadFileToGridFS } from '@lenne.tech/nest-server';
77
+ ```
78
+
79
+ **Option B: Delete and update all migrations** (Not recommended)
80
+
81
+ Delete the file and update all existing migration files to import directly from `@lenne.tech/nest-server`:
82
+ ```typescript
83
+ // Old (in migrations)
84
+ import { getDb } from '../migrations-utils/db';
85
+
86
+ // New (in migrations)
87
+ import { getDb } from '@lenne.tech/nest-server';
88
+ ```
89
+
90
+ **We recommend Option A** to maintain backwards compatibility with existing migrations.
91
+
92
+ #### ❌ Delete `migrations-utils/template.ts`
93
+ Use the built-in project template from nest-server instead.
94
+
95
+ #### ❌ Delete `migrations-utils/ts-compiler.js`
96
+ The TypeScript compiler is now provided by nest-server.
97
+
98
+ **Files to keep:**
99
+ - ✅ `migrations-utils/migrate.js` (project-specific configuration)
100
+ - ✅ `migrations-utils/db.ts` (optional proxy for backwards compatibility)
101
+
102
+ ### Step 4: Update package.json Scripts
103
+
104
+ **File:** `package.json`
105
+
106
+ **IMPORTANT:** The CLI syntax has changed. The command must come FIRST, then the options.
107
+
108
+ **Before (WRONG):**
109
+ ```json
110
+ {
111
+ "scripts": {
112
+ "migrate:create": "migrate create --template-file ./migrations-utils/template.ts --migrations-dir=\"./migrations\" --compiler=\"ts:./migrations-utils/ts-compiler.js\"",
113
+ "migrate:up": "migrate --store=./migrations-utils/migrate.js --migrations-dir=\"./migrations\" --compiler=\"ts:./migrations-utils/ts-compiler.js\" up"
114
+ }
115
+ }
116
+ ```
117
+
118
+ **After (CORRECT):**
119
+ ```json
120
+ {
121
+ "scripts": {
122
+ "migrate:create": "migrate create --template-file ./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/templates/migration-project.template.ts --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
123
+ "migrate:up": "migrate up --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
124
+ "migrate:down": "migrate down --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
125
+ "migrate:list": "migrate list --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
126
+ "migrate:develop:up": "NODE_ENV=develop migrate up --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
127
+ "migrate:test:up": "NODE_ENV=test migrate up --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
128
+ "migrate:preview:up": "NODE_ENV=preview migrate up --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js",
129
+ "migrate:prod:up": "NODE_ENV=production migrate up --store ./migrations-utils/migrate.js --migrations-dir ./migrations --compiler ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/helpers/ts-compiler.js"
130
+ }
131
+ }
132
+ ```
133
+
134
+ **Key changes:**
135
+ - ✅ Command (`up`, `down`, `create`, `list`) comes FIRST
136
+ - ✅ Use `ts:./path` instead of `"ts:./path"` for compiler
137
+ - ✅ Remove quotes around paths (not needed)
138
+ - ✅ Use nest-server paths for template and compiler
139
+
140
+ ### Step 5: Test Migration
141
+
142
+ ```bash
143
+ # Install dependencies
144
+ npm install
145
+
146
+ # Test migration creation
147
+ npm run migrate:create -- test-migration
148
+
149
+ # Test migration status
150
+ npm run migrate:list
151
+
152
+ # Clean up test migration
153
+ rm migrations/*-test-migration.ts
154
+ ```
155
+
156
+ ## Verification Checklist
157
+
158
+ - [ ] `migrate` package removed from package.json
159
+ - [ ] `@nodepit/migrate-state-store-mongodb` removed from package.json
160
+ - [ ] `ts-migrate-mongoose` removed from package.json (if present)
161
+ - [ ] `migrations-utils/migrate.js` updated to use `createMigrationStore`
162
+ - [ ] `migrations-utils/db.ts` **updated to proxy** or deleted (recommended: create proxy for backwards compatibility)
163
+ - [ ] `migrations-utils/template.ts` **deleted** (use nest-server template)
164
+ - [ ] `migrations-utils/ts-compiler.js` **deleted** (use nest-server compiler)
165
+ - [ ] package.json scripts updated with correct syntax
166
+ - [ ] `npm install` completed successfully
167
+ - [ ] `npm run migrate:create -- test` works
168
+ - [ ] Existing migrations still in database (no data loss)
169
+
170
+ ## What Stays the Same
171
+
172
+ - ✅ All migration files in `migrations/` folder
173
+ - ✅ All migration data in MongoDB
174
+ - ✅ Migration state collection (`migrations`)
175
+ - ✅ npm script names (can keep existing names)
176
+
177
+ ## What Changes
178
+
179
+ ### Files Removed (2 files)
180
+ - ❌ `migrations-utils/template.ts` - **DELETED** (use nest-server template)
181
+ - ❌ `migrations-utils/ts-compiler.js` - **DELETED** (use nest-server compiler)
182
+
183
+ ### Files Updated (2 files)
184
+ - ✅ `migrations-utils/migrate.js` - simplified from ~14 lines to ~7 lines
185
+ - ✅ `migrations-utils/db.ts` - converted to simple re-export proxy (~5 lines)
186
+
187
+ ### Files Kept
188
+ - ✅ `migrations-utils/migrate.js` - **REQUIRED** (project-specific configuration)
189
+ - ✅ `migrations-utils/db.ts` - **OPTIONAL** (backwards compatibility proxy)
190
+
191
+ ### Other Changes
192
+ - ✅ package.json scripts syntax updated
193
+ - ✅ CLI comes from nest-server instead of external package
194
+
195
+ ## Rollback (if needed)
196
+
197
+ If you need to rollback:
198
+
199
+ ```bash
200
+ # Reinstall old packages
201
+ npm install --save-dev migrate @nodepit/migrate-state-store-mongodb
202
+
203
+ # Restore old files from git
204
+ git checkout migrations-utils/migrate.js
205
+ git checkout migrations-utils/db.ts
206
+ git checkout migrations-utils/template.ts
207
+ git checkout migrations-utils/ts-compiler.js
208
+ git checkout package.json
209
+ ```
210
+
211
+ ## Benefits After Migration
212
+
213
+ 1. **85% less boilerplate** - Only 1-2 small files instead of 4 in migrations-utils
214
+ 2. **No external dependencies** - `migrate` CLI comes from nest-server
215
+ 3. **Better TypeScript** - Native TypeScript implementation
216
+ 4. **MongoDB 7.x support** - Works with latest MongoDB versions
217
+ 5. **Central maintenance** - Updates come with nest-server
218
+ 6. **Consistent across projects** - All projects use same utilities
219
+
220
+ ## Migration Template Usage
221
+
222
+ After migration, your migrations will look like this:
223
+
224
+ ```typescript
225
+ import { getDb, uploadFileToGridFS } from '@lenne.tech/nest-server';
226
+ import { Db, ObjectId } from 'mongodb';
227
+ import config from '../src/config.env';
228
+
229
+ const MONGO_URL = config.mongoose.uri;
230
+
231
+ export const up = async () => {
232
+ const db: Db = await getDb(MONGO_URL);
233
+
234
+ // Your migration code here
235
+ await db.collection('users').updateMany(
236
+ { email: { $exists: false } },
237
+ { $set: { email: '' } }
238
+ );
239
+ };
240
+
241
+ export const down = async () => {
242
+ const db: Db = await getDb(MONGO_URL);
243
+
244
+ // Your rollback code here
245
+ await db.collection('users').updateMany(
246
+ {},
247
+ { $unset: { email: '' } }
248
+ );
249
+ };
250
+ ```
251
+
252
+ **No need to create helper files!** All utilities are imported directly from `@lenne.tech/nest-server`.
253
+
254
+ ## Support
255
+
256
+ If issues occur during migration:
257
+ - Check that `@lenne.tech/nest-server` is at version 11.3.0 or higher
258
+ - Verify `ts-node` is installed as devDependency
259
+ - Ensure `migrations-utils/migrate.js` exports the state store correctly
260
+ - Test with `migrate --help` to verify CLI is available
261
+ - Remember: Command must come FIRST in CLI syntax
262
+
263
+ ## File Structure After Migration
264
+
265
+ ```
266
+ project-root/
267
+ ├── migrations/ # Unchanged
268
+ │ └── TIMESTAMP-*.ts # Your migrations
269
+ ├── migrations-utils/
270
+ │ ├── migrate.js # ⭐ REQUIRED (7 lines)
271
+ │ └── db.ts # ⭐ OPTIONAL proxy (5 lines, for backwards compatibility)
272
+ └── package.json # Scripts updated
273
+ ```
274
+
275
+ **Everything else comes from `@lenne.tech/nest-server`!**
276
+
277
+ **Note:** The `db.ts` file is optional but recommended to keep old migrations working without modifications.
278
+
279
+ ## CLI Command Reference
280
+
281
+ ```bash
282
+ # Create new migration
283
+ migrate create <name> --template-file <path> --migrations-dir <dir> --compiler ts:<path>
284
+
285
+ # Run all pending migrations
286
+ migrate up --store <path> --migrations-dir <dir> --compiler ts:<path>
287
+
288
+ # Rollback last migration
289
+ migrate down --store <path> --migrations-dir <dir> --compiler ts:<path>
290
+
291
+ # List migration status
292
+ migrate list --store <path> --migrations-dir <dir> --compiler ts:<path>
293
+ ```
294
+
295
+ **Key points:**
296
+ - Command (`create`, `up`, `down`, `list`) comes FIRST
297
+ - Options use `--option value` format (not `--option=value`)
298
+ - Compiler format: `ts:./path` (not `"ts:./path"`)