@biorate/sequelize 2.2.0 → 2.2.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.
Files changed (2) hide show
  1. package/README.md +100 -61
  2. package/package.json +22 -21
package/README.md CHANGED
@@ -1,79 +1,120 @@
1
- # Sequelize
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
- ### Examples:
5
+ ## Features
6
6
 
7
- ```ts
8
- import { join } from 'path';
9
- import { tmpdir } from 'os';
10
- import { container, Core, inject, Types } from '@biorate/inversion';
11
- import { Config, IConfig } from '@biorate/config';
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
- // Assign models with sequelize connector
34
- class SequelizeConnector extends BaseSequelizeConnector {
35
- protected readonly models = { [connectionName]: [TestModel] };
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
- // Create Root class
39
- export class Root extends Core() {
40
- @inject(SequelizeConnector) public connector: ISequelizeConnector;
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<ISequelizeConnector>(SequelizeConnector).toSelf().inSingletonScope();
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
- name: connectionName,
53
- options: {
54
- logging: false,
55
- dialect: 'sqlite',
56
- storage: join(tmpdir(), 'sqlite-test.db'),
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
- await container.get<Root>(Root).$run();
65
- // Drop table if exists
66
- await TestModel.drop();
67
- // Create table
68
- await TestModel.sync();
69
- // Create model item
70
- await TestModel.create({ key: 'test', value: 1 });
71
- // Create find model item by key
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
- ### License
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biorate/sequelize",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Sequelize ORM connector",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -18,6 +18,21 @@
18
18
  "README.md",
19
19
  "LICENSE"
20
20
  ],
21
+ "scripts": {
22
+ "clean": "cleanup dist",
23
+ "build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
24
+ "build:esm": "npx tsc -p ./tsconfig.build.esm.json",
25
+ "build:types": "npx tsc -p ./tsconfig.build.types.json",
26
+ "build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
27
+ "postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs",
28
+ "postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module",
29
+ "format": "prettier --check src",
30
+ "lint": "eslint src --ext .ts",
31
+ "format:fix": "prettier --write src",
32
+ "lint:fix": "eslint src --ext .ts --fix",
33
+ "test": "npx vitest run --coverage",
34
+ "prepublishOnly": "pnpm run build"
35
+ },
21
36
  "repository": {
22
37
  "type": "git",
23
38
  "url": "git+https://github.com/biorate/core.git",
@@ -33,32 +48,18 @@
33
48
  ],
34
49
  "author": "llevkin",
35
50
  "license": "MIT",
36
- "gitHead": "fdd6dbd61368f7a9f9deb3b72243b19ecb3767a6",
51
+ "gitHead": "e17a03900999e261a0eb04c6afb09b9535cad952",
37
52
  "peerDependencies": {
38
53
  "sequelize": "6.37.3",
39
54
  "sequelize-typescript": "2.1.6"
40
55
  },
41
56
  "dependencies": {
42
- "@biorate/config": "3.2.0",
43
- "@biorate/errors": "3.1.0",
44
- "@biorate/connector": "3.1.0",
45
- "@biorate/inversion": "3.1.0"
57
+ "@biorate/config": "3.2.2",
58
+ "@biorate/connector": "3.1.2",
59
+ "@biorate/errors": "3.1.2",
60
+ "@biorate/inversion": "3.1.2"
46
61
  },
47
62
  "devDependencies": {
48
63
  "sqlite3": "5.0.7"
49
- },
50
- "scripts": {
51
- "clean": "cleanup dist",
52
- "build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
53
- "build:esm": "npx tsc -p ./tsconfig.build.esm.json",
54
- "build:types": "npx tsc -p ./tsconfig.build.types.json",
55
- "build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
56
- "postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs",
57
- "postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module",
58
- "format": "prettier --check src",
59
- "lint": "eslint src --ext .ts",
60
- "format:fix": "prettier --write src",
61
- "lint:fix": "eslint src --ext .ts --fix",
62
- "test": "npx vitest run --coverage"
63
64
  }
64
- }
65
+ }