@medyll/idae-db 0.86.0 → 0.88.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 +169 -169
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
# Idae Database Library
|
|
2
|
-
|
|
3
|
-
The Idae Database Library provides a flexible and extensible way to interact with various databases such as MongoDB, MySQL. It includes features like event emitters for pre/post hooks, auto-increment fields, and more.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
To install the library, run:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install idae-db
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
### Initialization
|
|
16
|
-
|
|
17
|
-
First, initialize the database connection:
|
|
18
|
-
|
|
19
|
-
#### MongoDB
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
import { IdaeDb } from './lib/idaeDb.js';
|
|
23
|
-
import { DbType } from './lib/@types/types.js';
|
|
24
|
-
|
|
25
|
-
const mongoDb = IdaeDb.init('mongodb://localhost:27017', {
|
|
26
|
-
dbType: DbType.MONGODB,
|
|
27
|
-
dbScope: 'a_idae_db_sitebase',
|
|
28
|
-
dbScopeSeparator: '_',
|
|
29
|
-
idaeModelOptions: {
|
|
30
|
-
autoIncrementFormat: (collection: string) => `id${collection}`,
|
|
31
|
-
autoIncrementDbCollection: 'auto_increment'
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
#### MySQL
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
import { IdaeDb } from './lib/idaeDb.js';
|
|
40
|
-
import { DbType } from './lib/@types/types.js';
|
|
41
|
-
|
|
42
|
-
const mysqlDb = IdaeDb.init('mysql://user:password@localhost:3306', {
|
|
43
|
-
dbType: DbType.MYSQL,
|
|
44
|
-
dbScope: 'a_idae_db_sitebase',
|
|
45
|
-
dbScopeSeparator: '_',
|
|
46
|
-
idaeModelOptions: {
|
|
47
|
-
autoIncrementFormat: (collection: string) => `id${collection}`,
|
|
48
|
-
autoIncrementDbCollection: 'auto_increment'
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### Create a Connection
|
|
54
|
-
|
|
55
|
-
Create a connection to the database:
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
await mongoDb.db('app');
|
|
59
|
-
await mysqlDb.db('app');
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Define a Model
|
|
63
|
-
|
|
64
|
-
Define a model interface:
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
interface User {
|
|
68
|
-
iduser: number;
|
|
69
|
-
name: string;
|
|
70
|
-
email: string;
|
|
71
|
-
age: number;
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Register Event Listeners
|
|
76
|
-
|
|
77
|
-
Register event listeners for various operations:
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
const usersCollection = mongoDb.collection<User>('user');
|
|
81
|
-
|
|
82
|
-
usersCollection.registerEvents<any>({
|
|
83
|
-
findById: {
|
|
84
|
-
pre: (id) => console.log(`About to find document with id: ${id}`),
|
|
85
|
-
post: (result, id) => console.log(`Found document for id ${id}:`, result),
|
|
86
|
-
error: (error) => console.error('Error in findById:', error)
|
|
87
|
-
},
|
|
88
|
-
update: {
|
|
89
|
-
pre: (id, data) => console.log(`About to update document ${id} with:`, data),
|
|
90
|
-
post: (result, id, data) => console.log(`Updated document ${id}:`, result)
|
|
91
|
-
},
|
|
92
|
-
create: {
|
|
93
|
-
pre: (data) => console.log(`About to create document with:`, data),
|
|
94
|
-
post: (data, result) => console.log(`Created document `, data, result)
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### CRUD Operations
|
|
100
|
-
|
|
101
|
-
Perform CRUD operations:
|
|
102
|
-
|
|
103
|
-
#### MongoDB
|
|
104
|
-
|
|
105
|
-
```typescript
|
|
106
|
-
// Create a new user
|
|
107
|
-
const newUser = await usersCollection.create({
|
|
108
|
-
name: 'John Doe',
|
|
109
|
-
email: 'john@example.com',
|
|
110
|
-
age: 30
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// Find a user by email
|
|
114
|
-
const user = await usersCollection.findOne({ query: { email: 'john@example.com' } });
|
|
115
|
-
|
|
116
|
-
// Update a user's age
|
|
117
|
-
const updateResult = await usersCollection.update(user.iduser, { age: 31 });
|
|
118
|
-
|
|
119
|
-
// Find users with age greater than 25
|
|
120
|
-
const users = await usersCollection.find({
|
|
121
|
-
query: { age: { $gt: 25 } },
|
|
122
|
-
sortBy: 'name:asc',
|
|
123
|
-
limit: 10
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// Delete a user by ID
|
|
127
|
-
const deleteResult = await usersCollection.deleteById(5);
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
#### MySQL
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
const usersCollection = mysqlDb.collection<User>('user');
|
|
134
|
-
|
|
135
|
-
// Create a new user
|
|
136
|
-
const newUser = await usersCollection.create({
|
|
137
|
-
name: 'John Doe',
|
|
138
|
-
email: 'john@example.com',
|
|
139
|
-
age: 30
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// Find a user by email
|
|
143
|
-
const user = await usersCollection.findOne({ query: { email: 'john@example.com' } });
|
|
144
|
-
|
|
145
|
-
// Update a user's age
|
|
146
|
-
const updateResult = await usersCollection.update(user.iduser, { age: 31 });
|
|
147
|
-
|
|
148
|
-
// Find users with age greater than 25
|
|
149
|
-
const users = await usersCollection.find({
|
|
150
|
-
query: { age: { $gt: 25 } },
|
|
151
|
-
sortBy: 'name:asc',
|
|
152
|
-
limit: 10
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// Delete a user by ID
|
|
156
|
-
const deleteResult = await usersCollection.deleteById(5);
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### Close Connections
|
|
160
|
-
|
|
161
|
-
Close all connections when done:
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
await mongoDb.closeAllConnections();
|
|
165
|
-
await mysqlDb.closeAllConnections();
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
## License
|
|
169
|
-
|
|
1
|
+
# Idae Database Library
|
|
2
|
+
|
|
3
|
+
The Idae Database Library provides a flexible and extensible way to interact with various databases such as MongoDB, MySQL. It includes features like event emitters for pre/post hooks, auto-increment fields, and more.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install the library, run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install idae-db
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Initialization
|
|
16
|
+
|
|
17
|
+
First, initialize the database connection:
|
|
18
|
+
|
|
19
|
+
#### MongoDB
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { IdaeDb } from './lib/idaeDb.js';
|
|
23
|
+
import { DbType } from './lib/@types/types.js';
|
|
24
|
+
|
|
25
|
+
const mongoDb = IdaeDb.init('mongodb://localhost:27017', {
|
|
26
|
+
dbType: DbType.MONGODB,
|
|
27
|
+
dbScope: 'a_idae_db_sitebase',
|
|
28
|
+
dbScopeSeparator: '_',
|
|
29
|
+
idaeModelOptions: {
|
|
30
|
+
autoIncrementFormat: (collection: string) => `id${collection}`,
|
|
31
|
+
autoIncrementDbCollection: 'auto_increment'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
#### MySQL
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { IdaeDb } from './lib/idaeDb.js';
|
|
40
|
+
import { DbType } from './lib/@types/types.js';
|
|
41
|
+
|
|
42
|
+
const mysqlDb = IdaeDb.init('mysql://user:password@localhost:3306', {
|
|
43
|
+
dbType: DbType.MYSQL,
|
|
44
|
+
dbScope: 'a_idae_db_sitebase',
|
|
45
|
+
dbScopeSeparator: '_',
|
|
46
|
+
idaeModelOptions: {
|
|
47
|
+
autoIncrementFormat: (collection: string) => `id${collection}`,
|
|
48
|
+
autoIncrementDbCollection: 'auto_increment'
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Create a Connection
|
|
54
|
+
|
|
55
|
+
Create a connection to the database:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
await mongoDb.db('app');
|
|
59
|
+
await mysqlDb.db('app');
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Define a Model
|
|
63
|
+
|
|
64
|
+
Define a model interface:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
interface User {
|
|
68
|
+
iduser: number;
|
|
69
|
+
name: string;
|
|
70
|
+
email: string;
|
|
71
|
+
age: number;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Register Event Listeners
|
|
76
|
+
|
|
77
|
+
Register event listeners for various operations:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const usersCollection = mongoDb.collection<User>('user');
|
|
81
|
+
|
|
82
|
+
usersCollection.registerEvents<any>({
|
|
83
|
+
findById: {
|
|
84
|
+
pre: (id) => console.log(`About to find document with id: ${id}`),
|
|
85
|
+
post: (result, id) => console.log(`Found document for id ${id}:`, result),
|
|
86
|
+
error: (error) => console.error('Error in findById:', error)
|
|
87
|
+
},
|
|
88
|
+
update: {
|
|
89
|
+
pre: (id, data) => console.log(`About to update document ${id} with:`, data),
|
|
90
|
+
post: (result, id, data) => console.log(`Updated document ${id}:`, result)
|
|
91
|
+
},
|
|
92
|
+
create: {
|
|
93
|
+
pre: (data) => console.log(`About to create document with:`, data),
|
|
94
|
+
post: (data, result) => console.log(`Created document `, data, result)
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### CRUD Operations
|
|
100
|
+
|
|
101
|
+
Perform CRUD operations:
|
|
102
|
+
|
|
103
|
+
#### MongoDB
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Create a new user
|
|
107
|
+
const newUser = await usersCollection.create({
|
|
108
|
+
name: 'John Doe',
|
|
109
|
+
email: 'john@example.com',
|
|
110
|
+
age: 30
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Find a user by email
|
|
114
|
+
const user = await usersCollection.findOne({ query: { email: 'john@example.com' } });
|
|
115
|
+
|
|
116
|
+
// Update a user's age
|
|
117
|
+
const updateResult = await usersCollection.update(user.iduser, { age: 31 });
|
|
118
|
+
|
|
119
|
+
// Find users with age greater than 25
|
|
120
|
+
const users = await usersCollection.find({
|
|
121
|
+
query: { age: { $gt: 25 } },
|
|
122
|
+
sortBy: 'name:asc',
|
|
123
|
+
limit: 10
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Delete a user by ID
|
|
127
|
+
const deleteResult = await usersCollection.deleteById(5);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### MySQL
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
const usersCollection = mysqlDb.collection<User>('user');
|
|
134
|
+
|
|
135
|
+
// Create a new user
|
|
136
|
+
const newUser = await usersCollection.create({
|
|
137
|
+
name: 'John Doe',
|
|
138
|
+
email: 'john@example.com',
|
|
139
|
+
age: 30
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Find a user by email
|
|
143
|
+
const user = await usersCollection.findOne({ query: { email: 'john@example.com' } });
|
|
144
|
+
|
|
145
|
+
// Update a user's age
|
|
146
|
+
const updateResult = await usersCollection.update(user.iduser, { age: 31 });
|
|
147
|
+
|
|
148
|
+
// Find users with age greater than 25
|
|
149
|
+
const users = await usersCollection.find({
|
|
150
|
+
query: { age: { $gt: 25 } },
|
|
151
|
+
sortBy: 'name:asc',
|
|
152
|
+
limit: 10
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Delete a user by ID
|
|
156
|
+
const deleteResult = await usersCollection.deleteById(5);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Close Connections
|
|
160
|
+
|
|
161
|
+
Close all connections when done:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
await mongoDb.closeAllConnections();
|
|
165
|
+
await mysqlDb.closeAllConnections();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
170
|
This project is licensed under the MIT License.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from './idaeDb.js';
|
|
2
1
|
export * from './IdaeEventEmitter.js';
|
|
2
|
+
export * from './IdaeDBModel.js';
|
|
3
3
|
export * from './IdaeDbConnection.js';
|
|
4
4
|
export * from './IdaeDbAdapter.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './idaeDb.js';
|
|
6
6
|
export * from './adapters/MySQLAdapter.js';
|
|
7
7
|
export * from './adapters/MongoDBAdapter.js';
|
|
8
8
|
export * from './adapters/ChromaDBAdapter.js';
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// auto exports of entry components
|
|
2
|
-
export * from './idaeDb.js';
|
|
3
2
|
export * from './IdaeEventEmitter.js';
|
|
3
|
+
export * from './IdaeDBModel.js';
|
|
4
4
|
export * from './IdaeDbConnection.js';
|
|
5
5
|
export * from './IdaeDbAdapter.js';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './idaeDb.js';
|
|
7
7
|
export * from './adapters/MySQLAdapter.js';
|
|
8
8
|
export * from './adapters/MongoDBAdapter.js';
|
|
9
9
|
export * from './adapters/ChromaDBAdapter.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.88.0",
|
|
4
4
|
"description": "@medyll/idae-db is a flexible and powerful library for interacting with various databases, with a particular focus on MongoDB support. It offers robust connection management, an intuitive API, and simplified CRUD operations.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "vite dev",
|