@midwayjs/sequelize 3.3.5 → 3.4.0-beta.2
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 +4 -69
- package/dist/configuration.d.ts +7 -3
- package/dist/configuration.js +29 -11
- package/dist/dataSourceManager.d.ts +12 -0
- package/dist/dataSourceManager.js +83 -0
- package/dist/decorator.d.ts +11 -0
- package/dist/{core/baseTable.js → decorator.js} +14 -5
- package/dist/index.d.ts +3 -1
- package/dist/index.js +20 -3
- package/dist/interface.d.ts +6 -0
- package/dist/interface.js +3 -0
- package/index.d.ts +2 -5
- package/package.json +8 -7
- package/dist/config/config.default.d.ts +0 -8
- package/dist/config/config.default.js +0 -12
- package/dist/core/baseTable.d.ts +0 -4
package/README.md
CHANGED
|
@@ -1,74 +1,9 @@
|
|
|
1
1
|
# midway sequelize component
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
this is a sub package for midway.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
@Configuration({
|
|
9
|
-
imports: [
|
|
10
|
-
'@midwayjs/sequelize',
|
|
11
|
-
],
|
|
12
|
-
importConfigs: [
|
|
13
|
-
'./config'
|
|
14
|
-
]
|
|
15
|
-
})
|
|
16
|
-
export class ContainerConfiguration {
|
|
17
|
-
}
|
|
7
|
+
## License
|
|
18
8
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
## Configuration
|
|
22
|
-
|
|
23
|
-
in config files
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
```ts
|
|
27
|
-
export const sequelize = {
|
|
28
|
-
options: {
|
|
29
|
-
database: 'test4',
|
|
30
|
-
username: 'root',
|
|
31
|
-
password: '123456',
|
|
32
|
-
host: '127.0.0.1',
|
|
33
|
-
port: 3306,
|
|
34
|
-
encrypt: false,
|
|
35
|
-
logging: console.log
|
|
36
|
-
},
|
|
37
|
-
sync: false
|
|
38
|
-
}
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Define EntityModel
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
import { Column, Model } from "sequelize-typescript";
|
|
45
|
-
import { BaseTable } from "@midwayjs/sequelize";
|
|
46
|
-
|
|
47
|
-
@BaseTable
|
|
48
|
-
export default class UserModel extends Model{
|
|
49
|
-
@Column({
|
|
50
|
-
comment: '名字'
|
|
51
|
-
})
|
|
52
|
-
name: string;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Use Model
|
|
59
|
-
|
|
60
|
-
in code files
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
import { UserModel } from './model/user';
|
|
64
|
-
|
|
65
|
-
@Provide()
|
|
66
|
-
export class IndexHandler {
|
|
67
|
-
|
|
68
|
-
@Func('index.handler')
|
|
69
|
-
async handler() {
|
|
70
|
-
const users = await this.UserModel.findAll();
|
|
71
|
-
return users;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
```
|
|
9
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
package/dist/configuration.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SequelizeDataSourceManager } from './dataSourceManager';
|
|
2
|
+
import { IMidwayContainer, MidwayDecoratorService } from '@midwayjs/core';
|
|
2
3
|
export declare class SequelizeConfiguration {
|
|
3
|
-
instance: Sequelize;
|
|
4
4
|
sequelizeConfig: any;
|
|
5
|
-
|
|
5
|
+
decoratorService: MidwayDecoratorService;
|
|
6
|
+
dataSourceManager: SequelizeDataSourceManager;
|
|
7
|
+
init(): Promise<void>;
|
|
8
|
+
onReady(container: IMidwayContainer): Promise<void>;
|
|
9
|
+
onStop(): Promise<void>;
|
|
6
10
|
}
|
|
7
11
|
//# sourceMappingURL=configuration.d.ts.map
|
package/dist/configuration.js
CHANGED
|
@@ -11,17 +11,23 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.SequelizeConfiguration = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
-
const
|
|
15
|
-
const
|
|
14
|
+
const dataSourceManager_1 = require("./dataSourceManager");
|
|
15
|
+
const core_1 = require("@midwayjs/core");
|
|
16
|
+
const decorator_2 = require("./decorator");
|
|
16
17
|
let SequelizeConfiguration = class SequelizeConfiguration {
|
|
17
|
-
async
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
async init() {
|
|
19
|
+
this.decoratorService.registerPropertyHandler(decorator_2.ENTITY_MODEL_KEY, (propertyName, meta) => {
|
|
20
|
+
return this.dataSourceManager
|
|
21
|
+
.getDataSource(meta.connectionName)
|
|
22
|
+
.getRepository(meta.modelKey);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async onReady(container) {
|
|
26
|
+
this.dataSourceManager = await container.getAsync(dataSourceManager_1.SequelizeDataSourceManager);
|
|
27
|
+
}
|
|
28
|
+
async onStop() {
|
|
29
|
+
if (this.dataSourceManager) {
|
|
30
|
+
await this.dataSourceManager.stop();
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
};
|
|
@@ -29,12 +35,24 @@ __decorate([
|
|
|
29
35
|
(0, decorator_1.Config)('sequelize'),
|
|
30
36
|
__metadata("design:type", Object)
|
|
31
37
|
], SequelizeConfiguration.prototype, "sequelizeConfig", void 0);
|
|
38
|
+
__decorate([
|
|
39
|
+
(0, decorator_1.Inject)(),
|
|
40
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
41
|
+
], SequelizeConfiguration.prototype, "decoratorService", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, decorator_1.Init)(),
|
|
44
|
+
__metadata("design:type", Function),
|
|
45
|
+
__metadata("design:paramtypes", []),
|
|
46
|
+
__metadata("design:returntype", Promise)
|
|
47
|
+
], SequelizeConfiguration.prototype, "init", null);
|
|
32
48
|
SequelizeConfiguration = __decorate([
|
|
33
49
|
(0, decorator_1.Configuration)({
|
|
34
50
|
namespace: 'sequelize',
|
|
35
51
|
importConfigs: [
|
|
36
52
|
{
|
|
37
|
-
default:
|
|
53
|
+
default: {
|
|
54
|
+
sequelize: {},
|
|
55
|
+
},
|
|
38
56
|
},
|
|
39
57
|
],
|
|
40
58
|
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataSourceManager, ILogger } from '@midwayjs/core';
|
|
2
|
+
import { Sequelize } from 'sequelize-typescript';
|
|
3
|
+
export declare class SequelizeDataSourceManager extends DataSourceManager<Sequelize> {
|
|
4
|
+
sequelizeConfig: any;
|
|
5
|
+
coreLogger: ILogger;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
getName(): string;
|
|
8
|
+
protected createDataSource(config: any, dataSourceName: string): Promise<Sequelize>;
|
|
9
|
+
protected checkConnected(dataSource: Sequelize): Promise<boolean>;
|
|
10
|
+
protected destroyDataSource(dataSource: Sequelize): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=dataSourceManager.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
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.SequelizeDataSourceManager = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const core_1 = require("@midwayjs/core");
|
|
15
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
16
|
+
let SequelizeDataSourceManager = class SequelizeDataSourceManager extends core_1.DataSourceManager {
|
|
17
|
+
async init() {
|
|
18
|
+
if (this.sequelizeConfig.options) {
|
|
19
|
+
this.coreLogger.warn('[midway:sequelize] sequelize.options is deprecated, please use new config format.');
|
|
20
|
+
this.sequelizeConfig.options.sync = this.sequelizeConfig.sync || false;
|
|
21
|
+
// legacy config
|
|
22
|
+
this.sequelizeConfig.dataSource = {
|
|
23
|
+
default: this.sequelizeConfig.options,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
await this.initDataSource(this.sequelizeConfig);
|
|
27
|
+
}
|
|
28
|
+
getName() {
|
|
29
|
+
return 'sequelize';
|
|
30
|
+
}
|
|
31
|
+
async createDataSource(config, dataSourceName) {
|
|
32
|
+
const client = new sequelize_typescript_1.Sequelize(config);
|
|
33
|
+
const entities = config['entities'];
|
|
34
|
+
if (entities && entities.length > 0) {
|
|
35
|
+
client.addModels(entities);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const entities = (0, decorator_1.listModule)('sequelize:core');
|
|
39
|
+
client.addModels(entities);
|
|
40
|
+
}
|
|
41
|
+
await client.authenticate();
|
|
42
|
+
if (config.sync) {
|
|
43
|
+
await client.sync();
|
|
44
|
+
}
|
|
45
|
+
this.coreLogger.info('[midway:sequelize] connecting and start');
|
|
46
|
+
return client;
|
|
47
|
+
}
|
|
48
|
+
async checkConnected(dataSource) {
|
|
49
|
+
try {
|
|
50
|
+
await dataSource.authenticate();
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
this.coreLogger.error(err);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async destroyDataSource(dataSource) {
|
|
59
|
+
if (await this.checkConnected(dataSource)) {
|
|
60
|
+
await dataSource.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, decorator_1.Config)('sequelize'),
|
|
66
|
+
__metadata("design:type", Object)
|
|
67
|
+
], SequelizeDataSourceManager.prototype, "sequelizeConfig", void 0);
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, decorator_1.Logger)('coreLogger'),
|
|
70
|
+
__metadata("design:type", Object)
|
|
71
|
+
], SequelizeDataSourceManager.prototype, "coreLogger", void 0);
|
|
72
|
+
__decorate([
|
|
73
|
+
(0, decorator_1.Init)(),
|
|
74
|
+
__metadata("design:type", Function),
|
|
75
|
+
__metadata("design:paramtypes", []),
|
|
76
|
+
__metadata("design:returntype", Promise)
|
|
77
|
+
], SequelizeDataSourceManager.prototype, "init", null);
|
|
78
|
+
SequelizeDataSourceManager = __decorate([
|
|
79
|
+
(0, decorator_1.Provide)(),
|
|
80
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
81
|
+
], SequelizeDataSourceManager);
|
|
82
|
+
exports.SequelizeDataSourceManager = SequelizeDataSourceManager;
|
|
83
|
+
//# sourceMappingURL=dataSourceManager.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Model, TableOptions } from 'sequelize-typescript';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated
|
|
4
|
+
* @param options
|
|
5
|
+
* @constructor
|
|
6
|
+
*/
|
|
7
|
+
export declare function BaseTable<M extends Model = Model>(options: TableOptions<M>): any;
|
|
8
|
+
export declare function BaseTable(target: any): void;
|
|
9
|
+
export declare const ENTITY_MODEL_KEY = "sequelize:entity_model_key";
|
|
10
|
+
export declare function InjectRepository(modelKey: any, connectionName?: string): PropertyDecorator;
|
|
11
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseTable = void 0;
|
|
4
|
-
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
3
|
+
exports.InjectRepository = exports.ENTITY_MODEL_KEY = exports.BaseTable = void 0;
|
|
5
4
|
const decorator_1 = require("@midwayjs/decorator");
|
|
5
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
6
|
+
const decorator_2 = require("@midwayjs/decorator");
|
|
6
7
|
function BaseTable(arg) {
|
|
7
8
|
if (typeof arg === 'function') {
|
|
8
|
-
(0,
|
|
9
|
+
(0, decorator_2.saveModule)('sequelize:core', arg);
|
|
9
10
|
annotate(arg);
|
|
10
11
|
}
|
|
11
12
|
else {
|
|
12
13
|
const options = Object.assign({}, arg);
|
|
13
14
|
return target => {
|
|
14
|
-
(0,
|
|
15
|
+
(0, decorator_2.saveModule)('sequelize:core', target);
|
|
15
16
|
annotate(target, options);
|
|
16
17
|
};
|
|
17
18
|
}
|
|
@@ -21,4 +22,12 @@ function annotate(target, options = {}) {
|
|
|
21
22
|
(0, sequelize_typescript_1.setModelName)(target.prototype, options.modelName || target.name);
|
|
22
23
|
(0, sequelize_typescript_1.addOptions)(target.prototype, options);
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
+
exports.ENTITY_MODEL_KEY = 'sequelize:entity_model_key';
|
|
26
|
+
function InjectRepository(modelKey, connectionName = 'default') {
|
|
27
|
+
return (0, decorator_1.createCustomPropertyDecorator)(exports.ENTITY_MODEL_KEY, {
|
|
28
|
+
modelKey,
|
|
29
|
+
connectionName,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.InjectRepository = InjectRepository;
|
|
33
|
+
//# sourceMappingURL=decorator.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { SequelizeConfiguration as Configuration } from './configuration';
|
|
2
|
-
export { BaseTable } from './
|
|
2
|
+
export { BaseTable, InjectRepository } from './decorator';
|
|
3
|
+
export * from './interface';
|
|
4
|
+
export * from './dataSourceManager';
|
|
3
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseTable = exports.Configuration = void 0;
|
|
17
|
+
exports.InjectRepository = exports.BaseTable = exports.Configuration = void 0;
|
|
4
18
|
var configuration_1 = require("./configuration");
|
|
5
19
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.SequelizeConfiguration; } });
|
|
6
|
-
var
|
|
7
|
-
Object.defineProperty(exports, "BaseTable", { enumerable: true, get: function () { return
|
|
20
|
+
var decorator_1 = require("./decorator");
|
|
21
|
+
Object.defineProperty(exports, "BaseTable", { enumerable: true, get: function () { return decorator_1.BaseTable; } });
|
|
22
|
+
Object.defineProperty(exports, "InjectRepository", { enumerable: true, get: function () { return decorator_1.InjectRepository; } });
|
|
23
|
+
__exportStar(require("./interface"), exports);
|
|
24
|
+
__exportStar(require("./dataSourceManager"), exports);
|
|
8
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DataSourceManagerConfigOption } from '@midwayjs/core';
|
|
2
|
+
import { SequelizeOptions } from 'sequelize-typescript';
|
|
3
|
+
export declare type SequelizeConfigOptions = DataSourceManagerConfigOption<SequelizeOptions & {
|
|
4
|
+
sync?: boolean;
|
|
5
|
+
}>;
|
|
6
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SequelizeConfigOptions } from './dist';
|
|
2
2
|
|
|
3
3
|
export * from './dist/index';
|
|
4
4
|
|
|
5
5
|
declare module '@midwayjs/core/dist/interface' {
|
|
6
6
|
interface MidwayConfig {
|
|
7
|
-
sequelize?:
|
|
8
|
-
options?: SequelizeOptions;
|
|
9
|
-
sync?: boolean;
|
|
10
|
-
};
|
|
7
|
+
sequelize?: PowerPartial<SequelizeConfigOptions>;
|
|
11
8
|
}
|
|
12
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/sequelize",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0-beta.2",
|
|
4
4
|
"main": "dist/index",
|
|
5
5
|
"typings": "index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
"index.d.ts"
|
|
10
10
|
],
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@midwayjs/
|
|
13
|
-
"@midwayjs/
|
|
14
|
-
"@midwayjs/
|
|
15
|
-
"
|
|
12
|
+
"@midwayjs/core": "^3.4.0-beta.2",
|
|
13
|
+
"@midwayjs/decorator": "^3.4.0-beta.2",
|
|
14
|
+
"@midwayjs/koa": "^3.4.0-beta.2",
|
|
15
|
+
"@midwayjs/mock": "^3.4.0-beta.2",
|
|
16
|
+
"sqlite3": "5.0.8"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"sequelize": "
|
|
19
|
+
"sequelize": "6.21.0",
|
|
19
20
|
"sequelize-typescript": "^2.1.0"
|
|
20
21
|
},
|
|
21
22
|
"keywords": [
|
|
@@ -40,5 +41,5 @@
|
|
|
40
41
|
"type": "git",
|
|
41
42
|
"url": "http://github.com/midwayjs/midway.git"
|
|
42
43
|
},
|
|
43
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "a61721d3946b30fd4cac183265edcd99b31ac72b"
|
|
44
45
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sequelize = void 0;
|
|
4
|
-
const path = require("path");
|
|
5
|
-
exports.sequelize = {
|
|
6
|
-
options: {
|
|
7
|
-
dialect: 'sqlite',
|
|
8
|
-
storage: path.join(__dirname, '../../test', 'database.sqlite'),
|
|
9
|
-
},
|
|
10
|
-
sync: false,
|
|
11
|
-
};
|
|
12
|
-
//# sourceMappingURL=config.default.js.map
|
package/dist/core/baseTable.d.ts
DELETED