query-core 0.1.23 → 0.1.24

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/services.js CHANGED
@@ -83,6 +83,35 @@ 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
+ for (var i = 1; i <= length; i++) {
104
+ ps.push(this.db.param(i));
105
+ }
106
+ var 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(sql, ids, this.map, this.bools);
111
+ };
112
+ return QueryRepository;
113
+ }());
114
+ exports.QueryRepository = QueryRepository;
86
115
  var SqlLoadRepository = (function () {
87
116
  function SqlLoadRepository(query, table, attrs, param, id1Field, id2Field, fromDB, id1Col, id2Col) {
88
117
  this.query = query;
@@ -136,7 +165,7 @@ var SqlLoadRepository = (function () {
136
165
  SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
137
166
  var _this = this;
138
167
  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) {
139
- if (!objs || objs.length == 0) {
168
+ if (!objs || objs.length === 0) {
140
169
  return null;
141
170
  }
142
171
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/services.ts CHANGED
@@ -83,6 +83,33 @@ 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
+ for (let i = 1; i <= length; i++) {
103
+ ps.push(this.db.param(i));
104
+ }
105
+ let sql = `select * from ${this.table} where ${this.id} in (${ps.join(',')})`;
106
+ if (this.sort && this.sort.length > 0) {
107
+ sql = sql + ' order by ' + this.sort;
108
+ }
109
+ return this.db.query<T>(sql, ids, this.map, this.bools);
110
+ }
111
+ }
112
+ // tslint:disable-next-line:max-classes-per-file
86
113
  export class SqlLoadRepository<T, K1, K2> {
87
114
  map?: StringMap;
88
115
  attributes: Attributes;
@@ -141,7 +168,7 @@ export class SqlLoadRepository<T, K1, K2> {
141
168
  }
142
169
  load(id1: K1, id2: K2, ctx?: any): Promise<T|null> {
143
170
  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 => {
144
- if (!objs || objs.length == 0) {
171
+ if (!objs || objs.length === 0) {
145
172
  return null;
146
173
  } else {
147
174
  const fn = this.fromDB;