@aio-server/database
v1.0.0
Published
This is a database module for @aio-server
Downloads
12
Readme
@aio-server - Database Module
This package is a module of @aio-server/core. Please make sure you read its documents before you try to use this module. If you are not familiar with our core package please go back and read about it.
Getting Started
This module is made so you can make database connections and tables easier without needing a client to set everything up. Our tables are file based. You will learn about that later. Make sure the first thing you do is add the package to your app by using npm i @aio-server/database
.
After you have downloaded and added the package to your app. You can now add it to your project.
// import the core, required for all modules.
const server = require("@aio-server/core");
// import the module.
const Database = require("@aio-server/database");
(async () => {
// install the module.
server.install(Database, {
default: true,
type: "local"
});
// test the database.
console.log(server.get("database").get("default").table("users").find());
console.log(server.db.table("users").find()); // only works with a default database.
// start the server.
server.start({ port: 8080 });
})();
Table of Contents
Find One
Find Many
Find and Count
Create
Update
Delete
Count
Query
Clear
Get Database Instance
Get Database Table
Create Instance
Our databases are instance based so that you can make more then one database connection without having to take extra steps. Its super easy to create a database instance. Check out below to see how to make different instances that we support.
MySQL
Required Options
- type: STRING -
This tells the server which type of database you want to use.
- host: STRING -
The host of the database.
- username: STRING -
The username of the database.
- password: STRING -
The password of the database.
- database: STRING -
The name of the database.
Optional Options
- modelsPath: STRING -
The path you have your models in. Only use this if you are using more then one database in your project.
- instanceName: STRING -
The name of the instance. What you would use to get the database connection. Only used this is you are using more then one database in your project.
- default: BOOLEAN -
To tell the server that its the default database to use. This makes a shortcut to server.db. Make sure you set this on your main database connection.
Default Database Example
await server.install(server.modules.get("database"), {
default: true,
type: "mysql",
host: "localhost",
username: "test",
password: "test",
database: "test"
});
More Then One Database Example
await server.install(server.modules.get("database"), {
default: true,
type: "mysql",
host: "localhost",
username: "test",
password: "test",
database: "test"
});
await server.install(server.modules.get("database"), {
type: "mysql",
host: "localhost",
username: "test",
password: "test",
database: "testing"
instanceName: "local-database",
modelsPath: "local-models"
});
Local
Required Options
- type: STRING -
This tells the server which type of database you want to use.
Optional Options
- modelsPath: STRING -
The path you have your models in. Only use this if you are using more then one database in your project.
- instanceName: STRING -
The name of the instance. What you would use to get the database connection. Only used this is you are using more then one database in your project.
- default: BOOLEAN -
To tell the server that its the default database to use. This makes a shortcut to server.db. Make sure you set this on your main database connection.
Default Database Example
await server.install(server.modules.get("database"), {
default: true,
type: "local"
});
More Then One Database Example
await server.install(server.modules.get("database"), {
default: true,
type: "local"
});
await server.install(server.modules.get("database"), {
type: "local",
instanceName: "local-database",
modelsPath: "local-models"
});
Functions
As of right now we are using a package called TypeORM. You can read up on their documentation for Repository API. Its what we use for our databases. We plan on creating our own package in the future to remove the need to have TypeORM as a dependency.
Functions are what you would use to interact with the database.
Find One
Find Many
Find and Count
Create
Update
Delete
Count
Query
Clear
Get Database Instance
Get Database Table
Find One
Finds first entity that matches some id or find options.
const user1 = await server.db.table("users").findOne(1);
const user2 = await server.db.table("users").findOne({ username: "test" });
Find Many
Finds entities that match given options.
const user1 = await server.db.table("users").find({ age: 21 });
const user2 = await server.db.table("users").find({ age: 21, country: "usa" });
Find and Count
Finds entities that match given find options. Also counts all entities that match given conditions, but ignores pagination settings (skip
and take
options).
const [users, usersCount] = await server.db.table("users").findAndCount({ age: 21 });
Create
Accepts an object literal with your table properties which will be written into a newly created table object.
const user1 = await server.db.table("users").create({ username: "test", age: 21 });
Update
Partially updates entity by a given update options or entity id.
const user1 = await server.db.table("users").update({ username: "test" }, { age: 18 });
const user2 = await server.db.table("users").update(1, { age: 18 });
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({ username: "test" });
Count
Counts entities that match given options. Useful for pagination.
const user1 = await server.db.table("users").count({ age: 21 });
Query
Executes a raw SQL query. May not be supported for every database type.
const users = await server.db.table("users").query(`SELECT * FROM USERS`);
Clear
Clears all the data from the given table (truncates/drops it).
const user1 = await server.db.table("users").clear();
Get Database Instance
Get the instance of a database.
const db = await server.modules.get("database").get("default");
Get Database Table
Get a table from the database so you can edit it.
const table = await server.modules.get("database").get("default").table("users");