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

@saffellikhan/epic-sql

v2.0.7

Published

A Simple But Powerful SQL ORM!!!

Downloads

4

Readme

Epic Sql

A Powerful MySQL ORM. Epic SQL allows you to build applications by writing less code without a single line of SQL Query!

Usage

Epic SQL is very easy to use library. View the documentation below to start working with Epic SQL quickly!

Available DataTypes

Following are the available datatypes in the ORM.

  1. SQLString
  2. SQLNumber
  3. SQLBoolean
  4. SQLEnum
  5. SQLContent
  6. SQLObject
  7. SQLMeta
  8. SQLOneRelation
  9. SQLManyRelation

Build a Schema

Following is the example of a Users Schema.

import { Schema, SQLBoolean, SQLEnum, SQLNumber, SQLString } from "epic-sql";

export const Users = new Schema("users", {
  userId: new SQLString({
    isSearchable: true, // Add Match Against Index
    isUnique: true,
  }),

  name: new SQLString({
    isSearchable: true, // Add Match Against Index
  }),

  isVerified: new SQLBoolean({
    defaultValue: false,
  }),

  status: new SQLEnum(["Active", "Blocked"], {
    isSearchable: true, // Add Match Against Index
    defaultValue: "Active",
  }),

  createdOn: new SQLNumber({
    defaultValue: Date.now(),
  }),

  modifiedOn: new SQLNumber({
    defaultValue: Date.now(),
    updatedValue: Date.now(), // Timestamp is updated if any record changes.
  }),
} as const);

Create a Connection

Follow the method below to create a Connection with MySql Server.

import { Connector, ConnectionManager } from "epic-sql";

// Import All Schemas
import { Users } from "./schema/users";
import { Profiles } from "./schema/profiles";
import { Orders } from "./schema/orders";

(async () => {
  // Create and Initialize a Connection Manager Instance.
  const Connection = await new ConnectionManager(
    // You need to pass a Connector Instance to Connection Manager.
    new Connector(
      {
        host: "localhost",
        user: "root",
        password: "",
        database: "mydatabase",
      },
      {
        /**
         * Schema will be synced to the MySql Server on each connection if sync is set to True.
         * Please don't forget to set it to False on the production.
         */
        sync: false,

        // Prints each exectued SQL Query in the Console.
        logs: true,
      }
    ),

    // Pass all Schemas to the Connection Manager.
    [Users, Profiles, Orders]
  ).init();

  // End the Connection
  Connection.getConnector().end();
})();

Insert Data

Follow the method below to insert data to Users Schema we created just now.

import { Users } from "./schema/users";

(async () => {
  // Create a Locked Users Schema by calling new() method on it.
  // It is required to Lock the Schema before doing any CRUD operation on it.
  const User = Users.new();

  // Insert Row to Users Schema
  // If there is a problem, the operation will throw an error.
  // You can insert one row passed as an object or multiple rows passed as an Array.
  const NewUser = await User.insert({
    userId: "john",
    name: "John Doe",
    isVerified: true,
    status: "Active",
  });

  // If the operation was successful, A new User object will be stored on NewUser variable.
  console.log(NewUser);

  /**
   * Will Print:
   *
   * {
   * 	_id: 1,
   * 	userId: "john",
   * 	name: "John Doe",
   * 	isVerified: true,
   * 	status: "Active",
   * 	createdOn: 356210359812,
   * 	modifiedOn: 356210359812,
   * }
   *
   */
})();

Fetch Data

Follow the method below to fetch data from Users Schema.

import { Users } from "./schema/users";

(async () => {
  // Example 1:
  // Select all Users from Users Schema.
  // Use select() method for selection.
  console.log(
    await Users.new()
      // You can pass an Array of column names that you want to select.
      .select(["userId", "name", "status"])
  );

  /**
   * Will Print:
   *
   * [
   *   {
   * 	 userId: "john",
   * 	 name: "John Doe",
   * 	 status: "Active",
   *   }
   * ]
   *
   */

  // Example 2:
  // Select One User where userId is "john"
  console.log(
    await Users.new()
      .where({
        userId: "john",
      })
      .selectOne()
  );

  /**
   * Will Print:
   *
   * {
   * 	_id: 1,
   * 	userId: "john",
   * 	name: "John Doe",
   * 	isVerified: true,
   * 	status: "Active",
   * 	createdOn: 356210359812,
   * 	modifiedOn: 356210359812,
   * }
   *
   */

  // Exmaple 3:
  // Advance Data Selection
  const SpecialUsers = await Users.new()
    .where({
      isVerified: true,
      status: "Active",
    })
    // Extra Conditions
    .having({
      _id: {
        // _id Ranges Between 1 to 100
        BT: [1, 100],
      },
    })
    // Set Selection Offset
    .offset(0)
    // Set Selection Limit
    .limit(10)
    // Set Selection Sorting ASC or DESC
    .sort("DESC")
    .select();

  // Example 4:
  // Search Data
  const SearchedUsers = await Users.new()
    // Search your data using Match Against feature
    // Make sure you have set isSearchable to True on the Schema for the columns you want to search.
    // In our case userId, name and status column is searchable.
    // You cannot use where() method with search() method.
    .search("my search string")
    .select();
})();

Update Data

Follow the method below to update data on Users Schema.

import { Users } from "./schema/users";

(async () => {
  // Example 1:
  // Update User Status
  // Use update() method to update User Data
  await Users.new().where({ userId: "john" }).update({
    status: "Blocked",
  });

  // Example 2:
  // Update User Status, use updateOrFail() method to throw error if nothing has been updated.
  await Users.new().where({ userId: "john" }).updateOrFail({
    status: "Blocked",
  });
})();

Delete Data

Follow the method below to delete data from Users Schema.

import { Users } from "./schema/users";

(async () => {
  // Example 1:
  // Delete a User
  // Use delete() method to delete User Data
  await Users.new().where({ userId: "john" }).delete();

  // Example 2:
  // Delete a User, use deleteOrFail() method to throw error if nothing has been deleted.
  await Users.new().where({ userId: "john" }).deleteOrFail({
    status: "Blocked",
  });
})();

I hope that the documentation is easy enough to understand the library. You will find it very easy to work with and will be able to explore more features while using this library.

We are still working on the documentation, there are allot of features missing in the documentation but available on the library. We will add them to the docs soon.

Good Luck!