Database tiers with Node.jsNoSQL



Resources
Database tiers (a.k.a. back-end software) ⤳ MongoDB

MongoDB Atlas is a cloud-based service to get access and usage of MongoDB databases without any on-site installation. The MongoDB Shell is a Command Line Interface -CLI- tool for database rapid setup, test, and management.

mongosh "mongodb+srv://FB:<password>@web-scraping.98xi5.mongodb.net/"
Atlas Web-scraping-shard-0 [primary] test> db
test
Atlas Web-scraping-shard-0 [primary] test> use Web_scraping
switched to db Web_scraping
Atlas Web-scraping-shard-0 [primary] Web_scraping> db.my_data.insertOne({forname: "Franck", surname: "Barbier"});
{
  acknowledged: true,
  insertedId: ObjectId("649e9ff943abecf90e76aec3")
}
Atlas Web-scraping-shard-0 [primary] Web_scraping> show databases
New_York_City_Penitentiary_database   52.00 KiB
Web_scraping                          24.00 KiB
…
admin                                336.00 KiB
local                                  4.55 GiB
Atlas Web-scraping-shard-0 [primary] Web_scraping> db.my_data.drop();
true
Node.js MongoDB driver

Application Programming Interface -API-

import * as MongoDB from "mongodb"; // Node.JS MongoDB driver...
// Database within MongoDB Atlas:
const Database_URI = "mongodb+srv://FB:<password>@web-scraping.98xi5.mongodb.net/";

const Client = new MongoDB.MongoClient(Database_URI, {
        serverApi: {
            version: MongoDB.ServerApiVersion.v1,
            strict: true,
            deprecationErrors: true,
        }
    }
);

async function Connect() {
    try {
        // Connect client to database:
        await Client.connect();
        // @ts-ignore ('s' field isn't accessible, but:
        console.log("'Client.s.url': " + Client.s.url);
        // Send ping to confirm successful connection:
        await Client.db("admin").command({ ping: 1 });
        console.log("Pinged... Successfully connected to MongoDB...");
    } finally {
        // Ensures that client closes:
        await Client.close();
    }
}
Connect().catch(console.dir);

Application Programming Interface -API- cont'd

async function Connect_() {
    try {
        await Client.connect();
        const Web_scraping = Client.db("Web_scraping");
        const result = await Web_scraping.admin().listDatabases();
        console.log("Databases: " + JSON.stringify(result.databases));
        const my_data = Web_scraping.collection("data");
        my_data.insertOne({forname: "Franck", surname: "Barbier"}).then(() => {
            my_data.countDocuments({}).then(value => {
                console.log("\n'my_data.countDocuments({})': " + value); // '1'
                my_data.drop().then(() => {
                    Client.close().then(() => {
                        console.log("\nSee you later!");
                    });
                });
            });
        });
    } catch (error: unknown) {
        console.log(`Problem? ${error}`);
        await Client.close();
    }
}

Connect_();
Mongoose

Mongoose here… is a library for MongoDB. Mongoose promotes disciplined data modeling (Schema) in the spirit of SQL (primary keys, foreign keys and constraints on data in general).

Introduction data modeling (Schema) in Mongoose

import * as assert from 'assert';
import * as mongoose from "mongoose";

const currencies_schema: mongoose.Schema = new mongoose.Schema({ // USA dollar:
    common_name: String, // Dollar
    common_symbol: {type: String}, // $
    description: {type: String, default: ""},
    iso_code: {type: Number}, // 840
    iso_symbol: String, // USD
    material_icon: {type: String, default: ""},
    substitute: {type: mongoose.Schema.Types.Mixed},
    substitution_date: {type: Date, default: Date.now}
});
// mongoose automatically looks for the plural, lowercase version of the model name...
// So, 'currencies' collection is created within MongoDB:
const currencies_model = mongoose.model('currencies', currencies_schema);
const Dollar = new currencies_model({
    common_name: "Dollar",
    common_symbol: "$",
    description: "First leading currency in the world...",
    iso_code: 840,
    iso_symbol: "USD",
    material_icon: "attach_money",
    substitute: null
});
console.assert(Dollar.get('common_symbol') === "$"); // Common style...
const {message} = new assert.AssertionError({
    actual: Dollar.get('common_symbol'),
    expected: "$",
    operator: "strictEqual"
});
try {
    assert.strictEqual(Dollar.get('common_symbol'), "$");
} catch (error: unknown) {
    assert(error instanceof assert.AssertionError);
    assert.strictEqual(error.message, message);
    assert.strictEqual(error.name, 'AssertionError');
    assert.strictEqual(error.actual, Dollar.get('common_symbol'));
    assert.strictEqual(error.expected, "$");
    assert.strictEqual(error.code, 'ERR_ASSERTION');
    assert.strictEqual(error.operator, 'strictEqual');
    assert.strictEqual(error.generatedMessage, true);
}

// Database within MongoDB Atlas:
const Database_URI = "mongodb+srv://FB:<password>@web-scraping-98xi5.mongodb.net/Web_scraping?retryWrites=true&w=majority";
mongoose.connect(Database_URI).then((value: mongoose.Mongoose) => {
    console.log("Connected database: '" + (value.connections.length > 0 ? value.connections[0].name : "?")
        + "' on '" + (value.connections.length > 0 ? value.connections[0].host : "?") + "'");
});
const connection: mongoose.Connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', () => { // Remove existing data (for test):
    currencies_model.deleteMany({common_symbol: "$"});
    Dollar.save().then(() => { // Then, check if present:
        currencies_model.find({common_symbol: "$"}).exec().then(currency => {
                console.log(currency);
                connection.close().then(() => {
                    console.log("\nSee you later!");
                });
            }
        );
    });
});

Exercise