Welcome, our package was made to make developement easier to start out. Instead of having to worry about making a server end everytime you start a new project. You can use this package to have a modular based server end. Our package supports both CommonJS and ES6. We do not do anything on the client side. If you want to use a client you can do so by using our Proxy Module.
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-core
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);
})();
Query
Find One
Find Many
Find By IDs
Save
Update
Delete
Count
Clear
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.
const db = await Server.Database.create("mysql", {
modelsPath: "models",
name: "default",
host: "localhost",
port: 3306,
username: "testing",
password: "testing",
database: "testing",
});
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"
});
Executes a raw SQL query.
const rawData = await Server.db.query(`SELECT * FROM USERS`);
Finds the first entity that matches some id or find options.
const response = await Server.db.table("users").findOne({
where: {
username: "test"
}
})
Finds entities that match given options.
const userData = await Server.db.table("users").find({ username: "admin" });
Finds multiple entities by id.
const count = await Server.db.table("users").findByIds([1, 2, 3]);
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" }
]);
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" });
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" }]);
Counts entities that match given options. Useful for pagination.
const count = await Server.db.table("users").count({ active: true });
Clears all the data from the given table (truncates/drops it).
const count = await Server.db.table("users").clear();
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".
});
This module allows our server and you to interact with cookies on the clients browser.
await Server.Cookies.install();
This module requires Cookies module to be enabled. This module created a session storage for you to use.
await Server.Session.install({
secret: "somethingrandom"
});
This module adds Server Side Events to the servers router.
await Server.ServerSideEvents.install();
This module adds Web Sockets to the servers router.
await Server.WebSockets.install();