Comparing version 0.4.3 to 0.4.4
@@ -0,1 +1,6 @@ | ||
0.4.4 / 2019-07-15 | ||
================== | ||
* Fix: append default scope when declaring relations, fixes #10 | ||
0.4.3 / 2019-05-09 | ||
@@ -2,0 +7,0 @@ ================== |
@@ -32,3 +32,2 @@ 'use strict' | ||
* Convert strings connected with hyphen or underscore into camel case. e.g. | ||
* @param {string} str | ||
* @example | ||
@@ -38,2 +37,4 @@ * camelCase('FooBar') // => 'fooBar' | ||
* camelCase('foo_bar') // => 'fooBar' | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
@@ -46,6 +47,7 @@ function camelCase(str) { | ||
* Convert strings from camelCase to snake_case. | ||
* @param {string} str | ||
* @example | ||
* snakeCase('FooBar') // => 'foo_bar' | ||
* snakeCase('fooBar') // => 'foo_bar' | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
@@ -749,3 +751,5 @@ function snakeCase(str) { | ||
} | ||
this.relations[name] = { ...opts, Model: this.reflectClass(opts.className) } | ||
const Model = this.reflectClass(opts.className) | ||
if (Model.schema.deletedAt && !opts.where) opts.where = { deletedAt: null } | ||
this.relations[name] = { ...opts, Model } | ||
} | ||
@@ -867,6 +871,6 @@ | ||
/** | ||
* An alias of {@link Bone.find} without any conditions. To get all records in database, including those ones marked deleted, use {@link Bone.unscoped}. This getter returns all records by querying them at once, which can be inefficient if table contains loads of data. It is recommended to consume data by {@link Bone.batch}. | ||
* An alias of {@link Bone.find} without any conditions. To get all records in database, including those ones marked deleted, use {@link spell#$unscoped}. This getter returns all records by querying them at once, which can be inefficient if table contains loads of data. It is recommended to consume data by {@link spell#$batch}. | ||
* @example | ||
* Post.all // fetches at once. | ||
* Post.unscoped // fetches (soft) deleted records too. | ||
* Post.all.unscoped // fetches (soft) deleted records too. | ||
* Post.all.batch() // fetches records 1000 by 1000s. | ||
@@ -873,0 +877,0 @@ * @returns {Spell} |
@@ -946,7 +946,9 @@ 'use strict' | ||
*/ | ||
function Spell_deletedAtIsNull() { | ||
const { Model, table, whereConditions } = this | ||
const { schema } = Model | ||
function Spell_deletedAtIsNull(spell) { | ||
const { table, whereConditions } = spell | ||
if (!schema.deletedAt) return | ||
// from subquery | ||
if (table.type !== 'id') return | ||
// deletedAt already specified | ||
for (const condition of whereConditions) { | ||
@@ -961,3 +963,4 @@ let found = false | ||
} | ||
if (table.type == 'id') this.$where({ deletedAt: null }) | ||
spell.$where({ deletedAt: null }) | ||
} | ||
@@ -992,7 +995,7 @@ | ||
* Create a spell. | ||
* @param {Model} Model - A sub class of {@link Bone}. | ||
* @param {Spell~factory} factory - The factory function to call when spell.then is called. | ||
* @param {Object} opts - Extra attributes to be set. | ||
* @param {Model} Model - A sub class of {@link Bone}. | ||
* @param {Spell~callback} callback - The callback function to call when spell.then is called. | ||
* @param {Object} opts - Extra attributes to be set. | ||
*/ | ||
constructor(Model, factory, opts) { | ||
constructor(Model, callback, opts) { | ||
/** | ||
@@ -1002,8 +1005,13 @@ * A sub-class of Bone. | ||
this.Model = Model | ||
/** | ||
* The factory callback which gets the final SQL string, execute it on database, and return the dispatched results. The results might be an instance of model, a {@link Collection} of models, or a result set. See {@link Bone.find} for detail of this callback. | ||
* @callback Spell~factory | ||
* The callback callback which gets the final SQL string, execute it on database, and return the dispatched results. The results might be an instance of model, a {@link Collection} of models, or a result set. See {@link Bone.find} for detail of this callback. | ||
* @callback Spell~callback | ||
* @param {Spell} - the final spell | ||
*/ | ||
this.factory = factory | ||
this.callback = callback | ||
const scopes = [] | ||
if (Model.schema.deletedAt) scopes.push(Spell_deletedAtIsNull) | ||
Object.assign(this, { | ||
@@ -1019,3 +1027,3 @@ command: 'select', | ||
skip: 0, | ||
scopes: [ Spell_deletedAtIsNull ], | ||
scopes, | ||
subqueryIndex: 0 | ||
@@ -1092,3 +1100,3 @@ }, opts) | ||
get dup() { | ||
return new Spell(this.Model, this.factory, { | ||
return new Spell(this.Model, this.callback, { | ||
command: this.command, | ||
@@ -1115,7 +1123,7 @@ columns: [...this.columns], | ||
$get(index) { | ||
const { factory, Model } = this | ||
const { callback, Model } = this | ||
this.$limit(1) | ||
if (index > 0) this.$offset(index) | ||
this.factory = spell => { | ||
return factory(spell).then(results => { | ||
this.callback = spell => { | ||
return callback(spell).then(results => { | ||
const result = results[0] | ||
@@ -1138,3 +1146,3 @@ return result instanceof Model ? result : null | ||
this.promise = new Promise(resolve => { | ||
resolve(this.factory.call(null, this)) | ||
resolve(this.callback.call(null, this)) | ||
}) | ||
@@ -1464,3 +1472,3 @@ return this.promise.then(resolve, reject) | ||
format() { | ||
for (const scope of this.scopes) scope.call(this) | ||
for (const scope of this.scopes) scope(this) | ||
@@ -1467,0 +1475,0 @@ switch (this.command) { |
{ | ||
"name": "leoric", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "JavaScript Object-relational mapping alchemy", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
120320
3377