No description
|
||
---|---|---|
src | ||
.gitignore | ||
.npmignore | ||
.woodpecker.yml | ||
package-lock.json | ||
package.json | ||
readme.md | ||
tsconfig.json |
DBlang
TypeScript Libary for relational Database Requests. Requests are written in js/ts and it supports automatic Schema evulution.
Fetures
- select Query
- insert Query
- update Query
- remove Query
- create Schema
- remove unused Schema
- change Schema
- Key-Value Store Shortcut
- Views
Supported Databses:
- mariadb
- mysql (not tested)
- postrges
Getting Started
Connect
import { DB } from "dblang";
const db = new DB({
host: "localhost",
user: "root",
password: "0123456789",
database: "databaseName"
});
Schema
Tables:
const Table1 = db.newTable("Table1Name");
const Table2 = db.newTable("Table2Name");
Add Attributes:
Table1.addAttribute("AttrName", TYPE, {
default: 5,
// other settings
});
Table2.addAttributes({
"AttrName2":{
type: TYPE,
default: 6
// other settings
},
"AttrName3":{
type: TYPE,
default: 6
// other settings
},
// more Attributes
});
TYPE: See Types
Other Settings:
unique: boolean
: (optional) Add Unique ConstraintautoIncrement: boolean
: (optional) Set auto Increment (must be primary key)default: any
: (optional) Set default Property.notNull: boolean
: (optional) Set not nullable.primaryKey: boolean
: (optinal) mark as part of primary Key.foreignKey: Object
: Add foreign Constraint for single Attribute.check: BooleanModifier | ((a: Attribute) => BooleanModifier)
: Add check Constraint (A function must be used when check references its self.)
Sync: Create Tables, Attributes and Constraints
await db.sync();
await db.sync(true); //delete unused Elements (May do a backup befor.)
Querys
See: Modifiers, Aggregations
Select:
import {select} from "dblang"
let res = await select([Table2.AttrName2], Table1)
.where(eq(Table2.AttrName2, 5)) //optional
.groupBy(Table2.AttrName3) //optional
.having(le(Table2.AttrName3, 5)) //optional
.limit(10) //optional
.query(db);
Insert:
import {select} from "dblang"
await insert(Table2.AttrName2, Table2.AttrName3)
.add(1, 2)
.add(2, 3)
.addValues([4, 5], [3, 4])
.query(db);
await insert(Table2.AttrName2, Table2.AttrName3)
.setSelect(select(Table1.AttrName, 4))
.query(db);
Update:
await update(Table2)
.set(Table2.AttrName2, plus(Table2.AttrName2,1))
.set(Table2.AttrName3, minus(Table2.AttrName2,1))
.where(leq(Table2.AttrName2, 5))
.query(db);
Remove:
await remove(Table2)
.where(geq(Table2.AttrName2, 5))
.query(db);
Content List
Types
CHAR(size)
VARCHAR(size)
BINARY(size)
VARBINARY(size)
TINYBLOB
BLOB
MEDIUMBLOB
LONGBLOB
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT
ENUM(val1, val2, ...)
SET(val1, val2, ...)
BOOL
BIT
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
FLOAT(size, d)
DOUBLE(size, d)
DECIMAL(size, d)
DATE
DATETIME
TIMESTAMP
TIME
YEAR
onAction
Action for onDelete or onUpdate.
Usage:
import { onAction } from "dblang"
onAction.cascade;
onAction.noAction; // dangerous
onAction.setNull;
onAction.setDefault;
Modifier
BooleanModifier:
and(v1, v2, ...)
or(v1, v2, ...)
ge(v1, v2, ...)
geq(v1, v2, ...)
eq(v1, v2, ...)
leq(v1, v2, ...)
le(v1, v2, ...)
not(v1)
like(v1, v2)
NumberModifier:
plus(v1, v2, ...)
minus(v1, v2, ...)
mult(v1, v2, ...)
divide(v1, v2, ...)
StringModifier:
concat(v1, v2, ...)
(v* = string, number, boolean, null, Modifier, Aggregation, select Query, Attribute)
Aggregations
count(a)
sum(a)
avg(a)
min(a)
max(a)
(a = Attribute)