duckdb-tinyorm 1.0.43 → 2.0.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/README.md +387 -46
- package/dist/constants/data-type.decorator.d.ts +20 -1
- package/dist/constants/data-type.decorator.js +62 -14
- package/dist/constants/data-type.decorator.js.map +1 -1
- package/dist/errors/orm-errors.d.ts +27 -0
- package/dist/errors/orm-errors.js +80 -0
- package/dist/errors/orm-errors.js.map +1 -0
- package/dist/helpers/bulk-insert.helper.d.ts +1 -0
- package/dist/helpers/bulk-insert.helper.js +19 -0
- package/dist/helpers/bulk-insert.helper.js.map +1 -0
- package/dist/helpers/db.helper.d.ts +10 -0
- package/dist/helpers/db.helper.js +62 -0
- package/dist/helpers/db.helper.js.map +1 -1
- package/dist/helpers/mapping.helper.d.ts +1 -1
- package/dist/helpers/mapping.helper.js +35 -35
- package/dist/helpers/mapping.helper.js.map +1 -1
- package/dist/helpers/table-util.helper.js +46 -19
- package/dist/helpers/table-util.helper.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.js +24 -1
- package/dist/index.js.map +1 -1
- package/dist/migration/migration.d.ts +22 -0
- package/dist/migration/migration.js +143 -0
- package/dist/migration/migration.js.map +1 -0
- package/dist/pagination/pagination.d.ts +10 -0
- package/dist/pagination/pagination.js +3 -0
- package/dist/pagination/pagination.js.map +1 -0
- package/dist/query/query-builder.d.ts +27 -0
- package/dist/query/query-builder.js +113 -0
- package/dist/query/query-builder.js.map +1 -0
- package/dist/repositories/base.interface.d.ts +19 -2
- package/dist/repositories/base.repository.d.ts +18 -2
- package/dist/repositories/base.repository.js +159 -14
- package/dist/repositories/base.repository.js.map +1 -1
- package/dist/repositories/duckdb.repository.d.ts +18 -0
- package/dist/repositories/duckdb.repository.js +91 -16
- package/dist/repositories/duckdb.repository.js.map +1 -1
- package/dist/repositories/transaction.d.ts +9 -0
- package/dist/repositories/transaction.js +48 -0
- package/dist/repositories/transaction.js.map +1 -0
- package/dist/test.d.ts +4 -3
- package/dist/test.js +126 -28
- package/dist/test.js.map +1 -1
- package/package.json +3 -2
- package/tsconfig.json +9 -1
package/README.md
CHANGED
|
@@ -1,79 +1,420 @@
|
|
|
1
|
-
# DuckDB
|
|
1
|
+
# 🦆 DuckDB TinyORM
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight, TypeScript-friendly ORM designed specifically for DuckDB, focusing on simplicity and ease of use.
|
|
4
|
+
|
|
5
|
+
## 📋 Table of Contents
|
|
6
|
+
|
|
7
|
+
- [🦆 DuckDB TinyORM](#-duckdb-tinyorm)
|
|
8
|
+
- [📋 Table of Contents](#-table-of-contents)
|
|
9
|
+
- [📥 Installation](#-installation)
|
|
10
|
+
- [🚀 Quick Start](#-quick-start)
|
|
11
|
+
- [✨ Core Features](#-core-features)
|
|
12
|
+
- [🏗️ Entity Definition](#️-entity-definition)
|
|
13
|
+
- [🔧 Column Options](#-column-options)
|
|
14
|
+
- [📚 Repository Pattern](#-repository-pattern)
|
|
15
|
+
- [💾 Data Operations](#-data-operations)
|
|
16
|
+
- [✏️ Create](#️-create)
|
|
17
|
+
- [🔍 Read](#-read)
|
|
18
|
+
- [🔄 Update](#-update)
|
|
19
|
+
- [🗑️ Delete](#️-delete)
|
|
20
|
+
- [🔧 Query Builder](#-query-builder)
|
|
21
|
+
- [🔒 Transactions](#-transactions)
|
|
22
|
+
- [📊 Data Export](#-data-export)
|
|
23
|
+
- [🧠 Advanced Usage](#-advanced-usage)
|
|
24
|
+
- [🛠️ Custom Repositories](#️-custom-repositories)
|
|
25
|
+
- [⚙️ DuckDB Database Configuration](#️-duckdb-database-configuration)
|
|
26
|
+
- [📘 API Reference](#-api-reference)
|
|
27
|
+
- [🏷️ Decorators](#️-decorators)
|
|
28
|
+
- [🔙 Legacy Decorators (backward compatibility)](#-legacy-decorators-backward-compatibility)
|
|
29
|
+
- [🧰 Repository Methods](#-repository-methods)
|
|
30
|
+
- [📜 License](#-license)
|
|
31
|
+
|
|
32
|
+
## 📥 Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install duckdb-tinyorm
|
|
36
|
+
# or
|
|
37
|
+
yarn add duckdb-tinyorm
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
DuckDB TinyORM requires `reflect-metadata` for decorator support:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm install reflect-metadata
|
|
44
|
+
# or
|
|
45
|
+
yarn add reflect-metadata
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Make sure to import `reflect-metadata` at the beginning of your application:
|
|
4
49
|
|
|
5
50
|
```typescript
|
|
6
51
|
import 'reflect-metadata';
|
|
7
|
-
|
|
52
|
+
```
|
|
8
53
|
|
|
54
|
+
## 🚀 Quick Start
|
|
9
55
|
|
|
56
|
+
```typescript
|
|
57
|
+
import 'reflect-metadata';
|
|
58
|
+
import {
|
|
59
|
+
DuckDbRepository,
|
|
60
|
+
Entity,
|
|
61
|
+
Column,
|
|
62
|
+
Repository,
|
|
63
|
+
BaseRepository,
|
|
64
|
+
DuckDbLocation
|
|
65
|
+
} from 'duckdb-tinyorm';
|
|
10
66
|
|
|
11
|
-
//
|
|
12
|
-
const duckDbRepository
|
|
67
|
+
// Initialize DuckDB repository (in-memory)
|
|
68
|
+
const duckDbRepository = DuckDbRepository.getInstances({
|
|
69
|
+
name: 'default',
|
|
70
|
+
location: DuckDbLocation.Memory
|
|
71
|
+
});
|
|
13
72
|
|
|
14
|
-
|
|
15
|
-
|
|
73
|
+
// Define your entity
|
|
74
|
+
@Entity({ name: 'products' })
|
|
75
|
+
class Product {
|
|
76
|
+
@Column({
|
|
77
|
+
type: 'INTEGER',
|
|
78
|
+
primaryKey: true,
|
|
79
|
+
autoIncrement: true
|
|
80
|
+
})
|
|
81
|
+
Id!: number;
|
|
82
|
+
|
|
83
|
+
@Column({
|
|
84
|
+
type: 'VARCHAR',
|
|
85
|
+
notNull: true
|
|
86
|
+
})
|
|
87
|
+
Name!: string;
|
|
88
|
+
|
|
89
|
+
@Column({
|
|
90
|
+
type: 'DOUBLE',
|
|
91
|
+
notNull: true
|
|
92
|
+
})
|
|
93
|
+
Price!: number;
|
|
94
|
+
|
|
95
|
+
constructor(name: string = "", price: number = 0) {
|
|
96
|
+
this.Name = name;
|
|
97
|
+
this.Price = price;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Create a repository for your entity
|
|
102
|
+
@Repository(Product)
|
|
103
|
+
class ProductRepository extends BaseRepository<Product, number> {
|
|
104
|
+
constructor() {
|
|
105
|
+
super(duckDbRepository);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
16
108
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
109
|
+
// Use the repository
|
|
110
|
+
async function main() {
|
|
111
|
+
const productRepo = new ProductRepository();
|
|
112
|
+
await productRepo.init(); // Initialize the repository (creates the table)
|
|
113
|
+
|
|
114
|
+
// Create and save a new product
|
|
115
|
+
const product = new Product("Laptop", 999.99);
|
|
116
|
+
const savedProduct = await productRepo.save(product);
|
|
117
|
+
console.log(`Created product with ID: ${savedProduct.Id}`);
|
|
118
|
+
|
|
119
|
+
// Query products
|
|
120
|
+
const allProducts = await productRepo.findAll();
|
|
121
|
+
console.table(allProducts);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
main();
|
|
125
|
+
```
|
|
23
126
|
|
|
24
|
-
|
|
25
|
-
@DataTypeDecorator('VARCHAR')
|
|
26
|
-
Id: string ;
|
|
127
|
+
## ✨ Core Features
|
|
27
128
|
|
|
28
|
-
|
|
29
|
-
|
|
129
|
+
- 🚀 **Modern TypeScript Support**: Full TypeScript integration with decorators
|
|
130
|
+
- 🗃️ **Entity-Relationship Mapping**: Map your classes to DuckDB tables
|
|
131
|
+
- 📚 **Repository Pattern**: Type-safe data access operations
|
|
132
|
+
- 📝 **SQL Generation**: Automatic SQL query generation
|
|
133
|
+
- 🔒 **Transaction Support**: ACID-compliant transaction handling
|
|
134
|
+
- 🔍 **Query Builder**: Fluent API for building complex queries
|
|
135
|
+
- 📊 **Data Export**: Export data to CSV, JSON, and Parquet formats
|
|
136
|
+
- 📄 **Pagination**: Built-in support for paginating large datasets
|
|
30
137
|
|
|
138
|
+
## 🏗️ Entity Definition
|
|
31
139
|
|
|
32
|
-
|
|
33
|
-
Description?: string;
|
|
140
|
+
Entities represent tables in your database. Use decorators to define your entity schema:
|
|
34
141
|
|
|
142
|
+
```typescript
|
|
143
|
+
@Entity({ name: 'subjects' }) // Optional table name (defaults to class name)
|
|
144
|
+
export class Subject {
|
|
145
|
+
@Column({
|
|
146
|
+
type: 'INTEGER',
|
|
147
|
+
primaryKey: true,
|
|
148
|
+
autoIncrement: true
|
|
149
|
+
})
|
|
150
|
+
Id!: number;
|
|
35
151
|
|
|
36
|
-
|
|
37
|
-
|
|
152
|
+
@Column({
|
|
153
|
+
type: 'VARCHAR',
|
|
154
|
+
notNull: true,
|
|
155
|
+
unique: true
|
|
156
|
+
})
|
|
157
|
+
Code!: string;
|
|
38
158
|
|
|
159
|
+
@Column({
|
|
160
|
+
type: 'VARCHAR',
|
|
161
|
+
notNull: true
|
|
162
|
+
})
|
|
163
|
+
Name!: string;
|
|
164
|
+
|
|
165
|
+
@Column({
|
|
166
|
+
type: 'TEXT'
|
|
167
|
+
})
|
|
168
|
+
Description?: string;
|
|
169
|
+
|
|
170
|
+
@Column({
|
|
171
|
+
type: 'INT',
|
|
172
|
+
defaultValue: new Date().getFullYear(),
|
|
173
|
+
check: 'Year >= 2000'
|
|
174
|
+
})
|
|
175
|
+
Year!: number;
|
|
176
|
+
|
|
177
|
+
constructor(code: string = "", name: string = "", description?: string, year: number = new Date().getFullYear()) {
|
|
178
|
+
this.Code = code;
|
|
179
|
+
this.Name = name;
|
|
180
|
+
this.Description = description;
|
|
181
|
+
this.Year = year;
|
|
182
|
+
}
|
|
39
183
|
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### 🔧 Column Options
|
|
40
187
|
|
|
188
|
+
- 📊 `type`: SQL data type ('INTEGER', 'VARCHAR', 'DOUBLE', etc.)
|
|
189
|
+
- 🔑 `primaryKey`: Defines a primary key column
|
|
190
|
+
- 🔢 `autoIncrement`: Auto-increments the column value
|
|
191
|
+
- ⚠️ `notNull`: Adds NOT NULL constraint
|
|
192
|
+
- 🎯 `unique`: Adds UNIQUE constraint
|
|
193
|
+
- 🏷️ `defaultValue`: Sets a default value
|
|
194
|
+
- ✅ `check`: Adds a CHECK constraint
|
|
195
|
+
|
|
196
|
+
## 📚 Repository Pattern
|
|
197
|
+
|
|
198
|
+
Repositories provide data access operations for entities:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
41
201
|
@Repository(Subject)
|
|
42
|
-
class SubjectRepository extends BaseRepository<Subject,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
202
|
+
class SubjectRepository extends BaseRepository<Subject, number> {
|
|
203
|
+
constructor() {
|
|
204
|
+
super(duckDbRepository);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Add custom methods specific to this entity
|
|
208
|
+
async findByCode(code: string): Promise<Subject | null> {
|
|
209
|
+
const query = `SELECT * FROM main.${this.tableName} WHERE Code='${code}'`;
|
|
210
|
+
const result = await this.repository.executeQuery(query);
|
|
211
|
+
return result.length > 0 ? result[0] : null;
|
|
212
|
+
}
|
|
46
213
|
}
|
|
214
|
+
```
|
|
47
215
|
|
|
216
|
+
## 💾 Data Operations
|
|
48
217
|
|
|
49
|
-
|
|
50
|
-
const subjectRepository = new SubjectRepository();
|
|
218
|
+
### ✏️ Create
|
|
51
219
|
|
|
52
|
-
|
|
53
|
-
|
|
220
|
+
```typescript
|
|
221
|
+
const subject = new Subject('CS101', 'Computer Science', 'Introduction to CS', 2024);
|
|
222
|
+
const savedSubject = await subjectRepository.save(subject);
|
|
223
|
+
console.log(`Created with ID: ${savedSubject.Id}`);
|
|
224
|
+
|
|
225
|
+
// Bulk insert
|
|
226
|
+
const subjects = [
|
|
227
|
+
new Subject('CS102', 'Data Structures', 'Advanced data structures', 2024),
|
|
228
|
+
new Subject('CS103', 'Algorithms', 'Algorithm design', 2024)
|
|
229
|
+
];
|
|
230
|
+
await subjectRepository.saveAll(subjects);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 🔍 Read
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
// Find by ID
|
|
237
|
+
const subject = await subjectRepository.findById(1);
|
|
54
238
|
|
|
239
|
+
// Find all
|
|
240
|
+
const allSubjects = await subjectRepository.findAll();
|
|
55
241
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
await subjectRepository.save(subject2);
|
|
242
|
+
// Find with criteria
|
|
243
|
+
const subjects2024 = await subjectRepository.findBy({ Year: 2024 }, ["Year"]);
|
|
59
244
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
245
|
+
// Pagination
|
|
246
|
+
const page = await subjectRepository.findWithPagination({ page: 0, size: 10 });
|
|
247
|
+
console.log(`Found ${page.totalElements} subjects across ${page.totalPages} pages`);
|
|
248
|
+
```
|
|
63
249
|
|
|
64
|
-
|
|
65
|
-
const subjectFound1: Subject = await subjectRepository.findById("JB");
|
|
66
|
-
console.info(subjectFound1);
|
|
67
|
-
const subjectFound2: Subject = await subjectRepository.findById("OOP");
|
|
68
|
-
console.info(subjectFound2);
|
|
250
|
+
### 🔄 Update
|
|
69
251
|
|
|
70
|
-
|
|
252
|
+
```typescript
|
|
253
|
+
subject.Description = "Updated description";
|
|
254
|
+
await subjectRepository.save(subject);
|
|
255
|
+
```
|
|
71
256
|
|
|
72
|
-
|
|
257
|
+
### 🗑️ Delete
|
|
73
258
|
|
|
74
|
-
|
|
75
|
-
|
|
259
|
+
```typescript
|
|
260
|
+
// Delete by ID
|
|
261
|
+
await subjectRepository.removeById(1);
|
|
262
|
+
|
|
263
|
+
// Custom delete method
|
|
264
|
+
await subjectRepository.removeByCode("CS101");
|
|
265
|
+
|
|
266
|
+
// Delete all
|
|
267
|
+
await subjectRepository.removeAll();
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## 🔧 Query Builder
|
|
271
|
+
|
|
272
|
+
Build complex queries with the fluent query builder API:
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
const queryBuilder = await subjectRepository.createQueryBuilder();
|
|
276
|
+
const query = queryBuilder
|
|
277
|
+
.select(['Id', 'Name', 'Year'])
|
|
278
|
+
.where('Year = 2024')
|
|
279
|
+
.andWhere('Name LIKE \'%Science%\'')
|
|
280
|
+
.orderBy('Name', 'ASC')
|
|
281
|
+
.limit(5)
|
|
282
|
+
.offset(10)
|
|
283
|
+
.getQuery();
|
|
284
|
+
|
|
285
|
+
const results = await duckDbRepository.executeQuery(query);
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## 🔒 Transactions
|
|
289
|
+
|
|
290
|
+
Handle multiple operations in a transaction:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
await subjectRepository.withTransaction(async (transaction) => {
|
|
294
|
+
const subject1 = new Subject('MATH101', 'Mathematics', 'Basic math', 2024);
|
|
295
|
+
const subject2 = new Subject('PHYS101', 'Physics', 'Basic physics', 2024);
|
|
296
|
+
|
|
297
|
+
await subjectRepository.save(subject1);
|
|
298
|
+
await subjectRepository.save(subject2);
|
|
299
|
+
|
|
300
|
+
// If any operation throws an error, the transaction will roll back
|
|
301
|
+
// Otherwise, it will commit automatically
|
|
302
|
+
});
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## 📊 Data Export
|
|
306
|
+
|
|
307
|
+
Export your data to various formats:
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
// Export table to CSV
|
|
311
|
+
await subjectRepository.exportData({
|
|
312
|
+
format: 'csv',
|
|
313
|
+
fileName: 'subjects.csv',
|
|
314
|
+
csvOptions: {
|
|
315
|
+
header: true,
|
|
316
|
+
delimiter: ','
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Export query results to JSON
|
|
321
|
+
const query = `SELECT * FROM main.subjects WHERE Year = 2024`;
|
|
322
|
+
await subjectRepository.exportQuery(query, {
|
|
323
|
+
format: 'json',
|
|
324
|
+
fileName: 'subjects-2024.json',
|
|
325
|
+
jsonOptions: {
|
|
326
|
+
pretty: true
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Export table to Parquet
|
|
331
|
+
await duckDbRepository.exportTable('subjects', {
|
|
332
|
+
format: 'parquet',
|
|
333
|
+
fileName: 'subjects.parquet',
|
|
334
|
+
parquetOptions: {
|
|
335
|
+
compression: 'ZSTD'
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## 🧠 Advanced Usage
|
|
341
|
+
|
|
342
|
+
### 🛠️ Custom Repositories
|
|
343
|
+
|
|
344
|
+
Extend the base repository with custom methods:
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
@Repository(Subject)
|
|
348
|
+
class SubjectRepository extends BaseRepository<Subject, number> {
|
|
349
|
+
constructor() {
|
|
350
|
+
super(duckDbRepository);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async findByCodeAndYear(code: string, year: number): Promise<Subject | null> {
|
|
354
|
+
const query = `SELECT * FROM main.${this.tableName} WHERE Code='${code}' AND Year=${year}`;
|
|
355
|
+
const result = await this.repository.executeQuery(query);
|
|
356
|
+
return result.length > 0 ? result[0] : null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async findActive(): Promise<Subject[]> {
|
|
360
|
+
const currentYear = new Date().getFullYear();
|
|
361
|
+
return this.findBy({ Year: currentYear }, ["Year"]);
|
|
362
|
+
}
|
|
76
363
|
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### ⚙️ DuckDB Database Configuration
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
// In-memory database
|
|
370
|
+
const inMemoryDb = DuckDbRepository.getInstances({
|
|
371
|
+
name: 'default',
|
|
372
|
+
location: DuckDbLocation.Memory
|
|
373
|
+
});
|
|
77
374
|
|
|
78
|
-
|
|
375
|
+
// File-based database
|
|
376
|
+
const fileDb = DuckDbRepository.getInstances({
|
|
377
|
+
name: 'production',
|
|
378
|
+
location: DuckDbLocation.File,
|
|
379
|
+
filename: './data/mydb.db'
|
|
380
|
+
});
|
|
79
381
|
```
|
|
382
|
+
|
|
383
|
+
## 📘 API Reference
|
|
384
|
+
|
|
385
|
+
### 🏷️ Decorators
|
|
386
|
+
|
|
387
|
+
- 🏢 `@Entity(options?)`: Defines a class as an entity
|
|
388
|
+
- 📝 `@Column(options)`: Defines a property as a column
|
|
389
|
+
- 🗂️ `@Repository(entityClass)`: Defines a repository for an entity
|
|
390
|
+
|
|
391
|
+
### 🔙 Legacy Decorators (backward compatibility)
|
|
392
|
+
|
|
393
|
+
- 📋 `@DataTypeDecorator(type)`: Defines a column type `@Column()`
|
|
394
|
+
- 🔑 `@Id()`: Defines a primary key
|
|
395
|
+
- 🎯 `@Unique()`: Adds a unique constraint
|
|
396
|
+
- ⚠️ `@NotNull()`: Adds a NOT NULL constraint
|
|
397
|
+
- 🏷️ `@Default(value)`: Sets a default value
|
|
398
|
+
- ✅ `@Check(constraint)`: Adds a CHECK constraint
|
|
399
|
+
- 🔢 `@AutoIncrement()`: Sets column as auto-increment
|
|
400
|
+
|
|
401
|
+
### 🧰 Repository Methods
|
|
402
|
+
|
|
403
|
+
- 🚀 `init()`: Initializes the repository and creates the table
|
|
404
|
+
- 💾 `save(entity)`: Saves an entity (insert or update)
|
|
405
|
+
- 📦 `saveAll(entities)`: Saves multiple entities
|
|
406
|
+
- 📋 `findAll()`: Retrieves all entities
|
|
407
|
+
- 🔍 `findById(id)`: Retrieves an entity by ID
|
|
408
|
+
- ⚡ `findByIdOrThrow(id)`: Retrieves an entity by ID or throws an error
|
|
409
|
+
- 🔎 `findBy(criteria, columns)`: Retrieves entities matching criteria
|
|
410
|
+
- 📄 `findWithPagination(pageable)`: Retrieves entities with pagination
|
|
411
|
+
- 🗑️ `removeById(id)`: Deletes an entity by ID
|
|
412
|
+
- 🧹 `removeAll()`: Deletes all entities
|
|
413
|
+
- 🔧 `createQueryBuilder()`: Creates a query builder
|
|
414
|
+
- 🔄 `withTransaction(callback)`: Executes operations within a transaction
|
|
415
|
+
- 📊 `exportData(options)`: Exports table data
|
|
416
|
+
- 📈 `exportQuery(query, options)`: Exports query results
|
|
417
|
+
|
|
418
|
+
## 📜 License
|
|
419
|
+
|
|
420
|
+
MIT
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
+
export interface ColumnOptions {
|
|
3
|
+
type?: string;
|
|
4
|
+
notNull?: boolean;
|
|
5
|
+
unique?: boolean;
|
|
6
|
+
defaultValue?: any;
|
|
7
|
+
check?: string;
|
|
8
|
+
primaryKey?: boolean;
|
|
9
|
+
autoIncrement?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function Column(options?: ColumnOptions): PropertyDecorator;
|
|
2
12
|
export declare function DataTypeDecorator(dataType: string): PropertyDecorator;
|
|
3
13
|
export declare function Id(): PropertyDecorator;
|
|
4
14
|
export declare function Unique(): PropertyDecorator;
|
|
5
|
-
export declare function
|
|
15
|
+
export declare function NotNull(): PropertyDecorator;
|
|
16
|
+
export declare function Default(value: any): PropertyDecorator;
|
|
17
|
+
export declare function Check(constraint: string): PropertyDecorator;
|
|
18
|
+
export declare function AutoIncrement(): PropertyDecorator;
|
|
19
|
+
export interface TableOptions {
|
|
20
|
+
name?: string;
|
|
21
|
+
schema?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function Entity(): ClassDecorator;
|
|
24
|
+
export declare function Entity(options: TableOptions): ClassDecorator;
|
|
6
25
|
export declare function Repository(entity: new () => any): (target: Function) => void;
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Column = Column;
|
|
3
4
|
exports.DataTypeDecorator = DataTypeDecorator;
|
|
4
5
|
exports.Id = Id;
|
|
5
6
|
exports.Unique = Unique;
|
|
7
|
+
exports.NotNull = NotNull;
|
|
8
|
+
exports.Default = Default;
|
|
9
|
+
exports.Check = Check;
|
|
10
|
+
exports.AutoIncrement = AutoIncrement;
|
|
6
11
|
exports.Entity = Entity;
|
|
7
12
|
exports.Repository = Repository;
|
|
8
13
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
14
|
require("reflect-metadata");
|
|
10
|
-
|
|
15
|
+
// Column decorator with options
|
|
16
|
+
function Column(options = {}) {
|
|
11
17
|
return function (target, propertyKey) {
|
|
12
18
|
let propertyValue;
|
|
13
19
|
const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
@@ -20,7 +26,7 @@ function DataTypeDecorator(dataType) {
|
|
|
20
26
|
Object.defineProperty(target, propertyKey, descriptor);
|
|
21
27
|
}
|
|
22
28
|
else {
|
|
23
|
-
// If the property doesn't exist, create it
|
|
29
|
+
// If the property doesn't exist, create it
|
|
24
30
|
Object.defineProperty(target, propertyKey, {
|
|
25
31
|
value: propertyValue,
|
|
26
32
|
writable: true,
|
|
@@ -28,25 +34,67 @@ function DataTypeDecorator(dataType) {
|
|
|
28
34
|
configurable: true,
|
|
29
35
|
});
|
|
30
36
|
}
|
|
31
|
-
// Set
|
|
32
|
-
|
|
37
|
+
// Set all options as metadata
|
|
38
|
+
if (options.type) {
|
|
39
|
+
Reflect.defineMetadata('FieldType', options.type, target, propertyKey);
|
|
40
|
+
}
|
|
41
|
+
if (options.primaryKey) {
|
|
42
|
+
Reflect.defineMetadata('PrimaryKey', true, target, propertyKey);
|
|
43
|
+
}
|
|
44
|
+
if (options.unique) {
|
|
45
|
+
Reflect.defineMetadata('Unique', true, target, propertyKey);
|
|
46
|
+
}
|
|
47
|
+
if (options.notNull) {
|
|
48
|
+
Reflect.defineMetadata('NotNull', true, target, propertyKey);
|
|
49
|
+
}
|
|
50
|
+
if (options.defaultValue !== undefined) {
|
|
51
|
+
Reflect.defineMetadata('DefaultValue', options.defaultValue, target, propertyKey);
|
|
52
|
+
}
|
|
53
|
+
if (options.check) {
|
|
54
|
+
Reflect.defineMetadata('Check', options.check, target, propertyKey);
|
|
55
|
+
}
|
|
56
|
+
if (options.autoIncrement) {
|
|
57
|
+
Reflect.defineMetadata('AutoIncrement', true, target, propertyKey);
|
|
58
|
+
}
|
|
33
59
|
};
|
|
34
60
|
}
|
|
61
|
+
// Legacy decorators for backward compatibility
|
|
62
|
+
function DataTypeDecorator(dataType) {
|
|
63
|
+
return Column({ type: dataType });
|
|
64
|
+
}
|
|
35
65
|
function Id() {
|
|
36
|
-
return
|
|
37
|
-
Reflect.defineMetadata('PrimaryKey', true, target, propertyKey);
|
|
38
|
-
};
|
|
66
|
+
return Column({ primaryKey: true });
|
|
39
67
|
}
|
|
40
68
|
function Unique() {
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
|
|
69
|
+
return Column({ unique: true });
|
|
70
|
+
}
|
|
71
|
+
function NotNull() {
|
|
72
|
+
return Column({ notNull: true });
|
|
73
|
+
}
|
|
74
|
+
function Default(value) {
|
|
75
|
+
return Column({ defaultValue: value });
|
|
76
|
+
}
|
|
77
|
+
function Check(constraint) {
|
|
78
|
+
return Column({ check: constraint });
|
|
44
79
|
}
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
|
|
80
|
+
function AutoIncrement() {
|
|
81
|
+
return Column({ autoIncrement: true });
|
|
82
|
+
}
|
|
83
|
+
function Entity(options) {
|
|
84
|
+
return function (target) {
|
|
85
|
+
// Add metadata to the class
|
|
86
|
+
Reflect.defineMetadata('entityType', target, target);
|
|
87
|
+
// Add table name if provided
|
|
88
|
+
if (options?.name) {
|
|
89
|
+
Reflect.defineMetadata('TableName', options.name, target);
|
|
90
|
+
}
|
|
91
|
+
// Add schema if provided
|
|
92
|
+
if (options?.schema) {
|
|
93
|
+
Reflect.defineMetadata('Schema', options.schema, target);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
48
96
|
}
|
|
49
|
-
//
|
|
97
|
+
// Repository decorator
|
|
50
98
|
function Repository(entity) {
|
|
51
99
|
return function (target) {
|
|
52
100
|
Reflect.defineMetadata('entityType', entity, target);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-type.decorator.js","sourceRoot":"","sources":["../../src/constants/data-type.decorator.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"data-type.decorator.js","sourceRoot":"","sources":["../../src/constants/data-type.decorator.ts"],"names":[],"mappings":";;AAeA,wBA6CC;AAGD,8CAEC;AAED,gBAEC;AAED,wBAEC;AAED,0BAEC;AAED,0BAEC;AAED,sBAEC;AAED,sCAEC;AAUD,wBAeC;AAGD,gCAIC;AAzHD,uDAAuD;AACvD,4BAA0B;AAa1B,gCAAgC;AAChC,SAAgB,MAAM,CAAC,UAAyB,EAAE;IAC9C,OAAO,UAAU,MAAW,EAAE,WAA4B;QACtD,IAAI,aAAkB,CAAC;QACvB,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,WAAqB,CAAC,CAAC;QAElF,IAAI,UAAU,EAAE,CAAC;YACb,2DAA2D;YAC3D,UAAU,CAAC,KAAK,GAAG,aAAa,CAAC;YACjC,UAAU,CAAC,QAAQ,GAAG,IAAI,CAAC;YAC3B,UAAU,CAAC,UAAU,GAAG,IAAI,CAAC;YAC7B,UAAU,CAAC,YAAY,GAAG,IAAI,CAAC;YAC/B,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;QAC3D,CAAC;aAAM,CAAC;YACJ,2CAA2C;YAC3C,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;gBACvC,KAAK,EAAE,aAAa;gBACpB,QAAQ,EAAE,IAAI;gBACd,UAAU,EAAE,IAAI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C,CAAC;IACzD,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class OrmBaseError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class ConnectionError extends OrmBaseError {
|
|
5
|
+
constructor(entityName: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class EntityNotFoundError extends OrmBaseError {
|
|
8
|
+
constructor(entityName: string, id: any);
|
|
9
|
+
}
|
|
10
|
+
export declare class PrimaryKeyError extends OrmBaseError {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class TableCreationError extends OrmBaseError {
|
|
14
|
+
constructor(tableName: string, originalError?: Error);
|
|
15
|
+
}
|
|
16
|
+
export declare class QueryExecutionError extends OrmBaseError {
|
|
17
|
+
constructor(query: string, originalError?: Error);
|
|
18
|
+
}
|
|
19
|
+
export declare class TransactionError extends OrmBaseError {
|
|
20
|
+
constructor(operation: string, originalError?: Error);
|
|
21
|
+
}
|
|
22
|
+
export declare class ValidationError extends OrmBaseError {
|
|
23
|
+
constructor(entityName: string, propertyName: string, value: any, constraint: string);
|
|
24
|
+
}
|
|
25
|
+
export declare class MigrationError extends OrmBaseError {
|
|
26
|
+
constructor(version: string, operation: string, originalError?: Error);
|
|
27
|
+
}
|