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.
- SQLString
- SQLNumber
- SQLBoolean
- SQLEnum
- SQLContent
- SQLObject
- SQLMeta
- SQLOneRelation
- 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,
isUnique: true,
}),
name: new SQLString({
isSearchable: true,
}),
isVerified: new SQLBoolean({
defaultValue: false,
}),
status: new SQLEnum(["Active", "Blocked"], {
isSearchable: true,
defaultValue: "Active",
}),
createdOn: new SQLNumber({
defaultValue: Date.now(),
}),
modifiedOn: new SQLNumber({
defaultValue: Date.now(),
updatedValue: Date.now(),
}),
} as const);
Create a Connection
Follow the method below to create a Connection with MySql Server.
import { Connector, ConnectionManager } from "epic-sql";
import { Users } from "./schema/users";
import { Profiles } from "./schema/profiles";
import { Orders } from "./schema/orders";
(async () => {
const Connection = await new ConnectionManager(
new Connector(
{
host: "localhost",
user: "root",
password: "",
database: "mydatabase",
},
{
sync: false,
logs: true,
}
),
[Users, Profiles, Orders]
).init();
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 () => {
const User = Users.new();
const NewUser = await User.insert({
userId: "john",
name: "John Doe",
isVerified: true,
status: "Active",
});
console.log(NewUser);
})();
Fetch Data
Follow the method below to fetch data from Users Schema.
import { Users } from "./schema/users";
(async () => {
console.log(
await Users.new()
.select(["userId", "name", "status"])
);
console.log(
await Users.new()
.where({
userId: "john",
})
.selectOne()
);
const SpecialUsers = await Users.new()
.where({
isVerified: true,
status: "Active",
})
.having({
_id: {
BT: [1, 100],
},
})
.offset(0)
.limit(10)
.sort("DESC")
.select();
const SearchedUsers = await Users.new()
.search("my search string")
.select();
})();
Update Data
Follow the method below to update data on Users Schema.
import { Users } from "./schema/users";
(async () => {
await Users.new().where({ userId: "john" }).update({
status: "Blocked",
});
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 () => {
await Users.new().where({ userId: "john" }).delete();
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!