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

turiy-mysql

v1.1.1

Published

This package is built on mysql2.

Downloads

96

Readme

Turiy MySQL Package

This package is made on mysql2 and next. The functions exported here is treated as server side functions. This package is able to create connection with your MySQL database and handle user authentication.

Recommended to use inside server components or use server files.

Environment Variables

Remember to add following at your .env.local

  • CRYPTO_PASSWORD [optional]
  • MYSQL_HOST
  • MYSQL_USER
  • MYSQL_PASSWORD
  • MYSQL_DATABASE

Queries with where clause

Select queries

select({
  user: { where: "(userId, password) IN (('123456789', 'P#@$%745458'))" },
});
// or
select({ user: { where: "rating>2000" } });

Update queries

update(
  { item: { price: 200, discount: 5 } },
  { where: "(itemId) IN (('IT78945'))" }
);
// or
update({ item: { price: 200, discount: 5 } }, { where: "price=300" });

Delete queries

del({ item: { where: "(itemId) IN (('IT78945'))" } });
// or
del({ item: { where: "price<10" } });

Queries without where clause

These queries only checks the equality of the given fields.

Select queries

select({ user: { userId: 123456789, password: "P#@$%745458" } });

Update queries

update({ item: { price: 200, discount: 5 } }, { itemId: "IT78945" });

Delete queries

del({ item: { itemId: "IT78945" } });

Other queries

Insert queries

insert({ item: { itemId: "IT78945", price: 300, discount: 15 } });

You can directly executes any SQL queries including complex queries with the execute.

execute("SELECT ...");
execute("INSERT ...");
execute("UPDATE ...");
execute("DELETE ...");

Authentication

Following functions are provided to handle authentication.

Sign-in

signin = async (userTable: Table): Promise<Tuple|undefined>

This function takes the user table-name and field information to check whether the given information exists inside database or not. If exists it returns the tuple containing complete user information as defined by your user table of your database; otherwise returns undefined.

//_Examples are given here_
signin({ user: { id: "my-user-id", password: "my-password" } });
// or
signin({ user: { emailId: "my-user-id", password: "my-password" } });
//or
signin({ user: { emailId: "my-user-id", password: "my-password", active: 1 } });

Sign-out

You only need to invoke signout()

Auth check client to server

authCheck = async (): Promise<Tuple|undefined>

After successfully signed-in this function is invoked for every request to see whether the user is signed-in or not. If the user is authentic and signed-in user, then it returns the tuple containing complete user information as defined by your user table of your database; otherwise returns undefined.

This function works only when the request is made directly from the user's browser.

Auth check client to middleware (server) to api (server)

authCheckFor = async ({sessionCipher, ip, agent}: BrowserClientAuth): Promise<Tuple|undefined>

This function works exactly same way as the above function authCheck. The only difference is that it allows the middleware to communicate with the api existing in the same server to validate the user authentication.

Since middleware doesn't have access to full functionality of server, it sometimes use api call to get information from the server.

  • At first middleware invokes the browserClientAuth function. getBrowserClientAuth = async (): Promise<BrowserClientAuth | undefined>.
const clientAuth = await getBrowserClientAuth();
  • Then middleware uses fetch or axios to invoke an auth check api.
//Inside `middleware` this `api` is invoked
const response = await fetch("<api-url>", {
  method: "POST",
  body: JSON.stringify(clientAuth),
});
const userInfo = await response.json();
  • Then the api, that may be declared as follows, uses authCheckFor to validate the end user authentication. and response the user information to the middleware.
//The `api` can be written as follows:
export async function POST(req: Request) {
  const { sessionCipher, ip, agent } = await req.json();
  const user = await authCheckFor({ sessionCipher, ip, agent });
  return Response.json(user);
}