@expressive/sql 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressive/sql",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Query builder and ORM",
5
5
  "license": "ISC",
6
6
  "publishConfig": {
@@ -13,6 +13,9 @@
13
13
  },
14
14
  "main": "dist",
15
15
  "module": "dist",
16
+ "files": [
17
+ "dist"
18
+ ],
16
19
  "dependencies": {
17
20
  "sql-formatter": "^10.7.2"
18
21
  },
@@ -1,21 +0,0 @@
1
-
2
- Debugger attached.
3
-
4
- > @expressive/sql@0.1.0 build /Users/gabeklein/Expressive/orm/packages/sql
5
- > tsup src/index.ts --dts --format esm,cjs
6
-
7
- Debugger attached.
8
- CLI Building entry: src/index.ts
9
- CLI Using tsconfig: ../../tsconfig.json
10
- CLI tsup v8.3.6
11
- CLI Target: es2020
12
- ESM Build start
13
- CJS Build start
14
- CJS dist/index.js 30.61 KB
15
- CJS ⚡️ Build success in 1481ms
16
- ESM dist/index.mjs 29.24 KB
17
- ESM ⚡️ Build success in 1524ms
18
- DTS Build start
19
- DTS ⚡️ Build success in 5194ms
20
- DTS dist/index.d.mts 21.17 KB
21
- DTS dist/index.d.ts 21.17 KB
@@ -1,64 +0,0 @@
1
- import { Field } from '../type/Field';
2
-
3
- class BooleanColumn extends Field<boolean> {
4
- readonly type: "tinyint" | "varchar" = "tinyint";
5
- readonly datatype: string;
6
-
7
- readonly TRUE: string | number = 1;
8
- readonly FALSE: string | number = 0;
9
-
10
- constructor(opts: Bool.Opts = {}) {
11
- let type: "tinyint" | "varchar" = "tinyint";
12
- let datatype = "tinyint";
13
- let TRUE: string | number = 1;
14
- let FALSE: string | number = 0;
15
-
16
- if ("either" in opts && opts.either) {
17
- [TRUE, FALSE] = opts.either;
18
- type = "varchar";
19
- datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
20
- }
21
-
22
- super(opts);
23
-
24
- this.type = type;
25
- this.datatype = datatype;
26
- this.TRUE = TRUE;
27
- this.FALSE = FALSE;
28
- }
29
-
30
- get(value: unknown) {
31
- return value === this.TRUE;
32
- }
33
-
34
- set(value: boolean) {
35
- if (typeof value !== "boolean")
36
- throw "Value must be a boolean.";
37
-
38
- return value ? this.TRUE : this.FALSE;
39
- }
40
- }
41
-
42
- declare namespace Bool {
43
- interface TinyInt extends BooleanColumn {
44
- readonly type: "tinyint";
45
- }
46
-
47
- interface Char extends BooleanColumn {
48
- readonly type: "varchar";
49
- either?: [true: string, false: string];
50
- }
51
-
52
- type Any = TinyInt | Char;
53
- type Opts = Partial<Any>;
54
- }
55
-
56
- interface Bool extends BooleanColumn {}
57
-
58
- function Bool<T extends Bool.Opts>(opts?: T){
59
- return new BooleanColumn(opts) as Field.Specify<T, Bool.Any>;
60
- }
61
-
62
- Bool.Type = BooleanColumn;
63
-
64
- export { Bool };
@@ -1,54 +0,0 @@
1
- import { Field } from '../type/Field';
2
-
3
- class NumericColumn extends Field<number> {
4
- readonly type: "int" | "tinyint" | "smallint" | "bigint" | "float" | "double" = "int";
5
-
6
- set(value: number) {
7
- if (typeof value !== "number" || isNaN(value))
8
- throw `Got '${value}' but value must be a number.`;
9
-
10
- if (this.type === "int" && value !== Math.floor(value))
11
- throw `Got '${value}' but datatype is integer.`;
12
-
13
- return value;
14
- }
15
- }
16
-
17
- declare namespace Num {
18
- interface Int extends Num {
19
- readonly type: "int";
20
- }
21
-
22
- interface TinyInt extends Num {
23
- readonly type: "tinyint";
24
- }
25
-
26
- interface SmallInt extends Num {
27
- readonly type: "smallint";
28
- }
29
-
30
- interface BigInt extends Num {
31
- readonly type: "bigint";
32
- }
33
-
34
- interface Float extends Num {
35
- readonly type: "float";
36
- }
37
-
38
- interface Double extends Num {
39
- readonly type: "double";
40
- }
41
-
42
- type Any = Int | TinyInt | SmallInt | BigInt | Float | Double;
43
- type Opts = Partial<Any>;
44
- }
45
-
46
- interface Num extends NumericColumn {}
47
-
48
- function Num<T extends Num.Opts>(opts?: T){
49
- return new NumericColumn(opts) as Field.Specify<T, Num.Any, Num.Int>;
50
- }
51
-
52
- Num.Type = NumericColumn;
53
-
54
- export { Num };
@@ -1,131 +0,0 @@
1
- import { Num, One, Query, Str, Type } from '..';
2
-
3
- class A extends Type {
4
- b = One(B);
5
- value = Str();
6
- }
7
-
8
- class B extends Type {
9
- c = One(C);
10
- value = Str();
11
- }
12
-
13
- class C extends Type {
14
- value = Num();
15
- label = Str();
16
- }
17
-
18
- it("will query via direct selection", () => {
19
- const query = Query(where => {
20
- return where(A).b.value;
21
- });
22
-
23
- expect(query).toMatchInlineSnapshot(`
24
- SELECT
25
- b.value
26
- FROM
27
- a
28
- INNER JOIN b ON b.id = a.b_id
29
- `);
30
- })
31
-
32
- it("will select via an object", () => {
33
- const query = Query(where => {
34
- const a = where(A);
35
-
36
- return {
37
- aValue: a.value,
38
- cValue: a.b.c.value
39
- }
40
- });
41
-
42
- expect(query).toMatchInlineSnapshot(`
43
- SELECT
44
- a.value AS "aValue",
45
- c.value AS "cValue"
46
- FROM
47
- a
48
- INNER JOIN b ON b.id = a.b_id
49
- INNER JOIN c ON c.id = b.c_id
50
- `);
51
- })
52
-
53
- it("will query nested relationships", () => {
54
- const query = Query(where => {
55
- const a = where(A);
56
-
57
- where(a.b.c.value).is(100);
58
-
59
- return a.b.c.label;
60
- })
61
-
62
- expect(query).toMatchInlineSnapshot(`
63
- SELECT
64
- c.label
65
- FROM
66
- a
67
- INNER JOIN b ON b.id = a.b_id
68
- INNER JOIN c ON c.id = b.c_id
69
- WHERE
70
- c.value = 100
71
- `);
72
- })
73
-
74
- // it("will join on id if type is passed", async () => {
75
- // class Foo extends Type {
76
- // name = Str();
77
- // }
78
-
79
- // class Bar extends Type {
80
- // foo = One(Foo);
81
- // rating = Num();
82
- // }
83
-
84
- // const query = Query(where => {
85
- // const foo = where(Foo);
86
- // const bar = where(Bar, () => {
87
- // where(bar.foo).is(foo);
88
- // });
89
-
90
- // where(bar.rating).over(50);
91
-
92
- // return foo.name;
93
- // });
94
-
95
- // expect(query).toMatchInlineSnapshot(`
96
- // SELECT
97
- // foo.name
98
- // FROM
99
- // foo
100
- // INNER JOIN bar ON bar.foo_id = foo.id
101
- // WHERE
102
- // bar.rating > 50
103
- // `);
104
- // });
105
-
106
- it("will assert a property-joined value", () => {
107
- class Foo extends Type {
108
- name = Str();
109
- color = Str();
110
- }
111
-
112
- class Bar extends Type {
113
- foo = One(Foo);
114
- }
115
-
116
- const query = Query(where => {
117
- const bar = where(Bar);
118
-
119
- where(bar.foo.color).is("blue");
120
- });
121
-
122
- expect(query).toMatchInlineSnapshot(`
123
- SELECT
124
- COUNT(*)
125
- FROM
126
- bar
127
- INNER JOIN foo ON foo.id = bar.foo_id
128
- WHERE
129
- foo.color = 'blue'
130
- `);
131
- })
@@ -1,49 +0,0 @@
1
- import { Builder, Nullable, Type } from '..';
2
- import { Field } from '../type/Field';
3
- import { underscore } from '../utils';
4
-
5
- class ForeignColumn<T extends Type> extends Field<Type.Values<T>> {
6
- readonly entity: Type.EntityType<T>;
7
- readonly foreignKey: string = "id";
8
- readonly foreignTable: string;
9
- readonly nullable: boolean = false;
10
-
11
- constructor(type: Type.EntityType<T>, nullable?: boolean) {
12
- super(({ property }) => ({
13
- column: underscore(property) + "_id"
14
- }));
15
-
16
- this.entity = type;
17
- this.type = "int";
18
- this.foreignKey = "id";
19
- this.foreignTable = type.table;
20
- this.foreignTable = type.table;
21
- this.nullable = nullable || false;
22
- }
23
-
24
- set(value: Type.Values<T> | number) {
25
- if (this.query?.tables.has(value))
26
- return (value as any)[this.foreignKey];
27
-
28
- return value;
29
- }
30
-
31
- use(query: Builder) {
32
- // TODO: avoid any here?
33
- const table = query.use(this.entity) as any;
34
- query.where(table.id, "=", this);
35
- return table;
36
- }
37
- }
38
-
39
- interface One<T extends Type> extends ForeignColumn<T> {}
40
-
41
- function One<T extends Type>(type: Type.EntityType<T>, nullable?: false): One<T>;
42
- function One<T extends Type>(type: Type.EntityType<T>, nullable: boolean): One<T> & Nullable;
43
- function One<T extends Type>(type: Type.EntityType<T>, nullable?: boolean){
44
- return new ForeignColumn(type, nullable);
45
- }
46
-
47
- One.Type = ForeignColumn;
48
-
49
- export { One };
@@ -1,75 +0,0 @@
1
- import { Field } from '../type/Field';
2
-
3
- const VARIABLE_TYPE = new Set(["char", "varchar", "text"]);
4
-
5
- class StringColumn extends Field<string> {
6
- readonly type: "char" | "varchar" | "text" | "tinytext" | "mediumtext" | "longtext" = "varchar";
7
- readonly length = 255;
8
-
9
- constructor(opts?: Str.Opts) {
10
- super(({ parent, property }) => {
11
- let { type = "varchar", length = 255, datatype } = opts || {};
12
-
13
- if(VARIABLE_TYPE.has(type!))
14
- if(length)
15
- datatype = `${datatype || type}(${length})`;
16
- else
17
- throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`
18
-
19
- return {
20
- datatype,
21
- ...opts
22
- }
23
- });
24
- }
25
-
26
- set(value: string) {
27
- if (typeof value !== 'string')
28
- throw 'Value must be a string.';
29
-
30
- if (this.length && value.length > this.length)
31
- throw `Value length ${value.length} exceeds maximum of ${this.length}.`;
32
-
33
- return value;
34
- }
35
- }
36
-
37
- declare namespace Str {
38
- interface Char extends Str {
39
- readonly type: "char";
40
- }
41
-
42
- interface VarChar extends Str {
43
- readonly type: "varchar";
44
- }
45
-
46
- interface Text extends Str {
47
- readonly type: "text";
48
- }
49
-
50
- interface TinyText extends Str {
51
- readonly type: "tinytext";
52
- }
53
-
54
- interface MediumText extends Str {
55
- readonly type: "mediumtext";
56
- }
57
-
58
- interface LongText extends Str {
59
- readonly type: "longtext";
60
- }
61
-
62
- type Any = Char | VarChar | Text | TinyText | MediumText | LongText;
63
-
64
- type Opts = Partial<Any>;
65
- }
66
-
67
- interface Str extends StringColumn {}
68
-
69
- function Str<T extends Str.Opts>(opts?: T){
70
- return new StringColumn(opts) as Field.Specify<T, Str.Any, Str.VarChar>;
71
- }
72
-
73
- Str.Type = StringColumn;
74
-
75
- export { Str }
@@ -1,29 +0,0 @@
1
- import { Field } from '../type/Field';
2
-
3
- class TimeColumn extends Field<Date> {
4
- readonly type: "datetime" = "datetime";
5
- readonly fallback?: "NOW" | Date = undefined;
6
-
7
- get(value: string) {
8
- return new global.Date(value.replace(/[-]/g, '/') + "Z");
9
- }
10
-
11
- set(value: string | Date) {
12
- if (value === "NOW")
13
- return "CURRENT_TIMESTAMP";
14
- else if (value instanceof Date)
15
- return value.toISOString().slice(0, 19).replace("T", " ");
16
- else
17
- throw "Value must be a Date or 'NOW'.";
18
- }
19
- }
20
-
21
- interface Time extends TimeColumn {}
22
-
23
- function Time(options?: Partial<Time>): Time {
24
- return new TimeColumn(options);
25
- }
26
-
27
- Time.Type = TimeColumn;
28
-
29
- export { Time };
@@ -1,117 +0,0 @@
1
- import { format } from 'sql-formatter';
2
-
3
- import { Builder, Type } from '..';
4
- import { Generator } from '../connection/Generator';
5
- import { values } from '../utils';
6
-
7
- declare namespace Connection {
8
- type Types =
9
- | typeof Type[]
10
- | { [key: string | number]: typeof Type }
11
-
12
- type Using = { [key: string | number]: typeof Type };
13
-
14
- type Ready<T extends Connection = Connection> = Omit<T, "then">;
15
-
16
- let None: Connection;
17
- }
18
-
19
- abstract class Connection {
20
- static generator = Generator;
21
-
22
- readonly using: Readonly<Set<typeof Type>>;
23
- readonly ready: boolean = false;
24
-
25
- constructor(using: Connection.Types){
26
- this.using = new Set(values(using).map(type => {
27
- if(type.hasOwnProperty('connection'))
28
- throw new Error(`Type ${type.name} already has assigned connection.`);
29
-
30
- type.connection = this;
31
- return type;
32
- }))
33
- }
34
-
35
- generate(builder: Builder){
36
- const { generator } = this.constructor as typeof Connection;
37
- return new generator(builder).toString();
38
- }
39
-
40
- async then(
41
- onfulfilled?: (value: Connection.Ready<this>) => any,
42
- onrejected?: (reason: any) => any) {
43
-
44
- return (this.ready ? Promise.resolve() : this.sync(true))
45
- .then(() => {
46
- Object.defineProperty(this, 'then', { value: undefined });
47
-
48
- if (onfulfilled)
49
- onfulfilled(this);
50
- })
51
- .catch(onrejected);
52
- }
53
-
54
- abstract get schema(): string;
55
-
56
- async close(){
57
- for(const type of this.using)
58
- delete type.connection;
59
- }
60
-
61
- abstract sync(fix?: boolean): Promise<void>;
62
- abstract valid(type: Type.EntityType): Promise<boolean>;
63
-
64
- abstract prepare<T = any>(query: string): {
65
- all: (args?: any[]) => Promise<T[]>;
66
- get: (args?: any[]) => Promise<T | undefined>;
67
- run: (args?: any[]) => Promise<number>;
68
- }
69
- }
70
-
71
- class NoConnection extends Connection {
72
- constructor(){
73
- super([]);
74
- }
75
-
76
- get schema(): never {
77
- throw new Error("No connection to generate schema.");
78
- }
79
-
80
- prepare(template: string){
81
- async function run(): Promise<never> {
82
- throw new Error("No connection to run query.");
83
- }
84
-
85
- return {
86
- all: run,
87
- get: run,
88
- run,
89
- toString(){
90
- return pretty(template);
91
- }
92
- }
93
- }
94
-
95
- async sync(){
96
- return;
97
- }
98
-
99
- async valid(){
100
- return true;
101
- }
102
-
103
- async close(){
104
- return;
105
- }
106
- }
107
-
108
- function pretty(sql: string){
109
- return format(sql, { keywordCase: "upper" })
110
- // TODO: this shouldn't apply - drop CTE from this module
111
- .replace(/ - > > /g, " ->> ")
112
- .replace(/`([a-zA-Z][a-zA-Z0-9_]*)`/g, "$1");
113
- }
114
-
115
- Connection.None = new NoConnection();
116
-
117
- export { Connection };