npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

Local MySQL

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");