@biorate/sequelize 2.1.0 → 2.2.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 +100 -61
- package/dist/types/src/index.d.ts +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,79 +1,120 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @biorate/sequelize
|
|
2
2
|
|
|
3
|
-
Sequelize ORM connector
|
|
3
|
+
Sequelize ORM connector — connection manager for Sequelize with model auto-loading and multi-connection support.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
ISequelizeConnector,
|
|
14
|
-
SequelizeConnector as BaseSequelizeConnector,
|
|
15
|
-
} from '@biorate/sequelize';
|
|
16
|
-
import { Table, Column, Model, DataType } from '@biorate/sequelize';
|
|
17
|
-
|
|
18
|
-
const connectionName = 'db';
|
|
19
|
-
|
|
20
|
-
// Create model
|
|
21
|
-
@Table({
|
|
22
|
-
tableName: 'test',
|
|
23
|
-
timestamps: false,
|
|
24
|
-
})
|
|
25
|
-
export class TestModel extends Model {
|
|
26
|
-
@Column({ type: DataType.CHAR, primaryKey: true })
|
|
27
|
-
key: string;
|
|
28
|
-
|
|
29
|
-
@Column(DataType.INTEGER)
|
|
30
|
-
value: number;
|
|
31
|
-
}
|
|
7
|
+
- **Auto-connect** — creates Sequelize instance on `@init()` via config namespace `Sequelize`.
|
|
8
|
+
- **Model auto-loading** — `add()` registers models; `load()` attaches them to all connections.
|
|
9
|
+
- **Connection verification** — calls `authenticate()` after initialisation.
|
|
10
|
+
- **Multi-connection** — named connections with model-copy per connection.
|
|
11
|
+
- **Typed errors** — `SequelizeCantConnectError` on failure.
|
|
32
12
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @biorate/sequelize
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires `@biorate/connector`, `@biorate/inversion`, `@biorate/config`, `sequelize`.
|
|
37
20
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { inject, container, Types, Core } from '@biorate/inversion';
|
|
25
|
+
import { IConfig, Config } from '@biorate/config';
|
|
26
|
+
import { SequelizeConnector } from '@biorate/sequelize';
|
|
27
|
+
import { Sequelize, DataTypes, Model } from 'sequelize';
|
|
28
|
+
|
|
29
|
+
class User extends Model {}
|
|
30
|
+
|
|
31
|
+
const attributes = {
|
|
32
|
+
firstName: { type: DataTypes.STRING, allowNull: false },
|
|
33
|
+
lastName: { type: DataTypes.STRING, allowNull: false },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
class Root extends Core() {
|
|
37
|
+
@inject(SequelizeConnector) public connector: SequelizeConnector;
|
|
38
|
+
protected constructor() {
|
|
39
|
+
super();
|
|
40
|
+
this.connector.add(User, { tableName: 'users' }, attributes);
|
|
41
|
+
}
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
// Bind dependencies
|
|
44
44
|
container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
|
|
45
|
-
container.bind<
|
|
45
|
+
container.bind<SequelizeConnector>(SequelizeConnector).toSelf().inSingletonScope();
|
|
46
46
|
container.bind<Root>(Root).toSelf().inSingletonScope();
|
|
47
47
|
|
|
48
|
-
// Merge config
|
|
49
48
|
container.get<IConfig>(Types.Config).merge({
|
|
50
|
-
Sequelize: [
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
Sequelize: [{
|
|
50
|
+
name: 'connection',
|
|
51
|
+
options: {
|
|
52
|
+
dialect: 'postgres',
|
|
53
|
+
host: 'localhost',
|
|
54
|
+
port: 5432,
|
|
55
|
+
username: 'postgres',
|
|
56
|
+
password: 'postgres',
|
|
57
|
+
database: 'postgres',
|
|
58
58
|
},
|
|
59
|
-
],
|
|
59
|
+
}],
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
// Example
|
|
63
62
|
(async () => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const data = await TestModel.findOne({ where: { key: 'test' } });
|
|
73
|
-
console.log(data.toJSON()); // { key: 'test', value: 1 }
|
|
63
|
+
const root = container.get<Root>(Root);
|
|
64
|
+
await root.$run();
|
|
65
|
+
root.connector.load('connection'); // attaches User model to 'connection'
|
|
66
|
+
await root.connector.current!.sync();
|
|
67
|
+
const user = await root.connector.current!.model('User').create({
|
|
68
|
+
firstName: 'Vasya', lastName: 'Pupkin',
|
|
69
|
+
});
|
|
70
|
+
console.log(user.toJSON());
|
|
74
71
|
})();
|
|
75
72
|
```
|
|
76
73
|
|
|
74
|
+
## API Reference
|
|
75
|
+
|
|
76
|
+
### `SequelizeConnector`
|
|
77
|
+
|
|
78
|
+
| Member | Type | Description |
|
|
79
|
+
|------------------|---------------------------------------------|------------------------------------------|
|
|
80
|
+
| `namespace` | `'Sequelize'` | Config key for connection definitions. |
|
|
81
|
+
| `connect(config)` | `(config) => Promise<ISequelizeConnection>` | Creates Sequelize instance and authenticates. |
|
|
82
|
+
| `add(model, options?, attributes?, indexes?)` | `(...) => void` | Register a model class for later loading. |
|
|
83
|
+
| `load(name)` | `(name) => void` | Copies all registered models into a named connection. |
|
|
84
|
+
|
|
85
|
+
### Config
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
interface ISequelizeConfig extends IConnectorConfig {
|
|
89
|
+
options: SequelizeOptions; // dialect, host, port, username, password, database, etc.
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Errors
|
|
94
|
+
|
|
95
|
+
| Error | Condition |
|
|
96
|
+
|--------------------------------|----------------------------------------------|
|
|
97
|
+
| `SequelizeCantConnectError` | `new Sequelize()` or `authenticate()` fails. |
|
|
98
|
+
|
|
99
|
+
## Architecture
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
SequelizeConnector extends Connector<ISequelizeConfig, ISequelizeConnection>
|
|
103
|
+
│
|
|
104
|
+
├── namespace = 'Sequelize'
|
|
105
|
+
├── connect(config) → new Sequelize(config.options)
|
|
106
|
+
│ └── await connection.authenticate()
|
|
107
|
+
│
|
|
108
|
+
├── add(Model, options?, attributes?, indexes?)
|
|
109
|
+
│ └── stores model definition in internal registry
|
|
110
|
+
│
|
|
111
|
+
├── load(name)
|
|
112
|
+
│ ├── get connection(name)
|
|
113
|
+
│ └── for each registered model → connection.define(...)
|
|
114
|
+
│
|
|
115
|
+
└── connection is a Sequelize instance with attached model classes
|
|
116
|
+
```
|
|
117
|
+
|
|
77
118
|
### Learn
|
|
78
119
|
|
|
79
120
|
- Documentation can be found here - [docs](https://biorate.github.io/core/modules/sequelize.html).
|
|
@@ -82,8 +123,6 @@ container.get<IConfig>(Types.Config).merge({
|
|
|
82
123
|
|
|
83
124
|
See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/sequelize/CHANGELOG.md)
|
|
84
125
|
|
|
85
|
-
|
|
126
|
+
## License
|
|
86
127
|
|
|
87
128
|
[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/sequelize/LICENSE)
|
|
88
|
-
|
|
89
|
-
Copyright (c) 2021-present [Leonid Levkin (llevkin)](mailto:llevkin@yandex.ru)
|
|
@@ -9,7 +9,7 @@ export { QueryTypes, QueryOptions, QueryOptionsWithType, Transaction, Transactio
|
|
|
9
9
|
export declare class SequelizeConnector extends Connector<ISequelizeConfig, ISequelizeConnection> {
|
|
10
10
|
private '#connections';
|
|
11
11
|
private '#current';
|
|
12
|
-
protected readonly namespace
|
|
12
|
+
protected readonly namespace: string;
|
|
13
13
|
protected readonly models: {
|
|
14
14
|
[key: string]: IModels;
|
|
15
15
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/sequelize",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Sequelize ORM connector",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -48,16 +48,16 @@
|
|
|
48
48
|
],
|
|
49
49
|
"author": "llevkin",
|
|
50
50
|
"license": "MIT",
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "ff1dcf14a733ce8004ca582d2c421a2154ce97cb",
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"sequelize": "6.37.3",
|
|
54
54
|
"sequelize-typescript": "2.1.6"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@biorate/config": "3.1
|
|
58
|
-
"@biorate/connector": "3.
|
|
59
|
-
"@biorate/errors": "3.
|
|
60
|
-
"@biorate/inversion": "3.
|
|
57
|
+
"@biorate/config": "3.2.1",
|
|
58
|
+
"@biorate/connector": "3.1.1",
|
|
59
|
+
"@biorate/errors": "3.1.1",
|
|
60
|
+
"@biorate/inversion": "3.1.1"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"sqlite3": "5.0.7"
|