Gathering detailed insights and metrics for @medyll/idae-db
Gathering detailed insights and metrics for @medyll/idae-db
Gathering detailed insights and metrics for @medyll/idae-db
Gathering detailed insights and metrics for @medyll/idae-db
npm install @medyll/idae-db
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
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.
To install the library, run:
1npm install idae-db
First, initialize the database connection:
1import { IdaeDb } from './lib/idaeDb.js'; 2import { DbType } from './lib/@types/types.js'; 3 4const mongoDb = IdaeDb.init('mongodb://localhost:27017', { 5 dbType: DbType.MONGODB, 6 dbScope: 'a_idae_db_sitebase', 7 dbScopeSeparator: '_', 8 idaeModelOptions: { 9 autoIncrementFormat: (collection: string) => `id${collection}`, 10 autoIncrementDbCollection: 'auto_increment' 11 } 12});
1import { IdaeDb } from './lib/idaeDb.js'; 2import { DbType } from './lib/@types/types.js'; 3 4const mysqlDb = IdaeDb.init('mysql://user:password@localhost:3306', { 5 dbType: DbType.MYSQL, 6 dbScope: 'a_idae_db_sitebase', 7 dbScopeSeparator: '_', 8 idaeModelOptions: { 9 autoIncrementFormat: (collection: string) => `id${collection}`, 10 autoIncrementDbCollection: 'auto_increment' 11 } 12});
Create a connection to the database:
1await mongoDb.db('app'); 2await mysqlDb.db('app');
Define a model interface:
1interface User { 2 iduser: number; 3 name: string; 4 email: string; 5 age: number; 6}
Register event listeners for various operations:
1const usersCollection = mongoDb.collection<User>('user'); 2 3usersCollection.registerEvents<any>({ 4 findById: { 5 pre: (id) => console.log(`About to find document with id: ${id}`), 6 post: (result, id) => console.log(`Found document for id ${id}:`, result), 7 error: (error) => console.error('Error in findById:', error) 8 }, 9 update: { 10 pre: (id, data) => console.log(`About to update document ${id} with:`, data), 11 post: (result, id, data) => console.log(`Updated document ${id}:`, result) 12 }, 13 create: { 14 pre: (data) => console.log(`About to create document with:`, data), 15 post: (data, result) => console.log(`Created document `, data, result) 16 } 17});
Perform CRUD operations:
1// Create a new user 2const newUser = await usersCollection.create({ 3 name: 'John Doe', 4 email: 'john@example.com', 5 age: 30 6}); 7 8// Find a user by email 9const user = await usersCollection.findOne({ query: { email: 'john@example.com' } }); 10 11// Update a user's age 12const updateResult = await usersCollection.update(user.iduser, { age: 31 }); 13 14// Find users with age greater than 25 15const users = await usersCollection.find({ 16 query: { age: { $gt: 25 } }, 17 sortBy: 'name:asc', 18 limit: 10 19}); 20 21// Delete a user by ID 22const deleteResult = await usersCollection.deleteById(5);
1const usersCollection = mysqlDb.collection<User>('user'); 2 3// Create a new user 4const newUser = await usersCollection.create({ 5 name: 'John Doe', 6 email: 'john@example.com', 7 age: 30 8}); 9 10// Find a user by email 11const user = await usersCollection.findOne({ query: { email: 'john@example.com' } }); 12 13// Update a user's age 14const updateResult = await usersCollection.update(user.iduser, { age: 31 }); 15 16// Find users with age greater than 25 17const users = await usersCollection.find({ 18 query: { age: { $gt: 25 } }, 19 sortBy: 'name:asc', 20 limit: 10 21}); 22 23// Delete a user by ID 24const deleteResult = await usersCollection.deleteById(5);
Close all connections when done:
1await mongoDb.closeAllConnections(); 2await mysqlDb.closeAllConnections();
This project is licensed under the MIT License.
No vulnerabilities found.
No security vulnerabilities found.