@zola_do/typeorm 0.2.8 → 0.2.12
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/README.md +50 -46
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/seeder-options.d.ts +3 -0
- package/dist/seeder-options.js +3 -0
- package/dist/seeder-options.js.map +1 -0
- package/dist/typeorm-config-helper.d.ts +1 -1
- package/dist/typeorm-config-helper.js +3 -6
- package/dist/typeorm-config-helper.js.map +1 -1
- package/dist/typeorm.service.d.ts +1 -1
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ TypeORM configuration helpers and service for NestJS applications.
|
|
|
12
12
|
|
|
13
13
|
- **Environment-based Configuration** — Database settings from environment variables
|
|
14
14
|
- **Auto-entity Discovery** — Automatically loads entities and migrations
|
|
15
|
-
- **DataSource Options** — TypeORM 0.3+ compatible configuration
|
|
15
|
+
- **DataSource Options** — TypeORM 0.3+ and 1.0 compatible configuration
|
|
16
16
|
- **Config Service** — Injectable service for database access
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
@@ -28,9 +28,13 @@ npm install @zola_do/nestjs-shared
|
|
|
28
28
|
### Dependencies
|
|
29
29
|
|
|
30
30
|
```bash
|
|
31
|
-
npm install @nestjs/typeorm typeorm
|
|
31
|
+
npm install @nestjs/typeorm typeorm dotenv
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
+
`typeorm-extension` is optional (for seeding CLI). `@zola_do/typeorm` exports a local `SeederOptions` type so seed config works without importing `typeorm-extension`.
|
|
35
|
+
|
|
36
|
+
**TypeORM 1.0:** Use `@nestjs/typeorm` ≥ 11.0.1 and Node.js 20+. Peer range supports `^0.3.0 || ^1.0.0`.
|
|
37
|
+
|
|
34
38
|
## Read replicas (TypeORM `replication`)
|
|
35
39
|
|
|
36
40
|
`@zola_do/typeorm` exposes `dataSourceOptions` as a starting point. For read scaling, use TypeORM's [`replication`](https://typeorm.io/data-source-options#postgres--cockroachdb--sap-hana--sql.js) option in the object you pass to `TypeOrmModule.forRoot`: supply `master` (writer) and `slaves` (readers). Environment variables are then modeled in your app (for example `DATABASE_REPLICA_HOSTS` parsed to an array) rather than hard-coded in the library.
|
|
@@ -52,9 +56,9 @@ APP_NAME=myapp
|
|
|
52
56
|
### 2. Register in AppModule
|
|
53
57
|
|
|
54
58
|
```typescript
|
|
55
|
-
import { Module } from
|
|
56
|
-
import { TypeOrmModule } from
|
|
57
|
-
import { dataSourceOptions } from
|
|
59
|
+
import { Module } from '@nestjs/common';
|
|
60
|
+
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
61
|
+
import { dataSourceOptions } from '@zola_do/typeorm';
|
|
58
62
|
|
|
59
63
|
@Module({
|
|
60
64
|
imports: [TypeOrmModule.forRoot(dataSourceOptions)],
|
|
@@ -65,15 +69,15 @@ export class AppModule {}
|
|
|
65
69
|
### 3. Use Entities
|
|
66
70
|
|
|
67
71
|
```typescript
|
|
68
|
-
import { CommonEntity } from
|
|
69
|
-
import { Entity, Column } from
|
|
72
|
+
import { CommonEntity } from '@zola_do/nestjs-shared';
|
|
73
|
+
import { Entity, Column } from 'typeorm';
|
|
70
74
|
|
|
71
|
-
@Entity(
|
|
75
|
+
@Entity('products')
|
|
72
76
|
export class Product extends CommonEntity {
|
|
73
77
|
@Column()
|
|
74
78
|
name: string;
|
|
75
79
|
|
|
76
|
-
@Column(
|
|
80
|
+
@Column('decimal', { precision: 10, scale: 2 })
|
|
77
81
|
price: number;
|
|
78
82
|
}
|
|
79
83
|
```
|
|
@@ -85,12 +89,12 @@ export class Product extends CommonEntity {
|
|
|
85
89
|
All entities should extend `CommonEntity` for consistent audit fields:
|
|
86
90
|
|
|
87
91
|
```typescript
|
|
88
|
-
import { CommonEntity } from
|
|
89
|
-
import { Entity, Column, PrimaryGeneratedColumn } from
|
|
92
|
+
import { CommonEntity } from '@zola_do/nestjs-shared';
|
|
93
|
+
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
|
|
90
94
|
|
|
91
|
-
@Entity(
|
|
95
|
+
@Entity('products')
|
|
92
96
|
export class Product extends CommonEntity {
|
|
93
|
-
@PrimaryGeneratedColumn(
|
|
97
|
+
@PrimaryGeneratedColumn('uuid')
|
|
94
98
|
id: string;
|
|
95
99
|
|
|
96
100
|
@Column()
|
|
@@ -114,9 +118,9 @@ export class Product extends CommonEntity {
|
|
|
114
118
|
#### Basic Entity
|
|
115
119
|
|
|
116
120
|
```typescript
|
|
117
|
-
@Entity(
|
|
121
|
+
@Entity('categories')
|
|
118
122
|
export class Category extends CommonEntity {
|
|
119
|
-
@PrimaryGeneratedColumn(
|
|
123
|
+
@PrimaryGeneratedColumn('uuid')
|
|
120
124
|
id: string;
|
|
121
125
|
|
|
122
126
|
@Column()
|
|
@@ -133,22 +137,22 @@ export class Category extends CommonEntity {
|
|
|
133
137
|
#### Entity with Relations
|
|
134
138
|
|
|
135
139
|
```typescript
|
|
136
|
-
@Entity(
|
|
140
|
+
@Entity('orders')
|
|
137
141
|
export class Order extends CommonEntity {
|
|
138
|
-
@PrimaryGeneratedColumn(
|
|
142
|
+
@PrimaryGeneratedColumn('uuid')
|
|
139
143
|
id: string;
|
|
140
144
|
|
|
141
145
|
@Column()
|
|
142
146
|
customerId: string;
|
|
143
147
|
|
|
144
|
-
@Column(
|
|
148
|
+
@Column('decimal', { precision: 10, scale: 2 })
|
|
145
149
|
total: number;
|
|
146
150
|
|
|
147
|
-
@Column({ default:
|
|
151
|
+
@Column({ default: 'pending' })
|
|
148
152
|
status: string;
|
|
149
153
|
|
|
150
154
|
@ManyToOne(() => Customer, (customer) => customer.orders)
|
|
151
|
-
@JoinColumn({ name:
|
|
155
|
+
@JoinColumn({ name: 'customerId' })
|
|
152
156
|
customer: Customer;
|
|
153
157
|
|
|
154
158
|
@OneToMany(() => OrderItem, (item) => item.order)
|
|
@@ -159,20 +163,20 @@ export class Order extends CommonEntity {
|
|
|
159
163
|
#### Entity with Indexes
|
|
160
164
|
|
|
161
165
|
```typescript
|
|
162
|
-
@Entity(
|
|
166
|
+
@Entity('products', {
|
|
163
167
|
indexes: [
|
|
164
|
-
{ name:
|
|
165
|
-
{ name:
|
|
168
|
+
{ name: 'idx_product_name', columns: ['name'] },
|
|
169
|
+
{ name: 'idx_product_status_price', columns: ['status', 'price'] },
|
|
166
170
|
],
|
|
167
171
|
})
|
|
168
172
|
export class Product extends CommonEntity {
|
|
169
173
|
@Column()
|
|
170
174
|
name: string;
|
|
171
175
|
|
|
172
|
-
@Column({ default:
|
|
176
|
+
@Column({ default: 'active' })
|
|
173
177
|
status: string;
|
|
174
178
|
|
|
175
|
-
@Column(
|
|
179
|
+
@Column('decimal', { precision: 10, scale: 2 })
|
|
176
180
|
price: number;
|
|
177
181
|
}
|
|
178
182
|
```
|
|
@@ -182,7 +186,7 @@ export class Product extends CommonEntity {
|
|
|
182
186
|
Access individual configuration values:
|
|
183
187
|
|
|
184
188
|
```typescript
|
|
185
|
-
import { TypeOrmConfigHelper } from
|
|
189
|
+
import { TypeOrmConfigHelper } from '@zola_do/typeorm';
|
|
186
190
|
|
|
187
191
|
console.log(TypeOrmConfigHelper.DATABASE_HOST); // 'localhost'
|
|
188
192
|
console.log(TypeOrmConfigHelper.DATABASE_PORT); // 5432
|
|
@@ -207,8 +211,8 @@ console.log(TypeOrmConfigHelper.DATABASE_PASSWORD); // 'secret'
|
|
|
207
211
|
Injectable service for direct database access:
|
|
208
212
|
|
|
209
213
|
```typescript
|
|
210
|
-
import { Injectable } from
|
|
211
|
-
import { TypeOrmService } from
|
|
214
|
+
import { Injectable } from '@nestjs/common';
|
|
215
|
+
import { TypeOrmService } from '@zola_do/typeorm';
|
|
212
216
|
|
|
213
217
|
@Injectable()
|
|
214
218
|
export class AnalyticsService {
|
|
@@ -256,7 +260,7 @@ import { dataSourceOptions } from '@zola_do/typeorm';
|
|
|
256
260
|
|
|
257
261
|
```typescript
|
|
258
262
|
// app.module.ts
|
|
259
|
-
import { dataSourceOptions } from
|
|
263
|
+
import { dataSourceOptions } from '@zola_do/typeorm';
|
|
260
264
|
|
|
261
265
|
@Module({
|
|
262
266
|
imports: [
|
|
@@ -264,7 +268,7 @@ import { dataSourceOptions } from "@zola_do/typeorm";
|
|
|
264
268
|
...dataSourceOptions,
|
|
265
269
|
synchronize: false,
|
|
266
270
|
migrationsRun: true,
|
|
267
|
-
logging: [
|
|
271
|
+
logging: ['error', 'warn'],
|
|
268
272
|
}),
|
|
269
273
|
],
|
|
270
274
|
})
|
|
@@ -294,12 +298,12 @@ Entities and migrations are auto-discovered:
|
|
|
294
298
|
```typescript
|
|
295
299
|
// dataSourceOptions.entities supports glob patterns
|
|
296
300
|
entities: [
|
|
297
|
-
__dirname +
|
|
298
|
-
|
|
301
|
+
__dirname + '/../../**/*.entity.js', // Compiled output
|
|
302
|
+
'**/*.entity.js', // Source files
|
|
299
303
|
];
|
|
300
304
|
|
|
301
305
|
// dataSourceOptions.migrations supports glob patterns
|
|
302
|
-
migrations: [__dirname +
|
|
306
|
+
migrations: [__dirname + '/../../**/*.migration.js'];
|
|
303
307
|
```
|
|
304
308
|
|
|
305
309
|
### Entity File Location
|
|
@@ -330,24 +334,24 @@ npx typeorm migration:run -d dist/typeorm/data-source.js
|
|
|
330
334
|
### Create Migration Manually
|
|
331
335
|
|
|
332
336
|
```typescript
|
|
333
|
-
import { MigrationInterface, QueryRunner, Table } from
|
|
337
|
+
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
|
|
334
338
|
|
|
335
339
|
export class CreateProducts1704064000000 implements MigrationInterface {
|
|
336
340
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
337
341
|
await queryRunner.createTable(
|
|
338
342
|
new Table({
|
|
339
|
-
name:
|
|
343
|
+
name: 'products',
|
|
340
344
|
columns: [
|
|
341
345
|
{
|
|
342
|
-
name:
|
|
343
|
-
type:
|
|
346
|
+
name: 'id',
|
|
347
|
+
type: 'uuid',
|
|
344
348
|
isPrimary: true,
|
|
345
|
-
generationStrategy:
|
|
349
|
+
generationStrategy: 'uuid',
|
|
346
350
|
},
|
|
347
|
-
{ name:
|
|
348
|
-
{ name:
|
|
349
|
-
{ name:
|
|
350
|
-
{ name:
|
|
351
|
+
{ name: 'name', type: 'varchar' },
|
|
352
|
+
{ name: 'price', type: 'decimal', precision: 10, scale: 2 },
|
|
353
|
+
{ name: 'created_at', type: 'timestamp', default: 'now()' },
|
|
354
|
+
{ name: 'updated_at', type: 'timestamp', default: 'now()' },
|
|
351
355
|
],
|
|
352
356
|
}),
|
|
353
357
|
true,
|
|
@@ -355,7 +359,7 @@ export class CreateProducts1704064000000 implements MigrationInterface {
|
|
|
355
359
|
}
|
|
356
360
|
|
|
357
361
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
358
|
-
await queryRunner.dropTable(
|
|
362
|
+
await queryRunner.dropTable('products');
|
|
359
363
|
}
|
|
360
364
|
}
|
|
361
365
|
```
|
|
@@ -375,14 +379,14 @@ TypeOrmConfigHelper.DATABASE_PASSWORD;
|
|
|
375
379
|
### DataSource
|
|
376
380
|
|
|
377
381
|
```typescript
|
|
378
|
-
import { dataSourceOptions } from
|
|
382
|
+
import { dataSourceOptions } from '@zola_do/typeorm';
|
|
379
383
|
// Type: DataSourceOptions
|
|
380
384
|
```
|
|
381
385
|
|
|
382
386
|
### Service
|
|
383
387
|
|
|
384
388
|
```typescript
|
|
385
|
-
import { TypeOrmService } from
|
|
389
|
+
import { TypeOrmService } from '@zola_do/typeorm';
|
|
386
390
|
|
|
387
391
|
@Injectable()
|
|
388
392
|
class MyService {
|
|
@@ -401,7 +405,7 @@ class MyService {
|
|
|
401
405
|
Check glob pattern in `dataSourceOptions.entities`. The pattern should match your compiled `.js` files:
|
|
402
406
|
|
|
403
407
|
```typescript
|
|
404
|
-
entities: [__dirname +
|
|
408
|
+
entities: [__dirname + '/../../**/*.entity.js'];
|
|
405
409
|
```
|
|
406
410
|
|
|
407
411
|
### Q: Migrations not running?
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./typeorm-config-helper"), exports);
|
|
18
18
|
__exportStar(require("./typeorm.service"), exports);
|
|
19
|
+
__exportStar(require("./seeder-options"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC;AACxC,oDAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0DAAwC;AACxC,oDAAkC;AAClC,mDAAiC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seeder-options.js","sourceRoot":"","sources":["../src/seeder-options.ts"],"names":[],"mappings":""}
|
|
@@ -42,10 +42,7 @@ exports.dataSourceOptions = {
|
|
|
42
42
|
database: exports.TypeOrmConfigHelper.DATABASE_NAME,
|
|
43
43
|
username: exports.TypeOrmConfigHelper.DATABASE_USER,
|
|
44
44
|
password: exports.TypeOrmConfigHelper.DATABASE_PASSWORD,
|
|
45
|
-
entities: [
|
|
46
|
-
entities,
|
|
47
|
-
views
|
|
48
|
-
],
|
|
45
|
+
entities: [entities, views],
|
|
49
46
|
migrations: [migrations],
|
|
50
47
|
migrationsRun: true,
|
|
51
48
|
seeds: [seeds],
|
|
@@ -55,8 +52,8 @@ exports.dataSourceOptions = {
|
|
|
55
52
|
synchronize: false,
|
|
56
53
|
autoLoadEntities: true,
|
|
57
54
|
invalidWhereValuesBehavior: {
|
|
58
|
-
null:
|
|
59
|
-
undefined:
|
|
55
|
+
null: 'sql-null',
|
|
56
|
+
undefined: 'throw',
|
|
60
57
|
},
|
|
61
58
|
};
|
|
62
59
|
const dataSources = new typeorm_1.DataSource(exports.dataSourceOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeorm-config-helper.js","sourceRoot":"","sources":["../src/typeorm-config-helper.ts"],"names":[],"mappings":";;;;AAAA,iCAAiC;
|
|
1
|
+
{"version":3,"file":"typeorm-config-helper.js","sourceRoot":"","sources":["../src/typeorm-config-helper.ts"],"names":[],"mappings":";;;;AAAA,iCAAiC;AACjC,qCAAwD;AAExD,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAGhC,MAAM,sBAAsB,GAAG,GAAG,EAAE;IAClC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,iGAAiG,CAClG,CAAC;IACJ,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;QACxD,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAGF,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE,CAAC;IAC1C,sBAAsB,EAAE,CAAC;AAC3B,CAAC;AAEY,QAAA,mBAAmB,GAAG;IACjC,aAAa,EAAE,MAAA,OAAO,CAAC,GAAG,CAAC,aAAa,mCAAI,WAAW;IACvD,aAAa,EAAE,MAAA,OAAO,CAAC,GAAG,CAAC,aAAa,mCAAI,MAAM;IAClD,aAAa,EAAE,MAAA,OAAO,CAAC,GAAG,CAAC,aAAa,mCAAI,OAAO,CAAC,GAAG,CAAC,QAAQ;IAChE,aAAa,EAAE,MAAA,OAAO,CAAC,GAAG,CAAC,aAAa,mCAAI,UAAU;IACtD,iBAAiB,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB;CACjD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AS,EAAE,OAAO;KACnB;CACmC,CAAC;AAEvC,MAAM,WAAW,GAAG,IAAI,oBAAU,CAAC,yBAAiB,CAAC,CAAC;AAEtD,kBAAe,WAAW,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TypeOrmOptionsFactory, TypeOrmModuleOptions } from '@nestjs/typeorm';
|
|
2
|
-
import { SeederOptions } from '
|
|
2
|
+
import { SeederOptions } from './seeder-options';
|
|
3
3
|
export declare class TypeOrmConfigService implements TypeOrmOptionsFactory {
|
|
4
4
|
createTypeOrmOptions(): TypeOrmModuleOptions & SeederOptions;
|
|
5
5
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zola_do/typeorm",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"description": "TypeORM configuration for NestJS",
|
|
5
5
|
"author": "zolaDO",
|
|
6
6
|
"license": "ISC",
|
|
@@ -32,10 +32,15 @@
|
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
35
|
-
"@nestjs/typeorm": "^10.0.0 || ^11.0.
|
|
36
|
-
"typeorm": "^0.3.0",
|
|
35
|
+
"@nestjs/typeorm": "^10.0.0 || ^11.0.1",
|
|
36
|
+
"typeorm": "^0.3.0 || ^1.0.0",
|
|
37
37
|
"typeorm-extension": "^3.7.1"
|
|
38
38
|
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"typeorm-extension": {
|
|
41
|
+
"optional": true
|
|
42
|
+
}
|
|
43
|
+
},
|
|
39
44
|
"dependencies": {
|
|
40
45
|
"dotenv": "^17.3.1"
|
|
41
46
|
},
|