query-core 0.1.5 → 0.1.9

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