diff --git a/readme.md b/readme.md index d023b05..5c91f67 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,214 @@ # DBlang -sql Querys with js or ts Syntax. -[![status-badge](https://ci.jusax.de/api/badges/jusax23/dblang/status.svg)](https://ci.jusax.de/jusax23/dblang) \ No newline at end of file +[![status-badge](https://ci.jusax.de/api/badges/jusax23/dblang/status.svg)](https://ci.jusax.de/jusax23/dblang) + +TypeScript Libary for relational Database Requests. Requests are written in js/ts and it supports automatic Schema evulution. + +Fetures +- [x] select Query +- [x] insert Query +- [x] update Query +- [x] remove Query +- [x] create Schema +- [x] remove unused Schema +- [ ] change Schema +- [ ] Key-Value Store Shortcut +- [ ] Views + +Supported Databses: +- [x] mariadb +- [ ] mysql (not tested) +- [ ] postrges + +## Getting Started + +### Connect + +```javascript +import { DB } from "dblang"; +const db = new DB({ + host: "localhost", + user: "root", + password: "0123456789", + database: "databaseName" +}); + +``` + +### Schema + +Tables: +```javascript +const Table1 = db.newTable("Table1Name"); +const Table2 = db.newTable("Table2Name"); +``` + +Add Attributes: +```javascript +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](#types) + +Other Settings: +- `unique: boolean`: (optional) Add Unique Constraint +- `autoIncrement: 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. + - `link: Attribute`: Linked Attribute + - `onDelete: onAction`: Specify delete Action [onAction](#onaction) + - `onUpdate: onAction`: Specify update Action [onAction](#onaction) +- `check: BooleanModifier | ((a: Attribute) => BooleanModifier)`: Add check Constraint (A function must be used when check references its self.) + +Sync: +Create Tables, Attributes and Constraints +```javascript +await db.sync(); + +await db.sync(true); //delete unused Elements (May do a backup befor.) +``` + +### Querys +See: [Modifiers](#modifier), [Aggregations](#aggregations) + +#### Select: +```javascript +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: +```javascript +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: +```javascript +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: +```javascript +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: +```javascript +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) \ No newline at end of file diff --git a/src/funcs.ts b/src/funcs.ts index 9c9aebe..77ad471 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -16,7 +16,7 @@ export const minus = (...args: allModifierInput[]) => new NumberModifier("minus" export const mult = (...args: allModifierInput[]) => new NumberModifier("mult", args); export const divide = (...args: allModifierInput[]) => new NumberModifier("divide", args); -export const not = (...args: allModifierInput[]) => new BooleanModifier("not", args); +export const not = (arg: allModifierInput) => new BooleanModifier("not", [arg]); export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]); export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args); diff --git a/src/query.ts b/src/query.ts index 597ffb2..9fd00d6 100644 --- a/src/query.ts +++ b/src/query.ts @@ -77,8 +77,8 @@ export class selectQuery { this.groupByD = a; return this; } - havingD: Modifier | null = null; - having(m: Modifier) { + havingD: BooleanModifier | null = null; + having(m: BooleanModifier) { this.havingD = m; return this; }