@devmoods/express-extras 0.17.2 → 0.17.5

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/context.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare const getContext: typeof useContext;
17
17
  /**
18
18
  * Set a value on the request context
19
19
  */
20
- export declare function setContext<T>(key: string, value: T): T | undefined;
20
+ export declare function setContext<T>(key: string, value: T): any;
21
21
  export declare function useRequestId(): string | undefined;
22
22
  /**
23
23
  * @deprecated
package/dist/migrate.d.ts CHANGED
@@ -4,6 +4,11 @@ interface Migration {
4
4
  filename: string;
5
5
  version: number;
6
6
  }
7
+ interface MigrationFileAPI {
8
+ useTransaction?: boolean;
9
+ up(fn: any): Promise<string> | Promise<void> | string | void;
10
+ down(fn: any): Promise<string> | Promise<void> | string | void;
11
+ }
7
12
  export declare function cli(): Promise<void>;
8
13
  interface MigratorOptions {
9
14
  postgres?: ReturnType<typeof createPostgres>;
@@ -23,8 +28,8 @@ export declare class Migrator {
23
28
  runMigrations(direction: 'up' | 'down'): Promise<number>;
24
29
  log(...args: any[]): void;
25
30
  executeMigration(migrations: Migration[], currentVersion: number, direction: 'up' | 'down'): Promise<number>;
26
- readMigrationFile(filename: string): Promise<any>;
27
- migrate(tx: Connection, migration: Migration, direction: 'up' | 'down'): Promise<void>;
31
+ readMigrationFile(filename: string): Promise<MigrationFileAPI>;
32
+ migrate(connection: Connection, migrator: MigrationFileAPI, direction: 'up' | 'down'): Promise<void>;
28
33
  updateCurrentVersion(connection: Connection, version: number): Promise<import("pg").QueryResult<import("pg").QueryResultRow>>;
29
34
  getCurrentVersion(): Promise<number>;
30
35
  getMigrations(): Promise<{
package/dist/migrate.js CHANGED
@@ -104,10 +104,22 @@ export class Migrator {
104
104
  }
105
105
  for (const migration of pendingMigrations) {
106
106
  this.log('Running migration', migration.filename);
107
- await this.postgres.transaction(async (tx) => {
108
- await this.migrate(tx, migration, direction);
109
- await this.updateCurrentVersion(tx, migration.version);
110
- });
107
+ const migrator = await this.readMigrationFile(migration.filename);
108
+ if (!migrator.up || !migrator.down) {
109
+ throw new Error('Migrations must export both an up() and a down() function.');
110
+ }
111
+ if (migrator.useTransaction !== false) {
112
+ await this.postgres.transaction(async (tx) => {
113
+ await this.migrate(tx, migrator, direction);
114
+ await this.updateCurrentVersion(tx, migration.version);
115
+ });
116
+ }
117
+ else {
118
+ await this.postgres.withConnection(async (tx) => {
119
+ await this.migrate(tx, migrator, direction);
120
+ await this.updateCurrentVersion(tx, migration.version);
121
+ });
122
+ }
111
123
  }
112
124
  this.log(chalk.green('Done!'));
113
125
  return await this.getCurrentVersion();
@@ -122,10 +134,22 @@ export class Migrator {
122
134
  return currentVersion;
123
135
  }
124
136
  this.log('Rolling back migration', migrationToRollback.filename);
125
- await this.postgres.transaction(async (tx) => {
126
- await this.migrate(tx, migrationToRollback, 'down');
127
- await this.updateCurrentVersion(tx, updatedVersion);
128
- });
137
+ const migrator = await this.readMigrationFile(migrationToRollback.filename);
138
+ if (!migrator.up || !migrator.down) {
139
+ throw new Error('Migrations must export both an up() and a down() function.');
140
+ }
141
+ if (migrator.useTransaction !== false) {
142
+ await this.postgres.transaction(async (tx) => {
143
+ await this.migrate(tx, migrator, 'down');
144
+ await this.updateCurrentVersion(tx, updatedVersion);
145
+ });
146
+ }
147
+ else {
148
+ await this.postgres.withConnection(async (tx) => {
149
+ await this.migrate(tx, migrator, 'down');
150
+ await this.updateCurrentVersion(tx, updatedVersion);
151
+ });
152
+ }
129
153
  return await this.getCurrentVersion();
130
154
  }
131
155
  throw new Error(`Invalid direction ${direction}`);
@@ -137,6 +161,7 @@ export class Migrator {
137
161
  const content = await fs.readFile(filename, 'utf-8');
138
162
  const [up, down] = content.split('-- ROLLBACK --');
139
163
  return {
164
+ useTransaction: !content.includes('-- NO TRANSACTION --'),
140
165
  up() {
141
166
  return up.trim();
142
167
  },
@@ -145,17 +170,13 @@ export class Migrator {
145
170
  },
146
171
  };
147
172
  }
148
- async migrate(tx, migration, direction) {
149
- const migrator = await this.readMigrationFile(migration.filename);
150
- if (!migrator.up || !migrator.down) {
151
- throw new Error('Migrations must export both an up() and a down() function.');
152
- }
173
+ async migrate(connection, migrator, direction) {
153
174
  try {
154
175
  const queryWithLog = (sql) => {
155
176
  this.log(chalk.yellow(sql.toString()));
156
- return tx.query(sql);
177
+ return connection.query(sql);
157
178
  };
158
- const returnValue = await migrator[direction](queryWithLog.bind(tx));
179
+ const returnValue = await migrator[direction](queryWithLog.bind(connection));
159
180
  if (typeof returnValue === 'string') {
160
181
  if (returnValue !== '') {
161
182
  await queryWithLog(returnValue);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.17.2",
3
+ "version": "0.17.5",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -35,12 +35,12 @@
35
35
  "license": "ISC",
36
36
  "devDependencies": {
37
37
  "@devmoods/eslint-config": "^1.1.0",
38
- "@types/node": "20.2.5",
38
+ "@types/node": "20.3.1",
39
39
  "@types/supertest": "2.0.12",
40
- "@typescript-eslint/eslint-plugin": "^5.59.8",
41
- "@typescript-eslint/parser": "^5.59.8",
40
+ "@typescript-eslint/eslint-plugin": "^5.59.11",
41
+ "@typescript-eslint/parser": "^5.59.11",
42
42
  "chalk": "^5.2.0",
43
- "eslint": "^8.41.0",
43
+ "eslint": "^8.43.0",
44
44
  "eslint-plugin-jsx-a11y": "^6.7.1",
45
45
  "eslint-plugin-prettier": "^4.2.1",
46
46
  "eslint-plugin-react": "^7.32.2",
@@ -51,37 +51,37 @@
51
51
  "prettier": "2.8.8",
52
52
  "supertest": "6.3.3",
53
53
  "ts-node": "10.9.1",
54
- "typescript": "5.0.4",
55
- "vitest": "^0.31.2"
54
+ "typescript": "5.1.3",
55
+ "vitest": "^0.32.2"
56
56
  },
57
57
  "dependencies": {
58
58
  "@devmoods/fetch": "^3.3.0",
59
59
  "@godaddy/terminus": "^4.12.0",
60
- "@sentry/node": "^7.53.1",
61
- "@sentry/tracing": "^7.53.1",
60
+ "@sentry/node": "^7.55.2",
61
+ "@sentry/tracing": "^7.55.2",
62
62
  "@types/bcrypt": "^5.0.0",
63
63
  "@types/bunyan": "^1.8.8",
64
- "@types/cls-hooked": "^4.3.3",
64
+ "@types/cls-hooked": "^4.3.4",
65
65
  "@types/cookie-parser": "^1.4.3",
66
66
  "@types/express": "^4.17.17",
67
67
  "@types/jsonwebtoken": "^9.0.2",
68
68
  "@types/nodemailer": "^6.4.8",
69
- "@types/pg": "^8.10.1",
70
- "@types/uuid": "^9.0.1",
69
+ "@types/pg": "^8.10.2",
70
+ "@types/uuid": "^9.0.2",
71
71
  "ajv": "^8.12.0",
72
72
  "bcrypt": "^5.1.0",
73
73
  "bunyan": "^1.8.15",
74
74
  "cls-hooked": "^4.2.2",
75
75
  "compose-middleware": "^5.0.1",
76
76
  "cookie-parser": "^1.4.6",
77
- "dotenv": "^16.1.0",
77
+ "dotenv": "^16.3.1",
78
78
  "express": "^4.18.2",
79
79
  "helmet": "^7.0.0",
80
80
  "jsonwebtoken": "^9.0.0",
81
81
  "meow": "^12.0.1",
82
82
  "nodemailer": "^6.9.3",
83
83
  "pg": "^8.11.0",
84
- "redis": "^4.6.6",
84
+ "redis": "^4.6.7",
85
85
  "sql-template-strings": "^2.2.2",
86
86
  "uuid": "^9.0.0"
87
87
  },