query-core 0.1.22 → 0.1.25

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/index.js CHANGED
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var health_1 = require("./health");
7
7
  exports.SqlChecker = health_1.Checker;
8
8
  var services_1 = require("./services");
9
- exports.SqlLoadRepository = services_1.SqlLoader;
10
9
  exports.SqlViewRepository = services_1.SqlLoader;
11
10
  exports.SqlLoadService = services_1.SqlLoader;
12
11
  exports.SqlViewService = services_1.SqlLoader;
package/lib/services.js CHANGED
@@ -83,6 +83,170 @@ var SqlLoader = (function () {
83
83
  return SqlLoader;
84
84
  }());
85
85
  exports.SqlLoader = SqlLoader;
86
+ var QueryRepository = (function () {
87
+ function QueryRepository(db, table, attrs, sort, id) {
88
+ this.db = db;
89
+ this.table = table;
90
+ this.attrs = attrs;
91
+ this.sort = sort;
92
+ this.id = (id && id.length > 0 ? id : 'id');
93
+ this.query = this.query.bind(this);
94
+ var m = build_1.metadata(attrs);
95
+ this.map = m.map;
96
+ this.bools = m.bools;
97
+ }
98
+ QueryRepository.prototype.query = function (ids) {
99
+ if (!ids || ids.length === 0) {
100
+ return Promise.resolve([]);
101
+ }
102
+ var ps = [];
103
+ var length = ids.length;
104
+ for (var i = 1; i <= length; i++) {
105
+ ps.push(this.db.param(i));
106
+ }
107
+ var sql = "select * from " + this.table + " where " + this.id + " in (" + ps.join(',') + ")";
108
+ if (this.sort && this.sort.length > 0) {
109
+ sql = sql + ' order by ' + this.sort;
110
+ }
111
+ return this.db.query(sql, ids, this.map, this.bools);
112
+ };
113
+ return QueryRepository;
114
+ }());
115
+ exports.QueryRepository = QueryRepository;
116
+ var SqlLoadRepository = (function () {
117
+ function SqlLoadRepository(query, table, attrs, param, id1Field, id2Field, fromDB, id1Col, id2Col) {
118
+ this.query = query;
119
+ this.table = table;
120
+ this.param = param;
121
+ this.id1Field = id1Field;
122
+ this.id2Field = id2Field;
123
+ this.fromDB = fromDB;
124
+ var m = build_1.metadata(attrs);
125
+ this.attributes = attrs;
126
+ this.map = m.map;
127
+ this.bools = m.bools;
128
+ if (this.metadata) {
129
+ this.metadata = this.metadata.bind(this);
130
+ }
131
+ this.all = this.all.bind(this);
132
+ this.load = this.load.bind(this);
133
+ this.exist = this.exist.bind(this);
134
+ if (id1Col && id1Col.length > 0) {
135
+ this.id1Col = id1Col;
136
+ }
137
+ else {
138
+ var c = attrs[this.id1Field];
139
+ if (c) {
140
+ this.id1Col = (c.column && c.column.length > 0 ? c.column : this.id1Field);
141
+ }
142
+ else {
143
+ this.id1Col = this.id1Field;
144
+ }
145
+ }
146
+ if (id2Col && id2Col.length > 0) {
147
+ this.id2Col = id2Col;
148
+ }
149
+ else {
150
+ var c = attrs[this.id2Field];
151
+ if (c) {
152
+ this.id2Col = (c.column && c.column.length > 0 ? c.column : this.id2Field);
153
+ }
154
+ else {
155
+ this.id2Col = this.id2Field;
156
+ }
157
+ }
158
+ }
159
+ SqlLoadRepository.prototype.metadata = function () {
160
+ return this.attributes;
161
+ };
162
+ SqlLoadRepository.prototype.all = function () {
163
+ var sql = "select * from " + this.table;
164
+ return this.query(sql, [], this.map);
165
+ };
166
+ SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
167
+ var _this = this;
168
+ return this.query("select * from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], this.map, undefined, ctx).then(function (objs) {
169
+ if (!objs || objs.length === 0) {
170
+ return null;
171
+ }
172
+ else {
173
+ var fn = _this.fromDB;
174
+ if (fn) {
175
+ return fn(objs[0]);
176
+ }
177
+ else {
178
+ return objs[0];
179
+ }
180
+ }
181
+ });
182
+ };
183
+ SqlLoadRepository.prototype.exist = function (id1, id2, ctx) {
184
+ return this.query("select " + this.id1Col + " from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], undefined, undefined, ctx).then(function (objs) {
185
+ return (objs && objs.length > 0 ? true : false);
186
+ });
187
+ };
188
+ return SqlLoadRepository;
189
+ }());
190
+ exports.SqlLoadRepository = SqlLoadRepository;
191
+ var GenericRepository = (function (_super) {
192
+ __extends(GenericRepository, _super);
193
+ function GenericRepository(manager, table, attrs, id1Field, id2Field, toDB, fromDB, id1Col, id2Col) {
194
+ var _this = _super.call(this, manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col) || this;
195
+ _this.toDB = toDB;
196
+ var x = build_1.version(attrs);
197
+ _this.exec = manager.exec;
198
+ _this.execBatch = manager.execBatch;
199
+ if (x) {
200
+ _this.version = x.name;
201
+ }
202
+ _this.insert = _this.insert.bind(_this);
203
+ _this.update = _this.update.bind(_this);
204
+ _this.patch = _this.patch.bind(_this);
205
+ _this.delete = _this.delete.bind(_this);
206
+ return _this;
207
+ }
208
+ GenericRepository.prototype.insert = function (obj, ctx) {
209
+ var obj2 = obj;
210
+ if (this.toDB) {
211
+ obj2 = this.toDB(obj);
212
+ }
213
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
214
+ if (stmt) {
215
+ return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
216
+ if (err && err.error === 'duplicate') {
217
+ return 0;
218
+ }
219
+ else {
220
+ throw err;
221
+ }
222
+ });
223
+ }
224
+ else {
225
+ return Promise.resolve(0);
226
+ }
227
+ };
228
+ GenericRepository.prototype.update = function (obj, ctx) {
229
+ var obj2 = obj;
230
+ if (this.toDB) {
231
+ obj2 = this.toDB(obj);
232
+ }
233
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
234
+ if (stmt) {
235
+ return this.exec(stmt.query, stmt.params, ctx);
236
+ }
237
+ else {
238
+ return Promise.resolve(0);
239
+ }
240
+ };
241
+ GenericRepository.prototype.patch = function (obj, ctx) {
242
+ return this.update(obj, ctx);
243
+ };
244
+ GenericRepository.prototype.delete = function (id1, id2, ctx) {
245
+ return this.exec("delete from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], ctx);
246
+ };
247
+ return GenericRepository;
248
+ }(SqlLoadRepository));
249
+ exports.GenericRepository = GenericRepository;
86
250
  var SqlSearchLoader = (function (_super) {
87
251
  __extends(SqlSearchLoader, _super);
88
252
  function SqlSearchLoader(find, query, table, attrs, param, fromDB) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.22",
3
+ "version": "0.1.25",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -82,6 +82,7 @@ export class SearchBuilder<T, S> {
82
82
  }
83
83
  }
84
84
  }
85
+ // tslint:disable-next-line:max-classes-per-file
85
86
  export class Query<T, ID, S> extends SearchBuilder<T, S> {
86
87
  primaryKeys: Attribute[];
87
88
  map?: StringMap;
package/src/index.ts CHANGED
@@ -3,7 +3,7 @@ export {Checker as SqlChecker};
3
3
 
4
4
  import {Attribute, StringMap} from './metadata';
5
5
  import {SqlLoader, SqlSearchLoader, SqlSearchWriter, SqlWriter} from './services';
6
- export {SqlLoader as SqlLoadRepository};
6
+ // export {SqlLoader as SqlLoadRepository};
7
7
  export {SqlLoader as SqlViewRepository};
8
8
  export {SqlWriter as SqlGenericRepository};
9
9
  export {SqlWriter as Repository};
package/src/services.ts CHANGED
@@ -83,6 +83,173 @@ export class SqlLoader<T, ID> {
83
83
  }
84
84
  }
85
85
  // tslint:disable-next-line:max-classes-per-file
86
+ export class QueryRepository<T, ID> {
87
+ constructor(public db: DB, public table: string, public attrs: Attributes, public sort?: string, id?: string) {
88
+ this.id = (id && id.length > 0 ? id : 'id');
89
+ this.query = this.query.bind(this);
90
+ const m = metadata(attrs);
91
+ this.map = m.map;
92
+ this.bools = m.bools;
93
+ }
94
+ id: string;
95
+ map?: StringMap;
96
+ bools?: Attribute[];
97
+ query(ids: ID[]): Promise<T[]> {
98
+ if (!ids || ids.length === 0) {
99
+ return Promise.resolve([]);
100
+ }
101
+ const ps: string[] = [];
102
+ const length = ids.length;
103
+ for (let i = 1; i <= length; i++) {
104
+ ps.push(this.db.param(i));
105
+ }
106
+ let sql = `select * from ${this.table} where ${this.id} in (${ps.join(',')})`;
107
+ if (this.sort && this.sort.length > 0) {
108
+ sql = sql + ' order by ' + this.sort;
109
+ }
110
+ return this.db.query<T>(sql, ids, this.map, this.bools);
111
+ }
112
+ }
113
+ // tslint:disable-next-line:max-classes-per-file
114
+ export class SqlLoadRepository<T, K1, K2> {
115
+ map?: StringMap;
116
+ attributes: Attributes;
117
+ bools?: Attribute[];
118
+ id1Col: string;
119
+ id2Col: string;
120
+ constructor(
121
+ public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
122
+ public table: string,
123
+ attrs: Attributes,
124
+ public param: (i: number) => string,
125
+ public id1Field: string,
126
+ public id2Field: string,
127
+ public fromDB?: (v: T) => T,
128
+ id1Col?: string,
129
+ id2Col?: string) {
130
+
131
+ const m = metadata(attrs);
132
+ this.attributes = attrs;
133
+ this.map = m.map;
134
+ this.bools = m.bools;
135
+
136
+ if (this.metadata) {
137
+ this.metadata = this.metadata.bind(this);
138
+ }
139
+ this.all = this.all.bind(this);
140
+ this.load = this.load.bind(this);
141
+ this.exist = this.exist.bind(this);
142
+ if (id1Col && id1Col.length > 0) {
143
+ this.id1Col = id1Col;
144
+ } else {
145
+ const c = attrs[this.id1Field];
146
+ if (c) {
147
+ this.id1Col = (c.column && c.column.length > 0 ? c.column : this.id1Field);
148
+ } else {
149
+ this.id1Col = this.id1Field;
150
+ }
151
+ }
152
+ if (id2Col && id2Col.length > 0) {
153
+ this.id2Col = id2Col;
154
+ } else {
155
+ const c = attrs[this.id2Field];
156
+ if (c) {
157
+ this.id2Col = (c.column && c.column.length > 0 ? c.column : this.id2Field);
158
+ } else {
159
+ this.id2Col = this.id2Field;
160
+ }
161
+ }
162
+ }
163
+ metadata?(): Attributes|undefined {
164
+ return this.attributes;
165
+ }
166
+ all(): Promise<T[]> {
167
+ const sql = `select * from ${this.table}`;
168
+ return this.query(sql, [], this.map);
169
+ }
170
+ load(id1: K1, id2: K2, ctx?: any): Promise<T|null> {
171
+ return this.query<T>(`select * from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], this.map, undefined, ctx).then(objs => {
172
+ if (!objs || objs.length === 0) {
173
+ return null;
174
+ } else {
175
+ const fn = this.fromDB;
176
+ if (fn) {
177
+ return fn(objs[0]);
178
+ } else {
179
+ return objs[0];
180
+ }
181
+ }
182
+ });
183
+ }
184
+ exist(id1: K1, id2: K2, ctx?: any): Promise<boolean> {
185
+ return this.query<T>(`select ${this.id1Col} from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], undefined, undefined, ctx).then(objs => {
186
+ return (objs && objs.length > 0 ? true : false);
187
+ });
188
+ }
189
+ }
190
+ // tslint:disable-next-line:max-classes-per-file
191
+ export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
192
+ version?: string;
193
+ exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
194
+ execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>;
195
+ constructor(manager: Manager, table: string,
196
+ attrs: Attributes,
197
+ id1Field: string,
198
+ id2Field: string,
199
+ public toDB?: (v: T) => T,
200
+ fromDB?: (v: T) => T,
201
+ id1Col?: string,
202
+ id2Col?: string) {
203
+ super(manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col);
204
+ const x = version(attrs);
205
+ this.exec = manager.exec;
206
+ this.execBatch = manager.execBatch;
207
+ if (x) {
208
+ this.version = x.name;
209
+ }
210
+ this.insert = this.insert.bind(this);
211
+ this.update = this.update.bind(this);
212
+ this.patch = this.patch.bind(this);
213
+ this.delete = this.delete.bind(this);
214
+ }
215
+ insert(obj: T, ctx?: any): Promise<number> {
216
+ let obj2 = obj;
217
+ if (this.toDB) {
218
+ obj2 = this.toDB(obj);
219
+ }
220
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
221
+ if (stmt) {
222
+ return this.exec(stmt.query, stmt.params, ctx).catch(err => {
223
+ if (err && err.error === 'duplicate') {
224
+ return 0;
225
+ } else {
226
+ throw err;
227
+ }
228
+ });
229
+ } else {
230
+ return Promise.resolve(0);
231
+ }
232
+ }
233
+ update(obj: T, ctx?: any): Promise<number> {
234
+ let obj2 = obj;
235
+ if (this.toDB) {
236
+ obj2 = this.toDB(obj);
237
+ }
238
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
239
+ if (stmt) {
240
+ return this.exec(stmt.query, stmt.params, ctx);
241
+ } else {
242
+ return Promise.resolve(0);
243
+ }
244
+ }
245
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
246
+ return this.update(obj as any, ctx);
247
+ }
248
+ delete(id1: K1, id2: K2, ctx?: any): Promise<number> {
249
+ return this.exec(`delete from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], ctx);
250
+ }
251
+ }
252
+ // tslint:disable-next-line:max-classes-per-file
86
253
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
87
254
  constructor(
88
255
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,