1.0.10020 • Published 2 years ago

@shinerzoch/server v1.0.10020

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Getting Started

To get started please run the following commands from your project directory. If you havent done so yet please make sure you have NodeJS installed. Please make sure you init your project by using the command below.

npm init --yes

Now in order to get started using our package as your server. Please run the following command.

npm i @shinerzoch/server

This command will install our package inside your application. This will allow you to use the below coding to get started.

Javascript NodeJS Module

// index.mjs

import Server from '../serverLibrary/index.js';
const port = 8080;

    // App logic goes here...    

Server.start(port);

Javascript NodeJS

// index.js

const Server = require('../serverLibrary/index.js');

(async () => {
    const port = 8080;
    
        // App logic goes here...   

    Server.start(port);
})();

Table Of Contents

Database

Functions

Query Find One Find Many Find By IDs Save Update Delete Count Clear

Auto Routes

Cookies

Session

Server Side Events

Web Sockets

Database Module

Database Functions

Query Find One Find Many Find By IDs Save Update Delete Count Clear

You can set the default database by setting Server.db = DatabaseInstance. This is not a requirement, but it is recommended if you plan on using the database as your main projects database.

Database Create Mysql Instance

const db = await Server.Database.create("mysql", {
    modelsPath: "models",
    name: "default",
    host: "localhost",
    port: 3306,
    username: "testing",
    password: "testing",
    database: "testing",
});

Database Create Local Instance

This is a database that is completely file based and the contents are stored within your project. or any directory of choosing.

const db = await Server.Database.create("local", {
    name: "default", // Must be unqiue if not default.
    modelsPath: "models"
});

Database Query

Executes a raw SQL query.

const rawData = await Server.db.query(`SELECT * FROM USERS`);

Database Find One

Finds the first entity that matches some id or find options.

const response = await Server.db.table("users").findOne({
    where: {
        username: "test"
    }
})

Database Find Many

Finds entities that match given options.

const userData = await Server.db.table("users").find({ username: "admin" });

Database Find By IDs

Finds multiple entities by id.

const count = await Server.db.table("users").findByIds([1, 2, 3]);

Database save

Saves a given entity or array of entities. If the entity already exists in the database, then it's updated. If the entity does not exist in the database yet, it's inserted. It saves all given entities in a single transaction. Also supports partial updating since all undefined properties are skipped. In order to make a value NULL, you must manually set the property to equal null.

const userID = await Server.db.table("users").save({ username: "admin", password: "admin" });

const userIDs = await Server.db.table("users").save([
    { username: "admin", password: "admin" },
    { username: "test", password: "test" }
]);

Database update

Partially updates entity by a given update options or entity id.

const newData = await Server.db.table("users").update({ id: 1 }, { username: "superadmin" });

const newData = await Server.db.table("users").update(1, { username: "superadmin" });

Database delete

Deletes entities by entity id, ids or given conditions:

await Server.db.table("users").delete(1);
await Server.db.table("users").delete([1, 2, 3]);
await Server.db.table("users").delete({ id: 1 });
await Server.db.table("users").delete([{ id: 1 }, { username: "test" }]);

Database count

Counts entities that match given options. Useful for pagination.

const count = await Server.db.table("users").count({ active: true });

Database clear

Clears all the data from the given table (truncates/drops it).

const count = await Server.db.table("users").clear();

Auto Routes Module

This module is created to auto generate routes based of a directory.

await Server.AutoRoutes.install({
    path: "routes" // Optional Defaults to "routes", you can use something like "client/routes".
});

Cookies Module

This module allows our server and you to interact with cookies on the clients browser.

await Server.Cookies.install();

Session Module

This module requires Cookies module to be enabled. This module created a session storage for you to use.

await Server.Session.install({
    secret: "somethingrandom"
});

Server Side Events Module

This module adds Server Side Events to the servers router.

await Server.ServerSideEvents.install();

Web Sockets Module

This module adds Web Sockets to the servers router.

await Server.WebSockets.install();