linkgress-orm 0.0.2 → 0.1.0
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/entity/db-column.d.ts +38 -1
- package/dist/entity/db-column.d.ts.map +1 -1
- package/dist/entity/db-column.js.map +1 -1
- package/dist/entity/db-context.d.ts +429 -50
- package/dist/entity/db-context.d.ts.map +1 -1
- package/dist/entity/db-context.js +884 -203
- package/dist/entity/db-context.js.map +1 -1
- package/dist/entity/entity-base.d.ts +8 -0
- package/dist/entity/entity-base.d.ts.map +1 -1
- package/dist/entity/entity-base.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -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/collection-strategy.factory.d.ts.map +1 -1
- package/dist/query/collection-strategy.factory.js +7 -3
- package/dist/query/collection-strategy.factory.js.map +1 -1
- package/dist/query/collection-strategy.interface.d.ts +12 -6
- package/dist/query/collection-strategy.interface.d.ts.map +1 -1
- package/dist/query/conditions.d.ts +178 -24
- package/dist/query/conditions.d.ts.map +1 -1
- package/dist/query/conditions.js +165 -4
- package/dist/query/conditions.js.map +1 -1
- package/dist/query/cte-builder.d.ts +21 -5
- package/dist/query/cte-builder.d.ts.map +1 -1
- package/dist/query/cte-builder.js +31 -7
- package/dist/query/cte-builder.js.map +1 -1
- package/dist/query/grouped-query.d.ts +185 -8
- package/dist/query/grouped-query.d.ts.map +1 -1
- package/dist/query/grouped-query.js +516 -30
- package/dist/query/grouped-query.js.map +1 -1
- package/dist/query/join-builder.d.ts +5 -4
- package/dist/query/join-builder.d.ts.map +1 -1
- package/dist/query/join-builder.js +11 -33
- package/dist/query/join-builder.js.map +1 -1
- package/dist/query/query-builder.d.ts +89 -20
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +317 -168
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/query-utils.d.ts +45 -0
- package/dist/query/query-utils.d.ts.map +1 -0
- package/dist/query/query-utils.js +103 -0
- package/dist/query/query-utils.js.map +1 -0
- package/dist/query/sql-utils.d.ts +83 -0
- package/dist/query/sql-utils.d.ts.map +1 -0
- package/dist/query/sql-utils.js +218 -0
- package/dist/query/sql-utils.js.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts +85 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.js +338 -0
- package/dist/query/strategies/cte-collection-strategy.js.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts +59 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.js +243 -0
- package/dist/query/strategies/lateral-collection-strategy.js.map +1 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts +21 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts.map +1 -1
- package/dist/query/strategies/temptable-collection-strategy.js +216 -94
- package/dist/query/strategies/temptable-collection-strategy.js.map +1 -1
- package/dist/query/subquery.d.ts +24 -1
- package/dist/query/subquery.d.ts.map +1 -1
- package/dist/query/subquery.js +38 -2
- package/dist/query/subquery.js.map +1 -1
- package/package.json +1 -1
- package/dist/query/strategies/jsonb-collection-strategy.d.ts +0 -51
- package/dist/query/strategies/jsonb-collection-strategy.d.ts.map +0 -1
- package/dist/query/strategies/jsonb-collection-strategy.js +0 -210
- package/dist/query/strategies/jsonb-collection-strategy.js.map +0 -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
|
|
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)** -
|
|
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, LATERAL joins, 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 CTE, LATERAL, or temp table strategies
|
|
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)** - CTE, LATERAL, and temp table strategies for one-to-many queries
|
|
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.
|
|
@@ -22,10 +22,47 @@ export declare class DbColumn<TValue> {
|
|
|
22
22
|
*/
|
|
23
23
|
valueOf(): TValue;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Type helper to detect if a type is a class instance (has prototype methods)
|
|
27
|
+
* vs a plain data object. See conditions.ts for detailed explanation.
|
|
28
|
+
* Excludes DbColumn and SqlFragment which have valueOf but are not value types.
|
|
29
|
+
*/
|
|
30
|
+
type IsClassInstance<T> = T extends {
|
|
31
|
+
__isDbColumn: true;
|
|
32
|
+
} ? false : T extends {
|
|
33
|
+
mapWith: any;
|
|
34
|
+
as: any;
|
|
35
|
+
buildSql: any;
|
|
36
|
+
} ? false : T extends {
|
|
37
|
+
valueOf(): infer V;
|
|
38
|
+
} ? V extends T ? true : V extends number | string | boolean | bigint | symbol ? true : false : false;
|
|
39
|
+
/**
|
|
40
|
+
* Check for types with known class method signatures
|
|
41
|
+
*/
|
|
42
|
+
type HasClassMethods<T> = T extends {
|
|
43
|
+
getTime(): number;
|
|
44
|
+
} ? true : T extends {
|
|
45
|
+
size: number;
|
|
46
|
+
has(value: any): boolean;
|
|
47
|
+
} ? true : T extends {
|
|
48
|
+
byteLength: number;
|
|
49
|
+
} ? true : T extends {
|
|
50
|
+
then(onfulfilled?: any): any;
|
|
51
|
+
} ? true : T extends {
|
|
52
|
+
message: string;
|
|
53
|
+
name: string;
|
|
54
|
+
} ? true : T extends {
|
|
55
|
+
exec(string: string): any;
|
|
56
|
+
} ? true : false;
|
|
57
|
+
/**
|
|
58
|
+
* Combined check for value types that should not be recursively processed
|
|
59
|
+
*/
|
|
60
|
+
type IsValueType<T> = IsClassInstance<T> extends true ? true : HasClassMethods<T> extends true ? true : false;
|
|
25
61
|
/**
|
|
26
62
|
* Type helper to unwrap DbColumn types to their underlying values
|
|
63
|
+
* Preserves class instances (Date, Map, Set, Temporal, etc.) as-is
|
|
27
64
|
*/
|
|
28
|
-
export type UnwrapDbColumns<T> = T extends DbColumn<infer V> ? V : T extends object ? {
|
|
65
|
+
export type UnwrapDbColumns<T> = T extends DbColumn<infer V> ? V : T extends object ? IsValueType<T> extends true ? T : {
|
|
29
66
|
[K in keyof T]: T[K] extends DbColumn<infer V> ? V : T[K] extends (infer U)[] | undefined ? U extends DbEntity ? UnwrapDbColumns<U>[] : T[K] : T[K] extends DbEntity | undefined ? UnwrapDbColumns<NonNullable<T[K]>> : T[K];
|
|
30
67
|
} : T;
|
|
31
68
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db-column.d.ts","sourceRoot":"","sources":["../../src/entity/db-column.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,QAAQ,CAAC,MAAM;IAC1B,gBAAgB;IAChB,QAAQ,CAAC,YAAY,QAAQ;IAE7B,gBAAgB;IAChB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAE7B,gBAAgB;IAChB,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAEhC,gBAAgB;IAChB,QAAQ,CAAC,WAAW,EAAG,MAAM,CAAC;;IAG5B,gBAAgB;IAChB,UAAU,EAAE,MAAM;IAMpB;;;OAGG;IACH,OAAO,IAAI,MAAM;CAGlB;AAED;;GAEG;AACH,MA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AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,QAAQ,CAAC,GAAG,CAAC,CAE7D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db-column.js","sourceRoot":"","sources":["../../src/entity/db-column.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"db-column.js","sourceRoot":"","sources":["../../src/entity/db-column.ts"],"names":[],"mappings":";;;AA8IA,gCAEC;AAhJD;;;;;GAKG;AACH,MAAa,QAAQ;IAanB;IACE,gBAAgB;IAChB,UAAkB;QAdpB,gBAAgB;QACP,iBAAY,GAAG,IAAI,CAAC;QAe3B,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC;IACnC,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;IAC1E,CAAC;CACF;AA5BD,4BA4BC;AAyGD;;GAEG;AACH,SAAgB,UAAU,CAAC,KAAU;IACnC,OAAO,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,YAAY,KAAK,IAAI,CAAC;AAC3E,CAAC"}
|