1.0.1 • Published 6 years ago

@sasm/db v1.0.1

Weekly downloads
2
License
SEE LICENSE IN LI...
Repository
-
Last release
6 years ago

SasmDB

MongoDB driver for SASM

NPM Version Node Version MongoDB Version Sasm Version SasmDB Version

Install

SASM DB is a private extension package under the @sasm organization on NPM. You must be an authorized user of the @sasm organization to install SasmDB Version.

npm login

Once you have logged into NPM as an authorized @sasm user, you may install SasmDB Version.

npm i @sasm/db

Usage

Please see Sasm Version for information on how to use Sasm and extensions.

MongoDB Version is required in order to use SasmDB Version. Configuring and setting up a database is beyond the scope of this documentation. Please see the documentation on MongoDB Version for more information on setting up and running MongoDB.

Requiring the SasmDB Version extension will use the configuration from Sasm Version to make a database connection. By default, there is no authentication specified in the database connection configuration.

No configuration is necessary if you start you MongoDB server with this command:

mongod --dbpath data

However, it is recommended you use the --auth argument (or some other form of authentiaction) for security purposes.

Configuration

Configuring the connection string is possible through Sasm Version configuration. Recall from the Sasm System documentation that the default configuration looks like this:

{
	"connection": {
		"hostname": "localhost",
		"ip4": "127.0.0.1",
		"ip6": null,
		"port": 8080
	},
	"database": {
		"username": null,
		"password": null,
		"address": [
			{
				"host": "localhost",
				"port": 27017
			}
		],
		"name": "sasm",
		"options": {}
	}
}

If you left the default confuration as-is, which is recommended, you can make changes to this confuration before running sasm.start().

Here's an example of a working database connection to a MongoDB Version service with a database named "sasm" and a user on the sasm database named Admin with the password changeMe.

mongod command:

mongod --dbpath data --auth

server.js

const sasm = require('@sasm/system');

sasm.prep.config.database.username = "Admin";
sasm.prep.config.database.password = "changeMe";

sasm.
	start().
	registerExtension("@sasm/db").
	listen();

Before the above server will work correctly, you must set up a mongo user.

Note: The @sasm/db driver will not try to connect until a query is attempted, so you will not see a connection or authentication error even if the confuration is wrong or if the database is not started/installed. It is necessary to test your connection by running a query.

Setting up the Mongo User

Though beyond the scope of this documentation, we'll go over setting up a basic user using the Mongo Shell.

First, start the database by opening a terminal and executing this command:

mongod --dbpath data

In another terminal, start the mongo shell.

mongo

In the mongo shell, switch to the sasm database (MongoDB will create it automatically if it doesn't exist yet).

use sasm

You should see the message:

switched to db sasm

Now, create a user for the sasm database:

db.createUser({
  user: "Admin", 
  pwd: "changeMe", 
  roles: ["readWrite"]
})

You should see the message:

Successfully added user: { "user" : "Admin", "roles" : [ "readWrite" ] }

Testing your Connection

To test your connection, with the above authentication as an example, simply run a query. Here's an example of server.js with the @sasm/db extension, a username and password, and a query to an empty collection:

const sasm = require('@sasm/system');
sasm.prep.config.database.username = "Admin";
sasm.prep.config.database.password = "changeMe";

sasm.
	start().
	registerExtension('@sasm/db').
	listen();

// Testing the connection:
let users = sasm.db.collection("users");
users.find((err, docs) => {
	if (err) {
		console.log(err);
	}
	console.log(docs);
});

You should see output similar to this (host will vary):

Listening on: 6::::8080
DB Connected on: mongodb://Admin:changeMe@localhost:27017/sasm
[]

The last line, [] is the docs from the users collection. If we see that, we know our app is communicating with our database. The test portion of the above code can be removed now that we know it's possible to connect with our configuration.

License

LICENSE.md