@weapnl/js-junction 0.0.3 → 0.0.4

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,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.0.4
6
+ - Added support for whereNotIn.
7
+ - Reactive bugfix, private property.
8
+
5
9
  ## v0.0.3
6
10
  - Added ability to override the api instance on requests.
7
11
  - Scopes & Wheres improvements.
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.4",
4
4
  "description": "This project allows you to easily consume API's built with Junction.",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Accessors {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.accessors(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Accessors {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.accessors(), (options, key) => {
24
+ _.each(this.#model.constructor.accessors(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  if (_.isNil(value)) {
@@ -38,7 +40,7 @@ export default class Accessors {
38
40
  toJson () {
39
41
  const json = {};
40
42
 
41
- _.each(this.model.constructor.accessors(), (options, key) => {
43
+ _.each(this.#model.constructor.accessors(), (options, key) => {
42
44
  let jsonValue = this.get(key);
43
45
 
44
46
  jsonValue = Accessors._getCastedToJsonValue(jsonValue, options);
@@ -55,7 +57,7 @@ export default class Accessors {
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
59
  get (attribute) {
58
- return _.get(this.model, attribute);
60
+ return _.get(this.#model, attribute);
59
61
  }
60
62
 
61
63
  /**
@@ -65,7 +67,7 @@ export default class Accessors {
65
67
  * @returns {*} The value that was set.
66
68
  */
67
69
  set (attribute, value) {
68
- this.model[attribute] = value;
70
+ this.#model[attribute] = value;
69
71
 
70
72
  return value;
71
73
  }
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Attributes {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.attributes(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Attributes {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.attributes(), (options, key) => {
24
+ _.each(this.#model.constructor.attributes(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  if (_.isNil(value)) {
@@ -38,7 +40,7 @@ export default class Attributes {
38
40
  toJson () {
39
41
  const json = {};
40
42
 
41
- _.each(this.model.constructor.attributes(), (options, key) => {
43
+ _.each(this.#model.constructor.attributes(), (options, key) => {
42
44
  let jsonValue = this.get(key);
43
45
 
44
46
  jsonValue = Attributes._getCastedToJsonValue(jsonValue, options);
@@ -55,7 +57,7 @@ export default class Attributes {
55
57
  * @returns {*} The value of the attribute.
56
58
  */
57
59
  get (attribute) {
58
- return _.get(this.model, attribute);
60
+ return _.get(this.#model, attribute);
59
61
  }
60
62
 
61
63
  /**
@@ -66,7 +68,7 @@ export default class Attributes {
66
68
  */
67
69
  set (attribute, value = null) {
68
70
  if (_.isObject(attribute)) {
69
- _.each(this.model.constructor.attributes(), (options, key) => {
71
+ _.each(this.#model.constructor.attributes(), (options, key) => {
70
72
  if (! _.has(attribute, key)) return;
71
73
 
72
74
  this.set(key, attribute[key]);
@@ -75,7 +77,7 @@ export default class Attributes {
75
77
  return this;
76
78
  }
77
79
 
78
- this.model[attribute] = value;
80
+ this.#model[attribute] = value;
79
81
 
80
82
  return this;
81
83
  }
@@ -2,11 +2,13 @@
2
2
  * @implements {Property}
3
3
  */
4
4
  export default class Counts {
5
+ #model;
6
+
5
7
  /**
6
8
  * @param {Model} model Instance of the model.
7
9
  */
8
10
  constructor (model) {
9
- this.model = model;
11
+ this.#model = model;
10
12
 
11
13
  _.each(model.constructor.counts(), (options, key) => {
12
14
  this.set(this.key(key, true), _.has(options, 'default') ? options.default : null);
@@ -17,7 +19,7 @@ export default class Counts {
17
19
  * @param {Object} json.
18
20
  */
19
21
  fromJson (json) {
20
- _.each(this.model.constructor.counts(), (options, key) => {
22
+ _.each(this.#model.constructor.counts(), (options, key) => {
21
23
  let value = _.get(json, this.key(key));
22
24
 
23
25
  value = value !== undefined
@@ -34,7 +36,7 @@ export default class Counts {
34
36
  toJson () {
35
37
  const json = {};
36
38
 
37
- _.each(this.model.constructor.counts(), (options, key) => {
39
+ _.each(this.#model.constructor.counts(), (options, key) => {
38
40
  _.set(json, key, this.get(key));
39
41
  });
40
42
 
@@ -47,7 +49,7 @@ export default class Counts {
47
49
  * @returns {*} The value of the attribute.
48
50
  */
49
51
  get (attribute) {
50
- return _.get(this.model, this.key(attribute, true));
52
+ return _.get(this.#model, this.key(attribute, true));
51
53
  }
52
54
 
53
55
  /**
@@ -57,7 +59,7 @@ export default class Counts {
57
59
  * @returns {*} The value that was set.
58
60
  */
59
61
  set (attribute, value) {
60
- this.model[this.key(attribute, true)] = value;
62
+ this.#model[this.key(attribute, true)] = value;
61
63
 
62
64
  return value;
63
65
  }
@@ -4,11 +4,13 @@ import Caster from '../caster';
4
4
  * @implements {Property}
5
5
  */
6
6
  export default class Relations {
7
+ #model;
8
+
7
9
  /**
8
10
  * @param {Model} model Instance of the model.
9
11
  */
10
12
  constructor (model) {
11
- this.model = model;
13
+ this.#model = model;
12
14
 
13
15
  _.each(model.constructor.relations(), (options, key) => {
14
16
  this.set(key, _.has(options, 'default') ? options.default : null);
@@ -19,7 +21,7 @@ export default class Relations {
19
21
  * @param {Object} json.
20
22
  */
21
23
  fromJson (json) {
22
- _.each(this.model.constructor.relations(), (options, key) => {
24
+ _.each(this.#model.constructor.relations(), (options, key) => {
23
25
  let value = _.get(json, options.jsonKey ?? _.snakeCase(key), _.get(json, _.camelCase(key)));
24
26
 
25
27
  value = value
@@ -36,7 +38,7 @@ export default class Relations {
36
38
  toJson () {
37
39
  const json = {};
38
40
 
39
- _.each(this.model.constructor.relations(), (options, key) => {
41
+ _.each(this.#model.constructor.relations(), (options, key) => {
40
42
  let jsonValue = this.get(key);
41
43
 
42
44
  jsonValue = Relations._getCastedToJsonValue(jsonValue, options);
@@ -53,7 +55,7 @@ export default class Relations {
53
55
  * @returns {*} The value of the relation.
54
56
  */
55
57
  get (relation) {
56
- return _.get(this.model, relation);
58
+ return _.get(this.#model, relation);
57
59
  }
58
60
 
59
61
  /**
@@ -64,7 +66,7 @@ export default class Relations {
64
66
  */
65
67
  set (relation, value = null) {
66
68
  if (_.isObject(relation)) {
67
- _.each(this.model.constructor.relations(), (options, key) => {
69
+ _.each(this.#model.constructor.relations(), (options, key) => {
68
70
  if (! _.has(relation, key)) return;
69
71
 
70
72
  this.set(key, relation[key]);
@@ -73,7 +75,7 @@ export default class Relations {
73
75
  return this;
74
76
  }
75
77
 
76
- this.model[relation] = value;
78
+ this.#model[relation] = value;
77
79
 
78
80
  return this;
79
81
  }
@@ -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