mythix-orm 1.4.2 → 1.4.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/docs/Home.md CHANGED
@@ -1 +1,51 @@
1
- Welcome to the mythix-orm wiki!
1
+ # Welcome to the Mythix ORM wiki
2
+
3
+ ## Getting started
4
+
5
+ Here you will not only find documentation for the Mythix ORM API, but also articles (in the "Pages" section) that explain in detail the inner workings of Mythix ORM.
6
+
7
+ To start with, we highly recommend reading the following articles:
8
+
9
+ 1. [Query Engine](./QueryEngine)
10
+ 2. [Associations](./Associations)
11
+
12
+ These should give you a basic idea on how to work with Mythix ORM.
13
+
14
+ After you have read these articles and understand what you are working with, take a look at the API documentation to understand _what_ you have available to you, and how you can use it.
15
+
16
+ ## What Mythix ORM is (and is not)
17
+
18
+ Mythix ORM was designed and created to replace all the terrible other options that currently exist for Node. I mean, have you ever looked at the source code for some of the other popular ORM libraries out there? It is the stuff of nightmares!
19
+
20
+ I was tired of daily having to deal with malarky spewed by the other ORM libraries and their very poorly designed interfaces, their bloat, and their difficulty to setup and use (to say it mildly).
21
+
22
+ Though I firmly believe ORMs are--in nature--a poor and terrible fix for the plethora if crap us developers must go through daily trying to work with outdated and non-standard databases, they do still have their place in our world (until the database "situation" is resolved). So, while ORMs are still needed, why not at least use the best one available?
23
+
24
+ Meet Mythix ORM. It was designed from the ground-up to replace *all* other existing ORMs, forever. As part of the design, a few key decisions were made to make Mythix ORM a far better solution than the other "solutions" out there.
25
+
26
+ Let's go over those key design decisions real quick so we are all on the same page:
27
+
28
+ 1. Mythix ORM will never be bloated. It has a specific purpose, and it will stick to that purpose. It is an abstraction layer between different database types. It doesn't intend or pretend to be anything else. Mythix ORM is *bare bones*, just what you need, and nothing more. It is the intent and hope of the authors that Mythix ORM will grow a thriving community, and it was a deliberate design decision that the community would build extra libraries to add features to Mythix ORM. The motto over here in Mythix land is "take what you need, and nothing more".
29
+ 2. Mythix ORM is very extensible. Nearly every part of Mythix ORM can be overloaded, hijacked, or replaced entirely. Again, the intent is that the community will provide many cool features through extra libraries that *add* to Mythix ORM's feature set. It was also the intent of the authors who designed Mythix ORM to have it so developers could work with it *the way they like to*, without any of the mandates, and the "world **should** work this way" shenanigans. Mythix ORM is very powerful, and absolutely *will* allow you to shoot yourself in the foot--if you, as the developer, decide that is what you want to do.
30
+ 3. The primary author of this library--Wyatt Greenway--doesn't like the "black box" mentality. Encapsulation is absolutely a must for clean and stable code, but "black box"? Never. For this reason Mythix ORM was designed with no private variables, and nearly every method exists on a class, with the deliberate intent that methods could (and should) be overloaded to provide extra functionality. Please *do* overload methods, and change the behavior of Mythix ORM to fit your team needs. But *please* be a good citizen, and read and understand what you are doing and why before you do it. Mythix ORM will naturally change its interfaces over time (with appropriate versioning applied to each release), and it is up to the downstream developers to update and manage their custom code.
31
+ 4. Mythix ORM deliberately tries to abstract everything it can away from the database. Because of this, you will often find cases where you might need to do things differently then you are used to, or you might find some of our design decisions a little strange. ORMs are *supposed* to be an abstraction layer, so when I see other ORMs suggesting database specific code in their documentation it makes me shudder. Obviously there are times where this can not be avoided, and Mythix ORM does its best to handle these cases in an abstract way. However, there may be times where you just need to make a direct query to do something with your database, or use a custom literal, and that is okay. One area that you will immediately notice this abstraction is the field types. There is a shockingly small number of field types available in Mythix ORM, and this is deliberate. Mythix ORM will *never* supply database specific field types... if you need those, you can use literals, or you can define your own field types to suit your needs. The situation also isn't as bad as you might initially think. Take the `INTEGER` type for example. It is designed such that it can receive as an optional parameter the "number of bytes" needed to store a certain integer type--and then it is up to the specific connection you are using to decide how to implement said type. For example, Mythix ORM does not supply the MySQL specific types like `TINYINT`, `SMALLINT`, or `MEDIUMINT`. Instead, you always simply use `INTEGER`, and specify the number of bytes you need, and the MySQL connection will take care of the rest for you. Field types are just one example. Mythix ORM will abstract away everything it can by deliberate design.
32
+ 5. Mythix ORM and its API were deliberately designed to be as simple as possible, with all the complexity required tucked neatly away in the modular connection drivers. Deliberate intent and lots of thinking went into making the interface simple, yet flexible and powerful. For these reasons Mythix ORM is broken into many pieces, so the developer can pick and choose only what they want. Mythix ORM was also deliberately designed so that it could be run inside a browser. "Why in the heck would I ever do that?" I hear you asking. Well, Mythix ORM has more to offer than just being a layer between databases. Its model system, and especially its query system, are slick, and useful outside the context of server-only. I hope to one day see "browser based" connections that allow developers to use the same powerful query interface built into Mythix ORM inside the browser. I mean, how cool would it be to use the powerful built in query-engine inside the browser? Maybe to generate GraphQL? Or a custom query interface over HTTP? Let's think big and outside the box.
33
+ 6. SQL/NoSQL, who cares? From the ground up Mythix ORM was designed to support SQL and NoSQL (or even completely custom) databases. Connection drivers are painless to create, and the horizon is the limit. Currently Mythix ORM officially only has drivers for `PostgreSQL` and `SQLite`, but in the future it plans (and intends) to support every database on the planet, including file storage systems, and in-memory databases. If you don't see the database driver you are looking for, be patient, or better yet, help out! Before Mythix ORM is considered "done" it will have drivers for `MySQL`, `Microsoft SQL`, `Mongo`, `Snowflake`, `SOLR`, `ElasticSearch`, and many more.
34
+
35
+ Now let's take a little moment to discuss what Mythix ORM **is not**:
36
+
37
+ 1. Mythix ORM **is not** bloated, and it never intends to be. We will continue with the modular architecture, giving developers the ability to choose what they want to include (and what they don't want to include). This also means that you, as the developer, need to pay a little more attention. You can't just install Mythix ORM and away you go. You also need to select which database driver you need, and install it manually beside Mythix ORM.
38
+ 2. Mythix ORM **is not** "magical". In fact, we despise "magic" and won't have any of it. Everything must be clearly defined, and must visibly exist somewhere. Mythix ORM will not automatically create fields for you (i.e. `created_at` and `updated_at`), not even for relationships, polymorphic or not. The tedium you may come across was also considered, and accounted for. For example, if you *want* everyone of your models to have a `created_at`, and `updated_at` field, then simply create a base model that all of your other models inherit from, and on the base model itself you can define these "common" fields. The intent is to give the developer full control, while keeping all "magic" off the table.
39
+ 3. Mythix ORM **is not** opinionated. It is actually the opposite. It is the intent of the authors that developers will use and modify this library *exactly how they want to*. We don't make glaring statements like "We won't include a `toSQL` method because no one *should* ever use such a thing!", or silly statements like "You *should* do things our way... because we obviously know best". Ha! Whatever! No, we don't know what is best, and we openly admit it. You, **the developer**, know what is best, and we support you. Do want you want to do, and do it well. Make your mommy proud! Just remember that when you shoot yourself in the foot, it is *your foot*, not ours.
40
+ 4. Mythix ORM **is not** a swiss-army knife, batteries included, does everything under the sun type of library. It can do everything you need it to do however, and it does so in a modular, community supported way. If you want extra features, build them, or rely on what the community builds. Mythix ORM will deliberately always remain small, slim, sleek, and do exactly what it does the best it can, and nothing more.
41
+ 5. We--the authoring team of Mythix ORM--do **not** always know what is best. We know this, and are humble enough to admit it. This is one of the prime reasons behind deciding to lean heavily on the community for support and extra features. When the community builds something that thousands of people are using regularly, then--and only then--will we consider adding it as a core feature to Mythix ORM. In short, we want the community to drive the bus. We will take a back seat and see where it goes, cheering and supporting the entire trip.
42
+
43
+ ## Mythix certifications
44
+
45
+ Mythix, as a community of libraries, has a certification program. We will certify third party libraries, and we recommend that you select certified libraries first. Why are we doing this? Part of the mission Mythix has is to reengage engineers to actually be engineers, instead of StackOverflow monkeys. We can not even begin to tell you how many times we have wept over the stank we have seen in open source libraries. We would prefer that Mythix, as a community, not be involved in the stank of the rest of the world.
46
+
47
+ For this reason we will maintain a list of "certified" libraries, whose code bases have been groomed for cleanliness and correctness. We will also maintain a "black list" of libraries that use Mythix technologies, but that have severe stank. We welcome all developers, and hope to build a thriving community around Mythix technologies... but be warned, if you are a poor engineer and poop out poor code, then your project might end up on a blacklist...
48
+
49
+ The above statements are intended to inspire all developers to become better versions of themselves. Our world is a train wreck of smelly broken code (that is often popular, unfortunately). Mythix hopes to take a hand in changing the landscape, by lifting developers up and supporting good engineers, helping them standout as pillars of hope against the background noise.
50
+
51
+ We won't be rude, unfair, or bigoted. We will be precise, keen-eyed, care about what we build, and drive engineers to be better engineers. We like the word "accountable", and would like to hold ourselves and our community of engineers to a higher standard. Let's build great things together, not smelly things that everyone hates. Our certification and blacklists will simply serve as a means to try and prod engineers to do a better job, and to aspire to a higher standard.
@@ -383,8 +383,10 @@ Connection interface methods are methods which hand off the query to a connectio
383
383
  12. `NOT.LT(...)` (greater than or equal to `>=`)
384
384
  13. `LTE(...)` (less than or equal to `<=`)
385
385
  14. `NOT.LTE(...)` (greater than `>`)
386
- 15. `LIKE` **coming soon!**
387
- 16. `NOT.LIKE` **coming soon!**
386
+ 15. `LIKE(...)` (pattern match `LIKE '%something%'`)
387
+ 16. `LIKE(..., { caseSensitive: true })` (pattern match `LIKE '%something%'`) [only supported in PostgreSQL -- default is `ILIKE`]
388
+ 17. `NOT.LIKE(...)` (not matching pattern `NOT LIKE '%something%'`)
389
+ 18. `NOT.LIKE(..., { caseSensitive: true })` (not matching pattern `NOT LIKE '%something%'`) [only supported in PostgreSQL -- default is `NOT ILIKE`]
388
390
 
389
391
  ### Control
390
392
 
@@ -13,6 +13,10 @@ const Types = require('../types');
13
13
 
14
14
  const LiteralBase = Literals.LiteralBase;
15
15
 
16
+ /// ConnectionBase is the base class that
17
+ /// all connection classes should inherit from.
18
+ ///
19
+ /// Alias: Connection
16
20
  class ConnectionBase extends EventEmitter {
17
21
  static dialect = 'none';
18
22
 
@@ -353,28 +353,31 @@ class QueryGeneratorBase {
353
353
  let projectedFields = _projectedFields;
354
354
  let result;
355
355
 
356
- // If we are sub-selecting then only the last
357
- // field in the projection should be the projection
358
356
  if (options && options.isSubQuery) {
359
- result = result = Array.from(projectedFields.values());
357
+ // If we are sub-selecting then only one
358
+ // field in the projection is allowed
360
359
 
361
- let lastField = result[result.length - 1];
360
+ result = Array.from(projectedFields.values());
361
+ if (result.length !== 1)
362
+ throw new Error(`${this.constructor.name}::getProjectionFromQueryEngine: Only one field is allowed in the projection of a sub-query.`);
363
+
364
+ let subQueryField = result[0];
362
365
 
363
- if (LiteralBase.isLiteral(lastField))
364
- result = [ lastField ];
365
- else if (typeof lastField === 'string')
366
- result = [ lastField ];
366
+ if (LiteralBase.isLiteral(subQueryField))
367
+ result = [ subQueryField ];
368
+ else if (typeof subQueryField === 'string')
369
+ result = [ subQueryField ];
367
370
  else
368
- result = [ new Literals.DistinctLiteral(lastField.fullFieldName) ];
371
+ result = [ new Literals.DistinctLiteral(subQueryField.fullFieldName) ];
369
372
  } else {
373
+ projectedFields = addRequiredFieldsToProjection(projectedFields);
374
+
370
375
  // If distinct specifies a field, then
371
376
  // make sure that field isn't specified twice
372
377
  if (hasDistinct) {
373
378
  let distinctFieldName = hasDistinct.getFullyQualifiedFieldName();
374
379
  if (distinctFieldName)
375
380
  projectedFields.delete(distinctFieldName);
376
- } else {
377
- projectedFields = addRequiredFieldsToProjection(projectedFields);
378
381
  }
379
382
 
380
383
  result = Array.from(projectedFields.values());
package/lib/model.js CHANGED
@@ -6,12 +6,32 @@ const Type = require('./types/type');
6
6
  const DefaultHelpers = require('./types/helpers/default-helpers');
7
7
  const Field = require('./field');
8
8
 
9
- // Used as a unique key for cache
9
+ /// Used as a unique key for cache.
10
+ /// Caches will generally be stored in
11
+ /// a Map, and since a Map's keys can
12
+ /// be any instance, we use a "CacheKey"
13
+ /// object on Models that will change
14
+ /// when they are dirty. This will allow
15
+ /// downstream libraries to use this "dirty"
16
+ /// key as a cache key.
10
17
  class CacheKey {
18
+ /// A "number" argument is provided here
19
+ /// as a way to get an "id" from a CacheKey.
20
+ /// Another way to view this "number" is
21
+ /// a cache version. This number is incremented
22
+ /// every time a model's fields get dirty.
23
+ ///
24
+ /// Arguments:
25
+ /// number: number
26
+ /// An incrementing number, or "version" for the CacheKey
11
27
  constructor(number) {
12
28
  this.number = (number == null) ? 0 : (number.valueOf() + 1);
13
29
  }
14
30
 
31
+ /// Return the "number" or "version"
32
+ /// of this CacheKey.
33
+ ///
34
+ /// Return: number
15
35
  valueOf() {
16
36
  return this.number;
17
37
  }
@@ -85,9 +105,28 @@ function bindStaticWhereToModelClass(ModelClass) {
85
105
  });
86
106
  }
87
107
 
108
+ /// The base Model class for Mythix ORM.
109
+ /// Every Mythix ORM model should inherit
110
+ /// from this class. This class provides
111
+ /// support for all model operations in
112
+ /// Mythix ORM.
88
113
  class Model {
114
+ /// This property assists with type checking
115
+ ///
116
+ /// Type: boolean
89
117
  static _isMythixModel = true;
90
118
 
119
+ /// Use this method to check if a class
120
+ /// is a Mythix ORM model. It will return
121
+ /// `true` if the provided value is a class
122
+ /// that inherits from <see>Model</see>, or
123
+ /// if the provided value has an attribute
124
+ /// named `_isMythixModel` that is truthy.
125
+ ///
126
+ /// Return: boolean
127
+ /// Arguments:
128
+ /// value: Function
129
+ /// Value to check.
91
130
  static isModelClass(value) {
92
131
  if (!value)
93
132
  return false;
@@ -101,6 +140,20 @@ class Model {
101
140
  return false;
102
141
  }
103
142
 
143
+ /// Check to see if the provided value is
144
+ /// an *instance* of a Mythix ORM <see>Model</see>.
145
+ /// Unlike <see>Model.isModelClass</see>, which
146
+ /// checks if a *class* is a <see>Model</see>, this will check
147
+ /// to see if an *instance* is an instance of a
148
+ /// Mythix ORM <see>Model</see>. It will return
149
+ /// `true` if the provided value is an `instanceof`
150
+ /// <see>Model</see>, or if the values `constructor`
151
+ /// property has a truthy `_isMythixModel` property.
152
+ ///
153
+ /// Return: boolean
154
+ /// Arguments:
155
+ /// value: any
156
+ /// Value to check
104
157
  static isModel(value) {
105
158
  if (!value)
106
159
  return false;
@@ -114,6 +167,18 @@ class Model {
114
167
  return false;
115
168
  }
116
169
 
170
+ /// Like everywhere in Javascript, we can
171
+ /// call `.toString()` to a get a string
172
+ /// representation of our <see>Model</see>
173
+ /// *class*. The optional `showFields` argument,
174
+ /// if true, will list the models fields as well.
175
+ /// Without the `showFields` argument, the model
176
+ /// name alone will be returned as a string.
177
+ ///
178
+ /// Return: string
179
+ /// Arguments:
180
+ /// showFields?: boolean
181
+ /// If `true`, then list the models fields.
117
182
  static toString(showFields) {
118
183
  let fieldNames = [];
119
184
 
@@ -134,6 +199,30 @@ class Model {
134
199
  return this.toString((depth !== 0));
135
200
  }
136
201
 
202
+ /// Get the underlying connection bound to
203
+ /// the model's class. Connection binding is
204
+ /// optional, so this method can also be provided
205
+ /// a connection. If a connection is provided, then
206
+ /// it will simply be returned. Because Mythix ORM
207
+ /// has no globals, this is a design pattern to
208
+ /// supply a connection if you have one available,
209
+ /// or fallback to a "bound" connection (if one can
210
+ /// be found).
211
+ ///
212
+ /// Return: <see>Connection</see>
213
+ /// Arguments:
214
+ /// connection?: <see>Connection</see>
215
+ /// An optional connection that can be provided.
216
+ /// If provided, it will simply be returned.
217
+ /// Otherwise, if not provided, or a falsy value,
218
+ /// then attempt to fallback to the bound connection,
219
+ /// if any is available.
220
+ /// Note:
221
+ /// An underscore prefix on a method in Mythix ORM
222
+ /// implies that this method should not be overloaded
223
+ /// unless you know exactly what you are doing.
224
+ /// It also implies that there is another method
225
+ /// that can and should be overloaded instead.
137
226
  static _getConnection(_connection) {
138
227
  return _connection || this._mythixBoundConnection;
139
228
  }
@@ -4,16 +4,21 @@ const Nife = require('nife');
4
4
  const ProxyClass = require('../proxy-class');
5
5
  const QueryEngineBase = require('./query-engine-base');
6
6
 
7
- function addOperatorToQuery(name, inverseName, value) {
7
+ function addOperatorToQuery(name, inverseName, value, extraOptions) {
8
8
  if (Nife.instanceOf(value, 'object'))
9
9
  throw new Error(`QueryEngine::addOperatorToQuery: ${name}(${value}) makes no sense...`);
10
10
 
11
- this._addToQuery({
11
+ let conditionalParams = {
12
12
  condition: true,
13
13
  operator: name,
14
14
  inverseOperator: inverseName,
15
15
  value: this._fetchOperatorValue(value),
16
- });
16
+ };
17
+
18
+ if (extraOptions)
19
+ conditionalParams = Object.assign(conditionalParams, extraOptions);
20
+
21
+ this._addToQuery(conditionalParams);
17
22
 
18
23
  return this._fetchScope('model');
19
24
  }
@@ -29,9 +34,6 @@ class FieldScope extends QueryEngineBase {
29
34
  }
30
35
 
31
36
  toString(...args) {
32
- if (args.length === 0)
33
- return `${this.constructor.name} {}`;
34
-
35
37
  return this.currentContext.queryEngineScope.toString(...args);
36
38
  }
37
39
 
@@ -86,12 +88,14 @@ class FieldScope extends QueryEngineBase {
86
88
  return addOperatorToQuery.call(this, 'LTE', 'GT', value);
87
89
  }
88
90
 
89
- LIKE(value) {
90
- return addOperatorToQuery.call(this, 'LIKE', 'NOT_LIKE', value);
91
+ LIKE(value, options) {
92
+ let caseSensitive = ((options && options.caseSensitive) === true);
93
+ return addOperatorToQuery.call(this, 'LIKE', 'NOT_LIKE', value, { caseSensitive });
91
94
  }
92
95
 
93
- NOT_LIKE(value) {
94
- return addOperatorToQuery.call(this, 'NOT_LIKE', 'LIKE', value);
96
+ NOT_LIKE(value, options) {
97
+ let caseSensitive = ((options && options.caseSensitive) === true);
98
+ return addOperatorToQuery.call(this, 'NOT_LIKE', 'LIKE', value, { caseSensitive });
95
99
  }
96
100
 
97
101
  [ProxyClass.MISSING](target, prop) {
@@ -43,9 +43,6 @@ class ModelScope extends QueryEngineBase {
43
43
  }
44
44
 
45
45
  toString(...args) {
46
- if (args.length === 0)
47
- return `${this.constructor.name} {}`;
48
-
49
46
  return this.currentContext.queryEngineScope.toString(...args);
50
47
  }
51
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm",
3
- "version": "1.4.2",
3
+ "version": "1.4.5",
4
4
  "description": "ORM for Mythix framework",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",