@weapnl/js-junction 0.0.3 → 0.0.5

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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.0.5
6
+ - Added license file.
7
+ - Private field bugfix, proxy access.
8
+
9
+ ## v0.0.4
10
+ - Added support for whereNotIn.
11
+ - Reactive bugfix, private property.
12
+
5
13
  ## v0.0.3
6
14
  - Added ability to override the api instance on requests.
7
15
  - Scopes & Wheres improvements.
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) WEAP informatie@weap.nl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -192,6 +192,7 @@ const request = await new User()
192
192
  .search('john doe') // Search for 'john doe', on the searchable columns of the model (defined in the API)
193
193
  .search('john doe', ['name', 'email']) // Search for 'john doe' in columns 'name' and 'email'
194
194
  .whereIn('city', ['Gemert', 'Eindhoven', 'Amsterdam']) // Set where in clause
195
+ .whereNotIn('city', ['Rotterdam', 'London']) // Set where not in clause
195
196
  .where('name', '=', 'John') // Add where clause
196
197
  .where('name', 'John') // If no operator is given, '=' is used
197
198
  .appends('age') // Add accessor
package/index.d.ts CHANGED
@@ -57,6 +57,8 @@ declare class Mixins {
57
57
  wheres(...params: any[]): this;
58
58
  whereIn(column: string, values: any[]): this;
59
59
  whereIns(...params: any[]): this;
60
+ whereNotIn(column: string, values: any[]): this;
61
+ whereNotIns(...params: any[]): this;
60
62
  pluck(fields: any[]): this;
61
63
 
62
64
  appends(appends: string | string[]): this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapnl/js-junction",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "This project allows you to easily consume API's built with Junction.",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "prod": "webpack"
10
10
  },
11
11
  "author": "Robin",
12
- "license": "ISC",
12
+ "license": "MIT",
13
13
  "devDependencies": {
14
14
  "@babel/core": "^7.12.13",
15
15
  "@babel/preset-env": "^7.12.13",
@@ -26,10 +26,10 @@ export default class Model extends Request {
26
26
  static fromJson (json) {
27
27
  const instance = new (this)();
28
28
 
29
- instance._accessors.fromJson(json);
30
- instance._attributes.fromJson(json);
31
- instance._counts.fromJson(json);
32
- instance._relations.fromJson(json);
29
+ instance._accessors.fromJson(instance, json);
30
+ instance._attributes.fromJson(instance, json);
31
+ instance._counts.fromJson(instance, json);
32
+ instance._relations.fromJson(instance, json);
33
33
 
34
34
  return instance;
35
35
  }
@@ -41,10 +41,10 @@ export default class Model extends Request {
41
41
  */
42
42
  toJson () {
43
43
  return {
44
- ...this._accessors.toJson(),
45
- ...this._attributes.toJson(),
46
- ...this._counts.toJson(),
47
- ...this._relations.toJson(),
44
+ ...this._accessors.toJson(this),
45
+ ...this._attributes.toJson(this),
46
+ ...this._counts.toJson(this),
47
+ ...this._relations.toJson(this),
48
48
  };
49
49
  }
50
50
 
@@ -53,8 +53,8 @@ export default class Model extends Request {
53
53
  * @returns {this}
54
54
  */
55
55
  fill (values = {}) {
56
- this._attributes.set(values);
57
- this._relations.set(values);
56
+ this._attributes.set(this, values);
57
+ this._relations.set(this, values);
58
58
 
59
59
  return this;
60
60
  }
@@ -171,7 +171,7 @@ export default class Model extends Request {
171
171
  async store (extraData = {}) {
172
172
  this._response = await this._connection.post(
173
173
  this._queryString(),
174
- { ...this._attributes.toJson(), ...extraData },
174
+ { ...this._attributes.toJson(this), ...extraData },
175
175
  );
176
176
 
177
177
  let item;
@@ -195,7 +195,7 @@ export default class Model extends Request {
195
195
  async update (extraData = {}) {
196
196
  this._response = await this._connection.put(
197
197
  this._queryString(this._identifier),
198
- { ...this._attributes.toJson(), ...extraData },
198
+ { ...this._attributes.toJson(this), ...extraData },
199
199
  );
200
200
 
201
201
  let item;
@@ -8,18 +8,17 @@ export default class Accessors {
8
8
  * @param {Model} model Instance of the model.
9
9
  */
10
10
  constructor (model) {
11
- this.model = model;
12
-
13
11
  _.each(model.constructor.accessors(), (options, key) => {
14
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
15
13
  });
16
14
  }
17
15
 
18
16
  /**
17
+ * @param {Model} model
19
18
  * @param {Object} json.
20
19
  */
21
- fromJson (json) {
22
- _.each(this.model.constructor.accessors(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.accessors(), (options, key) => {
23
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
23
 
25
24
  if (_.isNil(value)) {
@@ -28,18 +27,20 @@ export default class Accessors {
28
27
  value = Accessors._getCastedFromJsonValue(value, options);
29
28
  }
30
29
 
31
- this.set(key, value);
30
+ this.set(model, key, value);
32
31
  });
33
32
  }
34
33
 
35
34
  /**
35
+ * @param {Model} model
36
+ *
36
37
  * @return {Object} The attributes casted to a json object.
37
38
  */
38
- toJson () {
39
+ toJson (model) {
39
40
  const json = {};
40
41
 
41
- _.each(this.model.constructor.accessors(), (options, key) => {
42
- let jsonValue = this.get(key);
42
+ _.each(model.constructor.accessors(), (options, key) => {
43
+ let jsonValue = this.get(model, key);
43
44
 
44
45
  jsonValue = Accessors._getCastedToJsonValue(jsonValue, options);
45
46
 
@@ -50,22 +51,24 @@ export default class Accessors {
50
51
  }
51
52
 
52
53
  /**
54
+ * @param {Model} model
53
55
  * @param {string} attribute
54
56
  *
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
- get (attribute) {
58
- return _.get(this.model, attribute);
59
+ get (model, attribute) {
60
+ return _.get(model, attribute);
59
61
  }
60
62
 
61
63
  /**
62
- * @param {string} attribute
63
- * @param {*} value
64
+ * @param {Model} model
65
+ * @param {string} attribute
66
+ * @param {*} value
64
67
  *
65
68
  * @returns {*} The value that was set.
66
69
  */
67
- set (attribute, value) {
68
- this.model[attribute] = value;
70
+ set (model, attribute, value) {
71
+ model[attribute] = value;
69
72
 
70
73
  return value;
71
74
  }
@@ -8,18 +8,17 @@ export default class Attributes {
8
8
  * @param {Model} model Instance of the model.
9
9
  */
10
10
  constructor (model) {
11
- this.model = model;
12
-
13
11
  _.each(model.constructor.attributes(), (options, key) => {
14
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
15
13
  });
16
14
  }
17
15
 
18
16
  /**
17
+ * @param {Model} model
19
18
  * @param {Object} json.
20
19
  */
21
- fromJson (json) {
22
- _.each(this.model.constructor.attributes(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.attributes(), (options, key) => {
23
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
23
 
25
24
  if (_.isNil(value)) {
@@ -28,18 +27,20 @@ export default class Attributes {
28
27
  value = Attributes._getCastedFromJsonValue(value, options);
29
28
  }
30
29
 
31
- this.set(key, value);
30
+ this.set(model, key, value);
32
31
  });
33
32
  }
34
33
 
35
34
  /**
35
+ * @param {Model} model
36
+ *
36
37
  * @return {Object} The attributes casted to a json object.
37
38
  */
38
- toJson () {
39
+ toJson (model) {
39
40
  const json = {};
40
41
 
41
- _.each(this.model.constructor.attributes(), (options, key) => {
42
- let jsonValue = this.get(key);
42
+ _.each(model.constructor.attributes(), (options, key) => {
43
+ let jsonValue = this.get(model, key);
43
44
 
44
45
  jsonValue = Attributes._getCastedToJsonValue(jsonValue, options);
45
46
 
@@ -50,32 +51,34 @@ export default class Attributes {
50
51
  }
51
52
 
52
53
  /**
54
+ * @param {Model} model
53
55
  * @param {string} attribute
54
56
  *
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
- get (attribute) {
58
- return _.get(this.model, attribute);
59
+ get (model, attribute) {
60
+ return _.get(model, attribute);
59
61
  }
60
62
 
61
63
  /**
62
- * @param {string|Object} attribute
63
- * @param {*} value
64
+ * @param {Model} model
65
+ * @param {string|Object} attribute
66
+ * @param {*} value
64
67
  *
65
68
  * @returns {Attributes}
66
69
  */
67
- set (attribute, value = null) {
70
+ set (model, attribute, value = null) {
68
71
  if (_.isObject(attribute)) {
69
- _.each(this.model.constructor.attributes(), (options, key) => {
72
+ _.each(model.constructor.attributes(), (options, key) => {
70
73
  if (! _.has(attribute, key)) return;
71
74
 
72
- this.set(key, attribute[key]);
75
+ this.set(model, key, attribute[key]);
73
76
  });
74
77
 
75
78
  return this;
76
79
  }
77
80
 
78
- this.model[attribute] = value;
81
+ model[attribute] = value;
79
82
 
80
83
  return this;
81
84
  }
@@ -6,58 +6,61 @@ export default class Counts {
6
6
  * @param {Model} model Instance of the model.
7
7
  */
8
8
  constructor (model) {
9
- this.model = model;
10
-
11
9
  _.each(model.constructor.counts(), (options, key) => {
12
- this.set(this.key(key, true), _.has(options, 'default') ? options.default : null);
10
+ this.set(model, this.key(key, true), _.has(options, 'default') ? options.default : null);
13
11
  });
14
12
  }
15
13
 
16
14
  /**
15
+ * @param {Model} model
17
16
  * @param {Object} json.
18
17
  */
19
- fromJson (json) {
20
- _.each(this.model.constructor.counts(), (options, key) => {
18
+ fromJson (model, json) {
19
+ _.each(model.constructor.counts(), (options, key) => {
21
20
  let value = _.get(json, this.key(key));
22
21
 
23
22
  value = value !== undefined
24
23
  ? _.toInteger(value)
25
24
  : null;
26
25
 
27
- this.set(this.key(key, true), value);
26
+ this.set(model, this.key(key, true), value);
28
27
  });
29
28
  }
30
29
 
31
30
  /**
31
+ * @param {Model} model
32
+ *
32
33
  * @return {Object} The attributes casted to a json object.
33
34
  */
34
- toJson () {
35
+ toJson (model) {
35
36
  const json = {};
36
37
 
37
- _.each(this.model.constructor.counts(), (options, key) => {
38
- _.set(json, key, this.get(key));
38
+ _.each(model.constructor.counts(), (options, key) => {
39
+ _.set(json, key, this.get(model, key));
39
40
  });
40
41
 
41
42
  return json;
42
43
  }
43
44
 
44
45
  /**
46
+ * @param {Model} model
45
47
  * @param {string} attribute
46
48
  *
47
49
  * @returns {*} The value of the attribute.
48
50
  */
49
- get (attribute) {
50
- return _.get(this.model, this.key(attribute, true));
51
+ get (model, attribute) {
52
+ return _.get(model, this.key(attribute, true));
51
53
  }
52
54
 
53
55
  /**
54
- * @param {string} attribute
55
- * @param {*} value
56
+ * @param {Model} model
57
+ * @param {string} attribute
58
+ * @param {*} value
56
59
  *
57
60
  * @returns {*} The value that was set.
58
61
  */
59
- set (attribute, value) {
60
- this.model[this.key(attribute, true)] = value;
62
+ set (model, attribute, value) {
63
+ model[this.key(attribute, true)] = value;
61
64
 
62
65
  return value;
63
66
  }
@@ -3,35 +3,40 @@
3
3
  */
4
4
  class Property {
5
5
  /**
6
+ * @param {Model} model
6
7
  * @param {string} key
7
8
  *
8
9
  * @returns {*} The value of the attribute.
9
10
  */
10
- get (key) {
11
+ get (model, key) {
11
12
  throw new Error('not implemented');
12
13
  }
13
14
 
14
15
  /**
15
- * @param {string} key
16
- * @param {*} value
16
+ * @param {Model} model
17
+ * @param {string} key
18
+ * @param {*} value
17
19
  *
18
20
  * @returns {*} The value that was set.
19
21
  */
20
- set (key, value) {
22
+ set (model, key, value) {
21
23
  throw new Error('not implemented');
22
24
  }
23
25
 
24
26
  /**
27
+ * @param {Model} model
25
28
  * @param {Object} json.
26
29
  */
27
- fromJson (json) {
30
+ fromJson (model, json) {
28
31
  throw new Error('not implemented');
29
32
  }
30
33
 
31
34
  /**
35
+ * @param {Model} model
36
+ *
32
37
  * @return {Object} The json object.
33
38
  */
34
- toJson () {
39
+ toJson (model) {
35
40
  throw new Error('not implemented');
36
41
  }
37
42
  }
@@ -8,36 +8,37 @@ export default class Relations {
8
8
  * @param {Model} model Instance of the model.
9
9
  */
10
10
  constructor (model) {
11
- this.model = model;
12
-
13
11
  _.each(model.constructor.relations(), (options, key) => {
14
- this.set(key, _.has(options, 'default') ? options.default : null);
12
+ this.set(model, key, _.has(options, 'default') ? options.default : null);
15
13
  });
16
14
  }
17
15
 
18
16
  /**
17
+ * @param {Model} model
19
18
  * @param {Object} json.
20
19
  */
21
- fromJson (json) {
22
- _.each(this.model.constructor.relations(), (options, key) => {
20
+ fromJson (model, json) {
21
+ _.each(model.constructor.relations(), (options, key) => {
23
22
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
23
 
25
24
  value = value
26
25
  ? Relations._getCastedFromJsonValue(value, options)
27
26
  : value;
28
27
 
29
- this.set(key, value);
28
+ this.set(model, key, value);
30
29
  });
31
30
  }
32
31
 
33
32
  /**
33
+ * @param {Model} model
34
+ *
34
35
  * @return {Object} The attributes casted to a json object.
35
36
  */
36
- toJson () {
37
+ toJson (model) {
37
38
  const json = {};
38
39
 
39
- _.each(this.model.constructor.relations(), (options, key) => {
40
- let jsonValue = this.get(key);
40
+ _.each(model.constructor.relations(), (options, key) => {
41
+ let jsonValue = this.get(model, key);
41
42
 
42
43
  jsonValue = Relations._getCastedToJsonValue(jsonValue, options);
43
44
 
@@ -48,32 +49,34 @@ export default class Relations {
48
49
  }
49
50
 
50
51
  /**
52
+ * @param {Model} model
51
53
  * @param {string} relation
52
54
  *
53
55
  * @returns {*} The value of the relation.
54
56
  */
55
- get (relation) {
56
- return _.get(this.model, relation);
57
+ get (model, relation) {
58
+ return _.get(model, relation);
57
59
  }
58
60
 
59
61
  /**
60
- * @param {string|Object} relation
61
- * @param {*} value
62
+ * @param {Model} model
63
+ * @param {string|Object} relation
64
+ * @param {*} value
62
65
  *
63
66
  * @returns {Relations}
64
67
  */
65
- set (relation, value = null) {
68
+ set (model, relation, value = null) {
66
69
  if (_.isObject(relation)) {
67
- _.each(this.model.constructor.relations(), (options, key) => {
70
+ _.each(model.constructor.relations(), (options, key) => {
68
71
  if (! _.has(relation, key)) return;
69
72
 
70
- this.set(key, relation[key]);
73
+ this.set(model, key, relation[key]);
71
74
  });
72
75
 
73
76
  return this;
74
77
  }
75
78
 
76
- this.model[relation] = value;
79
+ model[relation] = value;
77
80
 
78
81
  return this;
79
82
  }
@@ -5,6 +5,7 @@ import Scopes from './scopes';
5
5
  import Search from './search';
6
6
  import Wheres from './wheres';
7
7
  import WhereIn from './whereIn';
8
+ import WhereNotIn from './whereNotIn';
8
9
  import Count from './count';
9
10
  import Pluck from './pluck';
10
11
 
@@ -18,13 +19,14 @@ export default class Filters {
18
19
  this.search = new Search();
19
20
  this.wheres = new Wheres();
20
21
  this.whereIn = new WhereIn();
22
+ this.whereNotIn = new WhereNotIn();
21
23
  this.pluck = new Pluck();
22
24
  }
23
25
 
24
26
  toObject () {
25
27
  const items = [];
26
28
 
27
- for (let i = 0, filters = ['count', 'limit', 'order', 'relations', 'scopes', 'search', 'wheres', 'whereIn', 'pluck']; i < filters.length; i++) {
29
+ for (let i = 0, filters = ['count', 'limit', 'order', 'relations', 'scopes', 'search', 'wheres', 'whereIn', 'whereNotIn', 'pluck']; i < filters.length; i++) {
28
30
  if (this[filters[i]].filled()) items.push(this[filters[i]].toObject());
29
31
  }
30
32
 
@@ -1,7 +1,7 @@
1
1
  import Filter from './filter';
2
2
  import Format from '../utilities/format';
3
3
 
4
- export default class Relations extends Filter {
4
+ export default class WhereIn extends Filter {
5
5
  constructor () {
6
6
  super();
7
7
 
@@ -0,0 +1,31 @@
1
+ import Filter from './filter';
2
+ import Format from '../utilities/format';
3
+
4
+ export default class WhereNotIn extends Filter {
5
+ constructor () {
6
+ super();
7
+
8
+ this._whereNotIns = [];
9
+ }
10
+
11
+ filled () {
12
+ return this._whereNotIns.length > 0;
13
+ }
14
+
15
+ add (column, values) {
16
+ this._whereNotIns.push({
17
+ column: Format.snakeCase(column),
18
+ values,
19
+ });
20
+ }
21
+
22
+ toObject () {
23
+ const data = {};
24
+
25
+ if (this.filled()) {
26
+ data.where_not_in = this._whereNotIns;
27
+ }
28
+
29
+ return data;
30
+ }
31
+ }
@@ -112,6 +112,22 @@ const filterMixin = {
112
112
  return this;
113
113
  },
114
114
 
115
+ whereNotIn (column, values) {
116
+ if (! Array.isArray(values)) values = [values];
117
+
118
+ this._filters.whereNotIn.add(column, values);
119
+
120
+ return this;
121
+ },
122
+
123
+ whereNotIns (input) {
124
+ input.forEach(whereIn => {
125
+ this.whereNotIn(...whereIn);
126
+ });
127
+
128
+ return this;
129
+ },
130
+
115
131
  pluck (fields) {
116
132
  if (! Array.isArray(fields)) fields = [fields];
117
133
 
package/src/request.js CHANGED
@@ -68,7 +68,7 @@ export default class Request {
68
68
  const url = this.url ?? this.constructor.endpoint;
69
69
 
70
70
  this._response = await this._connection.get(
71
- `${url}`,
71
+ url,
72
72
  this.bodyParameters,
73
73
  );
74
74
 
@@ -86,7 +86,7 @@ export default class Request {
86
86
  const url = this.url ?? this.constructor.endpoint;
87
87
 
88
88
  this._response = await this._connection.post(
89
- `${url}`,
89
+ url,
90
90
  data,
91
91
  );
92
92
 
@@ -104,7 +104,7 @@ export default class Request {
104
104
  const url = this.url ?? this.constructor.endpoint;
105
105
 
106
106
  this._response = await this._connection.put(
107
- `${url}`,
107
+ url,
108
108
  { ...data, ...this.bodyParameters },
109
109
  );
110
110
 
@@ -120,7 +120,7 @@ export default class Request {
120
120
  const url = this.url ?? this.constructor.endpoint;
121
121
 
122
122
  this._response = await this._connection.delete(
123
- `${url}`,
123
+ url,
124
124
  );
125
125
 
126
126
  await this.triggerResponseEvents(this._response);