@ttoss/postgresdb 0.2.22 → 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.
Files changed (2) hide show
  1. package/README.md +87 -134
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ttoss/postgresdb
2
2
 
3
- This package uses [Sequelize](https://sequelize.org/) to provide a simple framework for working with PostgreSQL databases.
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
- ### ESM only
12
+ **ESM only**: Add `"type": "module"` to your `package.json`.
13
13
 
14
- This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Make sure to use it in an ESM environment.
14
+ ## Quick Start
15
15
 
16
- ```json
17
- {
18
- "type": "module"
19
- }
20
- ```
21
-
22
- ## Usage
23
-
24
- ### Setup the database
16
+ ### Database Setup
25
17
 
26
- If you already have a database, you can skip this step. If you don't, you can use the following Docker command to create a new PostgreSQL database on port 5432 using Docker:
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
- If you want to use [Docker Compose](https://docs.docker.com/compose/), you can create a `docker-compose.yml` file with the following content:
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
- ### Create a model
45
+ ### Define Models
56
46
 
57
- Create a folder called `models` and add a new file called `User.ts` with the following content:
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
- Export the model in the `models/index.ts` file:
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
- ### Create a new instance of the database
70
+ ### Initialize Database
81
71
 
82
- Create a new file called `src/db.ts` with the following content:
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
- _Note: the script [`sync`](#sync-the-database-schema) will use the `db` object to sync the database schema with the models._
92
-
93
- ### Environment variables
94
-
95
- You can set the database connection parameters in two ways:
81
+ ### Configuration
96
82
 
97
- 1. Defining them in the `src/db.ts` file using the `initialize` function.
83
+ **Option 1 - Direct configuration:**
98
84
 
99
- ```typescript
100
- export const db = initialize({
101
- database: '', // database name
102
- username: '', // database username
103
- password: '', // database password
104
- host: '', // database host
105
- port: 5432, // database port. Default: 5432
106
- models,
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
- `@ttoss/postgresdb` will use them automatically if they are defined.
96
+ **Option 2 - Environment variables (`.env`):**
119
97
 
120
- Here is an example of a `.env` file:
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
- ```env
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 the database schema
108
+ ### Sync Schema
131
109
 
132
- To [sync](https://sequelize.org/docs/v6/core-concepts/model-basics/#model-synchronization) the database schema with the models, use the [`sync` command](https://ttoss.dev/docs/modules/packages/postgresdb-cli/):
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
- By now, you should have a working database with a `User` table.
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 operations
118
+ ### CRUD Operations
143
119
 
144
- You can now use the `db` object to interact with the database. Check the [Sequelize documentation](https://sequelize.org/master/manual/model-querying-basics.html) for more information.
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
- All models are available in the `db` object.
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
- 1. Create your `postgresdb` package following the steps above.
133
+ Share models across packages with this setup:
164
134
 
165
- 1. Exports your main file in the `package.json` file:
135
+ **In the database package (`@yourproject/postgresdb`):**
166
136
 
167
- ```json
168
- {
169
- "type": "module",
170
- "exports": "./src/index.ts"
171
- }
172
- ```
137
+ `package.json`:
173
138
 
174
- 1. Create a new file called `src/index.ts` with the following content to exports the `models` you've created:
175
-
176
- ```typescript
177
- export * as models from './models';
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
- ```bash
187
- pnpm add @ttoss/postgresdb
188
- ```
146
+ `src/index.ts`:
189
147
 
190
- 1. Add your `postgresdb` package as a dependency. In the case you're using PNPM, you can use the [workspace protocol](https://pnpm.io/workspaces#workspace-protocol-workspace):
148
+ ```typescript
149
+ export * as models from './models';
150
+ ```
191
151
 
192
- ```json
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
- 1. Include the `postgresdb` package in the `include` field of the `tsconfig.json` file:
154
+ **In consuming packages:**
201
155
 
202
- ```json
203
- {
204
- "include": ["src", "../postgresdb/src"]
205
- }
206
- ```
156
+ Add dependencies to `package.json`:
207
157
 
208
- _This way, you can import the models using the `@yourproject/postgresdb` package._
158
+ ```json
159
+ {
160
+ "dependencies": {
161
+ "@ttoss/postgresdb": "^x.x.x",
162
+ "@yourproject/postgresdb": "workspace:^"
163
+ }
164
+ }
165
+ ```
209
166
 
210
- 1. Create a new file called `src/db.ts` with the following content:
167
+ Update `tsconfig.json`:
211
168
 
212
- ```typescript
213
- import { initialize } from '@ttoss/postgresdb';
214
- import { models } from '@yourproject/postgresdb';
169
+ ```json
170
+ {
171
+ "include": ["src", "../postgresdb/src"]
172
+ }
173
+ ```
215
174
 
216
- export const db = initialize({
217
- models,
218
- // other configurations
219
- });
220
- ```
175
+ Create `src/db.ts`:
221
176
 
222
- 1. Use the `db` object to interact with the database.
177
+ ```typescript
178
+ import { initialize } from '@ttoss/postgresdb';
179
+ import { models } from '@yourproject/postgresdb';
223
180
 
224
- ## API
181
+ export const db = initialize({ models });
182
+ ```
225
183
 
226
- ### `initialize(options: InitializeOptions): db`
184
+ ## API Reference
227
185
 
228
- Initialize the database connection and load the models.
186
+ ### `initialize(options)`
229
187
 
230
- #### Options
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) are available, expect `models`.
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`: An object with all models to be loaded. The keys are the model names, and the values are the model classes. This way, you can access the models using the `db` object.
192
+ - `models` (required): Object mapping model names to model classes
235
193
 
236
- ### Sequelize decorators
194
+ ### Decorators
237
195
 
238
- This package exports all decorators from [sequelize-typescript](https://www.npmjs.com/package/sequelize-typescript), i.e., `@Table`, `@Column`, `@ForeignKey`, etc.
196
+ All [sequelize-typescript](https://www.npmjs.com/package/sequelize-typescript) decorators are exported: `@Table`, `@Column`, `@ForeignKey`, etc.
239
197
 
240
- ## Types
198
+ ### Types
241
199
 
242
- ### `ModelColumns<T>`
200
+ #### `ModelColumns<T>`
243
201
 
244
- A type that represents the columns of a model.
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.22",
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
- "import": "./dist/esm/index.js",
19
- "types": "./dist/index.d.ts"
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/esm/index.js"
20
20
  }
21
21
  },
22
22
  "files": [