query-core 0.1.7 → 0.1.11
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/lib/SearchBuilder.js +16 -8
- package/lib/batch.js +1 -1
- package/lib/build.js +216 -114
- package/lib/client.js +180 -0
- package/lib/index.js +15 -70
- package/lib/map.js +75 -0
- package/lib/query.js +12 -8
- package/lib/search.js +4 -1
- package/lib/services.js +24 -16
- package/package.json +1 -1
- package/src/SearchBuilder.ts +23 -14
- package/src/batch.ts +2 -2
- package/src/build.ts +191 -100
- package/src/client.ts +202 -0
- package/src/index.ts +32 -63
- package/src/map.ts +67 -0
- package/src/metadata.ts +19 -5
- package/src/query.ts +19 -15
- package/src/search.ts +11 -8
- package/src/services.ts +32 -24
- package/tsconfig.json +1 -0
package/src/services.ts
CHANGED
|
@@ -2,13 +2,13 @@ import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata
|
|
|
2
2
|
import {Attribute, Attributes, Statement, StringMap} from './metadata';
|
|
3
3
|
import {SearchResult} from './search';
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface Filter {
|
|
6
6
|
fields?: string[];
|
|
7
7
|
sort?: string;
|
|
8
8
|
q?: string;
|
|
9
9
|
}
|
|
10
10
|
export class SqlLoader<T, ID> {
|
|
11
|
-
|
|
11
|
+
primaryKeys: Attribute[];
|
|
12
12
|
map?: StringMap;
|
|
13
13
|
attributes: Attributes;
|
|
14
14
|
bools?: Attribute[];
|
|
@@ -18,35 +18,42 @@ export class SqlLoader<T, ID> {
|
|
|
18
18
|
protected param: (i: number) => string,
|
|
19
19
|
protected fromDB?: (v: T) => T) {
|
|
20
20
|
if (Array.isArray(attrs)) {
|
|
21
|
-
this.
|
|
21
|
+
this.primaryKeys = attributes(attrs);
|
|
22
|
+
this.attributes = {} as any;
|
|
22
23
|
} else {
|
|
23
24
|
const m = metadata(attrs);
|
|
24
25
|
this.attributes = attrs;
|
|
25
|
-
this.
|
|
26
|
+
this.primaryKeys = m.keys;
|
|
26
27
|
this.map = m.map;
|
|
27
28
|
this.bools = m.bools;
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
+
if (this.metadata) {
|
|
31
|
+
this.metadata = this.metadata.bind(this);
|
|
32
|
+
}
|
|
30
33
|
this.all = this.all.bind(this);
|
|
31
34
|
this.load = this.load.bind(this);
|
|
32
35
|
this.exist = this.exist.bind(this);
|
|
33
36
|
}
|
|
34
|
-
metadata(): Attributes {
|
|
37
|
+
metadata?(): Attributes|undefined {
|
|
35
38
|
return this.attributes;
|
|
36
39
|
}
|
|
37
40
|
all(): Promise<T[]> {
|
|
38
41
|
const sql = `select * from ${this.table}`;
|
|
39
42
|
return this.query(sql, [], this.map);
|
|
40
43
|
}
|
|
41
|
-
load(id: ID, ctx?: any): Promise<T> {
|
|
42
|
-
const stmt = select<ID>(id, this.table, this.
|
|
43
|
-
if (
|
|
44
|
+
load(id: ID, ctx?: any): Promise<T|null> {
|
|
45
|
+
const stmt = select<ID>(id, this.table, this.primaryKeys, this.param);
|
|
46
|
+
if (!stmt) {
|
|
47
|
+
throw new Error('cannot build query by id');
|
|
48
|
+
}
|
|
49
|
+
const fn = this.fromDB;
|
|
50
|
+
if (fn) {
|
|
44
51
|
return this.query(stmt.query, stmt.params, this.map, ctx).then(res => {
|
|
45
52
|
if (!res || res.length === 0) {
|
|
46
53
|
return null;
|
|
47
54
|
} else {
|
|
48
55
|
const obj = res[0];
|
|
49
|
-
return
|
|
56
|
+
return fn(obj);
|
|
50
57
|
}
|
|
51
58
|
});
|
|
52
59
|
} else {
|
|
@@ -54,12 +61,15 @@ export class SqlLoader<T, ID> {
|
|
|
54
61
|
}
|
|
55
62
|
}
|
|
56
63
|
exist(id: ID, ctx?: any): Promise<boolean> {
|
|
57
|
-
const field = (this.
|
|
58
|
-
const stmt = exist<ID>(id, this.table, this.
|
|
64
|
+
const field = (this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name);
|
|
65
|
+
const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field);
|
|
66
|
+
if (!stmt) {
|
|
67
|
+
throw new Error('cannot build query by id');
|
|
68
|
+
}
|
|
59
69
|
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0) ? false : true);
|
|
60
70
|
}
|
|
61
71
|
}
|
|
62
|
-
export class SqlSearchLoader<T, ID, S extends
|
|
72
|
+
export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
|
|
63
73
|
constructor(
|
|
64
74
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
65
75
|
table: string,
|
|
@@ -77,7 +87,7 @@ export class SqlSearchLoader<T, ID, S extends SearchModel> extends SqlLoader<T,
|
|
|
77
87
|
export interface Manager {
|
|
78
88
|
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
79
89
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
80
|
-
query<T>(sql: string, args?: any[], m?: StringMap,
|
|
90
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
81
91
|
}
|
|
82
92
|
export function createSqlWriter<T, ID>(table: string,
|
|
83
93
|
manager: Manager,
|
|
@@ -85,7 +95,7 @@ export function createSqlWriter<T, ID>(table: string,
|
|
|
85
95
|
buildParam: (i: number) => string,
|
|
86
96
|
toDB?: (v: T) => T,
|
|
87
97
|
fromDB?: (v: T) => T) {
|
|
88
|
-
const writer = new SqlWriter<T, ID>(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB
|
|
98
|
+
const writer = new SqlWriter<T, ID>(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
|
|
89
99
|
return writer;
|
|
90
100
|
}
|
|
91
101
|
|
|
@@ -97,8 +107,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
97
107
|
attrs: Attributes,
|
|
98
108
|
buildParam: (i: number) => string,
|
|
99
109
|
protected toDB?: (v: T) => T,
|
|
100
|
-
fromDB?: (v: T) => T
|
|
101
|
-
public execBatch?: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>) {
|
|
110
|
+
fromDB?: (v: T) => T) {
|
|
102
111
|
super(table, query, attrs, buildParam, fromDB);
|
|
103
112
|
const x = version(attrs);
|
|
104
113
|
if (x) {
|
|
@@ -143,7 +152,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
143
152
|
return this.update(obj, ctx);
|
|
144
153
|
}
|
|
145
154
|
delete(id: ID, ctx?: any): Promise<number> {
|
|
146
|
-
const stmt = buildToDelete<ID>(id, this.table, this.
|
|
155
|
+
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param);
|
|
147
156
|
if (stmt) {
|
|
148
157
|
return this.exec(stmt.query, stmt.params, ctx);
|
|
149
158
|
} else {
|
|
@@ -151,7 +160,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
151
160
|
}
|
|
152
161
|
}
|
|
153
162
|
}
|
|
154
|
-
export class SqlSearchWriter<T, ID, S extends
|
|
163
|
+
export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
|
|
155
164
|
constructor(
|
|
156
165
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
157
166
|
table: string,
|
|
@@ -160,16 +169,15 @@ export class SqlSearchWriter<T, ID, S extends SearchModel> extends SqlWriter<T,
|
|
|
160
169
|
attrs: Attributes,
|
|
161
170
|
buildParam: (i: number) => string,
|
|
162
171
|
toDB?: (v: T) => T,
|
|
163
|
-
fromDB?: (v: T) => T
|
|
164
|
-
|
|
165
|
-
super(table, query, exec, attrs, buildParam, toDB, fromDB, execBatch);
|
|
172
|
+
fromDB?: (v: T) => T) {
|
|
173
|
+
super(table, query, exec, attrs, buildParam, toDB, fromDB);
|
|
166
174
|
this.search = this.search.bind(this);
|
|
167
175
|
}
|
|
168
176
|
search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
|
|
169
177
|
return this.find(s, limit, offset, fields);
|
|
170
178
|
}
|
|
171
179
|
}
|
|
172
|
-
export function createSqlSearchWriter<T, ID, S extends
|
|
180
|
+
export function createSqlSearchWriter<T, ID, S extends Filter>(
|
|
173
181
|
find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
174
182
|
table: string,
|
|
175
183
|
manager: Manager,
|
|
@@ -177,6 +185,6 @@ export function createSqlSearchWriter<T, ID, S extends SearchModel>(
|
|
|
177
185
|
buildParam: (i: number) => string,
|
|
178
186
|
toDB?: (v: T) => T,
|
|
179
187
|
fromDB?: (v: T) => T) {
|
|
180
|
-
const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB
|
|
188
|
+
const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
|
|
181
189
|
return writer;
|
|
182
190
|
}
|