@kos-ng-it-konsult/database
This is a database connection package for KOS NG IT Konsult.
Quick Start
Install
$ npm i --save @kos-ng-it-konsult/database
Basic Usage
"use strict";
const { Schema } = require('mongoose');
const lib = require('@kos-ng-it-konsult/database');
const db = new lib(console);
const mongodb_config = {
host: 'mongodb://127.0.0.1',
port: 27017,
name: 'db_test',
uri: 'mongodb://127.0.0.1:27017/db_test',
pool: 10,
};
let MongoDB = null;
async function connect () {
try {
MongoDB = await db.use('mongodb', mongodb_config).connect();
} catch (error) {
console.log(error);
}
}
const userSchema = new Schema({
username: { type: String, required: true, unique: true },
first_name: { type: String, required: true },
last_name: { type: String, required: true }
});
connect()
.then(() => {
const User = MongoDB.model('user', userSchema);
const user = new User({ username: 'oluwaseun', first_name: 'Olusi', last_name: 'Oluwaseun' });
User.create(user, function (err, doc) {
if (err) {
return console.error(err);
}
console.log(doc); // Outputs { _id: ..., username: 'oluwaseun', first_name: 'Olusi', last_name: 'Oluwaseun', __v: 0 }
process.exit(0);
});
});
Running the Example
To run the example, simply run the command:
$ node ./example/sample.js
Running Tests
Run
$ npm test