query-core 0.3.0 → 0.5.1
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 +5 -5
- package/lib/query.js +11 -25
- package/lib/services.js +3 -3
- package/package.json +1 -1
- package/src/SearchBuilder.ts +6 -8
- package/src/metadata.ts +2 -2
- package/src/query.ts +11 -22
- package/src/services.ts +7 -214
package/lib/SearchBuilder.js
CHANGED
|
@@ -99,11 +99,11 @@ var SearchBuilder = (function () {
|
|
|
99
99
|
exports.SearchBuilder = SearchBuilder;
|
|
100
100
|
var SqlSearchWriter = (function (_super) {
|
|
101
101
|
__extends(SqlSearchWriter, _super);
|
|
102
|
-
function SqlSearchWriter(
|
|
103
|
-
var _this = _super.call(this,
|
|
102
|
+
function SqlSearchWriter(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
103
|
+
var _this = _super.call(this, db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
|
|
104
104
|
_this.attributes = attributes;
|
|
105
105
|
_this.toDB = toDB;
|
|
106
|
-
_this.exec =
|
|
106
|
+
_this.exec = db.exec;
|
|
107
107
|
var x = build_1.version(attributes);
|
|
108
108
|
if (x) {
|
|
109
109
|
_this.version = x.name;
|
|
@@ -154,8 +154,8 @@ var SqlSearchWriter = (function (_super) {
|
|
|
154
154
|
exports.SqlSearchWriter = SqlSearchWriter;
|
|
155
155
|
var SqlRepository = (function (_super) {
|
|
156
156
|
__extends(SqlRepository, _super);
|
|
157
|
-
function SqlRepository(
|
|
158
|
-
var _this = _super.call(this,
|
|
157
|
+
function SqlRepository(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) {
|
|
158
|
+
var _this = _super.call(this, db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total) || this;
|
|
159
159
|
_this.attributes = attributes;
|
|
160
160
|
_this.toDB = toDB;
|
|
161
161
|
_this.metadata = _this.metadata.bind(_this);
|
package/lib/query.js
CHANGED
|
@@ -99,37 +99,23 @@ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, s
|
|
|
99
99
|
if (attr.q) {
|
|
100
100
|
ex.push(key);
|
|
101
101
|
}
|
|
102
|
-
if (attr.
|
|
102
|
+
if (attr.operator === "=") {
|
|
103
103
|
filters.push(field + " = " + param(i++));
|
|
104
104
|
args.push(v);
|
|
105
105
|
}
|
|
106
|
-
else if (attr.
|
|
106
|
+
else if (attr.operator === "like") {
|
|
107
107
|
filters.push(field + " " + like + " " + param(i++));
|
|
108
|
-
args.push(v + "%");
|
|
108
|
+
args.push("%" + v + "%");
|
|
109
109
|
}
|
|
110
110
|
else {
|
|
111
111
|
filters.push(field + " " + like + " " + param(i++));
|
|
112
|
-
args.push(
|
|
112
|
+
args.push(v + "%");
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
|
-
else if (v instanceof Date) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
args.push(v);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
filters.push(field + " >= " + param(i++));
|
|
123
|
-
args.push(v);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else if (typeof v === "number") {
|
|
127
|
-
if (attr.match === "max") {
|
|
128
|
-
filters.push(field + " <= " + v);
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
filters.push(field + " >= " + v);
|
|
132
|
-
}
|
|
116
|
+
else if (typeof v === "number" || v instanceof Date) {
|
|
117
|
+
var operator = attr.operator ? attr.operator : ">=";
|
|
118
|
+
filters.push(field + " " + operator + " " + param(i++));
|
|
133
119
|
}
|
|
134
120
|
else if (attr.type === "ObjectId") {
|
|
135
121
|
filters.push(field + " = " + param(i++));
|
|
@@ -236,17 +222,17 @@ function buildQuery(filter, param, sort, buildSort3, attrs, table, fields, sq, s
|
|
|
236
222
|
var attr = attrs[field];
|
|
237
223
|
if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
|
|
238
224
|
var column = attr.column ? attr.column : field;
|
|
239
|
-
if (attr.
|
|
225
|
+
if (attr.operator === "=") {
|
|
240
226
|
qfilters.push(column + " = " + param(i++));
|
|
241
227
|
args.push(q);
|
|
242
228
|
}
|
|
243
|
-
else if (attr.
|
|
229
|
+
else if (attr.operator === "like") {
|
|
244
230
|
qfilters.push(column + " " + like + " " + param(i++));
|
|
245
|
-
args.push(q + "%");
|
|
231
|
+
args.push("%" + q + "%");
|
|
246
232
|
}
|
|
247
233
|
else {
|
|
248
234
|
qfilters.push(column + " " + like + " " + param(i++));
|
|
249
|
-
args.push(
|
|
235
|
+
args.push(q + "%");
|
|
250
236
|
}
|
|
251
237
|
}
|
|
252
238
|
}
|
package/lib/services.js
CHANGED
|
@@ -461,10 +461,10 @@ var SqlWriter = (function () {
|
|
|
461
461
|
exports.SqlWriter = SqlWriter;
|
|
462
462
|
var CRUDRepository = (function (_super) {
|
|
463
463
|
__extends(CRUDRepository, _super);
|
|
464
|
-
function CRUDRepository(
|
|
465
|
-
var _this = _super.call(this,
|
|
464
|
+
function CRUDRepository(db, table, attributes, toDB, fromDB) {
|
|
465
|
+
var _this = _super.call(this, db.exec, db.param, table, attributes, toDB) || this;
|
|
466
466
|
_this.fromDB = fromDB;
|
|
467
|
-
_this.query =
|
|
467
|
+
_this.query = db.query;
|
|
468
468
|
var m = build_1.metadata(attributes);
|
|
469
469
|
_this.primaryKeys = m.keys;
|
|
470
470
|
_this.map = m.map;
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Manager } from "./services"
|
|
1
|
+
import { DB, Manager } from "./services"
|
|
2
2
|
import { buildToDelete, buildToInsert, buildToUpdate, exist, metadata, param, select, version } from "./build"
|
|
3
3
|
import { Attribute, Attributes, Statement, StringMap } from "./metadata"
|
|
4
4
|
import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
|
|
@@ -119,10 +119,9 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
|
119
119
|
protected version?: string
|
|
120
120
|
protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
|
|
121
121
|
constructor(
|
|
122
|
-
|
|
122
|
+
db: DB,
|
|
123
123
|
table: string,
|
|
124
124
|
protected attributes: Attributes,
|
|
125
|
-
provider?: string,
|
|
126
125
|
buildQ?: (
|
|
127
126
|
s: S,
|
|
128
127
|
param: (i: number) => string,
|
|
@@ -144,8 +143,8 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
|
144
143
|
buildParam?: (i: number) => string,
|
|
145
144
|
total?: string,
|
|
146
145
|
) {
|
|
147
|
-
super(
|
|
148
|
-
this.exec =
|
|
146
|
+
super(db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
|
|
147
|
+
this.exec = db.exec
|
|
149
148
|
const x = version(attributes)
|
|
150
149
|
if (x) {
|
|
151
150
|
this.version = x.name
|
|
@@ -191,10 +190,9 @@ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
|
|
|
191
190
|
// tslint:disable-next-line:max-classes-per-file
|
|
192
191
|
export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
193
192
|
constructor(
|
|
194
|
-
|
|
193
|
+
db: DB,
|
|
195
194
|
table: string,
|
|
196
195
|
protected attributes: Attributes,
|
|
197
|
-
provider?: string,
|
|
198
196
|
buildQ?: (
|
|
199
197
|
s: S,
|
|
200
198
|
param: (i: number) => string,
|
|
@@ -216,7 +214,7 @@ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
|
|
|
216
214
|
buildParam?: (i: number) => string,
|
|
217
215
|
total?: string,
|
|
218
216
|
) {
|
|
219
|
-
super(
|
|
217
|
+
super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
|
|
220
218
|
this.metadata = this.metadata.bind(this)
|
|
221
219
|
this.all = this.all.bind(this)
|
|
222
220
|
this.load = this.load.bind(this)
|
package/src/metadata.ts
CHANGED
|
@@ -40,7 +40,7 @@ export type DataType =
|
|
|
40
40
|
| "datetimes"
|
|
41
41
|
| "times"
|
|
42
42
|
export type FormatType = "currency" | "percentage" | "email" | "url" | "phone" | "fax" | "ipv4" | "ipv6"
|
|
43
|
-
export type
|
|
43
|
+
export type Operator = "=" | "like" | "!=" | "<>" | ">" | ">=" | "<" | "<="
|
|
44
44
|
|
|
45
45
|
export interface Model {
|
|
46
46
|
name?: string
|
|
@@ -55,7 +55,7 @@ export interface Attribute {
|
|
|
55
55
|
name?: string
|
|
56
56
|
column?: string
|
|
57
57
|
type?: DataType
|
|
58
|
-
|
|
58
|
+
operator?: Operator
|
|
59
59
|
default?: string | number | Date | boolean
|
|
60
60
|
key?: boolean
|
|
61
61
|
q?: boolean
|
package/src/query.ts
CHANGED
|
@@ -106,31 +106,20 @@ export function buildQuery<S>(
|
|
|
106
106
|
if (attr.q) {
|
|
107
107
|
ex.push(key)
|
|
108
108
|
}
|
|
109
|
-
if (attr.
|
|
109
|
+
if (attr.operator === "=") {
|
|
110
110
|
filters.push(`${field} = ${param(i++)}`)
|
|
111
111
|
args.push(v)
|
|
112
|
-
} else if (attr.
|
|
112
|
+
} else if (attr.operator === "like") {
|
|
113
113
|
filters.push(`${field} ${like} ${param(i++)}`)
|
|
114
|
-
args.push(v + "%")
|
|
114
|
+
args.push("%" + v + "%")
|
|
115
115
|
} else {
|
|
116
116
|
filters.push(`${field} ${like} ${param(i++)}`)
|
|
117
|
-
args.push(
|
|
117
|
+
args.push(v + "%")
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
} else if (v instanceof Date) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
args.push(v)
|
|
124
|
-
} else {
|
|
125
|
-
filters.push(`${field} >= ${param(i++)}`)
|
|
126
|
-
args.push(v)
|
|
127
|
-
}
|
|
128
|
-
} else if (typeof v === "number") {
|
|
129
|
-
if (attr.match === "max") {
|
|
130
|
-
filters.push(`${field} <= ${v}`)
|
|
131
|
-
} else {
|
|
132
|
-
filters.push(`${field} >= ${v}`)
|
|
133
|
-
}
|
|
120
|
+
} else if (typeof v === "number" || v instanceof Date) {
|
|
121
|
+
const operator = attr.operator ? attr.operator : ">="
|
|
122
|
+
filters.push(`${field} ${operator} ${param(i++)}`)
|
|
134
123
|
} else if (attr.type === "ObjectId") {
|
|
135
124
|
filters.push(`${field} = ${param(i++)}`)
|
|
136
125
|
args.push(v)
|
|
@@ -219,15 +208,15 @@ export function buildQuery<S>(
|
|
|
219
208
|
const attr = attrs[field]
|
|
220
209
|
if (attr.q && (attr.type === undefined || attr.type === "string") && !ex.includes(field)) {
|
|
221
210
|
const column = attr.column ? attr.column : field
|
|
222
|
-
if (attr.
|
|
211
|
+
if (attr.operator === "=") {
|
|
223
212
|
qfilters.push(`${column} = ${param(i++)}`)
|
|
224
213
|
args.push(q)
|
|
225
|
-
} else if (attr.
|
|
214
|
+
} else if (attr.operator === "like") {
|
|
226
215
|
qfilters.push(`${column} ${like} ${param(i++)}`)
|
|
227
|
-
args.push(q + "%")
|
|
216
|
+
args.push("%" + q + "%")
|
|
228
217
|
} else {
|
|
229
218
|
qfilters.push(`${column} ${like} ${param(i++)}`)
|
|
230
|
-
args.push(
|
|
219
|
+
args.push(q + "%")
|
|
231
220
|
}
|
|
232
221
|
}
|
|
233
222
|
}
|
package/src/services.ts
CHANGED
|
@@ -112,190 +112,16 @@ export class QueryRepository<T, ID> {
|
|
|
112
112
|
return this.db.query<T>(sql, ids, this.map, this.bools)
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
/*
|
|
116
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
117
|
-
export class SqlLoadRepository<T, K1, K2> {
|
|
118
|
-
map?: StringMap
|
|
119
|
-
attributes: Attributes
|
|
120
|
-
bools?: Attribute[]
|
|
121
|
-
id1Col: string
|
|
122
|
-
id2Col: string
|
|
123
|
-
constructor(
|
|
124
|
-
public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
125
|
-
public table: string,
|
|
126
|
-
attrs: Attributes,
|
|
127
|
-
public param: (i: number) => string,
|
|
128
|
-
public id1Field: string,
|
|
129
|
-
public id2Field: string,
|
|
130
|
-
public fromDB?: (v: T) => T,
|
|
131
|
-
id1Col?: string,
|
|
132
|
-
id2Col?: string,
|
|
133
|
-
) {
|
|
134
|
-
const m = metadata(attrs)
|
|
135
|
-
this.attributes = attrs
|
|
136
|
-
this.map = m.map
|
|
137
|
-
this.bools = m.bools
|
|
138
115
|
|
|
139
|
-
|
|
140
|
-
this.metadata = this.metadata.bind(this)
|
|
141
|
-
}
|
|
142
|
-
this.all = this.all.bind(this)
|
|
143
|
-
this.load = this.load.bind(this)
|
|
144
|
-
this.exist = this.exist.bind(this)
|
|
145
|
-
if (id1Col && id1Col.length > 0) {
|
|
146
|
-
this.id1Col = id1Col
|
|
147
|
-
} else {
|
|
148
|
-
const c = attrs[this.id1Field]
|
|
149
|
-
if (c) {
|
|
150
|
-
this.id1Col = c.column && c.column.length > 0 ? c.column : this.id1Field
|
|
151
|
-
} else {
|
|
152
|
-
this.id1Col = this.id1Field
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (id2Col && id2Col.length > 0) {
|
|
156
|
-
this.id2Col = id2Col
|
|
157
|
-
} else {
|
|
158
|
-
const c = attrs[this.id2Field]
|
|
159
|
-
if (c) {
|
|
160
|
-
this.id2Col = c.column && c.column.length > 0 ? c.column : this.id2Field
|
|
161
|
-
} else {
|
|
162
|
-
this.id2Col = this.id2Field
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
metadata?(): Attributes | undefined {
|
|
167
|
-
return this.attributes
|
|
168
|
-
}
|
|
169
|
-
all(): Promise<T[]> {
|
|
170
|
-
const sql = `select * from ${this.table}`
|
|
171
|
-
return this.query(sql, [], this.map)
|
|
172
|
-
}
|
|
173
|
-
load(id1: K1, id2: K2, ctx?: any): Promise<T | null> {
|
|
174
|
-
return this.query<T>(
|
|
175
|
-
`select * from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`,
|
|
176
|
-
[id1, id2],
|
|
177
|
-
this.map,
|
|
178
|
-
undefined,
|
|
179
|
-
ctx,
|
|
180
|
-
).then((objs) => {
|
|
181
|
-
if (!objs || objs.length === 0) {
|
|
182
|
-
return null
|
|
183
|
-
} else {
|
|
184
|
-
const fn = this.fromDB
|
|
185
|
-
if (fn) {
|
|
186
|
-
return fn(objs[0])
|
|
187
|
-
} else {
|
|
188
|
-
return objs[0]
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
}
|
|
193
|
-
exist(id1: K1, id2: K2, ctx?: any): Promise<boolean> {
|
|
194
|
-
return this.query<T>(
|
|
195
|
-
`select ${this.id1Col} from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`,
|
|
196
|
-
[id1, id2],
|
|
197
|
-
undefined,
|
|
198
|
-
undefined,
|
|
199
|
-
ctx,
|
|
200
|
-
).then((objs) => {
|
|
201
|
-
return objs && objs.length > 0 ? true : false
|
|
202
|
-
})
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
206
|
-
export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
|
|
207
|
-
version?: string
|
|
208
|
-
exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
|
|
209
|
-
// execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>
|
|
210
|
-
constructor(
|
|
211
|
-
manager: Manager,
|
|
212
|
-
table: string,
|
|
213
|
-
attrs: Attributes,
|
|
214
|
-
id1Field: string,
|
|
215
|
-
id2Field: string,
|
|
216
|
-
public toDB?: (v: T) => T,
|
|
217
|
-
fromDB?: (v: T) => T,
|
|
218
|
-
id1Col?: string,
|
|
219
|
-
id2Col?: string,
|
|
220
|
-
) {
|
|
221
|
-
super(manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col)
|
|
222
|
-
const x = version(attrs)
|
|
223
|
-
this.exec = manager.exec
|
|
224
|
-
// this.execBatch = manager.execBatch
|
|
225
|
-
if (x) {
|
|
226
|
-
this.version = x.name
|
|
227
|
-
}
|
|
228
|
-
this.create = this.create.bind(this)
|
|
229
|
-
this.update = this.update.bind(this)
|
|
230
|
-
this.patch = this.patch.bind(this)
|
|
231
|
-
this.delete = this.delete.bind(this)
|
|
232
|
-
}
|
|
233
|
-
create(obj: T, ctx?: any): Promise<number> {
|
|
234
|
-
let obj2 = obj
|
|
235
|
-
if (this.toDB) {
|
|
236
|
-
obj2 = this.toDB(obj)
|
|
237
|
-
}
|
|
238
|
-
const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
|
|
239
|
-
if (stmt) {
|
|
240
|
-
return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
|
|
241
|
-
if (err && err.error === "duplicate") {
|
|
242
|
-
return 0
|
|
243
|
-
} else {
|
|
244
|
-
throw err
|
|
245
|
-
}
|
|
246
|
-
})
|
|
247
|
-
} else {
|
|
248
|
-
return Promise.resolve(0)
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
update(obj: T, ctx?: any): Promise<number> {
|
|
252
|
-
let obj2 = obj
|
|
253
|
-
if (this.toDB) {
|
|
254
|
-
obj2 = this.toDB(obj)
|
|
255
|
-
}
|
|
256
|
-
const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
|
|
257
|
-
if (stmt) {
|
|
258
|
-
return this.exec(stmt.query, stmt.params, ctx)
|
|
259
|
-
} else {
|
|
260
|
-
return Promise.resolve(0)
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
patch(obj: Partial<T>, ctx?: any): Promise<number> {
|
|
264
|
-
return this.update(obj as any, ctx)
|
|
265
|
-
}
|
|
266
|
-
delete(id1: K1, id2: K2, ctx?: any): Promise<number> {
|
|
267
|
-
return this.exec(`delete from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], ctx)
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
*/
|
|
271
|
-
/*
|
|
272
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
273
|
-
export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
|
|
274
|
-
constructor(
|
|
275
|
-
protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
276
|
-
query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
277
|
-
table: string,
|
|
278
|
-
attrs: Attributes | string[],
|
|
279
|
-
param: (i: number) => string,
|
|
280
|
-
fromDB?: (v: T) => T,
|
|
281
|
-
) {
|
|
282
|
-
super(query, table, attrs, param, fromDB)
|
|
283
|
-
this.search = this.search.bind(this)
|
|
284
|
-
}
|
|
285
|
-
search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
|
|
286
|
-
return this.find(s, limit, offset, fields)
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
*/
|
|
290
|
-
export interface Manager {
|
|
116
|
+
export interface DB {
|
|
291
117
|
driver: string
|
|
292
118
|
param(i: number): string
|
|
293
119
|
exec(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
294
120
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>
|
|
295
121
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
|
|
296
122
|
}
|
|
297
|
-
export type
|
|
298
|
-
export interface
|
|
123
|
+
export type Manager = DB
|
|
124
|
+
export interface FullDB {
|
|
299
125
|
driver: string
|
|
300
126
|
param(i: number): string
|
|
301
127
|
exec(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
@@ -305,6 +131,7 @@ export interface ExtManager {
|
|
|
305
131
|
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>
|
|
306
132
|
count(sql: string, args?: any[], ctx?: any): Promise<number>
|
|
307
133
|
}
|
|
134
|
+
export type ExtManager = FullDB
|
|
308
135
|
export interface SimpleMap {
|
|
309
136
|
[key: string]: string | number | boolean | Date
|
|
310
137
|
}
|
|
@@ -632,18 +459,14 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
|
632
459
|
// tslint:disable-next-line:max-classes-per-file
|
|
633
460
|
export class SqlWriter<T> {
|
|
634
461
|
protected version?: string
|
|
635
|
-
// execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>
|
|
636
462
|
constructor(protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>, protected param: (i: number) => string, protected table: string, protected attributes: Attributes, public toDB?: (v: T) => T) {
|
|
637
|
-
// super(manager.query, table, attrs, manager.param, fromDB)
|
|
638
463
|
const x = version(attributes)
|
|
639
|
-
// this.execBatch = manager.execBatch
|
|
640
464
|
if (x) {
|
|
641
465
|
this.version = x.name
|
|
642
466
|
}
|
|
643
467
|
this.create = this.create.bind(this)
|
|
644
468
|
this.update = this.update.bind(this)
|
|
645
469
|
this.patch = this.patch.bind(this)
|
|
646
|
-
// this.delete = this.delete.bind(this)
|
|
647
470
|
}
|
|
648
471
|
create(obj: T, ctx?: any): Promise<number> {
|
|
649
472
|
let obj2 = obj
|
|
@@ -678,26 +501,15 @@ export class SqlWriter<T> {
|
|
|
678
501
|
patch(obj: Partial<T>, ctx?: any): Promise<number> {
|
|
679
502
|
return this.update(obj as any, ctx)
|
|
680
503
|
}
|
|
681
|
-
/*
|
|
682
|
-
delete(id: ID, ctx?: any): Promise<number> {
|
|
683
|
-
const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
|
|
684
|
-
if (stmt) {
|
|
685
|
-
return this.exec(stmt.query, stmt.params, ctx)
|
|
686
|
-
} else {
|
|
687
|
-
return Promise.resolve(0)
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
*/
|
|
691
504
|
}
|
|
692
505
|
export class CRUDRepository<T, ID> extends SqlWriter<T> {
|
|
693
506
|
protected primaryKeys: Attribute[]
|
|
694
507
|
protected map?: StringMap
|
|
695
|
-
// attributes: Attributes;
|
|
696
508
|
protected bools?: Attribute[]
|
|
697
509
|
protected query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>
|
|
698
|
-
constructor(
|
|
699
|
-
super(
|
|
700
|
-
this.query =
|
|
510
|
+
constructor(db: DB, table: string, attributes: Attributes, toDB?: (v: T) => T, protected fromDB?: (v: T) => T) {
|
|
511
|
+
super(db.exec, db.param, table, attributes, toDB)
|
|
512
|
+
this.query = db.query
|
|
701
513
|
const m = metadata(attributes)
|
|
702
514
|
this.primaryKeys = m.keys
|
|
703
515
|
this.map = m.map
|
|
@@ -751,22 +563,3 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
|
|
|
751
563
|
}
|
|
752
564
|
}
|
|
753
565
|
}
|
|
754
|
-
/*
|
|
755
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
756
|
-
export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
|
|
757
|
-
constructor(
|
|
758
|
-
protected find: (s: S, limit: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
759
|
-
manager: Manager,
|
|
760
|
-
table: string,
|
|
761
|
-
attrs: Attributes,
|
|
762
|
-
toDB?: (v: T) => T,
|
|
763
|
-
fromDB?: (v: T) => T,
|
|
764
|
-
) {
|
|
765
|
-
super(manager, table, attrs, toDB, fromDB)
|
|
766
|
-
this.search = this.search.bind(this)
|
|
767
|
-
}
|
|
768
|
-
search(s: S, limit: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>> {
|
|
769
|
-
return this.find(s, limit, offset, fields)
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
*/
|