@ttoss/postgresdb 0.2.5 → 0.2.7
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 +38 -1
- package/dist/esm/index.js +6600 -4
- package/dist/index.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,6 +29,29 @@ If you already have a database, you can skip this step. If you don't, you can us
|
|
|
29
29
|
docker run --name postgres-test -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
|
|
30
30
|
```
|
|
31
31
|
|
|
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:
|
|
33
|
+
|
|
34
|
+
```yaml
|
|
35
|
+
services:
|
|
36
|
+
db:
|
|
37
|
+
image: postgres
|
|
38
|
+
environment:
|
|
39
|
+
POSTGRES_PASSWORD: mysecretpassword
|
|
40
|
+
volumes:
|
|
41
|
+
- db-data:/var/lib/postgresql/data
|
|
42
|
+
ports:
|
|
43
|
+
- '5432:5432'
|
|
44
|
+
|
|
45
|
+
volumes:
|
|
46
|
+
db-data:
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
And run the following command:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
docker compose up -d
|
|
53
|
+
```
|
|
54
|
+
|
|
32
55
|
### Create a model
|
|
33
56
|
|
|
34
57
|
Create a folder called `models` and add a new file called `User.ts` with the following content:
|
|
@@ -62,9 +85,11 @@ Create a new file called `src/db.ts` with the following content:
|
|
|
62
85
|
import { initialize } from '@ttoss/postgresdb';
|
|
63
86
|
import * as models from './models';
|
|
64
87
|
|
|
65
|
-
export const db = initialize({ models });
|
|
88
|
+
export const db = await initialize({ models });
|
|
66
89
|
```
|
|
67
90
|
|
|
91
|
+
_Note: the script [`sync`](#sync-the-database-schema) will use the `db` object to sync the database schema with the models._
|
|
92
|
+
|
|
68
93
|
### Environment variables
|
|
69
94
|
|
|
70
95
|
You can set the database connection parameters in two ways:
|
|
@@ -92,6 +117,16 @@ You can set the database connection parameters in two ways:
|
|
|
92
117
|
|
|
93
118
|
`@ttoss/postgresdb` will use them automatically if they are defined.
|
|
94
119
|
|
|
120
|
+
Here is an example of a `.env` file:
|
|
121
|
+
|
|
122
|
+
```env
|
|
123
|
+
DB_NAME=postgres
|
|
124
|
+
DB_USERNAME=postgres
|
|
125
|
+
DB_PASSWORD=mysecretpassword
|
|
126
|
+
DB_HOST=localhost
|
|
127
|
+
DB_PORT=5432
|
|
128
|
+
```
|
|
129
|
+
|
|
95
130
|
### Sync the database schema
|
|
96
131
|
|
|
97
132
|
To [sync](https://sequelize.org/docs/v6/core-concepts/model-basics/#model-synchronization) the database schema with the models, use the [`sync` command](../postgresdb-cli/):
|
|
@@ -102,6 +137,8 @@ pnpm dlx @ttoss/postgresdb-cli sync
|
|
|
102
137
|
|
|
103
138
|
By now, you should have a working database with a `User` table.
|
|
104
139
|
|
|
140
|
+
This command works by importing the `db` object from the `src/db.ts` file and calling the `sync` method on it.
|
|
141
|
+
|
|
105
142
|
### CRUD operations
|
|
106
143
|
|
|
107
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.
|