@lenne.tech/nest-server 11.4.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.
- package/dist/core/modules/migrate/templates/migration-project.template.ts +53 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/src/core/modules/migrate/MIGRATION_FROM_NODEPIT.md +150 -71
- package/src/core/modules/migrate/README.md +9 -8
- package/src/core/modules/migrate/templates/migration-project.template.ts +53 -0
- package/dist/core/modules/migrate/templates/migration-with-helper.template.d.ts +0 -2
- package/dist/core/modules/migrate/templates/migration-with-helper.template.js +0 -10
- package/dist/core/modules/migrate/templates/migration-with-helper.template.js.map +0 -1
- package/dist/core/modules/migrate/templates/migration.template.d.ts +0 -2
- package/dist/core/modules/migrate/templates/migration.template.js +0 -15
- package/dist/core/modules/migrate/templates/migration.template.js.map +0 -1
- package/src/core/modules/migrate/templates/migration-with-helper.template.ts +0 -72
- package/src/core/modules/migrate/templates/migration.template.ts +0 -59
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "11.4.
|
|
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",
|
|
@@ -25,6 +25,8 @@ Ensure latest nest-server is installed:
|
|
|
25
25
|
npm install @lenne.tech/nest-server@latest
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
The `migrate` CLI is now provided by `@lenne.tech/nest-server` - no external package needed!
|
|
29
|
+
|
|
28
30
|
### Step 2: Update Migration State Store
|
|
29
31
|
|
|
30
32
|
**File:** `migrations-utils/migrate.js`
|
|
@@ -56,78 +58,84 @@ module.exports = createMigrationStore(
|
|
|
56
58
|
);
|
|
57
59
|
```
|
|
58
60
|
|
|
59
|
-
### Step 3: Update
|
|
61
|
+
### Step 3: Update or Remove Utility Files
|
|
60
62
|
|
|
61
|
-
**
|
|
63
|
+
**The following files should be updated or deleted:**
|
|
62
64
|
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
import * as fs from 'fs';
|
|
66
|
-
import { GridFSBucket, MongoClient, ObjectId } from 'mongodb';
|
|
67
|
-
import * as path from 'path';
|
|
68
|
-
import config from '../src/config.env';
|
|
65
|
+
#### 🔄 Update `migrations-utils/db.ts` to Proxy (Recommended for backwards compatibility)
|
|
69
66
|
|
|
70
|
-
|
|
67
|
+
**Option A: Create a simple re-export proxy** (Recommended)
|
|
71
68
|
|
|
72
|
-
|
|
73
|
-
const client: MongoClient = await MongoClient.connect(MONGO_URL);
|
|
74
|
-
return client.db();
|
|
75
|
-
};
|
|
69
|
+
This keeps old migrations working without modifications:
|
|
76
70
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
};
|
|
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';
|
|
83
77
|
```
|
|
84
78
|
|
|
85
|
-
**
|
|
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`:
|
|
86
82
|
```typescript
|
|
87
|
-
|
|
88
|
-
import { getDb
|
|
89
|
-
import { Db, ObjectId } from 'mongodb';
|
|
83
|
+
// Old (in migrations)
|
|
84
|
+
import { getDb } from '../migrations-utils/db';
|
|
90
85
|
|
|
91
|
-
|
|
86
|
+
// New (in migrations)
|
|
87
|
+
import { getDb } from '@lenne.tech/nest-server';
|
|
88
|
+
```
|
|
92
89
|
|
|
93
|
-
|
|
94
|
-
* Get database connection
|
|
95
|
-
*/
|
|
96
|
-
export const getDb = async (): Promise<Db> => {
|
|
97
|
-
return getDbHelper(MONGO_URL);
|
|
98
|
-
};
|
|
90
|
+
**We recommend Option A** to maintain backwards compatibility with existing migrations.
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
options?: { bucketName?: string; filename?: string }
|
|
106
|
-
): Promise<ObjectId> => {
|
|
107
|
-
return uploadFileToGridFS(MONGO_URL, relativePath, options);
|
|
108
|
-
};
|
|
109
|
-
```
|
|
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.
|
|
110
97
|
|
|
111
|
-
|
|
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
|
|
112
103
|
|
|
113
104
|
**File:** `package.json`
|
|
114
105
|
|
|
115
|
-
|
|
106
|
+
**IMPORTANT:** The CLI syntax has changed. The command must come FIRST, then the options.
|
|
116
107
|
|
|
108
|
+
**Before (WRONG):**
|
|
117
109
|
```json
|
|
118
110
|
{
|
|
119
111
|
"scripts": {
|
|
120
112
|
"migrate:create": "migrate create --template-file ./migrations-utils/template.ts --migrations-dir=\"./migrations\" --compiler=\"ts:./migrations-utils/ts-compiler.js\"",
|
|
121
|
-
"migrate:up": "migrate --store=./migrations-utils/migrate.js --migrations-dir=\"./migrations\" --compiler=\"ts:./migrations-utils/ts-compiler.js\" up"
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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"
|
|
126
130
|
}
|
|
127
131
|
}
|
|
128
132
|
```
|
|
129
133
|
|
|
130
|
-
|
|
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
|
|
131
139
|
|
|
132
140
|
### Step 5: Test Migration
|
|
133
141
|
|
|
@@ -138,17 +146,23 @@ npm install
|
|
|
138
146
|
# Test migration creation
|
|
139
147
|
npm run migrate:create -- test-migration
|
|
140
148
|
|
|
141
|
-
# Test migration
|
|
142
|
-
npm run migrate:
|
|
149
|
+
# Test migration status
|
|
150
|
+
npm run migrate:list
|
|
151
|
+
|
|
152
|
+
# Clean up test migration
|
|
153
|
+
rm migrations/*-test-migration.ts
|
|
143
154
|
```
|
|
144
155
|
|
|
145
156
|
## Verification Checklist
|
|
146
157
|
|
|
147
158
|
- [ ] `migrate` package removed from package.json
|
|
148
159
|
- [ ] `@nodepit/migrate-state-store-mongodb` removed from package.json
|
|
149
|
-
- [ ] `ts-migrate-mongoose` removed from package.json
|
|
160
|
+
- [ ] `ts-migrate-mongoose` removed from package.json (if present)
|
|
150
161
|
- [ ] `migrations-utils/migrate.js` updated to use `createMigrationStore`
|
|
151
|
-
- [ ] `migrations-utils/db.ts` updated to
|
|
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
|
|
152
166
|
- [ ] `npm install` completed successfully
|
|
153
167
|
- [ ] `npm run migrate:create -- test` works
|
|
154
168
|
- [ ] Existing migrations still in database (no data loss)
|
|
@@ -157,15 +171,25 @@ npm run migrate:up
|
|
|
157
171
|
|
|
158
172
|
- ✅ All migration files in `migrations/` folder
|
|
159
173
|
- ✅ All migration data in MongoDB
|
|
160
|
-
- ✅
|
|
161
|
-
- ✅
|
|
162
|
-
- ✅ Template files (if using custom templates)
|
|
174
|
+
- ✅ Migration state collection (`migrations`)
|
|
175
|
+
- ✅ npm script names (can keep existing names)
|
|
163
176
|
|
|
164
177
|
## What Changes
|
|
165
178
|
|
|
166
|
-
|
|
167
|
-
-
|
|
168
|
-
-
|
|
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
|
|
169
193
|
- ✅ CLI comes from nest-server instead of external package
|
|
170
194
|
|
|
171
195
|
## Rollback (if needed)
|
|
@@ -176,20 +200,56 @@ If you need to rollback:
|
|
|
176
200
|
# Reinstall old packages
|
|
177
201
|
npm install --save-dev migrate @nodepit/migrate-state-store-mongodb
|
|
178
202
|
|
|
179
|
-
#
|
|
203
|
+
# Restore old files from git
|
|
180
204
|
git checkout migrations-utils/migrate.js
|
|
181
|
-
|
|
182
|
-
# Revert migrations-utils/db.ts to old version from git
|
|
183
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
|
|
184
209
|
```
|
|
185
210
|
|
|
186
211
|
## Benefits After Migration
|
|
187
212
|
|
|
188
|
-
1. **
|
|
189
|
-
2. **
|
|
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
|
|
190
215
|
3. **Better TypeScript** - Native TypeScript implementation
|
|
191
216
|
4. **MongoDB 7.x support** - Works with latest MongoDB versions
|
|
192
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`.
|
|
193
253
|
|
|
194
254
|
## Support
|
|
195
255
|
|
|
@@ -198,6 +258,7 @@ If issues occur during migration:
|
|
|
198
258
|
- Verify `ts-node` is installed as devDependency
|
|
199
259
|
- Ensure `migrations-utils/migrate.js` exports the state store correctly
|
|
200
260
|
- Test with `migrate --help` to verify CLI is available
|
|
261
|
+
- Remember: Command must come FIRST in CLI syntax
|
|
201
262
|
|
|
202
263
|
## File Structure After Migration
|
|
203
264
|
|
|
@@ -206,14 +267,32 @@ project-root/
|
|
|
206
267
|
├── migrations/ # Unchanged
|
|
207
268
|
│ └── TIMESTAMP-*.ts # Your migrations
|
|
208
269
|
├── migrations-utils/
|
|
209
|
-
│ ├── migrate.js #
|
|
210
|
-
│
|
|
211
|
-
|
|
212
|
-
│ └── ts-compiler.js # Unchanged (optional, can be removed)
|
|
213
|
-
└── package.json # Dependencies removed
|
|
270
|
+
│ ├── migrate.js # ⭐ REQUIRED (7 lines)
|
|
271
|
+
│ └── db.ts # ⭐ OPTIONAL proxy (5 lines, for backwards compatibility)
|
|
272
|
+
└── package.json # Scripts updated
|
|
214
273
|
```
|
|
215
274
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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>
|
|
219
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"`)
|
|
@@ -389,10 +389,11 @@ const fileId = await uploadFileToGridFS(
|
|
|
389
389
|
|
|
390
390
|
### Migration Templates
|
|
391
391
|
|
|
392
|
-
|
|
392
|
+
Ready-to-use template for nest-server projects:
|
|
393
393
|
|
|
394
|
-
|
|
395
|
-
|
|
394
|
+
**Project Template**: `dist/core/modules/migrate/templates/migration-project.template.ts`
|
|
395
|
+
|
|
396
|
+
This template automatically imports `getDb()` and `uploadFileToGridFS()` from nest-server, along with your project's config. It's ready to use with zero configuration.
|
|
396
397
|
|
|
397
398
|
### TypeScript Compiler
|
|
398
399
|
|
|
@@ -404,7 +405,7 @@ migrate --compiler="ts:./node_modules/@lenne.tech/nest-server/dist/core/modules/
|
|
|
404
405
|
|
|
405
406
|
## Complete Setup Guide
|
|
406
407
|
|
|
407
|
-
For a complete step-by-step guide on
|
|
408
|
+
For a complete step-by-step guide on migrating from @nodepit/migrate-state-store-mongodb, see [MIGRATION_FROM_NODEPIT.md](./MIGRATION_FROM_NODEPIT.md).
|
|
408
409
|
|
|
409
410
|
### Quick Setup (No External Dependencies!) 🚀
|
|
410
411
|
|
|
@@ -419,16 +420,16 @@ For a complete step-by-step guide on setting up migrations in your project, see
|
|
|
419
420
|
```json
|
|
420
421
|
{
|
|
421
422
|
"scripts": {
|
|
422
|
-
"migrate:create": "migrate create --template-file ./
|
|
423
|
-
"migrate:up": "migrate --store
|
|
424
|
-
"migrate:down": "migrate --store
|
|
423
|
+
"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",
|
|
424
|
+
"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",
|
|
425
|
+
"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"
|
|
425
426
|
}
|
|
426
427
|
}
|
|
427
428
|
```
|
|
428
429
|
|
|
429
430
|
The `migrate` command comes from `@lenne.tech/nest-server` - no external package needed!
|
|
430
431
|
|
|
431
|
-
See the [Migration Guide](./
|
|
432
|
+
See the [Migration Guide](./MIGRATION_FROM_NODEPIT.md) for detailed migration instructions from @nodepit.
|
|
432
433
|
|
|
433
434
|
## Project Integration
|
|
434
435
|
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { getDb, uploadFileToGridFS } from '@lenne.tech/nest-server';
|
|
2
|
+
import { Db, ObjectId } from 'mongodb';
|
|
3
|
+
import config from '../src/config.env';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Migration template for nest-server projects
|
|
7
|
+
*
|
|
8
|
+
* This template is ready-to-use for @lenne.tech/nest-server projects.
|
|
9
|
+
* It imports the necessary helpers and config automatically.
|
|
10
|
+
*
|
|
11
|
+
* Available helpers:
|
|
12
|
+
* - getDb(uri): Get MongoDB connection
|
|
13
|
+
* - uploadFileToGridFS(uri, filePath, options): Upload file to GridFS
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const MONGO_URL = config.mongoose.uri;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Run migration
|
|
20
|
+
*
|
|
21
|
+
* Code your update script here!
|
|
22
|
+
*/
|
|
23
|
+
export const up = async () => {
|
|
24
|
+
const db: Db = await getDb(MONGO_URL);
|
|
25
|
+
|
|
26
|
+
// Example: Add a new field to all documents in a collection
|
|
27
|
+
// await db.collection('users').updateMany(
|
|
28
|
+
// { email: { $exists: false } },
|
|
29
|
+
// { $set: { email: '' } }
|
|
30
|
+
// );
|
|
31
|
+
|
|
32
|
+
// Example: Upload a file to GridFS
|
|
33
|
+
// const fileId: ObjectId = await uploadFileToGridFS(
|
|
34
|
+
// MONGO_URL,
|
|
35
|
+
// '../assets/image.png',
|
|
36
|
+
// { bucketName: 'images', filename: 'logo.png' }
|
|
37
|
+
// );
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Rollback migration
|
|
42
|
+
*
|
|
43
|
+
* Code your downgrade script here!
|
|
44
|
+
*/
|
|
45
|
+
export const down = async () => {
|
|
46
|
+
const db: Db = await getDb(MONGO_URL);
|
|
47
|
+
|
|
48
|
+
// Example: Remove the field added in the up() function
|
|
49
|
+
// await db.collection('users').updateMany(
|
|
50
|
+
// {},
|
|
51
|
+
// { $unset: { email: '' } }
|
|
52
|
+
// );
|
|
53
|
+
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.down = exports.up = void 0;
|
|
4
|
-
const up = async () => {
|
|
5
|
-
};
|
|
6
|
-
exports.up = up;
|
|
7
|
-
const down = async () => {
|
|
8
|
-
};
|
|
9
|
-
exports.down = down;
|
|
10
|
-
//# sourceMappingURL=migration-with-helper.template.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"migration-with-helper.template.js","sourceRoot":"","sources":["../../../../../src/core/modules/migrate/templates/migration-with-helper.template.ts"],"names":[],"mappings":";;;AAgCO,MAAM,EAAE,GAAG,KAAK,IAAI,EAAE;AAiB7B,CAAC,CAAC;AAjBW,QAAA,EAAE,MAiBb;AAKK,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE;AAiB/B,CAAC,CAAC;AAjBW,QAAA,IAAI,QAiBf"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.down = exports.up = void 0;
|
|
4
|
-
const getDb = async () => {
|
|
5
|
-
throw new Error('Please configure the getDb() function in this migration file or use the migration helper from @lenne.tech/nest-server');
|
|
6
|
-
};
|
|
7
|
-
const up = async () => {
|
|
8
|
-
const db = await getDb();
|
|
9
|
-
};
|
|
10
|
-
exports.up = up;
|
|
11
|
-
const down = async () => {
|
|
12
|
-
const db = await getDb();
|
|
13
|
-
};
|
|
14
|
-
exports.down = down;
|
|
15
|
-
//# sourceMappingURL=migration.template.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"migration.template.js","sourceRoot":"","sources":["../../../../../src/core/modules/migrate/templates/migration.template.ts"],"names":[],"mappings":";;;AAiBA,MAAM,KAAK,GAAG,KAAK,IAAiB,EAAE;IAQpC,MAAM,IAAI,KAAK,CACb,uHAAuH,CACxH,CAAC;AACJ,CAAC,CAAC;AAOK,MAAM,EAAE,GAAG,KAAK,IAAI,EAAE;IAC3B,MAAM,EAAE,GAAO,MAAM,KAAK,EAAE,CAAC;AAO/B,CAAC,CAAC;AARW,QAAA,EAAE,MAQb;AAOK,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE;IAC7B,MAAM,EAAE,GAAO,MAAM,KAAK,EAAE,CAAC;AAO/B,CAAC,CAAC;AARW,QAAA,IAAI,QAQf"}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// import { Db } from 'mongodb';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Migration template with helper function
|
|
5
|
-
*
|
|
6
|
-
* This template uses the migration helper from @lenne.tech/nest-server.
|
|
7
|
-
* To use this template, you need to create a helper function in your project
|
|
8
|
-
* that returns the database connection.
|
|
9
|
-
*
|
|
10
|
-
* Example setup in your project's migrations-utils/db.ts:
|
|
11
|
-
* ```typescript
|
|
12
|
-
* import config from '../src/config.env';
|
|
13
|
-
* import { MongoClient } from 'mongodb';
|
|
14
|
-
*
|
|
15
|
-
* export const getDb = async () => {
|
|
16
|
-
* const client = await MongoClient.connect(config.mongoose.uri);
|
|
17
|
-
* return client.db();
|
|
18
|
-
* };
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
// Import your project's database helper
|
|
23
|
-
// import { getDb } from '../migrations-utils/db';
|
|
24
|
-
|
|
25
|
-
// Or use the nest-server helper with your config:
|
|
26
|
-
// import config from '../src/config.env';
|
|
27
|
-
// import { getDb } from '@lenne.tech/nest-server';
|
|
28
|
-
// const db = await getDb(config.mongoose.uri);
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Run migration
|
|
32
|
-
*/
|
|
33
|
-
export const up = async () => {
|
|
34
|
-
// const db: Db = await getDb();
|
|
35
|
-
/*
|
|
36
|
-
Code your update script here!
|
|
37
|
-
|
|
38
|
-
Example: Add a new field to all documents
|
|
39
|
-
await db.collection('users').updateMany(
|
|
40
|
-
{ email: { $exists: false } },
|
|
41
|
-
{ $set: { email: '' } }
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
Example: Create a new collection
|
|
45
|
-
await db.createCollection('new_collection');
|
|
46
|
-
|
|
47
|
-
Example: Create an index
|
|
48
|
-
await db.collection('users').createIndex({ email: 1 }, { unique: true });
|
|
49
|
-
*/
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Rollback migration
|
|
54
|
-
*/
|
|
55
|
-
export const down = async () => {
|
|
56
|
-
// const db: Db = await getDb();
|
|
57
|
-
/*
|
|
58
|
-
Code your downgrade script here!
|
|
59
|
-
|
|
60
|
-
Example: Remove the field added in up()
|
|
61
|
-
await db.collection('users').updateMany(
|
|
62
|
-
{},
|
|
63
|
-
{ $unset: { email: '' } }
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
Example: Drop the collection
|
|
67
|
-
await db.dropCollection('new_collection');
|
|
68
|
-
|
|
69
|
-
Example: Drop the index
|
|
70
|
-
await db.collection('users').dropIndex('email_1');
|
|
71
|
-
*/
|
|
72
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Db } from 'mongodb';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Migration template for nest-server
|
|
5
|
-
*
|
|
6
|
-
* This template can be used with the migrate CLI:
|
|
7
|
-
* migrate create --template-file ./node_modules/@lenne.tech/nest-server/dist/core/modules/migrate/templates/migration.template.js
|
|
8
|
-
*
|
|
9
|
-
* Or copy this file to your project's migrations-utils folder and customize it.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Get database connection
|
|
14
|
-
*
|
|
15
|
-
* IMPORTANT: Replace this function with your actual database connection logic.
|
|
16
|
-
* This is a placeholder that should import your project's config.
|
|
17
|
-
*/
|
|
18
|
-
const getDb = async (): Promise<Db> => {
|
|
19
|
-
// TODO: Import your config and return the database connection
|
|
20
|
-
// Example:
|
|
21
|
-
// import config from '../src/config.env';
|
|
22
|
-
// const { MongoClient } = require('mongodb');
|
|
23
|
-
// const client = await MongoClient.connect(config.mongoose.uri);
|
|
24
|
-
// return client.db();
|
|
25
|
-
|
|
26
|
-
throw new Error(
|
|
27
|
-
'Please configure the getDb() function in this migration file or use the migration helper from @lenne.tech/nest-server',
|
|
28
|
-
);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Run migration
|
|
33
|
-
*
|
|
34
|
-
* Code your update script here!
|
|
35
|
-
*/
|
|
36
|
-
export const up = async () => {
|
|
37
|
-
const db: Db = await getDb();
|
|
38
|
-
|
|
39
|
-
// Example: Add a new field to all documents in a collection
|
|
40
|
-
// await db.collection('users').updateMany(
|
|
41
|
-
// { email: { $exists: false } },
|
|
42
|
-
// { $set: { email: '' } }
|
|
43
|
-
// );
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Rollback migration
|
|
48
|
-
*
|
|
49
|
-
* Code your downgrade script here!
|
|
50
|
-
*/
|
|
51
|
-
export const down = async () => {
|
|
52
|
-
const db: Db = await getDb();
|
|
53
|
-
|
|
54
|
-
// Example: Remove the field added in the up() function
|
|
55
|
-
// await db.collection('users').updateMany(
|
|
56
|
-
// {},
|
|
57
|
-
// { $unset: { email: '' } }
|
|
58
|
-
// );
|
|
59
|
-
};
|