@ttoss/postgresdb 0.2.1 → 0.2.3

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 CHANGED
@@ -9,6 +9,16 @@ pnpm add @ttoss/postgresdb
9
9
  pnpm add -D @ttoss/postgresdb-cli
10
10
  ```
11
11
 
12
+ ### ESM only
13
+
14
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Make sure to use it in an ESM environment.
15
+
16
+ ```json
17
+ {
18
+ "type": "module"
19
+ }
20
+ ```
21
+
12
22
  ## Usage
13
23
 
14
24
  ### Setup the database
@@ -107,6 +117,73 @@ const user = await db.User.create({
107
117
 
108
118
  All models are available in the `db` object.
109
119
 
120
+ ### Using in a monorepo
121
+
122
+ If you want to use in a monorepo by sharing the models between packages, you need to create some configurations to make it work.
123
+
124
+ #### On the `postgresdb` package
125
+
126
+ 1. Create your `postgresdb` package following the steps above.
127
+
128
+ 1. Exports your main file in the `package.json` file:
129
+
130
+ ```json
131
+ {
132
+ "type": "module",
133
+ "exports": "./src/index.ts"
134
+ }
135
+ ```
136
+
137
+ 1. Create a new file called `src/index.ts` with the following content to exports the `models` you've created:
138
+
139
+ ```typescript
140
+ export * as models from './models';
141
+ ```
142
+
143
+ _We recommend to not export the `db` object in this file because you may want to use different configurations in different packages._
144
+
145
+ #### On the other packages
146
+
147
+ 1. Install `@ttoss/postgresdb` package:
148
+
149
+ ```bash
150
+ pnpm add @ttoss/postgresdb
151
+ ```
152
+
153
+ 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):
154
+
155
+ ```json
156
+ {
157
+ "dependencies": {
158
+ "@yourproject/postgresdb": "workspace:^"
159
+ }
160
+ }
161
+ ```
162
+
163
+ 1. Include the `postgresdb` package in the `include` field of the `tsconfig.json` file:
164
+
165
+ ```json
166
+ {
167
+ "include": ["src", "../postgresdb/src"]
168
+ }
169
+ ```
170
+
171
+ _This way, you can import the models using the `@yourproject/postgresdb` package._
172
+
173
+ 1. Create a new file called `src/db.ts` with the following content:
174
+
175
+ ```typescript
176
+ import { initialize } from '@ttoss/postgresdb';
177
+ import { models } from '@yourproject/postgresdb';
178
+
179
+ export const db = initialize({
180
+ models,
181
+ // other configurations
182
+ });
183
+ ```
184
+
185
+ 1. Use the `db` object to interact with the database.
186
+
110
187
  ## API
111
188
 
112
189
  ### `initialize(options: InitializeOptions): db`
@@ -122,3 +199,30 @@ All [Sequelize options](https://sequelize.org/api/v6/class/src/sequelize.js~sequ
122
199
  ### Sequelize decorators
123
200
 
124
201
  This package exports all decorators from [sequelize-typescript](https://www.npmjs.com/package/sequelize-typescript), i.e., `@Table`, `@Column`, `@ForeignKey`, etc.
202
+
203
+ ## Types
204
+
205
+ ### `ModelColumns<T>`
206
+
207
+ A type that represents the columns of a model.
208
+
209
+ ```typescript
210
+ import { Column, Model, type ModelColumns, Table } from '@ttoss/postgresdb';
211
+
212
+ @Table
213
+ class User extends Model<User> {
214
+ @Column
215
+ declare name?: string;
216
+
217
+ @Column
218
+ declare email: string;
219
+ }
220
+
221
+ /**
222
+ * UserColumns = {
223
+ * name?: string;
224
+ * email: string;
225
+ * }
226
+ */
227
+ type UserColumns = ModelColumns<User>;
228
+ ```
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ModelCtor, Sequelize, SequelizeOptions } from 'sequelize-typescript';
1
+ import { ModelCtor, Sequelize, SequelizeOptions, Model } from 'sequelize-typescript';
2
2
  export * from 'sequelize-typescript';
3
3
 
4
4
  type Options<Models> = Omit<SequelizeOptions, 'models' | 'dialect'> & {
@@ -10,4 +10,6 @@ declare const initialize: <Models extends {
10
10
  sequelize: Sequelize;
11
11
  } & Models>;
12
12
 
13
- export { initialize };
13
+ type ModelColumns<T> = Omit<T, keyof Model> & Pick<Model, 'id' | 'createdAt' | 'updatedAt'>;
14
+
15
+ export { type ModelColumns, initialize };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/postgresdb",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A library to handle PostgreSQL database connections and queries",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",