@syntropix/database 0.0.6 → 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/README.md +80 -80
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
# DB TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript SDK for database operations, similar to a Python ORM.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
yarn add reflect-metadata
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Features
|
|
12
12
|
|
|
13
|
-
- 🎯
|
|
14
|
-
- 🔑
|
|
15
|
-
- 🔗
|
|
16
|
-
- 📝
|
|
17
|
-
- 🔍
|
|
18
|
-
- 🚀
|
|
19
|
-
- 🎨
|
|
13
|
+
- 🎯 **Decorator Support** - Define models using TypeScript decorators
|
|
14
|
+
- 🔑 **Automatic Primary Keys** - `id` field is automatically created if not defined
|
|
15
|
+
- 🔗 **Foreign Key Relations** - Supports defining and handling foreign key relations
|
|
16
|
+
- 📝 **Full CRUD** - Create, read, update, and delete operations
|
|
17
|
+
- 🔍 **Advanced Queries** - Filtering, sorting, aggregation, joins, etc.
|
|
18
|
+
- 🚀 **Batch Operations** - Bulk create and update support
|
|
19
|
+
- 🎨 **Type Safety** - Full TypeScript types, type-safe models
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Quick Start
|
|
22
22
|
|
|
23
|
-
### 1.
|
|
23
|
+
### 1. Define Models
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
26
|
import { BaseModel, Column } from 'db-ts-sdk';
|
|
27
27
|
|
|
28
|
-
//
|
|
28
|
+
// Audited base model
|
|
29
29
|
export class Audited extends BaseModel {
|
|
30
30
|
@Column({ type: 'date', name: 'created_at' })
|
|
31
31
|
createdAt!: Date;
|
|
@@ -34,7 +34,7 @@ export class Audited extends BaseModel {
|
|
|
34
34
|
updatedAt!: Date;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
//
|
|
37
|
+
// User model
|
|
38
38
|
export class User extends Audited {
|
|
39
39
|
static tableName = 'users';
|
|
40
40
|
|
|
@@ -55,28 +55,28 @@ export class User extends Audited {
|
|
|
55
55
|
}
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
### 2.
|
|
58
|
+
### 2. Table Operations
|
|
59
59
|
|
|
60
60
|
```typescript
|
|
61
|
-
//
|
|
61
|
+
// Create table
|
|
62
62
|
await User.createTable();
|
|
63
63
|
|
|
64
|
-
//
|
|
64
|
+
// Drop table
|
|
65
65
|
await User.dropTable();
|
|
66
66
|
|
|
67
|
-
//
|
|
67
|
+
// Rename table
|
|
68
68
|
await User.renameTable('new_users');
|
|
69
69
|
|
|
70
|
-
//
|
|
70
|
+
// Truncate table
|
|
71
71
|
await User.truncateTable();
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
### 3.
|
|
74
|
+
### 3. Data Operations
|
|
75
75
|
|
|
76
|
-
####
|
|
76
|
+
#### Create Data
|
|
77
77
|
|
|
78
78
|
```typescript
|
|
79
|
-
//
|
|
79
|
+
// Single insert
|
|
80
80
|
const user = await User.create({
|
|
81
81
|
id: '1',
|
|
82
82
|
email: 'user@example.com',
|
|
@@ -86,7 +86,7 @@ const user = await User.create({
|
|
|
86
86
|
updatedAt: new Date(),
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
//
|
|
89
|
+
// Bulk insert
|
|
90
90
|
await User.bulkCreate(
|
|
91
91
|
[
|
|
92
92
|
{
|
|
@@ -106,19 +106,19 @@ await User.bulkCreate(
|
|
|
106
106
|
updatedAt: new Date(),
|
|
107
107
|
},
|
|
108
108
|
],
|
|
109
|
-
32,
|
|
110
|
-
);
|
|
109
|
+
32, // Batch size
|
|
110
|
+
);
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
-
####
|
|
113
|
+
#### Query Data
|
|
114
114
|
|
|
115
115
|
```typescript
|
|
116
116
|
import { AND, EQ, OR, GTE } from 'db-ts-sdk';
|
|
117
117
|
|
|
118
|
-
//
|
|
118
|
+
// Get a single record
|
|
119
119
|
const user = await User.get(OR(AND(EQ('email', 'user@example.com'))));
|
|
120
120
|
|
|
121
|
-
//
|
|
121
|
+
// Filter query
|
|
122
122
|
const activeUsers = await User.filter({
|
|
123
123
|
filter: OR(AND(EQ('is_active', true))),
|
|
124
124
|
limit: 10,
|
|
@@ -126,38 +126,38 @@ const activeUsers = await User.filter({
|
|
|
126
126
|
sort: { column: 'created_at', order: 'DESC' },
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
-
//
|
|
129
|
+
// Count records
|
|
130
130
|
const count = await User.count({
|
|
131
131
|
filter: OR(AND(EQ('is_active', true))),
|
|
132
132
|
});
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
####
|
|
135
|
+
#### Update Data
|
|
136
136
|
|
|
137
137
|
```typescript
|
|
138
|
-
//
|
|
138
|
+
// Instance update
|
|
139
139
|
const user = await User.get(OR(AND(EQ('id', '1'))));
|
|
140
140
|
user.fullName = 'Jane Doe';
|
|
141
141
|
await user.save();
|
|
142
142
|
|
|
143
|
-
//
|
|
143
|
+
// Bulk update
|
|
144
144
|
await User.update(OR(AND(EQ('is_active', false))), { isActive: true });
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
-
####
|
|
147
|
+
#### Delete Data
|
|
148
148
|
|
|
149
149
|
```typescript
|
|
150
|
-
//
|
|
150
|
+
// Instance delete
|
|
151
151
|
const user = await User.get(OR(AND(EQ('id', '1'))));
|
|
152
152
|
await user.remove();
|
|
153
153
|
|
|
154
|
-
//
|
|
154
|
+
// Bulk delete
|
|
155
155
|
await User.delete(OR(AND(EQ('is_active', false))));
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
-
##
|
|
158
|
+
## Field Types
|
|
159
159
|
|
|
160
|
-
SDK
|
|
160
|
+
The SDK supports various field types:
|
|
161
161
|
|
|
162
162
|
```typescript
|
|
163
163
|
import {
|
|
@@ -180,22 +180,22 @@ import {
|
|
|
180
180
|
} from 'db-ts-sdk';
|
|
181
181
|
```
|
|
182
182
|
|
|
183
|
-
###
|
|
183
|
+
### Using Decorators
|
|
184
184
|
|
|
185
185
|
```typescript
|
|
186
186
|
@Column({
|
|
187
|
-
type: 'string', //
|
|
188
|
-
name: 'column_name', //
|
|
189
|
-
description: 'desc', //
|
|
190
|
-
primary: false, //
|
|
191
|
-
nullable: false, //
|
|
192
|
-
auto_increment: false,
|
|
193
|
-
default: null //
|
|
187
|
+
type: 'string', // Field type
|
|
188
|
+
name: 'column_name', // Database column name (optional)
|
|
189
|
+
description: 'desc', // Description (optional)
|
|
190
|
+
primary: false, // Is primary key (optional)
|
|
191
|
+
nullable: false, // Nullable (optional)
|
|
192
|
+
auto_increment: false, // Auto increment (optional)
|
|
193
|
+
default: null // Default value (optional)
|
|
194
194
|
})
|
|
195
195
|
fieldName!: string;
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
###
|
|
198
|
+
### Foreign Key Fields
|
|
199
199
|
|
|
200
200
|
```typescript
|
|
201
201
|
import { ForeignKey } from 'db-ts-sdk';
|
|
@@ -208,42 +208,42 @@ import { ForeignKey } from 'db-ts-sdk';
|
|
|
208
208
|
userId!: number;
|
|
209
209
|
```
|
|
210
210
|
|
|
211
|
-
##
|
|
211
|
+
## Filter Builder
|
|
212
212
|
|
|
213
213
|
```typescript
|
|
214
214
|
import { AND, OR, EQ, NE, GT, GTE, LT, LTE, IN, LIKE } from 'db-ts-sdk';
|
|
215
215
|
|
|
216
|
-
//
|
|
216
|
+
// Equals
|
|
217
217
|
EQ('field', value);
|
|
218
218
|
|
|
219
|
-
//
|
|
219
|
+
// Not equals
|
|
220
220
|
NE('field', value);
|
|
221
221
|
|
|
222
|
-
//
|
|
222
|
+
// Greater than
|
|
223
223
|
GT('field', value);
|
|
224
224
|
|
|
225
|
-
//
|
|
225
|
+
// Greater than or equal
|
|
226
226
|
GTE('field', value);
|
|
227
227
|
|
|
228
|
-
//
|
|
228
|
+
// Less than
|
|
229
229
|
LT('field', value);
|
|
230
230
|
|
|
231
|
-
//
|
|
231
|
+
// Less than or equal
|
|
232
232
|
LTE('field', value);
|
|
233
233
|
|
|
234
|
-
//
|
|
234
|
+
// In array
|
|
235
235
|
IN('field', [value1, value2]);
|
|
236
236
|
|
|
237
|
-
//
|
|
237
|
+
// Pattern matching (LIKE)
|
|
238
238
|
LIKE('field', '%pattern%');
|
|
239
239
|
|
|
240
|
-
//
|
|
240
|
+
// Combine conditions
|
|
241
241
|
OR(AND(EQ('status', 'active'), GTE('age', 18)), AND(EQ('status', 'pending')));
|
|
242
242
|
```
|
|
243
243
|
|
|
244
|
-
##
|
|
244
|
+
## Advanced Queries
|
|
245
245
|
|
|
246
|
-
###
|
|
246
|
+
### Aggregate Query
|
|
247
247
|
|
|
248
248
|
```typescript
|
|
249
249
|
import { AggregateFunction } from 'db-ts-sdk';
|
|
@@ -261,7 +261,7 @@ const results = await User.filter({
|
|
|
261
261
|
});
|
|
262
262
|
```
|
|
263
263
|
|
|
264
|
-
###
|
|
264
|
+
### Join Query
|
|
265
265
|
|
|
266
266
|
```typescript
|
|
267
267
|
const results = await User.filter({
|
|
@@ -274,9 +274,9 @@ const results = await User.filter({
|
|
|
274
274
|
});
|
|
275
275
|
```
|
|
276
276
|
|
|
277
|
-
##
|
|
277
|
+
## Configuration
|
|
278
278
|
|
|
279
|
-
|
|
279
|
+
In your `tsconfig.json`, make sure to enable decorator support:
|
|
280
280
|
|
|
281
281
|
```json
|
|
282
282
|
{
|
|
@@ -287,34 +287,34 @@ const results = await User.filter({
|
|
|
287
287
|
}
|
|
288
288
|
```
|
|
289
289
|
|
|
290
|
-
|
|
290
|
+
And at the entrypoint of your app, import reflect-metadata:
|
|
291
291
|
|
|
292
292
|
```typescript
|
|
293
293
|
import 'reflect-metadata';
|
|
294
294
|
```
|
|
295
295
|
|
|
296
|
-
## API
|
|
296
|
+
## API Reference
|
|
297
297
|
|
|
298
|
-
### BaseModel
|
|
298
|
+
### BaseModel Static Methods
|
|
299
299
|
|
|
300
|
-
- `createTable()` -
|
|
301
|
-
- `dropTable()` -
|
|
302
|
-
- `renameTable(newName)` -
|
|
303
|
-
- `truncateTable()` -
|
|
304
|
-
- `create(data)` -
|
|
305
|
-
- `bulkCreate(datas, batchSize)` -
|
|
306
|
-
- `get(filter, select?)` -
|
|
307
|
-
- `filter(options)` -
|
|
308
|
-
- `update(filter, data)` -
|
|
309
|
-
- `delete(filter)` -
|
|
310
|
-
- `count(options)` -
|
|
300
|
+
- `createTable()` - Create table
|
|
301
|
+
- `dropTable()` - Drop table
|
|
302
|
+
- `renameTable(newName)` - Rename table
|
|
303
|
+
- `truncateTable()` - Truncate table
|
|
304
|
+
- `create(data)` - Create a record
|
|
305
|
+
- `bulkCreate(datas, batchSize)` - Create records in bulk
|
|
306
|
+
- `get(filter, select?)` - Get a single record
|
|
307
|
+
- `filter(options)` - Filter query
|
|
308
|
+
- `update(filter, data)` - Bulk update
|
|
309
|
+
- `delete(filter)` - Bulk delete
|
|
310
|
+
- `count(options)` - Count records
|
|
311
311
|
|
|
312
|
-
### BaseModel
|
|
312
|
+
### BaseModel Instance Methods
|
|
313
313
|
|
|
314
|
-
- `save()` -
|
|
315
|
-
- `remove(filter?)` -
|
|
316
|
-
- `toString()` -
|
|
317
|
-
- `toJSON()` -
|
|
314
|
+
- `save()` - Save instance (create or update)
|
|
315
|
+
- `remove(filter?)` - Remove instance
|
|
316
|
+
- `toString()` - Convert to string
|
|
317
|
+
- `toJSON()` - Convert to JSON object
|
|
318
318
|
|
|
319
319
|
## License
|
|
320
320
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syntropix/database",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TypeScript SDK for database operations with ORM support",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"packageManager": "yarn@4.
|
|
8
|
+
"packageManager": "yarn@4.11.0",
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public",
|
|
11
11
|
"registry": "https://registry.npmjs.org/"
|