@avleon/data 0.4.0 → 0.4.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.
Files changed (3) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +213 -213
  3. package/package.json +5 -4
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Tareq Hossain
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Tareq Hossain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,213 +1,213 @@
1
- # @avleon/data
2
-
3
- TypeORM and Knex integration for [Avleon](https://avleonjs.github.io).
4
-
5
- `@avleon/data` is an Avleon [plugin](https://avleonjs.github.io/advanced/plugins).
6
- Once it is installed, the app gains `app.useTypeORM()` and `app.useKnex()`; nothing
7
- needs to be imported to enable them.
8
-
9
- ```bash
10
- npm install @avleon/data typeorm pg # TypeORM
11
- npm install @avleon/data knex pg # Knex
12
- ```
13
-
14
- Projects created with `avleon new` load the plugin's types through the generated
15
- `avleon-env.d.ts`. In other projects, add the file yourself and include it in
16
- `tsconfig.json`:
17
-
18
- ```typescript
19
- // avleon-env.d.ts
20
- /// <reference types="@avleon/data" />
21
- ```
22
-
23
- Both methods connect before they resolve, register the connection with the
24
- container, and close it when the app shuts down (`app.close()`, SIGTERM or SIGINT).
25
- For a bundled app, which has no `package.json` to discover plugins from, pass the
26
- plugin explicitly:
27
-
28
- ```typescript
29
- import { Avleon } from "@avleon/core";
30
- import { dataPlugin } from "@avleon/data";
31
-
32
- const app = Avleon.createApplication({ plugins: [dataPlugin] });
33
- ```
34
-
35
- ## Knex
36
-
37
- `useKnex` connects eagerly and verifies the connection, so it returns a promise:
38
-
39
- ```typescript
40
- await app.useKnex({
41
- client: "mysql",
42
- connection: {
43
- host: "127.0.0.1",
44
- port: 3306,
45
- user: "root",
46
- password: "password",
47
- database: "myapp",
48
- },
49
- });
50
- ```
51
-
52
- Using a config class:
53
-
54
- ```typescript
55
- import { AppConfig, AvleonConfig, Environment } from "@avleon/core";
56
- import type { Knex } from "knex";
57
-
58
- @AppConfig
59
- export class KnexConfig extends AvleonConfig<Knex.Config> {
60
- config(env: Environment): Knex.Config {
61
- return {
62
- client: "mysql",
63
- connection: {
64
- host: env.get("DB_HOST") || "127.0.0.1",
65
- port: Number(env.get("DB_PORT")) || 3306,
66
- user: env.get("DB_USER") || "root",
67
- password: env.get("DB_PASS") || "password",
68
- database: env.get("DB_NAME") || "myapp",
69
- },
70
- };
71
- }
72
- }
73
-
74
- await app.useKnex(KnexConfig);
75
- ```
76
-
77
- Using in a service:
78
-
79
- ```typescript
80
- import { AppService } from "@avleon/core";
81
- import { KnexDB } from "@avleon/data";
82
-
83
- @AppService
84
- export class UsersService {
85
- constructor(private readonly db: KnexDB) {}
86
-
87
- async findAll() {
88
- return this.db.client.select("*").from("users");
89
- }
90
-
91
- async page(page: number) {
92
- return this.db.paginate(this.db.client("users").orderBy("id"), {
93
- page,
94
- perPage: 20,
95
- });
96
- }
97
- }
98
- ```
99
-
100
- ---
101
-
102
- ## TypeORM
103
-
104
- `useTypeORM` initializes the DataSource, so it returns a promise:
105
-
106
- ```typescript
107
- await app.useTypeORM({
108
- type: "postgres",
109
- host: "localhost",
110
- port: 5432,
111
- username: "postgres",
112
- password: "password",
113
- database: "avleon",
114
- entities: [User],
115
- synchronize: true,
116
- });
117
- ```
118
-
119
- Using a config class:
120
-
121
- ```typescript
122
- import { AppConfig, AvleonConfig, Environment } from "@avleon/core";
123
- import type { DataSourceOptions } from "typeorm";
124
-
125
- @AppConfig
126
- export class DataSourceConfig extends AvleonConfig<DataSourceOptions> {
127
- config(env: Environment): DataSourceOptions {
128
- return {
129
- type: "postgres",
130
- host: env.get("DB_HOST") || "localhost",
131
- port: Number(env.get("DB_PORT")) || 5432,
132
- username: env.get("DB_USER") || "postgres",
133
- password: env.get("DB_PASS") || "password",
134
- database: env.get("DB_NAME") || "avleon",
135
- entities: [User],
136
- synchronize: true,
137
- };
138
- }
139
- }
140
-
141
- await app.useTypeORM(DataSourceConfig);
142
- ```
143
-
144
- **Injecting a repository.** `@InjectRepository` gives you the TypeORM repository
145
- plus a `paginate()` method:
146
-
147
- ```typescript
148
- import { AppService } from "@avleon/core";
149
- import { InjectRepository, PaginatedRepository } from "@avleon/data";
150
- import { User } from "./user.entity";
151
-
152
- @AppService
153
- export class UserService {
154
- constructor(
155
- @InjectRepository(User)
156
- private readonly userRepo: PaginatedRepository<User>,
157
- ) {}
158
-
159
- findAll() {
160
- return this.userRepo.find();
161
- }
162
-
163
- page(page: number) {
164
- return this.userRepo.paginate({ page, perPage: 20 });
165
- }
166
- }
167
- ```
168
-
169
- **Custom repositories.** Extend `AvleonRepository` for pagination, transactions
170
- and your own query methods. Query operators (`Like`, `In`, `Not`, `Between`,
171
- `IsNull`, `Equal`, `ArrayContains`, …) are exported from `@avleon/data`.
172
-
173
- In 0.3.0, give the class an explicit constructor that calls `super()`, even if
174
- it has no dependencies (see [Known Issues](#known-issues-in-030)):
175
-
176
- ```typescript
177
- import { AvleonRepository, Repository, Like } from "@avleon/data";
178
-
179
- @Repository(User)
180
- export class UserRepository extends AvleonRepository<User> {
181
- constructor(private readonly audit: AuditService) {
182
- super();
183
- }
184
-
185
- search(term: string, page = 1) {
186
- return this.paginate(
187
- { where: { name: Like(`%${term}%`) } },
188
- { page, perPage: 20 },
189
- );
190
- }
191
-
192
- async rename(id: number, name: string) {
193
- return this.transaction(async (tx) => {
194
- await tx.update({ id }, { name });
195
- await this.audit.log("rename", id);
196
- });
197
- }
198
- }
199
- ```
200
-
201
- `transaction()` joins an already active transaction instead of opening a nested
202
- one. `withManager(manager)` returns a copy of the repository bound to another
203
- `EntityManager`.
204
-
205
- ## Pagination
206
-
207
- `repo.paginate()`, `AvleonRepository.paginate()`, `KnexDB.paginate()` and
208
- `paginateKnex()` return core's unified `{ data, meta }` shape; see
209
- [Pagination](https://github.com/avleonjs/avleon/tree/main/packages/core#pagination).
210
-
211
- ## License
212
-
213
- MIT
1
+ # @avleon/data
2
+
3
+ TypeORM and Knex integration for [Avleon](https://avleonjs.github.io).
4
+
5
+ `@avleon/data` is an Avleon [plugin](https://avleonjs.github.io/advanced/plugins).
6
+ Once it is installed, the app gains `app.useTypeORM()` and `app.useKnex()`; nothing
7
+ needs to be imported to enable them.
8
+
9
+ ```bash
10
+ npm install @avleon/data typeorm pg # TypeORM
11
+ npm install @avleon/data knex pg # Knex
12
+ ```
13
+
14
+ Projects created with `avleon new` load the plugin's types through the generated
15
+ `avleon-env.d.ts`. In other projects, add the file yourself and include it in
16
+ `tsconfig.json`:
17
+
18
+ ```typescript
19
+ // avleon-env.d.ts
20
+ /// <reference types="@avleon/data" />
21
+ ```
22
+
23
+ Both methods connect before they resolve, register the connection with the
24
+ container, and close it when the app shuts down (`app.close()`, SIGTERM or SIGINT).
25
+ For a bundled app, which has no `package.json` to discover plugins from, pass the
26
+ plugin explicitly:
27
+
28
+ ```typescript
29
+ import { Avleon } from "@avleon/core";
30
+ import { dataPlugin } from "@avleon/data";
31
+
32
+ const app = Avleon.createApplication({ plugins: [dataPlugin] });
33
+ ```
34
+
35
+ ## Knex
36
+
37
+ `useKnex` connects eagerly and verifies the connection, so it returns a promise:
38
+
39
+ ```typescript
40
+ await app.useKnex({
41
+ client: "mysql",
42
+ connection: {
43
+ host: "127.0.0.1",
44
+ port: 3306,
45
+ user: "root",
46
+ password: "password",
47
+ database: "myapp",
48
+ },
49
+ });
50
+ ```
51
+
52
+ Using a config class:
53
+
54
+ ```typescript
55
+ import { AppConfig, AvleonConfig, Environment } from "@avleon/core";
56
+ import type { Knex } from "knex";
57
+
58
+ @AppConfig
59
+ export class KnexConfig extends AvleonConfig<Knex.Config> {
60
+ config(env: Environment): Knex.Config {
61
+ return {
62
+ client: "mysql",
63
+ connection: {
64
+ host: env.get("DB_HOST") || "127.0.0.1",
65
+ port: Number(env.get("DB_PORT")) || 3306,
66
+ user: env.get("DB_USER") || "root",
67
+ password: env.get("DB_PASS") || "password",
68
+ database: env.get("DB_NAME") || "myapp",
69
+ },
70
+ };
71
+ }
72
+ }
73
+
74
+ await app.useKnex(KnexConfig);
75
+ ```
76
+
77
+ Using in a service:
78
+
79
+ ```typescript
80
+ import { AppService } from "@avleon/core";
81
+ import { KnexDB } from "@avleon/data";
82
+
83
+ @AppService
84
+ export class UsersService {
85
+ constructor(private readonly db: KnexDB) {}
86
+
87
+ async findAll() {
88
+ return this.db.client.select("*").from("users");
89
+ }
90
+
91
+ async page(page: number) {
92
+ return this.db.paginate(this.db.client("users").orderBy("id"), {
93
+ page,
94
+ perPage: 20,
95
+ });
96
+ }
97
+ }
98
+ ```
99
+
100
+ ---
101
+
102
+ ## TypeORM
103
+
104
+ `useTypeORM` initializes the DataSource, so it returns a promise:
105
+
106
+ ```typescript
107
+ await app.useTypeORM({
108
+ type: "postgres",
109
+ host: "localhost",
110
+ port: 5432,
111
+ username: "postgres",
112
+ password: "password",
113
+ database: "avleon",
114
+ entities: [User],
115
+ synchronize: true,
116
+ });
117
+ ```
118
+
119
+ Using a config class:
120
+
121
+ ```typescript
122
+ import { AppConfig, AvleonConfig, Environment } from "@avleon/core";
123
+ import type { DataSourceOptions } from "typeorm";
124
+
125
+ @AppConfig
126
+ export class DataSourceConfig extends AvleonConfig<DataSourceOptions> {
127
+ config(env: Environment): DataSourceOptions {
128
+ return {
129
+ type: "postgres",
130
+ host: env.get("DB_HOST") || "localhost",
131
+ port: Number(env.get("DB_PORT")) || 5432,
132
+ username: env.get("DB_USER") || "postgres",
133
+ password: env.get("DB_PASS") || "password",
134
+ database: env.get("DB_NAME") || "avleon",
135
+ entities: [User],
136
+ synchronize: true,
137
+ };
138
+ }
139
+ }
140
+
141
+ await app.useTypeORM(DataSourceConfig);
142
+ ```
143
+
144
+ **Injecting a repository.** `@InjectRepository` gives you the TypeORM repository
145
+ plus a `paginate()` method:
146
+
147
+ ```typescript
148
+ import { AppService } from "@avleon/core";
149
+ import { InjectRepository, PaginatedRepository } from "@avleon/data";
150
+ import { User } from "./user.entity";
151
+
152
+ @AppService
153
+ export class UserService {
154
+ constructor(
155
+ @InjectRepository(User)
156
+ private readonly userRepo: PaginatedRepository<User>,
157
+ ) {}
158
+
159
+ findAll() {
160
+ return this.userRepo.find();
161
+ }
162
+
163
+ page(page: number) {
164
+ return this.userRepo.paginate({ page, perPage: 20 });
165
+ }
166
+ }
167
+ ```
168
+
169
+ **Custom repositories.** Extend `AvleonRepository` for pagination, transactions
170
+ and your own query methods. Query operators (`Like`, `In`, `Not`, `Between`,
171
+ `IsNull`, `Equal`, `ArrayContains`, …) are exported from `@avleon/data`.
172
+
173
+ In 0.3.0, give the class an explicit constructor that calls `super()`, even if
174
+ it has no dependencies (see [Known Issues](#known-issues-in-030)):
175
+
176
+ ```typescript
177
+ import { AvleonRepository, Repository, Like } from "@avleon/data";
178
+
179
+ @Repository(User)
180
+ export class UserRepository extends AvleonRepository<User> {
181
+ constructor(private readonly audit: AuditService) {
182
+ super();
183
+ }
184
+
185
+ search(term: string, page = 1) {
186
+ return this.paginate(
187
+ { where: { name: Like(`%${term}%`) } },
188
+ { page, perPage: 20 },
189
+ );
190
+ }
191
+
192
+ async rename(id: number, name: string) {
193
+ return this.transaction(async (tx) => {
194
+ await tx.update({ id }, { name });
195
+ await this.audit.log("rename", id);
196
+ });
197
+ }
198
+ }
199
+ ```
200
+
201
+ `transaction()` joins an already active transaction instead of opening a nested
202
+ one. `withManager(manager)` returns a copy of the repository bound to another
203
+ `EntityManager`.
204
+
205
+ ## Pagination
206
+
207
+ `repo.paginate()`, `AvleonRepository.paginate()`, `KnexDB.paginate()` and
208
+ `paginateKnex()` return core's unified `{ data, meta }` shape; see
209
+ [Pagination](https://github.com/avleonjs/avleon/tree/main/packages/core#pagination).
210
+
211
+ ## License
212
+
213
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avleon/data",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "TypeORM and Knex integration for Avleon",
5
5
  "author": "Tareq Hossain",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "prepublishOnly": "npm run build"
46
46
  },
47
47
  "peerDependencies": {
48
- "@avleon/core": "^0.4.0",
48
+ "@avleon/core": "^0.4.1",
49
49
  "better-sqlite3": ">=12.0.0",
50
50
  "knex": ">=2.0.0",
51
51
  "mssql": ">=11.0.0",
@@ -78,7 +78,7 @@
78
78
  }
79
79
  },
80
80
  "devDependencies": {
81
- "@avleon/core": "^0.4.0",
81
+ "@avleon/core": "^0.4.1",
82
82
  "@swc/core": "^1.16.20",
83
83
  "@types/node": "^20.19.17",
84
84
  "better-sqlite3": "^12.9.0",
@@ -96,5 +96,6 @@
96
96
  },
97
97
  "publishConfig": {
98
98
  "access": "public"
99
- }
99
+ },
100
+ "gitHead": "6c29ba1d028d41ade15fda5ec9444a86d7c3df19"
100
101
  }