onecore 0.1.6 → 0.2.0

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
@@ -1,30 +1,17 @@
1
1
  "use strict";
2
- var __extends =
3
- (this && this.__extends) ||
4
- (function () {
5
- var extendStatics = function (d, b) {
6
- extendStatics =
7
- Object.setPrototypeOf ||
8
- ({ __proto__: [] } instanceof Array &&
9
- function (d, b) {
10
- d.__proto__ = b;
11
- }) ||
12
- function (d, b) {
13
- for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
14
- };
15
- return extendStatics(d, b);
16
- };
17
- return function (d, b) {
18
- extendStatics(d, b);
19
- function __() {
20
- this.constructor = d;
21
- }
22
- d.prototype =
23
- b === null
24
- ? Object.create(b)
25
- : ((__.prototype = b.prototype), new __());
26
- };
27
- })();
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
28
15
  Object.defineProperty(exports, "__esModule", { value: true });
29
16
  var ViewManager = (function () {
30
17
  function ViewManager(r) {
@@ -34,32 +21,32 @@ var ViewManager = (function () {
34
21
  this.load = this.load.bind(this);
35
22
  }
36
23
  ViewManager.prototype.metadata = function () {
37
- return this.r.metadata ? this.r.metadata() : undefined;
24
+ return (this.r.metadata ? this.r.metadata() : undefined);
38
25
  };
39
26
  ViewManager.prototype.keys = function () {
40
- return this.r.keys ? this.r.keys() : [];
27
+ return (this.r.keys ? this.r.keys() : []);
41
28
  };
42
29
  ViewManager.prototype.load = function (id, ctx) {
43
30
  return this.r.load(id, ctx);
44
31
  };
45
32
  return ViewManager;
46
- })();
33
+ }());
47
34
  exports.ViewManager = ViewManager;
48
35
  var ViewUseCase = (function (_super) {
49
36
  __extends(ViewUseCase, _super);
50
37
  function ViewUseCase() {
51
- return (_super !== null && _super.apply(this, arguments)) || this;
38
+ return _super !== null && _super.apply(this, arguments) || this;
52
39
  }
53
40
  return ViewUseCase;
54
- })(ViewManager);
41
+ }(ViewManager));
55
42
  exports.ViewUseCase = ViewUseCase;
56
43
  var DefaultViewService = (function (_super) {
57
44
  __extends(DefaultViewService, _super);
58
45
  function DefaultViewService() {
59
- return (_super !== null && _super.apply(this, arguments)) || this;
46
+ return _super !== null && _super.apply(this, arguments) || this;
60
47
  }
61
48
  return DefaultViewService;
62
- })(ViewManager);
49
+ }(ViewManager));
63
50
  exports.DefaultViewService = DefaultViewService;
64
51
  var ViewSearchManager = (function (_super) {
65
52
  __extends(ViewSearchManager, _super);
@@ -69,19 +56,19 @@ var ViewSearchManager = (function (_super) {
69
56
  _this.search = _this.search.bind(_this);
70
57
  return _this;
71
58
  }
72
- ViewSearchManager.prototype.search = function (s, limit, offset, fields) {
73
- return this.find(s, limit, offset, fields);
59
+ ViewSearchManager.prototype.search = function (s, limit, page, fields) {
60
+ return this.find(s, limit, page, fields);
74
61
  };
75
62
  return ViewSearchManager;
76
- })(ViewManager);
63
+ }(ViewManager));
77
64
  exports.ViewSearchManager = ViewSearchManager;
78
65
  var ViewSearchUseCase = (function (_super) {
79
66
  __extends(ViewSearchUseCase, _super);
80
67
  function ViewSearchUseCase() {
81
- return (_super !== null && _super.apply(this, arguments)) || this;
68
+ return _super !== null && _super.apply(this, arguments) || this;
82
69
  }
83
70
  return ViewSearchUseCase;
84
- })(ViewSearchManager);
71
+ }(ViewSearchManager));
85
72
  exports.ViewSearchUseCase = ViewSearchUseCase;
86
73
  var SearchManager = (function (_super) {
87
74
  __extends(SearchManager, _super);
@@ -91,19 +78,19 @@ var SearchManager = (function (_super) {
91
78
  _this.search = _this.search.bind(_this);
92
79
  return _this;
93
80
  }
94
- SearchManager.prototype.search = function (s, limit, offset, fields) {
95
- return this.repository.search(s, limit, offset, fields);
81
+ SearchManager.prototype.search = function (s, limit, page, fields) {
82
+ return this.repository.search(s, limit, page, fields);
96
83
  };
97
84
  return SearchManager;
98
- })(ViewManager);
85
+ }(ViewManager));
99
86
  exports.SearchManager = SearchManager;
100
87
  var SearchUseCase = (function (_super) {
101
88
  __extends(SearchUseCase, _super);
102
89
  function SearchUseCase() {
103
- return (_super !== null && _super.apply(this, arguments)) || this;
90
+ return _super !== null && _super.apply(this, arguments) || this;
104
91
  }
105
92
  return SearchUseCase;
106
- })(SearchManager);
93
+ }(SearchManager));
107
94
  exports.SearchUseCase = SearchUseCase;
108
95
  var GenericManager = (function (_super) {
109
96
  __extends(GenericManager, _super);
@@ -123,49 +110,45 @@ var GenericManager = (function (_super) {
123
110
  return this.repository.update(obj, ctx);
124
111
  };
125
112
  GenericManager.prototype.patch = function (obj, ctx) {
126
- return this.repository.patch
127
- ? this.repository.patch(obj, ctx)
128
- : Promise.resolve(-1);
113
+ return (this.repository.patch ? this.repository.patch(obj, ctx) : Promise.resolve(-1));
129
114
  };
130
115
  GenericManager.prototype.delete = function (id, ctx) {
131
- return this.repository.delete
132
- ? this.repository.delete(id, ctx)
133
- : Promise.resolve(-1);
116
+ return (this.repository.delete ? this.repository.delete(id, ctx) : Promise.resolve(-1));
134
117
  };
135
118
  return GenericManager;
136
- })(ViewManager);
119
+ }(ViewManager));
137
120
  exports.GenericManager = GenericManager;
138
121
  var GenericUseCase = (function (_super) {
139
122
  __extends(GenericUseCase, _super);
140
123
  function GenericUseCase() {
141
- return (_super !== null && _super.apply(this, arguments)) || this;
124
+ return _super !== null && _super.apply(this, arguments) || this;
142
125
  }
143
126
  return GenericUseCase;
144
- })(GenericManager);
127
+ }(GenericManager));
145
128
  exports.GenericUseCase = GenericUseCase;
146
129
  var DefaultGenericService = (function (_super) {
147
130
  __extends(DefaultGenericService, _super);
148
131
  function DefaultGenericService() {
149
- return (_super !== null && _super.apply(this, arguments)) || this;
132
+ return _super !== null && _super.apply(this, arguments) || this;
150
133
  }
151
134
  return DefaultGenericService;
152
- })(GenericManager);
135
+ }(GenericManager));
153
136
  exports.DefaultGenericService = DefaultGenericService;
154
137
  var DefaultService = (function (_super) {
155
138
  __extends(DefaultService, _super);
156
139
  function DefaultService() {
157
- return (_super !== null && _super.apply(this, arguments)) || this;
140
+ return _super !== null && _super.apply(this, arguments) || this;
158
141
  }
159
142
  return DefaultService;
160
- })(GenericManager);
143
+ }(GenericManager));
161
144
  exports.DefaultService = DefaultService;
162
145
  var DefaultGenericSearchService = (function (_super) {
163
146
  __extends(DefaultGenericSearchService, _super);
164
147
  function DefaultGenericSearchService() {
165
- return (_super !== null && _super.apply(this, arguments)) || this;
148
+ return _super !== null && _super.apply(this, arguments) || this;
166
149
  }
167
150
  return DefaultGenericSearchService;
168
- })(GenericManager);
151
+ }(GenericManager));
169
152
  exports.DefaultGenericSearchService = DefaultGenericSearchService;
170
153
  var Manager = (function (_super) {
171
154
  __extends(Manager, _super);
@@ -175,112 +158,77 @@ var Manager = (function (_super) {
175
158
  _this.search = _this.search.bind(_this);
176
159
  return _this;
177
160
  }
178
- Manager.prototype.search = function (s, limit, offset, fields) {
179
- return this.find(s, limit, offset, fields);
161
+ Manager.prototype.search = function (s, limit, page, fields) {
162
+ return this.find(s, limit, page, fields);
180
163
  };
181
164
  return Manager;
182
- })(GenericManager);
165
+ }(GenericManager));
183
166
  exports.Manager = Manager;
184
167
  var UseCase = (function (_super) {
185
168
  __extends(UseCase, _super);
186
169
  function UseCase() {
187
- return (_super !== null && _super.apply(this, arguments)) || this;
170
+ return _super !== null && _super.apply(this, arguments) || this;
188
171
  }
189
172
  return UseCase;
190
- })(Manager);
173
+ }(Manager));
191
174
  exports.UseCase = UseCase;
192
175
  var GenericSearchManager = (function (_super) {
193
176
  __extends(GenericSearchManager, _super);
194
177
  function GenericSearchManager() {
195
- return (_super !== null && _super.apply(this, arguments)) || this;
178
+ return _super !== null && _super.apply(this, arguments) || this;
196
179
  }
197
180
  return GenericSearchManager;
198
- })(Manager);
181
+ }(Manager));
199
182
  exports.GenericSearchManager = GenericSearchManager;
200
183
  var GenericSearchUseCase = (function (_super) {
201
184
  __extends(GenericSearchUseCase, _super);
202
185
  function GenericSearchUseCase() {
203
- return (_super !== null && _super.apply(this, arguments)) || this;
186
+ return _super !== null && _super.apply(this, arguments) || this;
204
187
  }
205
188
  return GenericSearchUseCase;
206
- })(Manager);
189
+ }(Manager));
207
190
  exports.GenericSearchUseCase = GenericSearchUseCase;
208
191
  var SavedService = (function () {
209
- function SavedService(repository, query, max, autoRemove) {
210
- this.repository = repository;
211
- this.query = query;
192
+ function SavedService(savedRepository, max) {
193
+ this.savedRepository = savedRepository;
212
194
  this.max = max;
213
- this.autoRemove = autoRemove;
214
- this.load = this.load.bind(this);
195
+ this.isSaved = this.isSaved.bind(this);
215
196
  this.save = this.save.bind(this);
216
197
  this.remove = this.remove.bind(this);
217
198
  }
218
- SavedService.prototype.load = function (id) {
219
- var _this = this;
220
- return this.repository.load(id).then(function (items) {
221
- if (!items || items.length === 0) {
222
- return [];
223
- }
224
- return _this.query(items);
225
- });
199
+ SavedService.prototype.isSaved = function (userId, id) {
200
+ return this.savedRepository.isSaved(userId, id);
226
201
  };
227
- SavedService.prototype.save = function (id, itemId) {
202
+ SavedService.prototype.save = function (userId, id) {
228
203
  var _this = this;
229
- return this.repository.load(id).then(function (items) {
230
- if (items == null) {
231
- return _this.repository.create(id, [itemId]);
232
- } else {
233
- if (items.includes(itemId)) {
234
- return Promise.resolve(0);
235
- } else {
236
- items.push(itemId);
237
- if (items.length > _this.max) {
238
- if (_this.autoRemove) {
239
- items.shift();
240
- return _this.repository.update(id, items);
241
- } else {
242
- return Promise.resolve(-1);
243
- }
244
- } else {
245
- return _this.repository.update(id, items);
246
- }
247
- }
204
+ return this.savedRepository.count(userId).then(function (count) {
205
+ if (count >= _this.max) {
206
+ return -1;
248
207
  }
249
- });
250
- };
251
- SavedService.prototype.remove = function (id, itemId) {
252
- var _this = this;
253
- return this.repository.load(id).then(function (items) {
254
- if (items == null) {
255
- return Promise.resolve(0);
256
- } else {
257
- if (items.includes(itemId)) {
258
- items = items.filter(function (item) {
259
- return item !== itemId;
260
- });
261
- return _this.repository.update(id, items);
262
- } else {
263
- return Promise.resolve(0);
264
- }
208
+ else {
209
+ return _this.savedRepository.save(userId, id);
265
210
  }
266
211
  });
267
212
  };
213
+ SavedService.prototype.remove = function (userId, id) {
214
+ return this.savedRepository.remove(userId, id);
215
+ };
268
216
  return SavedService;
269
- })();
217
+ }());
270
218
  exports.SavedService = SavedService;
271
219
  var SavedManager = (function (_super) {
272
220
  __extends(SavedManager, _super);
273
221
  function SavedManager() {
274
- return (_super !== null && _super.apply(this, arguments)) || this;
222
+ return _super !== null && _super.apply(this, arguments) || this;
275
223
  }
276
224
  return SavedManager;
277
- })(SavedService);
225
+ }(SavedService));
278
226
  exports.SavedManager = SavedManager;
279
227
  var SavedUseCase = (function (_super) {
280
228
  __extends(SavedUseCase, _super);
281
229
  function SavedUseCase() {
282
- return (_super !== null && _super.apply(this, arguments)) || this;
230
+ return _super !== null && _super.apply(this, arguments) || this;
283
231
  }
284
232
  return SavedUseCase;
285
- })(SavedService);
233
+ }(SavedService));
286
234
  exports.SavedUseCase = SavedUseCase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onecore",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "onecore",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -228,64 +228,34 @@ export class GenericSearchManager<T, ID, F extends Filter> extends Manager<T, ID
228
228
  // tslint:disable-next-line:max-classes-per-file
229
229
  export class GenericSearchUseCase<T, ID, F extends Filter> extends Manager<T, ID, F> {
230
230
  }
231
- export interface SavedRepository<ID> {
232
- load(id: ID): Promise<string[]|null>;
233
- create(id: ID, arr: string[]): Promise<number>;
234
- update(id: ID, arr: string[]): Promise<number>;
231
+ export interface SavedRepository<UID, ID> {
232
+ isSaved(userId: UID, id: ID): Promise<boolean>
233
+ save(userId: UID, id: ID): Promise<number>
234
+ remove(userId: UID, id: ID): Promise<number>
235
+ count(userId: UID): Promise<number>
235
236
  }
237
+
236
238
  // tslint:disable-next-line:max-classes-per-file
237
- export class SavedService<ID, T> {
238
- constructor(protected repository: SavedRepository<ID>, protected query: (ids: string[]) => Promise<T[]>, public max: number, public autoRemove?: boolean) {
239
- this.load = this.load.bind(this);
239
+ export class SavedService<UID, ID> {
240
+ constructor(protected savedRepository: SavedRepository<UID, ID>, protected max: number) {
241
+ this.isSaved = this.isSaved.bind(this);
240
242
  this.save = this.save.bind(this);
241
243
  this.remove = this.remove.bind(this);
242
244
  }
243
- load(id: ID): Promise<T[]> {
244
- return this.repository.load(id).then(items => {
245
- if (!items || items.length === 0) {
246
- return [];
247
- }
248
- return this.query(items);
249
- });
245
+ isSaved(userId: UID, id: ID): Promise<boolean> {
246
+ return this.savedRepository.isSaved(userId, id)
250
247
  }
251
- save(id: ID, itemId: string): Promise<number> {
252
- return this.repository.load(id).then(items => {
253
- if (items == null) {
254
- return this.repository.create(id, [itemId]);
248
+ save(userId: UID, id: ID): Promise<number> {
249
+ return this.savedRepository.count(userId).then((count) => {
250
+ if (count >= this.max) {
251
+ return -1
255
252
  } else {
256
- if (items.includes(itemId)) {
257
- return Promise.resolve(0);
258
- } else {
259
- items.push(itemId);
260
- if (items.length > this.max) {
261
- if (this.autoRemove) {
262
- items.shift();
263
- return this.repository.update(id, items);
264
- } else {
265
- return Promise.resolve(-1);
266
- }
267
- } else {
268
- return this.repository.update(id, items);
269
- }
270
- }
253
+ return this.savedRepository.save(userId, id)
271
254
  }
272
- });
255
+ })
273
256
  }
274
- remove(id: ID, itemId: string): Promise<number> {
275
- return this.repository.load(id).then(items => {
276
- if (items == null) {
277
- return Promise.resolve(0);
278
- } else {
279
- if (items.includes(itemId)) {
280
- items = items.filter((item: any) => {
281
- return item !== itemId;
282
- });
283
- return this.repository.update(id, items);
284
- } else {
285
- return Promise.resolve(0);
286
- }
287
- }
288
- });
257
+ remove(userId: UID, id: ID): Promise<number> {
258
+ return this.savedRepository.remove(userId, id)
289
259
  }
290
260
  }
291
261
  // tslint:disable-next-line:max-classes-per-file
package/src/metadata.ts CHANGED
@@ -35,10 +35,10 @@ export interface Attribute {
35
35
  nopatch?: boolean;
36
36
  version?: boolean;
37
37
  length?: number;
38
- min?: number;
39
- max?: number;
40
- gt?: number;
41
- lt?: number;
38
+ min?: number | Date | string;
39
+ max?: number | Date | string;
40
+ gt?: number | Date | string;
41
+ lt?: number | Date | string;
42
42
  precision?: number;
43
43
  scale?: number;
44
44
  exp?: RegExp | string;
@@ -12,3 +12,9 @@ export interface Repository<T, ID> extends ViewRepository<T, ID> {
12
12
  patch(obj: Partial<T>, ctx?: any): Promise<number>;
13
13
  delete(id: ID, ctx?: any): Promise<number>;
14
14
  }
15
+ export interface CRUDRepository<T, ID> extends ViewRepository<T, ID> {
16
+ create(obj: T, ctx?: any): Promise<number>;
17
+ update(obj: T, ctx?: any): Promise<number>;
18
+ patch(obj: Partial<T>, ctx?: any): Promise<number>;
19
+ delete(id: ID, ctx?: any): Promise<number>;
20
+ }