linkgress-orm 0.0.1 → 0.0.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/LICENSE +21 -21
- package/README.md +196 -196
- package/dist/database/database-client.interface.d.ts +19 -3
- package/dist/database/database-client.interface.d.ts.map +1 -1
- package/dist/database/database-client.interface.js +7 -0
- package/dist/database/database-client.interface.js.map +1 -1
- package/dist/database/pg-client.d.ts +12 -3
- package/dist/database/pg-client.d.ts.map +1 -1
- package/dist/database/pg-client.js +47 -12
- package/dist/database/pg-client.js.map +1 -1
- package/dist/database/postgres-client.d.ts +13 -3
- package/dist/database/postgres-client.d.ts.map +1 -1
- package/dist/database/postgres-client.js +42 -10
- package/dist/database/postgres-client.js.map +1 -1
- package/dist/entity/db-context.d.ts +18 -0
- package/dist/entity/db-context.d.ts.map +1 -1
- package/dist/entity/db-context.js +11 -1
- package/dist/entity/db-context.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/migration/db-schema-manager.js +77 -77
- package/dist/migration/enum-migrator.js +6 -6
- package/dist/query/conditions.d.ts +44 -1
- package/dist/query/conditions.d.ts.map +1 -1
- package/dist/query/conditions.js +107 -4
- package/dist/query/conditions.js.map +1 -1
- package/dist/query/cte-builder.js +5 -5
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +100 -51
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/strategies/jsonb-collection-strategy.js +38 -38
- package/dist/query/strategies/temptable-collection-strategy.js +62 -62
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Linkgress ORM Contributors
|
|
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) 2025 Linkgress ORM Contributors
|
|
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,196 +1,196 @@
|
|
|
1
|
-
# Linkgress ORM
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/linkgress-orm)
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
[](https://www.typescriptlang.org/)
|
|
6
|
-
[](https://www.postgresql.org/)
|
|
7
|
-
|
|
8
|
-
A type-safe ORM for PostgreSQL and TypeScript with automatic type inference and powerful query capabilities.
|
|
9
|
-
|
|
10
|
-
**LINQ-Inspired Query Syntax:** The query API is designed to feel familiar to developers coming from C# LINQ, with chainable methods like `select()`, `where()`, `orderBy()`, and `groupBy()`. You also get magic SQL string interpolation for when you need raw SQL power without sacrificing type safety.
|
|
11
|
-
|
|
12
|
-
**PostgreSQL-First Philosophy:** While other ORMs aim high and try to support all platforms, Linkgress is built exclusively for PostgreSQL. This allows it to leverage PostgreSQL's advanced features to the maximum—particularly in how collections and aggregations are retrieved using CTEs, JSON aggregations, and native PostgreSQL optimizations.
|
|
13
|
-
|
|
14
|
-
## Features
|
|
15
|
-
|
|
16
|
-
- **Entity-First Approach** - Define entities with `DbColumn<T>`, no decorators needed
|
|
17
|
-
- **Fluent Configuration API** - Intuitive `DbContext` pattern with method chaining
|
|
18
|
-
- **Automatic Type Inference** - Full TypeScript support without manual type annotations
|
|
19
|
-
- **Nested Collection Queries** - Query one-to-many relationships with automatic CTE optimization
|
|
20
|
-
- **Type-Safe Aggregations** - `count()`, `sum()`, `max()`, `min()` return proper types
|
|
21
|
-
- **Powerful Filtering** - Type-checked query conditions
|
|
22
|
-
- **Transaction Support** - Safe, type-checked transactions
|
|
23
|
-
- **Multiple Clients** - Works with both `pg` and `postgres` npm packages
|
|
24
|
-
|
|
25
|
-
## Table of Contents
|
|
26
|
-
|
|
27
|
-
- [Quick Start](#quick-start)
|
|
28
|
-
- [Features](#features)
|
|
29
|
-
- [Documentation](#documentation)
|
|
30
|
-
- [Requirements](#requirements)
|
|
31
|
-
- [Contributing](#contributing)
|
|
32
|
-
- [License](#license)
|
|
33
|
-
- [Support](#support)
|
|
34
|
-
|
|
35
|
-
## Quick Start
|
|
36
|
-
|
|
37
|
-
### Installation
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
npm install linkgress-orm postgres
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
See [Installation Guide](./docs/installation.md) for detailed setup.
|
|
44
|
-
|
|
45
|
-
### Define Entities
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
import { DbEntity, DbColumn } from 'linkgress-orm';
|
|
49
|
-
|
|
50
|
-
export class User extends DbEntity {
|
|
51
|
-
id!: DbColumn<number>;
|
|
52
|
-
username!: DbColumn<string>;
|
|
53
|
-
email!: DbColumn<string>;
|
|
54
|
-
posts?: Post[]; // Navigation property
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export class Post extends DbEntity {
|
|
58
|
-
id!: DbColumn<number>;
|
|
59
|
-
title!: DbColumn<string>;
|
|
60
|
-
userId!: DbColumn<number>;
|
|
61
|
-
views!: DbColumn<number>;
|
|
62
|
-
user?: User; // Navigation property
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Create DbContext
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
import { DbContext, DbEntityTable, DbModelConfig, integer, varchar } from 'linkgress-orm';
|
|
70
|
-
|
|
71
|
-
export class AppDatabase extends DbContext {
|
|
72
|
-
get users(): DbEntityTable<User> {
|
|
73
|
-
return this.table(User);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get posts(): DbEntityTable<Post> {
|
|
77
|
-
return this.table(Post);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
protected override setupModel(model: DbModelConfig): void {
|
|
81
|
-
model.entity(User, entity => {
|
|
82
|
-
entity.toTable('users');
|
|
83
|
-
entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'users_id_seq' }));
|
|
84
|
-
entity.property(e => e.username).hasType(varchar('username', 100)).isRequired();
|
|
85
|
-
entity.property(e => e.email).hasType(varchar('email', 255)).isRequired();
|
|
86
|
-
|
|
87
|
-
entity.hasMany(e => e.posts, () => Post)
|
|
88
|
-
.withForeignKey(p => p.userId)
|
|
89
|
-
.withPrincipalKey(u => u.id);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
model.entity(Post, entity => {
|
|
93
|
-
entity.toTable('posts');
|
|
94
|
-
entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'posts_id_seq' }));
|
|
95
|
-
entity.property(e => e.title).hasType(varchar('title', 200)).isRequired();
|
|
96
|
-
entity.property(e => e.userId).hasType(integer('user_id')).isRequired();
|
|
97
|
-
entity.property(e => e.views).hasType(integer('views')).hasDefaultValue(0);
|
|
98
|
-
|
|
99
|
-
entity.hasOne(e => e.user, () => User)
|
|
100
|
-
.withForeignKey(p => p.userId)
|
|
101
|
-
.withPrincipalKey(u => u.id);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Query with Type Safety
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
import { eq, gt } from 'linkgress-orm';
|
|
111
|
-
import { PostgresClient } from 'linkgress-orm';
|
|
112
|
-
|
|
113
|
-
// Create a database client with connection pooling
|
|
114
|
-
const client = new PostgresClient('postgres://user:pass@localhost/db');
|
|
115
|
-
|
|
116
|
-
// Create a DbContext instance - reuse this across your application!
|
|
117
|
-
const db = new AppDatabase(client);
|
|
118
|
-
|
|
119
|
-
// Create schema
|
|
120
|
-
await db.ensureCreated();
|
|
121
|
-
|
|
122
|
-
// Insert
|
|
123
|
-
await db.users.insert({ username: 'alice', email: 'alice@example.com' });
|
|
124
|
-
|
|
125
|
-
// Query with filters
|
|
126
|
-
const activeUsers = await db.users
|
|
127
|
-
.where(u => eq(u.username, 'alice'))
|
|
128
|
-
.toList();
|
|
129
|
-
|
|
130
|
-
// Nested collection query with aggregations
|
|
131
|
-
const usersWithStats = await db.users
|
|
132
|
-
.select(u => ({
|
|
133
|
-
username: u.username,
|
|
134
|
-
postCount: u.posts.count(), // Automatic type inference - no casting!
|
|
135
|
-
maxViews: u.posts.max(p => p.views), // Returns number | null
|
|
136
|
-
posts: u.posts
|
|
137
|
-
.select(p => ({ title: p.title, views: p.views }))
|
|
138
|
-
.where(p => gt(p.views, 10))
|
|
139
|
-
.toList('posts'),
|
|
140
|
-
}))
|
|
141
|
-
.toList();
|
|
142
|
-
|
|
143
|
-
// Note: Only call dispose() when shutting down your application
|
|
144
|
-
// For long-running apps (servers), keep the db instance alive
|
|
145
|
-
// and dispose on process exit (see Connection Lifecycle docs)
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
**Result is fully typed:**
|
|
149
|
-
```typescript
|
|
150
|
-
Array<{
|
|
151
|
-
username: string;
|
|
152
|
-
postCount: number;
|
|
153
|
-
maxViews: number | null;
|
|
154
|
-
posts: Array<{ title: string; views: number }>;
|
|
155
|
-
}>
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
## Documentation
|
|
159
|
-
|
|
160
|
-
### Getting Started
|
|
161
|
-
- **[Getting Started Guide](./docs/getting-started.md)** - Complete walkthrough for beginners
|
|
162
|
-
- **[Installation](./docs/installation.md)** - Setup and installation instructions
|
|
163
|
-
- **[Database Clients](./docs/database-clients.md)** - Choose between `pg` and `postgres`, connection pooling, and lifecycle management
|
|
164
|
-
|
|
165
|
-
### Guides
|
|
166
|
-
- **[Schema Configuration](./docs/guides/schema-configuration.md)** - Entity configuration, relationships, and indexes
|
|
167
|
-
- **[Querying](./docs/guides/querying.md)** - Query data with type-safe filters, joins, aggregations, and more
|
|
168
|
-
- **[Insert/Update/Upsert/BULK](./docs/guides/insert-update-guide.md)** - Insert, update, delete, and bulk operations
|
|
169
|
-
|
|
170
|
-
### Advanced
|
|
171
|
-
- **[Collection Strategies](./docs/collection-strategies.md)** - JSONB vs temp table strategies, performance optimization, and multi-statement execution
|
|
172
|
-
- **[Subqueries](./docs/guides/subquery-guide.md)** - Using subqueries in your queries
|
|
173
|
-
- **[Custom Types](./docs/guides/schema-configuration.md#custom-types)** - Create custom type mappers
|
|
174
|
-
|
|
175
|
-
## Requirements
|
|
176
|
-
|
|
177
|
-
- Node.js 16+
|
|
178
|
-
- TypeScript 5.0+
|
|
179
|
-
- PostgreSQL 12+
|
|
180
|
-
|
|
181
|
-
## Contributing
|
|
182
|
-
|
|
183
|
-
Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
184
|
-
|
|
185
|
-
## License
|
|
186
|
-
|
|
187
|
-
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
188
|
-
|
|
189
|
-
## Support
|
|
190
|
-
|
|
191
|
-
- **[GitHub Issues](https://github.com/brunolau/linkgress-orm/issues)** - Report bugs or request features
|
|
192
|
-
- **[Discussions](https://github.com/brunolau/linkgress-orm/discussions)** - Ask questions and share ideas
|
|
193
|
-
|
|
194
|
-
---
|
|
195
|
-
|
|
196
|
-
Crafted with ❤️ for developers who love type safety and clean APIs.
|
|
1
|
+
# Linkgress ORM
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/linkgress-orm)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://www.postgresql.org/)
|
|
7
|
+
|
|
8
|
+
A type-safe ORM for PostgreSQL and TypeScript with automatic type inference and powerful query capabilities.
|
|
9
|
+
|
|
10
|
+
**LINQ-Inspired Query Syntax:** The query API is designed to feel familiar to developers coming from C# LINQ, with chainable methods like `select()`, `where()`, `orderBy()`, and `groupBy()`. You also get magic SQL string interpolation for when you need raw SQL power without sacrificing type safety.
|
|
11
|
+
|
|
12
|
+
**PostgreSQL-First Philosophy:** While other ORMs aim high and try to support all platforms, Linkgress is built exclusively for PostgreSQL. This allows it to leverage PostgreSQL's advanced features to the maximum—particularly in how collections and aggregations are retrieved using CTEs, JSON aggregations, and native PostgreSQL optimizations.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **Entity-First Approach** - Define entities with `DbColumn<T>`, no decorators needed
|
|
17
|
+
- **Fluent Configuration API** - Intuitive `DbContext` pattern with method chaining
|
|
18
|
+
- **Automatic Type Inference** - Full TypeScript support without manual type annotations
|
|
19
|
+
- **Nested Collection Queries** - Query one-to-many relationships with automatic CTE optimization
|
|
20
|
+
- **Type-Safe Aggregations** - `count()`, `sum()`, `max()`, `min()` return proper types
|
|
21
|
+
- **Powerful Filtering** - Type-checked query conditions
|
|
22
|
+
- **Transaction Support** - Safe, type-checked transactions
|
|
23
|
+
- **Multiple Clients** - Works with both `pg` and `postgres` npm packages
|
|
24
|
+
|
|
25
|
+
## Table of Contents
|
|
26
|
+
|
|
27
|
+
- [Quick Start](#quick-start)
|
|
28
|
+
- [Features](#features)
|
|
29
|
+
- [Documentation](#documentation)
|
|
30
|
+
- [Requirements](#requirements)
|
|
31
|
+
- [Contributing](#contributing)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
- [Support](#support)
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
### Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install linkgress-orm postgres
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
See [Installation Guide](./docs/installation.md) for detailed setup.
|
|
44
|
+
|
|
45
|
+
### Define Entities
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { DbEntity, DbColumn } from 'linkgress-orm';
|
|
49
|
+
|
|
50
|
+
export class User extends DbEntity {
|
|
51
|
+
id!: DbColumn<number>;
|
|
52
|
+
username!: DbColumn<string>;
|
|
53
|
+
email!: DbColumn<string>;
|
|
54
|
+
posts?: Post[]; // Navigation property
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class Post extends DbEntity {
|
|
58
|
+
id!: DbColumn<number>;
|
|
59
|
+
title!: DbColumn<string>;
|
|
60
|
+
userId!: DbColumn<number>;
|
|
61
|
+
views!: DbColumn<number>;
|
|
62
|
+
user?: User; // Navigation property
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Create DbContext
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { DbContext, DbEntityTable, DbModelConfig, integer, varchar } from 'linkgress-orm';
|
|
70
|
+
|
|
71
|
+
export class AppDatabase extends DbContext {
|
|
72
|
+
get users(): DbEntityTable<User> {
|
|
73
|
+
return this.table(User);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get posts(): DbEntityTable<Post> {
|
|
77
|
+
return this.table(Post);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected override setupModel(model: DbModelConfig): void {
|
|
81
|
+
model.entity(User, entity => {
|
|
82
|
+
entity.toTable('users');
|
|
83
|
+
entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'users_id_seq' }));
|
|
84
|
+
entity.property(e => e.username).hasType(varchar('username', 100)).isRequired();
|
|
85
|
+
entity.property(e => e.email).hasType(varchar('email', 255)).isRequired();
|
|
86
|
+
|
|
87
|
+
entity.hasMany(e => e.posts, () => Post)
|
|
88
|
+
.withForeignKey(p => p.userId)
|
|
89
|
+
.withPrincipalKey(u => u.id);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
model.entity(Post, entity => {
|
|
93
|
+
entity.toTable('posts');
|
|
94
|
+
entity.property(e => e.id).hasType(integer('id').primaryKey().generatedAlwaysAsIdentity({ name: 'posts_id_seq' }));
|
|
95
|
+
entity.property(e => e.title).hasType(varchar('title', 200)).isRequired();
|
|
96
|
+
entity.property(e => e.userId).hasType(integer('user_id')).isRequired();
|
|
97
|
+
entity.property(e => e.views).hasType(integer('views')).hasDefaultValue(0);
|
|
98
|
+
|
|
99
|
+
entity.hasOne(e => e.user, () => User)
|
|
100
|
+
.withForeignKey(p => p.userId)
|
|
101
|
+
.withPrincipalKey(u => u.id);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Query with Type Safety
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { eq, gt } from 'linkgress-orm';
|
|
111
|
+
import { PostgresClient } from 'linkgress-orm';
|
|
112
|
+
|
|
113
|
+
// Create a database client with connection pooling
|
|
114
|
+
const client = new PostgresClient('postgres://user:pass@localhost/db');
|
|
115
|
+
|
|
116
|
+
// Create a DbContext instance - reuse this across your application!
|
|
117
|
+
const db = new AppDatabase(client);
|
|
118
|
+
|
|
119
|
+
// Create schema
|
|
120
|
+
await db.ensureCreated();
|
|
121
|
+
|
|
122
|
+
// Insert
|
|
123
|
+
await db.users.insert({ username: 'alice', email: 'alice@example.com' });
|
|
124
|
+
|
|
125
|
+
// Query with filters
|
|
126
|
+
const activeUsers = await db.users
|
|
127
|
+
.where(u => eq(u.username, 'alice'))
|
|
128
|
+
.toList();
|
|
129
|
+
|
|
130
|
+
// Nested collection query with aggregations
|
|
131
|
+
const usersWithStats = await db.users
|
|
132
|
+
.select(u => ({
|
|
133
|
+
username: u.username,
|
|
134
|
+
postCount: u.posts.count(), // Automatic type inference - no casting!
|
|
135
|
+
maxViews: u.posts.max(p => p.views), // Returns number | null
|
|
136
|
+
posts: u.posts
|
|
137
|
+
.select(p => ({ title: p.title, views: p.views }))
|
|
138
|
+
.where(p => gt(p.views, 10))
|
|
139
|
+
.toList('posts'),
|
|
140
|
+
}))
|
|
141
|
+
.toList();
|
|
142
|
+
|
|
143
|
+
// Note: Only call dispose() when shutting down your application
|
|
144
|
+
// For long-running apps (servers), keep the db instance alive
|
|
145
|
+
// and dispose on process exit (see Connection Lifecycle docs)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
**Result is fully typed:**
|
|
149
|
+
```typescript
|
|
150
|
+
Array<{
|
|
151
|
+
username: string;
|
|
152
|
+
postCount: number;
|
|
153
|
+
maxViews: number | null;
|
|
154
|
+
posts: Array<{ title: string; views: number }>;
|
|
155
|
+
}>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Documentation
|
|
159
|
+
|
|
160
|
+
### Getting Started
|
|
161
|
+
- **[Getting Started Guide](./docs/getting-started.md)** - Complete walkthrough for beginners
|
|
162
|
+
- **[Installation](./docs/installation.md)** - Setup and installation instructions
|
|
163
|
+
- **[Database Clients](./docs/database-clients.md)** - Choose between `pg` and `postgres`, connection pooling, and lifecycle management
|
|
164
|
+
|
|
165
|
+
### Guides
|
|
166
|
+
- **[Schema Configuration](./docs/guides/schema-configuration.md)** - Entity configuration, relationships, and indexes
|
|
167
|
+
- **[Querying](./docs/guides/querying.md)** - Query data with type-safe filters, joins, aggregations, and more
|
|
168
|
+
- **[Insert/Update/Upsert/BULK](./docs/guides/insert-update-guide.md)** - Insert, update, delete, and bulk operations
|
|
169
|
+
|
|
170
|
+
### Advanced
|
|
171
|
+
- **[Collection Strategies](./docs/collection-strategies.md)** - JSONB vs temp table strategies, performance optimization, and multi-statement execution
|
|
172
|
+
- **[Subqueries](./docs/guides/subquery-guide.md)** - Using subqueries in your queries
|
|
173
|
+
- **[Custom Types](./docs/guides/schema-configuration.md#custom-types)** - Create custom type mappers
|
|
174
|
+
|
|
175
|
+
## Requirements
|
|
176
|
+
|
|
177
|
+
- Node.js 16+
|
|
178
|
+
- TypeScript 5.0+
|
|
179
|
+
- PostgreSQL 12+
|
|
180
|
+
|
|
181
|
+
## Contributing
|
|
182
|
+
|
|
183
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
188
|
+
|
|
189
|
+
## Support
|
|
190
|
+
|
|
191
|
+
- **[GitHub Issues](https://github.com/brunolau/linkgress-orm/issues)** - Report bugs or request features
|
|
192
|
+
- **[Discussions](https://github.com/brunolau/linkgress-orm/discussions)** - Ask questions and share ideas
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
Crafted with ❤️ for developers who love type safety and clean APIs.
|
|
@@ -5,12 +5,23 @@ export interface QueryResult<T = any> {
|
|
|
5
5
|
rows: T[];
|
|
6
6
|
rowCount: number | null;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* Query execution options
|
|
10
|
+
*/
|
|
11
|
+
export interface QueryExecutionOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Use binary protocol for data transfer (when supported by driver).
|
|
14
|
+
* Binary protocol can improve performance by avoiding string conversions.
|
|
15
|
+
* Default: false (uses text protocol)
|
|
16
|
+
*/
|
|
17
|
+
useBinaryProtocol?: boolean;
|
|
18
|
+
}
|
|
8
19
|
/**
|
|
9
20
|
* Database-agnostic pooled client/connection interface
|
|
10
21
|
* Represents a single connection from the pool for transactions
|
|
11
22
|
*/
|
|
12
23
|
export interface PooledConnection {
|
|
13
|
-
query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
|
|
24
|
+
query<T = any>(sql: string, params?: any[], options?: QueryExecutionOptions): Promise<QueryResult<T>>;
|
|
14
25
|
release(): void;
|
|
15
26
|
}
|
|
16
27
|
/**
|
|
@@ -18,9 +29,9 @@ export interface PooledConnection {
|
|
|
18
29
|
*/
|
|
19
30
|
export declare abstract class DatabaseClient {
|
|
20
31
|
/**
|
|
21
|
-
* Execute a query with optional parameters
|
|
32
|
+
* Execute a query with optional parameters and execution options
|
|
22
33
|
*/
|
|
23
|
-
abstract query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
|
|
34
|
+
abstract query<T = any>(sql: string, params?: any[], options?: QueryExecutionOptions): Promise<QueryResult<T>>;
|
|
24
35
|
/**
|
|
25
36
|
* Get a connection from the pool for transactions
|
|
26
37
|
*/
|
|
@@ -41,5 +52,10 @@ export declare abstract class DatabaseClient {
|
|
|
41
52
|
* Default: false for safety
|
|
42
53
|
*/
|
|
43
54
|
supportsMultiStatementQueries(): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Check if the driver supports binary protocol for improved performance.
|
|
57
|
+
* Default: false
|
|
58
|
+
*/
|
|
59
|
+
supportsBinaryProtocol(): boolean;
|
|
44
60
|
}
|
|
45
61
|
//# sourceMappingURL=database-client.interface.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database-client.interface.d.ts","sourceRoot":"","sources":["../../src/database/database-client.interface.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,GAAG;IAClC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"database-client.interface.d.ts","sourceRoot":"","sources":["../../src/database/database-client.interface.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,GAAG;IAClC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IACtG,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,8BAAsB,cAAc;IAClC;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAE9G;;OAEG;IACH,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAE7C;;OAEG;IACH,QAAQ,CAAC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAE7B;;OAEG;IACH,QAAQ,CAAC,aAAa,IAAI,MAAM;IAEhC;;;;;;OAMG;IACH,6BAA6B,IAAI,OAAO;IAIxC;;;OAGG;IACH,sBAAsB,IAAI,OAAO;CAGlC"}
|
|
@@ -15,6 +15,13 @@ class DatabaseClient {
|
|
|
15
15
|
supportsMultiStatementQueries() {
|
|
16
16
|
return false;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Check if the driver supports binary protocol for improved performance.
|
|
20
|
+
* Default: false
|
|
21
|
+
*/
|
|
22
|
+
supportsBinaryProtocol() {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
18
25
|
}
|
|
19
26
|
exports.DatabaseClient = DatabaseClient;
|
|
20
27
|
//# sourceMappingURL=database-client.interface.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database-client.interface.js","sourceRoot":"","sources":["../../src/database/database-client.interface.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"database-client.interface.js","sourceRoot":"","sources":["../../src/database/database-client.interface.ts"],"names":[],"mappings":";;;AA6BA;;GAEG;AACH,MAAsB,cAAc;IAqBlC;;;;;;OAMG;IACH,6BAA6B;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;OAGG;IACH,sBAAsB;QACpB,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvCD,wCAuCC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DatabaseClient, PooledConnection, QueryResult } from './database-client.interface';
|
|
1
|
+
import { DatabaseClient, PooledConnection, QueryResult, QueryExecutionOptions } from './database-client.interface';
|
|
2
2
|
import type { PoolConfig } from './types';
|
|
3
3
|
type Pool = any;
|
|
4
4
|
/**
|
|
@@ -10,8 +10,13 @@ type Pool = any;
|
|
|
10
10
|
*/
|
|
11
11
|
export declare class PgClient extends DatabaseClient {
|
|
12
12
|
private pool;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
private ownsConnection;
|
|
14
|
+
/**
|
|
15
|
+
* Create a PgClient
|
|
16
|
+
* @param config - Either a PoolConfig object or an existing pg.Pool instance
|
|
17
|
+
*/
|
|
18
|
+
constructor(config: PoolConfig | Pool);
|
|
19
|
+
query<T = any>(sql: string, params?: any[], options?: QueryExecutionOptions): Promise<QueryResult<T>>;
|
|
15
20
|
connect(): Promise<PooledConnection>;
|
|
16
21
|
end(): Promise<void>;
|
|
17
22
|
getDriverName(): string;
|
|
@@ -21,6 +26,10 @@ export declare class PgClient extends DatabaseClient {
|
|
|
21
26
|
* Use PostgresClient (postgres library) for true single-roundtrip multi-statement support
|
|
22
27
|
*/
|
|
23
28
|
supportsMultiStatementQueries(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* pg library supports binary protocol via rowMode option
|
|
31
|
+
*/
|
|
32
|
+
supportsBinaryProtocol(): boolean;
|
|
24
33
|
/**
|
|
25
34
|
* Get access to the underlying pg Pool for advanced use cases
|
|
26
35
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-client.d.ts","sourceRoot":"","sources":["../../src/database/pg-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"pg-client.d.ts","sourceRoot":"","sources":["../../src/database/pg-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACnH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAG1C,KAAK,IAAI,GAAG,GAAG,CAAC;AAmChB;;;;;;GAMG;AACH,qBAAa,QAAS,SAAQ,cAAc;IAC1C,OAAO,CAAC,IAAI,CAAO;IACnB,OAAO,CAAC,cAAc,CAAU;IAEhC;;;OAGG;gBACS,MAAM,EAAE,UAAU,GAAG,IAAI;IAsB/B,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAcrG,OAAO,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAKpC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAO1B,aAAa,IAAI,MAAM;IAIvB;;;;OAIG;IACH,6BAA6B,IAAI,OAAO;IAIxC;;OAEG;IACH,sBAAsB,IAAI,OAAO;IAIjC;;OAEG;IACH,OAAO,IAAI,IAAI;CAGhB"}
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PgClient = void 0;
|
|
4
4
|
const database_client_interface_1 = require("./database-client.interface");
|
|
5
|
+
/**
|
|
6
|
+
* Check if a value is a pg.Pool instance
|
|
7
|
+
*/
|
|
8
|
+
function isPgPoolInstance(value) {
|
|
9
|
+
return value && typeof value === 'object' && typeof value.query === 'function' && typeof value.connect === 'function' && typeof value.end === 'function';
|
|
10
|
+
}
|
|
5
11
|
/**
|
|
6
12
|
* Wrapper for the pooled connection from pg library
|
|
7
13
|
*/
|
|
@@ -9,8 +15,12 @@ class PgPooledConnection {
|
|
|
9
15
|
constructor(client) {
|
|
10
16
|
this.client = client;
|
|
11
17
|
}
|
|
12
|
-
async query(sql, params) {
|
|
13
|
-
|
|
18
|
+
async query(sql, params, options) {
|
|
19
|
+
// pg library supports binary protocol via types option
|
|
20
|
+
const queryConfig = options?.useBinaryProtocol
|
|
21
|
+
? { text: sql, values: params, rowMode: 'array' }
|
|
22
|
+
: { text: sql, values: params };
|
|
23
|
+
const result = await this.client.query(queryConfig);
|
|
14
24
|
return {
|
|
15
25
|
rows: result.rows,
|
|
16
26
|
rowCount: result.rowCount,
|
|
@@ -28,20 +38,36 @@ class PgPooledConnection {
|
|
|
28
38
|
* npm install pg
|
|
29
39
|
*/
|
|
30
40
|
class PgClient extends database_client_interface_1.DatabaseClient {
|
|
41
|
+
/**
|
|
42
|
+
* Create a PgClient
|
|
43
|
+
* @param config - Either a PoolConfig object or an existing pg.Pool instance
|
|
44
|
+
*/
|
|
31
45
|
constructor(config) {
|
|
32
46
|
super();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.
|
|
47
|
+
// Check if config is an existing pg.Pool instance
|
|
48
|
+
if (isPgPoolInstance(config)) {
|
|
49
|
+
this.pool = config;
|
|
50
|
+
this.ownsConnection = false;
|
|
37
51
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
else {
|
|
53
|
+
try {
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
55
|
+
const { Pool } = require('pg');
|
|
56
|
+
this.pool = new Pool(config);
|
|
57
|
+
this.ownsConnection = true;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new Error('PgClient requires the "pg" package to be installed. ' +
|
|
61
|
+
'Install it with: npm install pg');
|
|
62
|
+
}
|
|
41
63
|
}
|
|
42
64
|
}
|
|
43
|
-
async query(sql, params) {
|
|
44
|
-
|
|
65
|
+
async query(sql, params, options) {
|
|
66
|
+
// pg library supports binary protocol via types option
|
|
67
|
+
const queryConfig = options?.useBinaryProtocol
|
|
68
|
+
? { text: sql, values: params, rowMode: 'array' }
|
|
69
|
+
: { text: sql, values: params };
|
|
70
|
+
const result = await this.pool.query(queryConfig);
|
|
45
71
|
return {
|
|
46
72
|
rows: result.rows,
|
|
47
73
|
rowCount: result.rowCount,
|
|
@@ -52,7 +78,10 @@ class PgClient extends database_client_interface_1.DatabaseClient {
|
|
|
52
78
|
return new PgPooledConnection(client);
|
|
53
79
|
}
|
|
54
80
|
async end() {
|
|
55
|
-
|
|
81
|
+
// Only close the pool if we created it
|
|
82
|
+
if (this.ownsConnection) {
|
|
83
|
+
await this.pool.end();
|
|
84
|
+
}
|
|
56
85
|
}
|
|
57
86
|
getDriverName() {
|
|
58
87
|
return 'pg';
|
|
@@ -65,6 +94,12 @@ class PgClient extends database_client_interface_1.DatabaseClient {
|
|
|
65
94
|
supportsMultiStatementQueries() {
|
|
66
95
|
return false;
|
|
67
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* pg library supports binary protocol via rowMode option
|
|
99
|
+
*/
|
|
100
|
+
supportsBinaryProtocol() {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
68
103
|
/**
|
|
69
104
|
* Get access to the underlying pg Pool for advanced use cases
|
|
70
105
|
*/
|