query-core 0.1.22 → 0.1.23

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,140 @@ var SqlLoader = (function () {
83
83
  return SqlLoader;
84
84
  }());
85
85
  exports.SqlLoader = SqlLoader;
86
+ var SqlLoadRepository = (function () {
87
+ function SqlLoadRepository(query, table, attrs, param, id1Field, id2Field, fromDB, id1Col, id2Col) {
88
+ this.query = query;
89
+ this.table = table;
90
+ this.param = param;
91
+ this.id1Field = id1Field;
92
+ this.id2Field = id2Field;
93
+ this.fromDB = fromDB;
94
+ var m = build_1.metadata(attrs);
95
+ this.attributes = attrs;
96
+ this.map = m.map;
97
+ this.bools = m.bools;
98
+ if (this.metadata) {
99
+ this.metadata = this.metadata.bind(this);
100
+ }
101
+ this.all = this.all.bind(this);
102
+ this.load = this.load.bind(this);
103
+ this.exist = this.exist.bind(this);
104
+ if (id1Col && id1Col.length > 0) {
105
+ this.id1Col = id1Col;
106
+ }
107
+ else {
108
+ var c = attrs[this.id1Field];
109
+ if (c) {
110
+ this.id1Col = (c.column && c.column.length > 0 ? c.column : this.id1Field);
111
+ }
112
+ else {
113
+ this.id1Col = this.id1Field;
114
+ }
115
+ }
116
+ if (id2Col && id2Col.length > 0) {
117
+ this.id2Col = id2Col;
118
+ }
119
+ else {
120
+ var c = attrs[this.id2Field];
121
+ if (c) {
122
+ this.id2Col = (c.column && c.column.length > 0 ? c.column : this.id2Field);
123
+ }
124
+ else {
125
+ this.id2Col = this.id2Field;
126
+ }
127
+ }
128
+ }
129
+ SqlLoadRepository.prototype.metadata = function () {
130
+ return this.attributes;
131
+ };
132
+ SqlLoadRepository.prototype.all = function () {
133
+ var sql = "select * from " + this.table;
134
+ return this.query(sql, [], this.map);
135
+ };
136
+ SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
137
+ var _this = this;
138
+ 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) {
140
+ return null;
141
+ }
142
+ else {
143
+ var fn = _this.fromDB;
144
+ if (fn) {
145
+ return fn(objs[0]);
146
+ }
147
+ else {
148
+ return objs[0];
149
+ }
150
+ }
151
+ });
152
+ };
153
+ SqlLoadRepository.prototype.exist = function (id1, id2, ctx) {
154
+ 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) {
155
+ return (objs && objs.length > 0 ? true : false);
156
+ });
157
+ };
158
+ return SqlLoadRepository;
159
+ }());
160
+ exports.SqlLoadRepository = SqlLoadRepository;
161
+ var GenericRepository = (function (_super) {
162
+ __extends(GenericRepository, _super);
163
+ function GenericRepository(manager, table, attrs, id1Field, id2Field, toDB, fromDB, id1Col, id2Col) {
164
+ var _this = _super.call(this, manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col) || this;
165
+ _this.toDB = toDB;
166
+ var x = build_1.version(attrs);
167
+ _this.exec = manager.exec;
168
+ _this.execBatch = manager.execBatch;
169
+ if (x) {
170
+ _this.version = x.name;
171
+ }
172
+ _this.insert = _this.insert.bind(_this);
173
+ _this.update = _this.update.bind(_this);
174
+ _this.patch = _this.patch.bind(_this);
175
+ _this.delete = _this.delete.bind(_this);
176
+ return _this;
177
+ }
178
+ GenericRepository.prototype.insert = function (obj, ctx) {
179
+ var obj2 = obj;
180
+ if (this.toDB) {
181
+ obj2 = this.toDB(obj);
182
+ }
183
+ var stmt = build_1.buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
184
+ if (stmt) {
185
+ return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
186
+ if (err && err.error === 'duplicate') {
187
+ return 0;
188
+ }
189
+ else {
190
+ throw err;
191
+ }
192
+ });
193
+ }
194
+ else {
195
+ return Promise.resolve(0);
196
+ }
197
+ };
198
+ GenericRepository.prototype.update = function (obj, ctx) {
199
+ var obj2 = obj;
200
+ if (this.toDB) {
201
+ obj2 = this.toDB(obj);
202
+ }
203
+ var stmt = build_1.buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
204
+ if (stmt) {
205
+ return this.exec(stmt.query, stmt.params, ctx);
206
+ }
207
+ else {
208
+ return Promise.resolve(0);
209
+ }
210
+ };
211
+ GenericRepository.prototype.patch = function (obj, ctx) {
212
+ return this.update(obj, ctx);
213
+ };
214
+ GenericRepository.prototype.delete = function (id1, id2, ctx) {
215
+ return this.exec("delete from " + this.table + " where " + this.id1Col + " = " + this.param(1) + " and " + this.id2Col + " = " + this.param(2), [id1, id2], ctx);
216
+ };
217
+ return GenericRepository;
218
+ }(SqlLoadRepository));
219
+ exports.GenericRepository = GenericRepository;
86
220
  var SqlSearchLoader = (function (_super) {
87
221
  __extends(SqlSearchLoader, _super);
88
222
  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.23",
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,145 @@ export class SqlLoader<T, ID> {
83
83
  }
84
84
  }
85
85
  // tslint:disable-next-line:max-classes-per-file
86
+ export class SqlLoadRepository<T, K1, K2> {
87
+ map?: StringMap;
88
+ attributes: Attributes;
89
+ bools?: Attribute[];
90
+ id1Col: string;
91
+ id2Col: string;
92
+ constructor(
93
+ public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
94
+ public table: string,
95
+ attrs: Attributes,
96
+ public param: (i: number) => string,
97
+ public id1Field: string,
98
+ public id2Field: string,
99
+ public fromDB?: (v: T) => T,
100
+ id1Col?: string,
101
+ id2Col?: string) {
102
+
103
+ const m = metadata(attrs);
104
+ this.attributes = attrs;
105
+ this.map = m.map;
106
+ this.bools = m.bools;
107
+
108
+ if (this.metadata) {
109
+ this.metadata = this.metadata.bind(this);
110
+ }
111
+ this.all = this.all.bind(this);
112
+ this.load = this.load.bind(this);
113
+ this.exist = this.exist.bind(this);
114
+ if (id1Col && id1Col.length > 0) {
115
+ this.id1Col = id1Col;
116
+ } else {
117
+ const c = attrs[this.id1Field];
118
+ if (c) {
119
+ this.id1Col = (c.column && c.column.length > 0 ? c.column : this.id1Field);
120
+ } else {
121
+ this.id1Col = this.id1Field;
122
+ }
123
+ }
124
+ if (id2Col && id2Col.length > 0) {
125
+ this.id2Col = id2Col;
126
+ } else {
127
+ const c = attrs[this.id2Field];
128
+ if (c) {
129
+ this.id2Col = (c.column && c.column.length > 0 ? c.column : this.id2Field);
130
+ } else {
131
+ this.id2Col = this.id2Field;
132
+ }
133
+ }
134
+ }
135
+ metadata?(): Attributes|undefined {
136
+ return this.attributes;
137
+ }
138
+ all(): Promise<T[]> {
139
+ const sql = `select * from ${this.table}`;
140
+ return this.query(sql, [], this.map);
141
+ }
142
+ load(id1: K1, id2: K2, ctx?: any): Promise<T|null> {
143
+ 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) {
145
+ return null;
146
+ } else {
147
+ const fn = this.fromDB;
148
+ if (fn) {
149
+ return fn(objs[0]);
150
+ } else {
151
+ return objs[0];
152
+ }
153
+ }
154
+ });
155
+ }
156
+ exist(id1: K1, id2: K2, ctx?: any): Promise<boolean> {
157
+ 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 => {
158
+ return (objs && objs.length > 0 ? true : false);
159
+ });
160
+ }
161
+ }
162
+ // tslint:disable-next-line:max-classes-per-file
163
+ export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
164
+ version?: string;
165
+ exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
166
+ execBatch: (statements: Statement[], firstSuccess?: boolean, ctx?: any) => Promise<number>;
167
+ constructor(manager: Manager, table: string,
168
+ attrs: Attributes,
169
+ id1Field: string,
170
+ id2Field: string,
171
+ public toDB?: (v: T) => T,
172
+ fromDB?: (v: T) => T,
173
+ id1Col?: string,
174
+ id2Col?: string) {
175
+ super(manager.query, table, attrs, manager.param, id1Field, id2Field, fromDB, id1Col, id2Col);
176
+ const x = version(attrs);
177
+ this.exec = manager.exec;
178
+ this.execBatch = manager.execBatch;
179
+ if (x) {
180
+ this.version = x.name;
181
+ }
182
+ this.insert = this.insert.bind(this);
183
+ this.update = this.update.bind(this);
184
+ this.patch = this.patch.bind(this);
185
+ this.delete = this.delete.bind(this);
186
+ }
187
+ insert(obj: T, ctx?: any): Promise<number> {
188
+ let obj2 = obj;
189
+ if (this.toDB) {
190
+ obj2 = this.toDB(obj);
191
+ }
192
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version);
193
+ if (stmt) {
194
+ return this.exec(stmt.query, stmt.params, ctx).catch(err => {
195
+ if (err && err.error === 'duplicate') {
196
+ return 0;
197
+ } else {
198
+ throw err;
199
+ }
200
+ });
201
+ } else {
202
+ return Promise.resolve(0);
203
+ }
204
+ }
205
+ update(obj: T, ctx?: any): Promise<number> {
206
+ let obj2 = obj;
207
+ if (this.toDB) {
208
+ obj2 = this.toDB(obj);
209
+ }
210
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version);
211
+ if (stmt) {
212
+ return this.exec(stmt.query, stmt.params, ctx);
213
+ } else {
214
+ return Promise.resolve(0);
215
+ }
216
+ }
217
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
218
+ return this.update(obj as any, ctx);
219
+ }
220
+ delete(id1: K1, id2: K2, ctx?: any): Promise<number> {
221
+ return this.exec(`delete from ${this.table} where ${this.id1Col} = ${this.param(1)} and ${this.id2Col} = ${this.param(2)}`, [id1, id2], ctx);
222
+ }
223
+ }
224
+ // tslint:disable-next-line:max-classes-per-file
86
225
  export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
87
226
  constructor(
88
227
  protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,