@midwayjs/typeorm 3.4.0-beta.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/README.md +96 -0
- package/dist/configuration.d.ts +11 -0
- package/dist/configuration.js +62 -0
- package/dist/dataSourceManager.d.ts +12 -0
- package/dist/dataSourceManager.js +56 -0
- package/dist/decorator.d.ts +10 -0
- package/dist/decorator.js +28 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +27 -0
- package/dist/interface.d.ts +4 -0
- package/dist/interface.js +3 -0
- package/index.d.ts +9 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# midway typeorm component
|
|
2
|
+
|
|
3
|
+
## How to use
|
|
4
|
+
|
|
5
|
+
in Configuration.ts file
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import * as typeorm from '@midwayjs/typeorm';
|
|
9
|
+
import { join } from 'path';
|
|
10
|
+
|
|
11
|
+
@Configuration({
|
|
12
|
+
imports: [
|
|
13
|
+
typeorm,
|
|
14
|
+
],
|
|
15
|
+
importConfigs: [
|
|
16
|
+
join(__dirname, './config')
|
|
17
|
+
]
|
|
18
|
+
})
|
|
19
|
+
export class ContainerConfiguration {
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
in config files
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
export default {
|
|
30
|
+
typeorm: {
|
|
31
|
+
dataSource: {
|
|
32
|
+
default: {
|
|
33
|
+
type: 'mysql',
|
|
34
|
+
host: '',
|
|
35
|
+
port: 3306,
|
|
36
|
+
username: '',
|
|
37
|
+
password: '',
|
|
38
|
+
database: undefined,
|
|
39
|
+
synchronize: true,
|
|
40
|
+
logging: false,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Define EntityModel
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// model/user.ts
|
|
51
|
+
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
|
|
52
|
+
|
|
53
|
+
@Entity('test_user')
|
|
54
|
+
export class Photo {
|
|
55
|
+
@PrimaryGeneratedColumn({ name: "id" })
|
|
56
|
+
id: number;
|
|
57
|
+
|
|
58
|
+
@Column({ name: "name" })
|
|
59
|
+
name: string;
|
|
60
|
+
|
|
61
|
+
@OneToMany(type => Message, message => message.sender)
|
|
62
|
+
messages: Message[];
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
## Use Model
|
|
68
|
+
|
|
69
|
+
in code files
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { InjectEntityModel } from '@midwayjs/orm';
|
|
73
|
+
import { User } from './model/user';
|
|
74
|
+
import { Repository } from 'typeorm';
|
|
75
|
+
|
|
76
|
+
@Provide()
|
|
77
|
+
export class UserService {
|
|
78
|
+
|
|
79
|
+
@InjectEntityModel(User)
|
|
80
|
+
userModel: Repository<User>;
|
|
81
|
+
|
|
82
|
+
async testUser() {
|
|
83
|
+
const u = new User();
|
|
84
|
+
u.name = 'oneuser1';
|
|
85
|
+
const uu = await this.userModel.save(u);
|
|
86
|
+
console.log('user one id = ', uu.id);
|
|
87
|
+
|
|
88
|
+
const user = new User();
|
|
89
|
+
user.id = 1;
|
|
90
|
+
const users = await this.userModel.findAndCount({
|
|
91
|
+
where: user
|
|
92
|
+
});
|
|
93
|
+
return 'hello world' + JSON.stringify(users);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ILifeCycle, IMidwayApplication, IMidwayContainer, MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { TypeORMDataSourceManager } from './dataSourceManager';
|
|
3
|
+
export declare class OrmConfiguration implements ILifeCycle {
|
|
4
|
+
app: IMidwayApplication;
|
|
5
|
+
decoratorService: MidwayDecoratorService;
|
|
6
|
+
dataSourceManager: TypeORMDataSourceManager;
|
|
7
|
+
init(): Promise<void>;
|
|
8
|
+
onReady(container: IMidwayContainer): Promise<void>;
|
|
9
|
+
onStop(container: IMidwayContainer): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.OrmConfiguration = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
15
|
+
const decorator_2 = require("./decorator");
|
|
16
|
+
const dataSourceManager_1 = require("./dataSourceManager");
|
|
17
|
+
const typeorm_1 = require("typeorm");
|
|
18
|
+
let OrmConfiguration = class OrmConfiguration {
|
|
19
|
+
async init() {
|
|
20
|
+
this.decoratorService.registerPropertyHandler(decorator_2.ORM_MODEL_KEY, (propertyName, meta) => {
|
|
21
|
+
return this.dataSourceManager
|
|
22
|
+
.getDataSource(meta.connectionName)
|
|
23
|
+
.getRepository(meta.modelKey);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async onReady(container) {
|
|
27
|
+
(0, typeorm_1.useContainer)(container);
|
|
28
|
+
this.dataSourceManager = await container.getAsync(dataSourceManager_1.TypeORMDataSourceManager);
|
|
29
|
+
}
|
|
30
|
+
async onStop(container) {
|
|
31
|
+
const dataSourceManager = await container.getAsync(dataSourceManager_1.TypeORMDataSourceManager);
|
|
32
|
+
await dataSourceManager.stop();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
__decorate([
|
|
36
|
+
(0, decorator_1.App)(),
|
|
37
|
+
__metadata("design:type", Object)
|
|
38
|
+
], OrmConfiguration.prototype, "app", void 0);
|
|
39
|
+
__decorate([
|
|
40
|
+
(0, decorator_1.Inject)(),
|
|
41
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
42
|
+
], OrmConfiguration.prototype, "decoratorService", void 0);
|
|
43
|
+
__decorate([
|
|
44
|
+
(0, decorator_1.Init)(),
|
|
45
|
+
__metadata("design:type", Function),
|
|
46
|
+
__metadata("design:paramtypes", []),
|
|
47
|
+
__metadata("design:returntype", Promise)
|
|
48
|
+
], OrmConfiguration.prototype, "init", null);
|
|
49
|
+
OrmConfiguration = __decorate([
|
|
50
|
+
(0, decorator_1.Configuration)({
|
|
51
|
+
importConfigs: [
|
|
52
|
+
{
|
|
53
|
+
default: {
|
|
54
|
+
typeorm: {},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
namespace: 'typeorm',
|
|
59
|
+
})
|
|
60
|
+
], OrmConfiguration);
|
|
61
|
+
exports.OrmConfiguration = OrmConfiguration;
|
|
62
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataSourceManager, IMidwayContainer } from '@midwayjs/core';
|
|
2
|
+
import { DataSource } from 'typeorm';
|
|
3
|
+
export declare class TypeORMDataSourceManager extends DataSourceManager<DataSource> {
|
|
4
|
+
typeormConfig: any;
|
|
5
|
+
applicationContext: IMidwayContainer;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
getName(): string;
|
|
8
|
+
protected createDataSource(config: any, dataSourceName: string): Promise<DataSource>;
|
|
9
|
+
protected checkConnected(dataSource: DataSource): Promise<boolean>;
|
|
10
|
+
protected destroyDataSource(dataSource: DataSource): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=dataSourceManager.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TypeORMDataSourceManager = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const core_1 = require("@midwayjs/core");
|
|
15
|
+
const typeorm_1 = require("typeorm");
|
|
16
|
+
let TypeORMDataSourceManager = class TypeORMDataSourceManager extends core_1.DataSourceManager {
|
|
17
|
+
async init() {
|
|
18
|
+
await this.initDataSource(this.typeormConfig);
|
|
19
|
+
}
|
|
20
|
+
getName() {
|
|
21
|
+
return 'typeorm';
|
|
22
|
+
}
|
|
23
|
+
async createDataSource(config, dataSourceName) {
|
|
24
|
+
const dataSource = new typeorm_1.DataSource(config);
|
|
25
|
+
await dataSource.initialize();
|
|
26
|
+
return dataSource;
|
|
27
|
+
}
|
|
28
|
+
async checkConnected(dataSource) {
|
|
29
|
+
return dataSource.isInitialized;
|
|
30
|
+
}
|
|
31
|
+
async destroyDataSource(dataSource) {
|
|
32
|
+
if (dataSource.isInitialized) {
|
|
33
|
+
await dataSource.destroy();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, decorator_1.Config)('typeorm'),
|
|
39
|
+
__metadata("design:type", Object)
|
|
40
|
+
], TypeORMDataSourceManager.prototype, "typeormConfig", void 0);
|
|
41
|
+
__decorate([
|
|
42
|
+
(0, decorator_1.ApplicationContext)(),
|
|
43
|
+
__metadata("design:type", Object)
|
|
44
|
+
], TypeORMDataSourceManager.prototype, "applicationContext", void 0);
|
|
45
|
+
__decorate([
|
|
46
|
+
(0, decorator_1.Init)(),
|
|
47
|
+
__metadata("design:type", Function),
|
|
48
|
+
__metadata("design:paramtypes", []),
|
|
49
|
+
__metadata("design:returntype", Promise)
|
|
50
|
+
], TypeORMDataSourceManager.prototype, "init", null);
|
|
51
|
+
TypeORMDataSourceManager = __decorate([
|
|
52
|
+
(0, decorator_1.Provide)(),
|
|
53
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
54
|
+
], TypeORMDataSourceManager);
|
|
55
|
+
exports.TypeORMDataSourceManager = TypeORMDataSourceManager;
|
|
56
|
+
//# sourceMappingURL=dataSourceManager.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const ENTITY_MODEL_KEY = "typeorm:entity_model_key";
|
|
2
|
+
export declare const EVENT_SUBSCRIBER_KEY = "typeorm:event_subscriber_key";
|
|
3
|
+
export declare const ORM_MODEL_KEY = "typeorm:orm_model_key";
|
|
4
|
+
export declare function InjectEntityModel(modelKey: any, connectionName?: string): PropertyDecorator;
|
|
5
|
+
/**
|
|
6
|
+
* EventSubscriber - typeorm
|
|
7
|
+
* implements EntitySubscriberInterface
|
|
8
|
+
*/
|
|
9
|
+
export declare function EventSubscriberModel(): ClassDecorator;
|
|
10
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventSubscriberModel = exports.InjectEntityModel = exports.ORM_MODEL_KEY = exports.EVENT_SUBSCRIBER_KEY = exports.ENTITY_MODEL_KEY = void 0;
|
|
4
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
5
|
+
const typeorm_1 = require("typeorm");
|
|
6
|
+
exports.ENTITY_MODEL_KEY = 'typeorm:entity_model_key';
|
|
7
|
+
exports.EVENT_SUBSCRIBER_KEY = 'typeorm:event_subscriber_key';
|
|
8
|
+
exports.ORM_MODEL_KEY = 'typeorm:orm_model_key';
|
|
9
|
+
function InjectEntityModel(modelKey, connectionName = 'default') {
|
|
10
|
+
return (0, decorator_1.createCustomPropertyDecorator)(exports.ORM_MODEL_KEY, {
|
|
11
|
+
modelKey,
|
|
12
|
+
connectionName,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
exports.InjectEntityModel = InjectEntityModel;
|
|
16
|
+
/**
|
|
17
|
+
* EventSubscriber - typeorm
|
|
18
|
+
* implements EntitySubscriberInterface
|
|
19
|
+
*/
|
|
20
|
+
function EventSubscriberModel() {
|
|
21
|
+
return function (target) {
|
|
22
|
+
(0, decorator_1.Provide)()(target);
|
|
23
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)(target);
|
|
24
|
+
(0, typeorm_1.EventSubscriber)()(target);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
exports.EventSubscriberModel = EventSubscriberModel;
|
|
28
|
+
//# sourceMappingURL=decorator.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ENTITY_MODEL_KEY, EVENT_SUBSCRIBER_KEY, ORM_MODEL_KEY, } from './decorator';
|
|
2
|
+
export { OrmConfiguration as Configuration } from './configuration';
|
|
3
|
+
export * from './decorator';
|
|
4
|
+
export * from './interface';
|
|
5
|
+
export * from './dataSourceManager';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Configuration = exports.ORM_MODEL_KEY = exports.EVENT_SUBSCRIBER_KEY = exports.ENTITY_MODEL_KEY = void 0;
|
|
18
|
+
var decorator_1 = require("./decorator");
|
|
19
|
+
Object.defineProperty(exports, "ENTITY_MODEL_KEY", { enumerable: true, get: function () { return decorator_1.ENTITY_MODEL_KEY; } });
|
|
20
|
+
Object.defineProperty(exports, "EVENT_SUBSCRIBER_KEY", { enumerable: true, get: function () { return decorator_1.EVENT_SUBSCRIBER_KEY; } });
|
|
21
|
+
Object.defineProperty(exports, "ORM_MODEL_KEY", { enumerable: true, get: function () { return decorator_1.ORM_MODEL_KEY; } });
|
|
22
|
+
var configuration_1 = require("./configuration");
|
|
23
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.OrmConfiguration; } });
|
|
24
|
+
__exportStar(require("./decorator"), exports);
|
|
25
|
+
__exportStar(require("./interface"), exports);
|
|
26
|
+
__exportStar(require("./dataSourceManager"), exports);
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/typeorm",
|
|
3
|
+
"version": "3.4.0-beta.1",
|
|
4
|
+
"main": "dist/index",
|
|
5
|
+
"typings": "index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist/**/*.js",
|
|
8
|
+
"dist/**/*.d.ts",
|
|
9
|
+
"index.d.ts"
|
|
10
|
+
],
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@midwayjs/core": "^3.4.0-beta.1",
|
|
13
|
+
"@midwayjs/decorator": "^3.4.0-beta.1",
|
|
14
|
+
"@midwayjs/mock": "^3.4.0-beta.1",
|
|
15
|
+
"sqlite3": "5.0.8",
|
|
16
|
+
"typeorm": "0.3.6"
|
|
17
|
+
},
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "czy88840616",
|
|
20
|
+
"email": "czy88840616@gmail.com"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=12"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc",
|
|
28
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
29
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "http://github.com/midwayjs/midway.git"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "14d8440f20978426184c988808343cc24bcf6e20"
|
|
36
|
+
}
|