query-core 0.1.6 → 0.1.10

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/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 SearchModel {
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
- keys: Attribute[];
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.keys = attributes(attrs);
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.keys = m.keys;
26
+ this.primaryKeys = m.keys;
26
27
  this.map = m.map;
27
28
  this.bools = m.bools;
28
29
  }
29
- this.metadata = this.metadata.bind(this);
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.keys, this.param);
43
- if (this.fromDB) {
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 this.fromDB(obj);
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.keys[0].field ? this.keys[0].field : this.keys[0].name);
58
- const stmt = exist<ID>(id, this.table, this.keys, this.param, field);
64
+ const field = (this.primaryKeys[0].field ? this.primaryKeys[0].field : 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 SearchModel> extends SqlLoader<T, ID> {
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,
@@ -68,6 +78,7 @@ export class SqlSearchLoader<T, ID, S extends SearchModel> extends SqlLoader<T,
68
78
  param: (i: number) => string,
69
79
  fromDB?: (v: T) => T) {
70
80
  super(table, query, attrs, param, fromDB);
81
+ this.search = this.search.bind(this);
71
82
  }
72
83
  search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
73
84
  return this.find(s, limit, offset, fields);
@@ -76,7 +87,7 @@ export class SqlSearchLoader<T, ID, S extends SearchModel> extends SqlLoader<T,
76
87
  export interface Manager {
77
88
  exec(sql: string, args?: any[], ctx?: any): Promise<number>;
78
89
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
79
- query<T>(sql: string, args?: any[], m?: StringMap, fields?: string[], ctx?: any): Promise<T[]>;
90
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
80
91
  }
81
92
  export function createSqlWriter<T, ID>(table: string,
82
93
  manager: Manager,
@@ -84,7 +95,7 @@ export function createSqlWriter<T, ID>(table: string,
84
95
  buildParam: (i: number) => string,
85
96
  toDB?: (v: T) => T,
86
97
  fromDB?: (v: T) => T) {
87
- const writer = new SqlWriter<T, ID>(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
98
+ const writer = new SqlWriter<T, ID>(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
88
99
  return writer;
89
100
  }
90
101
 
@@ -96,8 +107,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
96
107
  attrs: Attributes,
97
108
  buildParam: (i: number) => string,
98
109
  protected toDB?: (v: T) => T,
99
- fromDB?: (v: T) => T,
100
- public execBatch?: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>) {
110
+ fromDB?: (v: T) => T) {
101
111
  super(table, query, attrs, buildParam, fromDB);
102
112
  const x = version(attrs);
103
113
  if (x) {
@@ -142,7 +152,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
142
152
  return this.update(obj, ctx);
143
153
  }
144
154
  delete(id: ID, ctx?: any): Promise<number> {
145
- const stmt = buildToDelete<ID>(id, this.table, this.keys, this.param);
155
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param);
146
156
  if (stmt) {
147
157
  return this.exec(stmt.query, stmt.params, ctx);
148
158
  } else {
@@ -150,7 +160,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
150
160
  }
151
161
  }
152
162
  }
153
- export class SqlSearchWriter<T, ID, S extends SearchModel> extends SqlWriter<T, ID> {
163
+ export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
154
164
  constructor(
155
165
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
156
166
  table: string,
@@ -159,15 +169,15 @@ export class SqlSearchWriter<T, ID, S extends SearchModel> extends SqlWriter<T,
159
169
  attrs: Attributes,
160
170
  buildParam: (i: number) => string,
161
171
  toDB?: (v: T) => T,
162
- fromDB?: (v: T) => T,
163
- execBatch?: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>) {
164
- super(table, query, exec, attrs, buildParam, toDB, fromDB, execBatch);
172
+ fromDB?: (v: T) => T) {
173
+ super(table, query, exec, attrs, buildParam, toDB, fromDB);
174
+ this.search = this.search.bind(this);
165
175
  }
166
176
  search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
167
177
  return this.find(s, limit, offset, fields);
168
178
  }
169
179
  }
170
- export function createSqlSearchWriter<T, ID, S extends SearchModel>(
180
+ export function createSqlSearchWriter<T, ID, S extends Filter>(
171
181
  find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
172
182
  table: string,
173
183
  manager: Manager,
@@ -175,6 +185,6 @@ export function createSqlSearchWriter<T, ID, S extends SearchModel>(
175
185
  buildParam: (i: number) => string,
176
186
  toDB?: (v: T) => T,
177
187
  fromDB?: (v: T) => T) {
178
- const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
188
+ const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
179
189
  return writer;
180
190
  }
package/tsconfig.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "outDir": "./lib",
7
7
  "module": "commonjs",
8
8
  "moduleResolution": "node",
9
+ "strict": true,
9
10
  "pretty": true,
10
11
  "sourceMap": false,
11
12
  "declaration": false,