@ttoss/postgresdb 0.2.21 → 0.2.23
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 +87 -134
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ttoss/postgresdb
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight [Sequelize](https://sequelize.org/) wrapper for PostgreSQL databases with TypeScript support.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -9,27 +9,19 @@ pnpm add @ttoss/postgresdb
|
|
|
9
9
|
pnpm add -D @ttoss/postgresdb-cli
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
**ESM only**: Add `"type": "module"` to your `package.json`.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## Quick Start
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
{
|
|
18
|
-
"type": "module"
|
|
19
|
-
}
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### Setup the database
|
|
16
|
+
### Database Setup
|
|
25
17
|
|
|
26
|
-
|
|
18
|
+
Use Docker to create a PostgreSQL instance:
|
|
27
19
|
|
|
28
20
|
```bash
|
|
29
21
|
docker run --name postgres-test -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
|
|
30
22
|
```
|
|
31
23
|
|
|
32
|
-
|
|
24
|
+
Or with Docker Compose (`docker-compose.yml`):
|
|
33
25
|
|
|
34
26
|
```yaml
|
|
35
27
|
services:
|
|
@@ -46,15 +38,13 @@ volumes:
|
|
|
46
38
|
db-data:
|
|
47
39
|
```
|
|
48
40
|
|
|
49
|
-
And run the following command:
|
|
50
|
-
|
|
51
41
|
```bash
|
|
52
42
|
docker compose up -d
|
|
53
43
|
```
|
|
54
44
|
|
|
55
|
-
###
|
|
45
|
+
### Define Models
|
|
56
46
|
|
|
57
|
-
Create
|
|
47
|
+
Create `models/User.ts`:
|
|
58
48
|
|
|
59
49
|
```typescript
|
|
60
50
|
import { Table, Column, Model } from '@ttoss/postgresdb';
|
|
@@ -67,19 +57,19 @@ export class User extends Model<User> {
|
|
|
67
57
|
@Column
|
|
68
58
|
declare email: string;
|
|
69
59
|
}
|
|
70
|
-
|
|
71
|
-
_This packages exports all decorators from [sequelize-typescript](https://github.com/sequelize/sequelize-typescript), so you can use them to define your models._
|
|
72
60
|
```
|
|
73
61
|
|
|
74
|
-
|
|
62
|
+
_All [sequelize-typescript](https://github.com/sequelize/sequelize-typescript) decorators are available._
|
|
63
|
+
|
|
64
|
+
Export in `models/index.ts`:
|
|
75
65
|
|
|
76
66
|
```typescript
|
|
77
67
|
export { User } from './User';
|
|
78
68
|
```
|
|
79
69
|
|
|
80
|
-
###
|
|
70
|
+
### Initialize Database
|
|
81
71
|
|
|
82
|
-
Create
|
|
72
|
+
Create `src/db.ts`:
|
|
83
73
|
|
|
84
74
|
```typescript
|
|
85
75
|
import { initialize } from '@ttoss/postgresdb';
|
|
@@ -88,60 +78,46 @@ import * as models from './models';
|
|
|
88
78
|
export const db = await initialize({ models });
|
|
89
79
|
```
|
|
90
80
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
### Environment variables
|
|
94
|
-
|
|
95
|
-
You can set the database connection parameters in two ways:
|
|
81
|
+
### Configuration
|
|
96
82
|
|
|
97
|
-
1
|
|
83
|
+
**Option 1 - Direct configuration:**
|
|
98
84
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
2. Using environment variables:
|
|
111
|
-
|
|
112
|
-
- `DB_NAME`: database name
|
|
113
|
-
- `DB_USERNAME`: database username
|
|
114
|
-
- `DB_PASSWORD`: database password
|
|
115
|
-
- `DB_HOST`: database host
|
|
116
|
-
- `DB_PORT`: database port. Default: 5432
|
|
85
|
+
```typescript
|
|
86
|
+
export const db = initialize({
|
|
87
|
+
database: 'mydb',
|
|
88
|
+
username: 'user',
|
|
89
|
+
password: 'pass',
|
|
90
|
+
host: 'localhost',
|
|
91
|
+
port: 5432,
|
|
92
|
+
models,
|
|
93
|
+
});
|
|
94
|
+
```
|
|
117
95
|
|
|
118
|
-
|
|
96
|
+
**Option 2 - Environment variables (`.env`):**
|
|
119
97
|
|
|
120
|
-
|
|
98
|
+
```env
|
|
99
|
+
DB_NAME=postgres
|
|
100
|
+
DB_USERNAME=postgres
|
|
101
|
+
DB_PASSWORD=mysecretpassword
|
|
102
|
+
DB_HOST=localhost
|
|
103
|
+
DB_PORT=5432
|
|
104
|
+
```
|
|
121
105
|
|
|
122
|
-
|
|
123
|
-
DB_NAME=postgres
|
|
124
|
-
DB_USERNAME=postgres
|
|
125
|
-
DB_PASSWORD=mysecretpassword
|
|
126
|
-
DB_HOST=localhost
|
|
127
|
-
DB_PORT=5432
|
|
128
|
-
```
|
|
106
|
+
Environment variables are automatically used if defined.
|
|
129
107
|
|
|
130
|
-
### Sync
|
|
108
|
+
### Sync Schema
|
|
131
109
|
|
|
132
|
-
|
|
110
|
+
[Synchronize](https://sequelize.org/docs/v6/core-concepts/model-basics/#model-synchronization) database schema with models:
|
|
133
111
|
|
|
134
112
|
```bash
|
|
135
113
|
pnpm dlx @ttoss/postgresdb-cli sync
|
|
136
114
|
```
|
|
137
115
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
This command works by importing the `db` object from the `src/db.ts` file and calling the `sync` method on it.
|
|
116
|
+
This imports `db` from `src/db.ts` and syncs the schema.
|
|
141
117
|
|
|
142
|
-
### CRUD
|
|
118
|
+
### CRUD Operations
|
|
143
119
|
|
|
144
|
-
|
|
120
|
+
All models are accessible via the `db` object. See [Sequelize documentation](https://sequelize.org/master/manual/model-querying-basics.html) for complete query API.
|
|
145
121
|
|
|
146
122
|
```typescript
|
|
147
123
|
import { db } from './db';
|
|
@@ -152,96 +128,78 @@ const user = await db.User.create({
|
|
|
152
128
|
});
|
|
153
129
|
```
|
|
154
130
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
### Using in a monorepo
|
|
158
|
-
|
|
159
|
-
If you want to use in a monorepo by sharing the models between packages, you need to create some configurations to make it work.
|
|
160
|
-
|
|
161
|
-
#### On the `postgresdb` package
|
|
131
|
+
## Monorepo Usage
|
|
162
132
|
|
|
163
|
-
|
|
133
|
+
Share models across packages with this setup:
|
|
164
134
|
|
|
165
|
-
|
|
135
|
+
**In the database package (`@yourproject/postgresdb`):**
|
|
166
136
|
|
|
167
|
-
|
|
168
|
-
{
|
|
169
|
-
"type": "module",
|
|
170
|
-
"exports": "./src/index.ts"
|
|
171
|
-
}
|
|
172
|
-
```
|
|
137
|
+
`package.json`:
|
|
173
138
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
_We recommend to not export the `db` object in this file because you may want to use different configurations in different packages._
|
|
181
|
-
|
|
182
|
-
#### On the other packages
|
|
183
|
-
|
|
184
|
-
1. Install `@ttoss/postgresdb` package:
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"type": "module",
|
|
142
|
+
"exports": "./src/index.ts"
|
|
143
|
+
}
|
|
144
|
+
```
|
|
185
145
|
|
|
186
|
-
|
|
187
|
-
pnpm add @ttoss/postgresdb
|
|
188
|
-
```
|
|
146
|
+
`src/index.ts`:
|
|
189
147
|
|
|
190
|
-
|
|
148
|
+
```typescript
|
|
149
|
+
export * as models from './models';
|
|
150
|
+
```
|
|
191
151
|
|
|
192
|
-
|
|
193
|
-
{
|
|
194
|
-
"dependencies": {
|
|
195
|
-
"@yourproject/postgresdb": "workspace:^"
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
```
|
|
152
|
+
_Don't export `db` here - each package may need different configurations._
|
|
199
153
|
|
|
200
|
-
|
|
154
|
+
**In consuming packages:**
|
|
201
155
|
|
|
202
|
-
|
|
203
|
-
{
|
|
204
|
-
"include": ["src", "../postgresdb/src"]
|
|
205
|
-
}
|
|
206
|
-
```
|
|
156
|
+
Add dependencies to `package.json`:
|
|
207
157
|
|
|
208
|
-
|
|
158
|
+
```json
|
|
159
|
+
{
|
|
160
|
+
"dependencies": {
|
|
161
|
+
"@ttoss/postgresdb": "^x.x.x",
|
|
162
|
+
"@yourproject/postgresdb": "workspace:^"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
209
166
|
|
|
210
|
-
|
|
167
|
+
Update `tsconfig.json`:
|
|
211
168
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
169
|
+
```json
|
|
170
|
+
{
|
|
171
|
+
"include": ["src", "../postgresdb/src"]
|
|
172
|
+
}
|
|
173
|
+
```
|
|
215
174
|
|
|
216
|
-
|
|
217
|
-
models,
|
|
218
|
-
// other configurations
|
|
219
|
-
});
|
|
220
|
-
```
|
|
175
|
+
Create `src/db.ts`:
|
|
221
176
|
|
|
222
|
-
|
|
177
|
+
```typescript
|
|
178
|
+
import { initialize } from '@ttoss/postgresdb';
|
|
179
|
+
import { models } from '@yourproject/postgresdb';
|
|
223
180
|
|
|
224
|
-
|
|
181
|
+
export const db = initialize({ models });
|
|
182
|
+
```
|
|
225
183
|
|
|
226
|
-
|
|
184
|
+
## API Reference
|
|
227
185
|
|
|
228
|
-
|
|
186
|
+
### `initialize(options)`
|
|
229
187
|
|
|
230
|
-
|
|
188
|
+
Initializes database connection and loads models.
|
|
231
189
|
|
|
232
|
-
All [Sequelize options](https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor)
|
|
190
|
+
**Options:** All [Sequelize options](https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor) except `dialect` (always `postgres`), plus:
|
|
233
191
|
|
|
234
|
-
- `models
|
|
192
|
+
- `models` (required): Object mapping model names to model classes
|
|
235
193
|
|
|
236
|
-
###
|
|
194
|
+
### Decorators
|
|
237
195
|
|
|
238
|
-
|
|
196
|
+
All [sequelize-typescript](https://www.npmjs.com/package/sequelize-typescript) decorators are exported: `@Table`, `@Column`, `@ForeignKey`, etc.
|
|
239
197
|
|
|
240
|
-
|
|
198
|
+
### Types
|
|
241
199
|
|
|
242
|
-
|
|
200
|
+
#### `ModelColumns<T>`
|
|
243
201
|
|
|
244
|
-
|
|
202
|
+
Extracts column types from a model:
|
|
245
203
|
|
|
246
204
|
```typescript
|
|
247
205
|
import { Column, Model, type ModelColumns, Table } from '@ttoss/postgresdb';
|
|
@@ -255,11 +213,6 @@ class User extends Model<User> {
|
|
|
255
213
|
declare email: string;
|
|
256
214
|
}
|
|
257
215
|
|
|
258
|
-
|
|
259
|
-
* UserColumns = {
|
|
260
|
-
* name?: string;
|
|
261
|
-
* email: string;
|
|
262
|
-
* }
|
|
263
|
-
*/
|
|
216
|
+
// Inferred type: { name?: string; email: string; }
|
|
264
217
|
type UserColumns = ModelColumns<User>;
|
|
265
218
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/postgresdb",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.23",
|
|
4
4
|
"description": "A library to handle PostgreSQL database connections and queries",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/esm/index.js"
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"jest": "^30.2.0",
|
|
33
|
-
"tsup": "^8.5.
|
|
34
|
-
"@ttoss/config": "^1.35.
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"@ttoss/config": "^1.35.10"
|
|
35
35
|
},
|
|
36
36
|
"keywords": [
|
|
37
37
|
"database",
|