@midwayjs/leoric 3.0.0
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 +63 -0
- package/dist/configuration.d.ts +10 -0
- package/dist/configuration.js +65 -0
- package/dist/dataSourceManager.d.ts +14 -0
- package/dist/dataSourceManager.js +99 -0
- package/dist/decorator.d.ts +6 -0
- package/dist/decorator.js +22 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +31 -0
- package/dist/interface.d.ts +5 -0
- package/dist/interface.js +3 -0
- package/index.d.ts +9 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# midway leoric component
|
|
2
|
+
|
|
3
|
+
this is a sub package for midway.
|
|
4
|
+
|
|
5
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// src/configuration.ts
|
|
11
|
+
import { Configuration, ILifeCycle } from '@midwayjs/core';
|
|
12
|
+
import * as leoric from '@midwayjs/leoric';
|
|
13
|
+
|
|
14
|
+
@Configuration({
|
|
15
|
+
imports: [
|
|
16
|
+
leoric,
|
|
17
|
+
],
|
|
18
|
+
|
|
19
|
+
})
|
|
20
|
+
export class ContainerLifeCycle implements ILifeCycle {}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// src/config/config.default.ts
|
|
25
|
+
export default () => {
|
|
26
|
+
return {
|
|
27
|
+
leoric: {
|
|
28
|
+
dataSource: {
|
|
29
|
+
default: {
|
|
30
|
+
dialect: 'sqlite',
|
|
31
|
+
database: path.join(__dirname, '../../', 'database.sqlite'),
|
|
32
|
+
sync: true,
|
|
33
|
+
models: [
|
|
34
|
+
'**/models/*{.ts,.js}'
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// src/controller/user.ts
|
|
45
|
+
import { Controller } from '@midwayjs/core';
|
|
46
|
+
import { InjectModel } from '@midwayjs/leoric';
|
|
47
|
+
import User from '../model/user';
|
|
48
|
+
|
|
49
|
+
@Controller('/api/users')
|
|
50
|
+
export class UserController {
|
|
51
|
+
@InjectModel(User)
|
|
52
|
+
User: typeof User;
|
|
53
|
+
|
|
54
|
+
@Get('/')
|
|
55
|
+
async index() {
|
|
56
|
+
return await this.User.order('id', 'desc').limit(10);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IMidwayContainer, MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { LeoricDataSourceManager } from './dataSourceManager';
|
|
3
|
+
export declare class LeoricConfiguration {
|
|
4
|
+
decoratorService: MidwayDecoratorService;
|
|
5
|
+
dataSourceManager: LeoricDataSourceManager;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
onReady(container: IMidwayContainer): Promise<void>;
|
|
8
|
+
onStop(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,65 @@
|
|
|
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.LeoricConfiguration = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const dataSourceManager_1 = require("./dataSourceManager");
|
|
15
|
+
const decorator_1 = require("./decorator");
|
|
16
|
+
function getModelName(model) {
|
|
17
|
+
if (typeof model === 'string')
|
|
18
|
+
return model;
|
|
19
|
+
return model.name;
|
|
20
|
+
}
|
|
21
|
+
let LeoricConfiguration = class LeoricConfiguration {
|
|
22
|
+
async init() {
|
|
23
|
+
this.decoratorService.registerPropertyHandler(decorator_1.MODEL_KEY, (propertyName, meta) => {
|
|
24
|
+
return this.dataSourceManager.getDataSource(meta.connectionName ||
|
|
25
|
+
this.dataSourceManager.getDataSourceNameByModel(meta.modelName) ||
|
|
26
|
+
this.dataSourceManager.getDefaultDataSourceName()).models[getModelName(meta.modelName)];
|
|
27
|
+
});
|
|
28
|
+
this.decoratorService.registerPropertyHandler(decorator_1.DATA_SOURCE_KEY, (propertyName, meta) => {
|
|
29
|
+
return this.dataSourceManager.getDataSource(meta.dataSourceName ||
|
|
30
|
+
this.dataSourceManager.getDefaultDataSourceName());
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async onReady(container) {
|
|
34
|
+
this.dataSourceManager = await container.getAsync(dataSourceManager_1.LeoricDataSourceManager);
|
|
35
|
+
}
|
|
36
|
+
async onStop() {
|
|
37
|
+
if (this.dataSourceManager) {
|
|
38
|
+
await this.dataSourceManager.stop();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, core_1.Inject)(),
|
|
44
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
45
|
+
], LeoricConfiguration.prototype, "decoratorService", void 0);
|
|
46
|
+
__decorate([
|
|
47
|
+
(0, core_1.Init)(),
|
|
48
|
+
__metadata("design:type", Function),
|
|
49
|
+
__metadata("design:paramtypes", []),
|
|
50
|
+
__metadata("design:returntype", Promise)
|
|
51
|
+
], LeoricConfiguration.prototype, "init", null);
|
|
52
|
+
LeoricConfiguration = __decorate([
|
|
53
|
+
(0, core_1.Configuration)({
|
|
54
|
+
namespace: 'leoric',
|
|
55
|
+
importConfigs: [
|
|
56
|
+
{
|
|
57
|
+
default: {
|
|
58
|
+
leoric: {},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
})
|
|
63
|
+
], LeoricConfiguration);
|
|
64
|
+
exports.LeoricConfiguration = LeoricConfiguration;
|
|
65
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DataSourceManager, ILogger } from '@midwayjs/core';
|
|
2
|
+
import Realm, { ConnectOptions } from 'leoric';
|
|
3
|
+
import { LeoricConfigOption } from './interface';
|
|
4
|
+
export declare class LeoricDataSourceManager extends DataSourceManager<Realm, ConnectOptions> {
|
|
5
|
+
leoricConfig: LeoricConfigOption;
|
|
6
|
+
coreLogger: ILogger;
|
|
7
|
+
baseDir: string;
|
|
8
|
+
init(): Promise<void>;
|
|
9
|
+
getName(): string;
|
|
10
|
+
protected createDataSource(config: any, dataSourceName: string): Promise<Realm>;
|
|
11
|
+
protected checkConnected(dataSource: Realm): Promise<boolean>;
|
|
12
|
+
protected destroyDataSource(dataSource: Realm): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=dataSourceManager.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.LeoricDataSourceManager = void 0;
|
|
36
|
+
const core_1 = require("@midwayjs/core");
|
|
37
|
+
const leoric_1 = __importStar(require("leoric"));
|
|
38
|
+
let LeoricDataSourceManager = class LeoricDataSourceManager extends core_1.DataSourceManager {
|
|
39
|
+
async init() {
|
|
40
|
+
await this.initDataSource(this.leoricConfig, {
|
|
41
|
+
baseDir: this.baseDir,
|
|
42
|
+
entitiesConfigKey: 'models',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getName() {
|
|
46
|
+
return 'leoric';
|
|
47
|
+
}
|
|
48
|
+
async createDataSource(config, dataSourceName) {
|
|
49
|
+
const { sync, models, ...options } = config;
|
|
50
|
+
const realm = new leoric_1.default({
|
|
51
|
+
...options,
|
|
52
|
+
models: models.filter(el => (0, leoric_1.isBone)(el)),
|
|
53
|
+
});
|
|
54
|
+
await realm.connect();
|
|
55
|
+
this.coreLogger.info('[midway:leoric] connecting and start');
|
|
56
|
+
if (sync)
|
|
57
|
+
await realm.sync();
|
|
58
|
+
return realm;
|
|
59
|
+
}
|
|
60
|
+
async checkConnected(dataSource) {
|
|
61
|
+
try {
|
|
62
|
+
await dataSource.connect();
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
this.coreLogger.error(err);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async destroyDataSource(dataSource) {
|
|
71
|
+
if (await this.checkConnected(dataSource)) {
|
|
72
|
+
await dataSource.disconnect();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
__decorate([
|
|
77
|
+
(0, core_1.Config)('leoric'),
|
|
78
|
+
__metadata("design:type", Object)
|
|
79
|
+
], LeoricDataSourceManager.prototype, "leoricConfig", void 0);
|
|
80
|
+
__decorate([
|
|
81
|
+
(0, core_1.Logger)('coreLogger'),
|
|
82
|
+
__metadata("design:type", Object)
|
|
83
|
+
], LeoricDataSourceManager.prototype, "coreLogger", void 0);
|
|
84
|
+
__decorate([
|
|
85
|
+
(0, core_1.Inject)(),
|
|
86
|
+
__metadata("design:type", String)
|
|
87
|
+
], LeoricDataSourceManager.prototype, "baseDir", void 0);
|
|
88
|
+
__decorate([
|
|
89
|
+
(0, core_1.Init)(),
|
|
90
|
+
__metadata("design:type", Function),
|
|
91
|
+
__metadata("design:paramtypes", []),
|
|
92
|
+
__metadata("design:returntype", Promise)
|
|
93
|
+
], LeoricDataSourceManager.prototype, "init", null);
|
|
94
|
+
LeoricDataSourceManager = __decorate([
|
|
95
|
+
(0, core_1.Provide)(),
|
|
96
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
97
|
+
], LeoricDataSourceManager);
|
|
98
|
+
exports.LeoricDataSourceManager = LeoricDataSourceManager;
|
|
99
|
+
//# sourceMappingURL=dataSourceManager.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ClassLikeBone } from './interface';
|
|
2
|
+
export declare const MODEL_KEY = "leoric:model_key";
|
|
3
|
+
export declare const DATA_SOURCE_KEY = "leoric:data_source_key";
|
|
4
|
+
export declare function InjectDataSource(dataSourceName?: string): PropertyDecorator;
|
|
5
|
+
export declare function InjectModel(modelName?: string | ClassLikeBone, dataSourceName?: string): (target: object, propertyKey: string | symbol) => void;
|
|
6
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectModel = exports.InjectDataSource = exports.DATA_SOURCE_KEY = exports.MODEL_KEY = void 0;
|
|
4
|
+
const core_1 = require("@midwayjs/core");
|
|
5
|
+
exports.MODEL_KEY = 'leoric:model_key';
|
|
6
|
+
exports.DATA_SOURCE_KEY = 'leoric:data_source_key';
|
|
7
|
+
function InjectDataSource(dataSourceName) {
|
|
8
|
+
return (0, core_1.createCustomPropertyDecorator)(exports.DATA_SOURCE_KEY, { dataSourceName });
|
|
9
|
+
}
|
|
10
|
+
exports.InjectDataSource = InjectDataSource;
|
|
11
|
+
function InjectModel(modelName, dataSourceName) {
|
|
12
|
+
return (target, propertyKey) => {
|
|
13
|
+
if (!modelName)
|
|
14
|
+
modelName = propertyKey.toString();
|
|
15
|
+
return (0, core_1.createCustomPropertyDecorator)(exports.MODEL_KEY, {
|
|
16
|
+
modelName,
|
|
17
|
+
dataSourceName,
|
|
18
|
+
})(target, propertyKey);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.InjectModel = InjectModel;
|
|
22
|
+
//# sourceMappingURL=decorator.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Realm from 'leoric';
|
|
2
|
+
export * from 'leoric';
|
|
3
|
+
export { Realm };
|
|
4
|
+
export { LeoricConfiguration as Configuration } from './configuration';
|
|
5
|
+
export { InjectDataSource, InjectModel } from './decorator';
|
|
6
|
+
export * from './dataSourceManager';
|
|
7
|
+
export * from './interface';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.InjectModel = exports.InjectDataSource = exports.Configuration = exports.Realm = void 0;
|
|
21
|
+
const leoric_1 = __importDefault(require("leoric"));
|
|
22
|
+
exports.Realm = leoric_1.default;
|
|
23
|
+
__exportStar(require("leoric"), exports);
|
|
24
|
+
var configuration_1 = require("./configuration");
|
|
25
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.LeoricConfiguration; } });
|
|
26
|
+
var decorator_1 = require("./decorator");
|
|
27
|
+
Object.defineProperty(exports, "InjectDataSource", { enumerable: true, get: function () { return decorator_1.InjectDataSource; } });
|
|
28
|
+
Object.defineProperty(exports, "InjectModel", { enumerable: true, get: function () { return decorator_1.InjectModel; } });
|
|
29
|
+
__exportStar(require("./dataSourceManager"), exports);
|
|
30
|
+
__exportStar(require("./interface"), exports);
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DataSourceManagerConfigOption } from '@midwayjs/core';
|
|
2
|
+
import { Bone, ConnectOptions } from 'leoric';
|
|
3
|
+
export type LeoricConfigOption = DataSourceManagerConfigOption<ConnectOptions, 'models'>;
|
|
4
|
+
export type ClassLikeBone = new (...args: any[]) => Bone;
|
|
5
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/leoric",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Leoric as a Midway model component",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"midway",
|
|
9
|
+
"component",
|
|
10
|
+
"leoric",
|
|
11
|
+
"orm"
|
|
12
|
+
],
|
|
13
|
+
"author": "Chen Yangjian (https://www.cyj.me)",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/**/*.js",
|
|
16
|
+
"dist/**/*.d.ts",
|
|
17
|
+
"index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"leoric": "2.11.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@midwayjs/core": "^3.11.1",
|
|
25
|
+
"@midwayjs/mock": "^3.11.3",
|
|
26
|
+
"sqlite3": "5.1.6"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc",
|
|
30
|
+
"prepack": "npm run build",
|
|
31
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
32
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/midwayjs/midway.git"
|
|
37
|
+
}
|
|
38
|
+
}
|