1.0.3 • Published 2 years ago

@txsa/mongodb v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@txsa/mongodb

This package provides a convenient way to interact with MongoDB using Mongoose.

Installation

Use the package manager npm to install @txsa/mongodb.

npm install @txsa/mongodb

Usage

First, import the necessary classes and types from the package.

import { MongooseAccess, TDbSchemaDefinition } from '@txsa/mongodb';

Define your own type and schema that extends Document.

interface User {
  name: string;
  email: string;
  password: string;
}

const userSchema: TDbSchemaDefinition<User> = {
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
};

Instantiate MongooseAccess with your model name and schema.

const UserAccess = new MongooseAccess<User>('User', userSchema);

Now, you can use UserAccess to interact with your MongoDB collection.

// Connect to MongoDB
await MongooseAccess.connect('mongodb://localhost:27017/myDatabase');

// Add a new user
await UserAccess.add({
  name: 'John Doe',
  email: 'john.doe@example.com',
  password: 'secret',
});

// Query users
await UserAccess.query({ name: 'John Doe' });

// Update a user
await UserAccess.updateById('60d5ecb8b48738259f43620e', { name: 'Jane Doe' });

// Delete a user
await UserAccess.deleteById('60d5ecb8b48738259f43620e');

Note: Please replace 'mongodb://localhost:27017/myDatabase' with your actual MongoDB connection string, and ensure to replace the user details in the add, updateById and deleteById methods with your actual data.

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago