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/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].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 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,
@@ -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, fields?: string[], ctx?: any): Promise<T[]>;
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, manager.execBatch);
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.keys, this.param);
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 SearchModel> extends SqlWriter<T, ID> {
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
- execBatch?: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>) {
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 SearchModel>(
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, manager.execBatch);
188
+ const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB);
181
189
  return writer;
182
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,