query-core 0.1.8 → 0.1.12

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,7 +2,7 @@ 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;
@@ -15,10 +15,11 @@ export class SqlLoader<T, ID> {
15
15
  constructor(public table: string,
16
16
  public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<T[]>,
17
17
  attrs: Attributes|string[],
18
- protected param: (i: number) => string,
19
- protected fromDB?: (v: T) => T) {
18
+ public param: (i: number) => string,
19
+ public fromDB?: (v: T) => T) {
20
20
  if (Array.isArray(attrs)) {
21
21
  this.primaryKeys = attributes(attrs);
22
+ this.attributes = {} as any;
22
23
  } else {
23
24
  const m = metadata(attrs);
24
25
  this.attributes = attrs;
@@ -26,27 +27,33 @@ export class SqlLoader<T, ID> {
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> {
44
+ load(id: ID, ctx?: any): Promise<T|null> {
42
45
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param);
43
- if (this.fromDB) {
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.primaryKeys[0].field ? this.primaryKeys[0].field : this.primaryKeys[0].name);
64
+ const field = (this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name);
58
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,
@@ -75,32 +85,23 @@ export class SqlSearchLoader<T, ID, S extends SearchModel> extends SqlLoader<T,
75
85
  }
76
86
  }
77
87
  export interface Manager {
88
+ param(i: number): string;
78
89
  exec(sql: string, args?: any[], ctx?: any): Promise<number>;
79
90
  execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
80
- query<T>(sql: string, args?: any[], m?: StringMap, fields?: string[], ctx?: any): Promise<T[]>;
81
- }
82
- export function createSqlWriter<T, ID>(table: string,
83
- manager: Manager,
84
- attrs: Attributes,
85
- buildParam: (i: number) => string,
86
- toDB?: (v: T) => T,
87
- fromDB?: (v: T) => T) {
88
- const writer = new SqlWriter<T, ID>(table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
89
- return writer;
91
+ query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
90
92
  }
91
-
92
93
  export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
93
94
  version?: string;
94
- constructor(table: string,
95
- query: (sql: string, args?: any[], m?: StringMap, ctx?: any) => Promise<T[]>,
96
- public exec: (sql: string, args?: any[], ctx?: any) => Promise<number>,
95
+ exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
96
+ execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>;
97
+ constructor(manager: Manager, table: string,
97
98
  attrs: Attributes,
98
- buildParam: (i: number) => string,
99
- protected toDB?: (v: T) => T,
100
- fromDB?: (v: T) => T,
101
- public execBatch?: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>) {
102
- super(table, query, attrs, buildParam, fromDB);
99
+ public toDB?: (v: T) => T,
100
+ fromDB?: (v: T) => T) {
101
+ super(table, manager.query, attrs, manager.param, fromDB);
103
102
  const x = version(attrs);
103
+ this.exec = manager.exec;
104
+ this.execBatch = manager.execBatch;
104
105
  if (x) {
105
106
  this.version = x.name;
106
107
  }
@@ -151,32 +152,18 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
151
152
  }
152
153
  }
153
154
  }
154
- export class SqlSearchWriter<T, ID, S extends SearchModel> extends SqlWriter<T, ID> {
155
+ export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
155
156
  constructor(
156
157
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
158
+ manager: Manager,
157
159
  table: string,
158
- query: (sql: string, args?: any[], m?: StringMap, ctx?: any) => Promise<T[]>,
159
- exec: (sql: string, args?: any[], ctx?: any) => Promise<number>,
160
160
  attrs: Attributes,
161
- buildParam: (i: number) => string,
162
161
  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);
162
+ fromDB?: (v: T) => T) {
163
+ super(manager, table, attrs, toDB, fromDB);
166
164
  this.search = this.search.bind(this);
167
165
  }
168
166
  search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>> {
169
167
  return this.find(s, limit, offset, fields);
170
168
  }
171
169
  }
172
- export function createSqlSearchWriter<T, ID, S extends SearchModel>(
173
- find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
174
- table: string,
175
- manager: Manager,
176
- attrs: Attributes,
177
- buildParam: (i: number) => string,
178
- toDB?: (v: T) => T,
179
- fromDB?: (v: T) => T) {
180
- const writer = new SqlSearchWriter<T, ID, S>(find, table, manager.query, manager.exec, attrs, buildParam, toDB, fromDB, manager.execBatch);
181
- return writer;
182
- }
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,