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

bdt105databaseapi

v1.0.24

Published

Create easy API based on MySql Database

Downloads

11

Readme

Database API

This package aims to make your life easier when it comes to create an API based on MySql data.

You may be able to access very easily a database object with CRUD functionnalities.

You may also create API object in order to create an api server based on MySql database in a blink.

Dependencies

  • bdt105toolbox
  • bdt105connexion

How to use api objects?

3 main objects are available.

TableApi

That object allows you to directecly create an api server to query you MySql database.

See full full example here: https://github.com/bdt105/modules/blob/master/databaseapi/src/server.ts

Methods

Constructor

  • ApiTable(app: any, connexion: Connexion, requiresToken: boolean = false)

For connexion object see https://github.com/bdt105/modules/tree/master/connexion.

Creates all post api entry points:

  • assign(tableName: string, idFieldName: string = null, fields: any = null)

For each entry don't forget in your post body the "token" if you've set requiresToken to true. See https://github.com/bdt105/modules/tree/master/connexion for more details.

  • POST /tableNames (with "s" at the end of the tble name) Lists all elements of the table. In the post use: where limit offset in the post body to select the records
  • PUT /tableName Saves the data only if idFieldName is set. If the record exixts then it's updated if not it is added. In the post body use: object in the post body to set the data to save.
  • POST /tableName/fresh Returns a full empty record of the table.
  • DELETE /tableName Deletes some records, use: where in the post body to select the records to delete.

How to use database objects?

3 main objects are exposed here as follows.

DatabaseObject

This is the basic object.

  • It allows to query the database
  • It can create where strings from an array of fields and values

Only availble for inherited objects

DatabaseRecordset

Inheritates from DatabaseObject and provides access to any database recordset

import { DatabaseRecordset } from "bdt105databaseapi/dist"
import { Connexion } from "bdt105connexion/dist";

let mySqlConfiguration = new MySqlConfiguration("localhost", 3306, "admin", "admin_password"); 
// see https://github.com/bdt105/modules/tree/master/connexion

let conn = new Connexion(mySqlConfiguration);

let databaseObject = new DatabaseObject(conn);

let callback = function (err: any, rows: any){
    if (err){
        console.log("Oups error");
    }else{
        console.log("Everything is fine");
    }
}

let queryAttribute = new QueryAttribute();
queryAttribute.select = "*";
queryAttribute.from = "customers";
queryAttribute.where = "customer.email like '%gmail.com%'";

let databaseRecordset1 = new DatabaseRecordset(conn, queryAttribute);
databaseRecordset1.load(callback);

let databaseRecordset2 = new DatabaseRecordset(conn);
databaseRecordset2.sql(callback, "select * from customers where customer.email like '%gmail.com%'");

QueryAttribute allows to create the query

class QueryAttribute {
    public select: string;
    public from: string;
    public orderby: string;
    public groupby: string;
    public where: string;
    public limit: number;
    public offset: number;
    public extra: string;
    public idFieldName: string;
}

DatabaseTable

Inheritates from DatabaseRecordset and provides access to crud functions + list of fields (fresh record)

import { DatabaseTable } from "bdt105databaseapi/dist"
import { Connexion } from "bdt105connexion/dist";

let mySqlConfiguration = new MySqlConfiguration("localhost", 3306, "admin", "admin_password"); 
// see https://github.com/bdt105/modules/tree/master/connexion

let conn = new Connexion(mySqlConfiguration);

let databaseObject = new DatabaseObject(conn);

let callback = function (err: any, rows: any){
    if (err){
        console.log("Oups error");
    }else{
        console.log("Everything is fine");
    }
}

let queryAttribute = new QueryAttribute();
queryAttribute.select = "*";
queryAttribute.from = "customers";
queryAttribute.where = "customer.email like '%gmail.com%'";
queryAttribute.idFieldName = "customerId";

let databaseTable = new DatabaseRecordset(conn, queryAttribute);
databaseTable.loadFromId(callback, 1234);

Methods

Load data from the id

  • loadFromId(callback: Function, id: string)

Load data from a where condition

  • loadFromWhere(callback: Function, where: string)

Retreives data from a string search in any field of the table

  • search (callback: Function, q: string, prefix: string, suffix: string)

Inserts a record inside the table

  • insert (callback: Function, body: any)

Deletes a record with a certain field equals a certain value

  • deleteFromField (callback: Function, fieldName: string, value: string)

Deletes a record with a certain id

  • deleteFromId (callback: Function, value: string)

Deletes a record with a certain condition

  • deleteFromWhere (callback: Function, where: string)

Updates a record

  • update (callback: Function, body: any, where: string)

Saves a record. If id is found in the database, then the object is updated if not it is inserted

  • save (callback: Function, body: any)

Retreives an empty record

  • fresh (callback: Function)