@fxjs/orm
Advanced tools
Comparing version 1.14.0 to 1.14.1
@@ -334,4 +334,4 @@ /// <reference lib="es5" /> | ||
Instance.$emit(`after:set:${association.name}`, $ref.associations); | ||
if (Instance.__opts.keys.length === 1) { | ||
const [key] = Instance.__opts.keys; | ||
if (Instance.__instRtd.keys.length === 1) { | ||
const [key] = Instance.__instRtd.keys; | ||
$ref.association_ids = $ref.associations.map((x) => x[key]); | ||
@@ -519,7 +519,7 @@ } | ||
get: function () { | ||
return Instance.__opts.associations[association.name].value; | ||
return Instance.__instRtd.associations[association.name].value; | ||
}, | ||
set: function (val) { | ||
Instance.__opts.associations[association.name].changed = true; | ||
Instance.__opts.associations[association.name].value = val; | ||
Instance.__instRtd.associations[association.name].changed = true; | ||
Instance.__instRtd.associations[association.name].value = val; | ||
}, | ||
@@ -540,5 +540,5 @@ enumerable: true | ||
// Set this way to prevent setting 'changed' status | ||
Instance.__opts.associations[association.name].value = Assoc; | ||
Instance.__instRtd.associations[association.name].value = Assoc; | ||
} | ||
catch (err) { } | ||
} |
@@ -45,9 +45,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
function manyAssocsFromInst(instance) { | ||
return instance.__opts.many_associations; | ||
return instance.__instRtd.many_associations; | ||
} | ||
function oneAssocsFromInst(instance) { | ||
return instance.__opts.one_associations; | ||
return instance.__instRtd.one_associations; | ||
} | ||
function extendsToAssocsFromInst(instance) { | ||
return instance.__opts.extend_associations; | ||
return instance.__instRtd.extend_associations; | ||
} | ||
@@ -54,0 +54,0 @@ /* by instance extname :start */ |
@@ -6,2 +6,3 @@ /// <reference types="@fibjs/types" /> | ||
const events_1 = require("events"); | ||
const FxORMCore = require("@fxjs/orm-core"); | ||
const Utilities = require("./Utilities"); | ||
@@ -13,11 +14,11 @@ const Hook = require("./Hook"); | ||
exports.Instance = function (Model, _opts) { | ||
const opts = util.extend({}, _opts); | ||
opts.data = opts.data || {}; | ||
opts.extra = opts.extra || {}; | ||
opts.keys = (opts.keys || "id"); | ||
opts.changes = (opts.is_new ? Object.keys(opts.data) : []); | ||
opts.extrachanges = []; | ||
opts.associations = {}; | ||
opts.events = util.extend({}, opts.events); | ||
opts.originalKeyValues = {}; | ||
const instRtd = util.extend({}, _opts); | ||
instRtd.data = instRtd.data || {}; | ||
instRtd.extra = instRtd.extra || {}; | ||
instRtd.keys = (instRtd.keys || "id"); | ||
instRtd.changes = (instRtd.isNew ? Object.keys(instRtd.data) : []); | ||
instRtd.extrachanges = []; | ||
instRtd.associations = {}; | ||
instRtd.events = util.extend({}, instRtd.events); | ||
instRtd.originalKeyValues = {}; | ||
const eventor = new events_1.EventEmitter(); | ||
@@ -30,5 +31,5 @@ var instance_saving = false; | ||
var rememberKeys = function () { | ||
for (let i = 0; i < opts.keyProperties.length; i++) { | ||
const prop = opts.keyProperties[i]; | ||
opts.originalKeyValues[prop.name] = opts.data[prop.name]; | ||
for (let i = 0; i < instRtd.keyProperties.length; i++) { | ||
const prop = instRtd.keyProperties[i]; | ||
instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name]; | ||
} | ||
@@ -47,3 +48,3 @@ }; | ||
let validationErr; | ||
Hook.wait(instance, opts.hooks.beforeValidation, function (err) { | ||
Hook.wait(instance, instRtd.hooks.beforeValidation, function (err) { | ||
if (err) { | ||
@@ -56,3 +57,3 @@ validationErr = err; | ||
const checks = new enforce.Enforce({ returnAllErrors }); | ||
for (let k in opts.validations) { | ||
for (let k in instRtd.validations) { | ||
required = false; | ||
@@ -64,6 +65,6 @@ if (Model.allProperties[k]) { | ||
else { | ||
for (let i = 0; i < opts.one_associations.length; i++) { | ||
for (let i = 0; i < instRtd.one_associations.length; i++) { | ||
/* non-normalized `field` maybe string now */ | ||
if (opts.one_associations[i].field === k) { | ||
required = opts.one_associations[i].required; | ||
if (instRtd.one_associations[i].field === k) { | ||
required = instRtd.one_associations[i].required; | ||
break; | ||
@@ -76,3 +77,3 @@ } | ||
} | ||
for (const validator of opts.validations[k]) { | ||
for (const validator of instRtd.validations[k]) { | ||
checks.add(k, validator); | ||
@@ -83,3 +84,3 @@ } | ||
checks.context("model", Model); | ||
checks.context("driver", opts.driver); | ||
checks.context("driver", instRtd.driver); | ||
const errors = checks.checkSync(instance); | ||
@@ -94,3 +95,3 @@ if (errors && errors.length) | ||
emitCallbackStyleEvent("save", err, instance); | ||
Hook.trigger(instance, opts.hooks.afterSave, false); | ||
Hook.trigger(instance, instRtd.hooks.afterSave, false); | ||
return instance; | ||
@@ -112,6 +113,6 @@ }; | ||
} | ||
if (opts.is_new) { | ||
if (instRtd.isNew) { | ||
Utilities.attachOnceTypedHookRefToInstance(instance, 'create', {}); | ||
Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {}); | ||
Hook.wait(instance, [opts.hooks['beforeCreate'], opts.hooks['beforeSave']], function (err) { | ||
Hook.wait(instance, [instRtd.hooks['beforeCreate'], instRtd.hooks['beforeSave']], function (err) { | ||
if (err) { | ||
@@ -126,3 +127,3 @@ saveError(err); | ||
Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {}); | ||
Hook.wait(instance, [opts.hooks['beforeSave']], function (err) { | ||
Hook.wait(instance, [instRtd.hooks['beforeSave']], function (err) { | ||
if (err) { | ||
@@ -140,4 +141,4 @@ saveError(err); | ||
if (is_create) | ||
Hook.trigger(instance, opts.hooks.afterCreate, !err); | ||
Hook.trigger(instance, opts.hooks.afterSave, !err); | ||
Hook.trigger(instance, instRtd.hooks.afterCreate, !err); | ||
Hook.trigger(instance, instRtd.hooks.afterSave, !err); | ||
}; | ||
@@ -147,19 +148,19 @@ const getInstanceData = function () { | ||
let prop; | ||
for (let k in opts.data) { | ||
if (!opts.data.hasOwnProperty(k)) | ||
for (let k in instRtd.data) { | ||
if (!instRtd.data.hasOwnProperty(k)) | ||
continue; | ||
prop = Model.allProperties[k]; | ||
if (prop) { | ||
if (opts.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) { | ||
if (instRtd.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) { | ||
continue; | ||
} | ||
if (opts.driver.propertyToValue) { | ||
data[k] = opts.driver.propertyToValue(opts.data[k], prop); | ||
if (instRtd.driver.propertyToValue) { | ||
data[k] = instRtd.driver.propertyToValue(instRtd.data[k], prop); | ||
} | ||
else { | ||
data[k] = opts.data[k]; | ||
data[k] = instRtd.data[k]; | ||
} | ||
} | ||
else { | ||
data[k] = opts.data[k]; | ||
data[k] = instRtd.data[k]; | ||
} | ||
@@ -170,17 +171,17 @@ } | ||
const resetChanges = function () { | ||
opts.changes.length = 0; | ||
instRtd.changes.length = 0; | ||
}; | ||
const saveNewSync = function (saveOptions, data) { | ||
data = Utilities.transformPropertyNames(data, Model.allProperties); | ||
const info = opts.driver.insert(opts.table, data, opts.keyProperties); | ||
const info = instRtd.driver.insert(instRtd.table, data, instRtd.keyProperties); | ||
resetChanges(); | ||
for (let i = 0, prop; i < opts.keyProperties.length; i++) { | ||
prop = opts.keyProperties[i]; | ||
opts.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name]; | ||
for (let i = 0, prop; i < instRtd.keyProperties.length; i++) { | ||
prop = instRtd.keyProperties[i]; | ||
instRtd.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name]; | ||
} | ||
opts.is_new = false; | ||
instRtd.isNew = false; | ||
rememberKeys(); | ||
let err; | ||
if (shouldSaveAssocs(saveOptions)) { | ||
const syncReponse = Utilities.catchBlocking(saveAssociationsSync); | ||
const syncReponse = FxORMCore.catchBlocking(saveAssociationsSync); | ||
err = syncReponse.error; | ||
@@ -204,3 +205,3 @@ } | ||
} | ||
const { error: err, result: assocSaved } = Utilities.catchBlocking(saveAssociationsSync); | ||
const { error: err, result: assocSaved } = FxORMCore.catchBlocking(saveAssociationsSync); | ||
if (saved || assocSaved) { | ||
@@ -215,14 +216,14 @@ runSyncAfterSaveActions(false, err); | ||
return savedCheckSync(false); | ||
for (let i = 0; i < opts.changes.length; i++) { | ||
changes[opts.changes[i]] = data[opts.changes[i]]; | ||
for (let i = 0; i < instRtd.changes.length; i++) { | ||
changes[instRtd.changes[i]] = data[instRtd.changes[i]]; | ||
} | ||
for (let i = 0; i < opts.keyProperties.length; i++) { | ||
const prop = opts.keyProperties[i]; | ||
conditions[prop.mapsTo] = opts.originalKeyValues[prop.name]; | ||
for (let i = 0; i < instRtd.keyProperties.length; i++) { | ||
const prop = instRtd.keyProperties[i]; | ||
conditions[prop.mapsTo] = instRtd.originalKeyValues[prop.name]; | ||
} | ||
changes = Utilities.transformPropertyNames(changes, Model.allProperties); | ||
Utilities.filterWhereConditionsInput(conditions, instance.model()); | ||
const syncResponse = Utilities.catchBlocking(() => opts.driver.update(opts.table, changes, conditions)); | ||
const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions)); | ||
fillBackAssociatedFieldsAfterPersist: { | ||
const couldParallel = opts.driver.db.isPool; | ||
const couldParallel = instRtd.driver.db.isPool; | ||
Utilities.parallelQueryIfPossible(couldParallel, oneOneAssocs, ({ association }) => { | ||
@@ -255,3 +256,3 @@ Utilities.parallelQueryIfPossible(couldParallel, Object.keys(association.field), propName => { | ||
let error = null; | ||
const syncResponse = Utilities.catchBlocking(() => { | ||
const syncResponse = FxORMCore.catchBlocking(() => { | ||
instance[syncVersionAccessor](instances); | ||
@@ -269,3 +270,3 @@ }); | ||
} | ||
Utilities.takeAwayResult({ error: error, result: assocSaved }, { callback: cb }); | ||
FxORMCore.takeAwayResult({ error: error, result: assocSaved }, { callback: cb }); | ||
}; | ||
@@ -281,3 +282,3 @@ const _saveOneAssociation = function (assoc) { | ||
const instances = instance[assoc.name]; | ||
Utilities.parallelQueryIfPossible(opts.driver.db.isPool, instances, (item) => { | ||
Utilities.parallelQueryIfPossible(instRtd.driver.db.isPool, instances, (item) => { | ||
if (!item.isInstance) { | ||
@@ -295,4 +296,4 @@ item = new assoc.model(item); | ||
}; | ||
for (let i = 0; i < opts.one_associations.length; i++) { | ||
_saveOneAssociation(opts.one_associations[i]); | ||
for (let i = 0; i < instRtd.one_associations.length; i++) { | ||
_saveOneAssociation(instRtd.one_associations[i]); | ||
} | ||
@@ -303,3 +304,3 @@ const _saveManyAssociation = function (assoc) { | ||
return; | ||
if (!opts.associations[assoc.name].changed) | ||
if (!instRtd.associations[assoc.name].changed) | ||
return; | ||
@@ -313,4 +314,4 @@ for (let j = 0; j < assocVal.length; j++) { | ||
}; | ||
for (let i = 0; i < opts.many_associations.length; i++) { | ||
_saveManyAssociation(opts.many_associations[i]); | ||
for (let i = 0; i < instRtd.many_associations.length; i++) { | ||
_saveManyAssociation(instRtd.many_associations[i]); | ||
} | ||
@@ -322,29 +323,29 @@ if (--pending === 0) | ||
var getNormalizedExtraDataAtPropertyTime = function () { | ||
return opts.extra; | ||
return instRtd.extra; | ||
}; | ||
const saveInstanceExtraSync = function () { | ||
if (opts.extrachanges.length === 0) | ||
if (instRtd.extrachanges.length === 0) | ||
return instance; | ||
var data = {}; | ||
var conditions = {}; | ||
for (let i = 0; i < opts.extrachanges.length; i++) { | ||
if (!opts.data.hasOwnProperty(opts.extrachanges[i])) | ||
for (let i = 0; i < instRtd.extrachanges.length; i++) { | ||
if (!instRtd.data.hasOwnProperty(instRtd.extrachanges[i])) | ||
continue; | ||
if (getNormalizedExtraDataAtPropertyTime()[opts.extrachanges[i]]) { | ||
data[opts.extrachanges[i]] = opts.data[opts.extrachanges[i]]; | ||
if (opts.driver.propertyToValue) { | ||
data[opts.extrachanges[i]] = opts.driver.propertyToValue(data[opts.extrachanges[i]], getNormalizedExtraDataAtPropertyTime()[opts.extrachanges[i]]); | ||
if (getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]) { | ||
data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]]; | ||
if (instRtd.driver.propertyToValue) { | ||
data[instRtd.extrachanges[i]] = instRtd.driver.propertyToValue(data[instRtd.extrachanges[i]], getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]); | ||
} | ||
} | ||
else { | ||
data[opts.extrachanges[i]] = opts.data[opts.extrachanges[i]]; | ||
data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]]; | ||
} | ||
} | ||
for (let i = 0; i < opts.extra_info.id.length; i++) { | ||
conditions[opts.extra_info.id_prop[i]] = opts.extra_info.id[i]; | ||
conditions[opts.extra_info.assoc_prop[i]] = opts.data[opts.keys[i]]; | ||
for (let i = 0; i < instRtd.extra_info.id.length; i++) { | ||
conditions[instRtd.extra_info.id_prop[i]] = instRtd.extra_info.id[i]; | ||
conditions[instRtd.extra_info.assoc_prop[i]] = instRtd.data[instRtd.keys[i]]; | ||
Utilities.filterWhereConditionsInput(conditions, instance.model()); | ||
} | ||
Utilities.filterWhereConditionsInput(conditions, instance.model()); | ||
opts.driver.update(opts.extra_info.table, data, conditions); | ||
instRtd.driver.update(instRtd.extra_info.table, data, conditions); | ||
return instance; | ||
@@ -356,11 +357,11 @@ }; | ||
if (Model.properties[key]) | ||
if (opts.driver.propertyToValue) | ||
changes[key] = opts.driver.propertyToValue(changes[key], Model.properties[key]); | ||
for (let i = 0; i < opts.keys.length; i++) { | ||
conditions[opts.keys[i]] = opts.data[opts.keys[i]]; | ||
if (instRtd.driver.propertyToValue) | ||
changes[key] = instRtd.driver.propertyToValue(changes[key], Model.properties[key]); | ||
for (let i = 0; i < instRtd.keys.length; i++) { | ||
conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]]; | ||
} | ||
Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {}); | ||
Hook.wait(instance, opts.hooks.beforeSave, function (err) { | ||
Hook.wait(instance, instRtd.hooks.beforeSave, function (err) { | ||
if (err) { | ||
Hook.trigger(instance, opts.hooks.afterSave, false); | ||
Hook.trigger(instance, instRtd.hooks.afterSave, false); | ||
emitCallbackStyleEvent("save", err, instance); | ||
@@ -370,6 +371,6 @@ return; | ||
Utilities.filterWhereConditionsInput(conditions, instance.model()); | ||
const syncReponse = Utilities.catchBlocking(() => opts.driver.update(opts.table, changes, conditions)); | ||
const syncReponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions)); | ||
if (!syncReponse.error) | ||
opts.data[key] = value; | ||
Hook.trigger(instance, opts.hooks.afterSave, !syncReponse.error); | ||
instRtd.data[key] = value; | ||
Hook.trigger(instance, instRtd.hooks.afterSave, !syncReponse.error); | ||
emitCallbackStyleEvent("save", syncReponse.error, instance); | ||
@@ -381,7 +382,7 @@ }); | ||
if (prop) { | ||
if ('valueToProperty' in opts.driver) { | ||
value = opts.driver.valueToProperty(value, prop); | ||
if ('valueToProperty' in instRtd.driver) { | ||
value = instRtd.driver.valueToProperty(value, prop); | ||
} | ||
if (opts.data[key] !== value) { | ||
opts.data[key] = value; | ||
if (instRtd.data[key] !== value) { | ||
instRtd.data[key] = value; | ||
return true; | ||
@@ -392,3 +393,3 @@ } | ||
}; | ||
// ('data.a.b', 5) => opts.data.a.b = 5 | ||
// ('data.a.b', 5) => instRtd.data.a.b = 5 | ||
const setPropertyByPath = function (path, value) { | ||
@@ -419,3 +420,3 @@ if (typeof path == 'string') { | ||
currObj[currKey] = value; | ||
opts.changes.push(propName); | ||
instRtd.changes.push(propName); | ||
} | ||
@@ -432,4 +433,4 @@ } | ||
global.console.log("Overwriting instance property"); | ||
if (key in opts.data) { | ||
defaultValue = opts.data[key]; | ||
if (key in instRtd.data) { | ||
defaultValue = instRtd.data[key]; | ||
} | ||
@@ -442,7 +443,7 @@ else if (prop && 'defaultValue' in prop) { | ||
get: function () { | ||
return opts.data[key]; | ||
return instRtd.data[key]; | ||
}, | ||
set: function (val) { | ||
if (prop.key === true) { | ||
if (prop.type == 'serial' && opts.data[key] != null) { | ||
if (prop.type == 'serial' && instRtd.data[key] != null) { | ||
if (!(val === null || val === undefined)) { | ||
@@ -453,3 +454,3 @@ return; | ||
else { | ||
opts.originalKeyValues[prop.name] = opts.data[prop.name]; | ||
instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name]; | ||
} | ||
@@ -460,7 +461,7 @@ } | ||
} | ||
if (opts.autoSave) { | ||
if (instRtd.autoSave) { | ||
saveInstanceProperty(key, val); | ||
} | ||
else if (opts.changes.indexOf(key) === -1) { | ||
opts.changes.push(key); | ||
else if (instRtd.changes.indexOf(key) === -1) { | ||
instRtd.changes.push(key); | ||
} | ||
@@ -477,11 +478,11 @@ }, | ||
get: function () { | ||
return opts.data[key]; | ||
return instRtd.data[key]; | ||
}, | ||
set: function (val) { | ||
setInstanceProperty(key, val); | ||
/*if (opts.autoSave) { | ||
/*if (instRtd.autoSave) { | ||
saveInstanceProperty(key, val); | ||
}*/ | ||
if (opts.extrachanges.indexOf(key) === -1) { | ||
opts.extrachanges.push(key); | ||
if (instRtd.extrachanges.indexOf(key) === -1) { | ||
instRtd.extrachanges.push(key); | ||
} | ||
@@ -495,9 +496,9 @@ }, | ||
} | ||
for (let k in opts.extra) { | ||
for (let k in instRtd.extra) { | ||
addInstanceProperty(k); | ||
} | ||
for (let k in opts.methods) { | ||
Utilities.addHiddenPropertyToInstance(instance, k, opts.methods[k].bind(instance), { writable: true }); | ||
for (let k in instRtd.methods) { | ||
Utilities.addHiddenPropertyToInstance(instance, k, instRtd.methods[k].bind(instance), { writable: true }); | ||
} | ||
for (let k in opts.extra) { | ||
for (let k in instRtd.extra) { | ||
addInstanceExtraProperty(k); | ||
@@ -581,4 +582,4 @@ } | ||
process.nextTick(() => { | ||
const syncResponse = Utilities.catchBlocking(instance.saveSync, args); | ||
Utilities.takeAwayResult({ error: syncResponse.error, result: instance }, { no_throw: !!cb, callback: cb }); | ||
const syncResponse = FxORMCore.catchBlocking(instance.saveSync, args); | ||
FxORMCore.takeAwayResult({ error: syncResponse.error, result: instance }, { no_throw: !!cb, callback: cb }); | ||
}); | ||
@@ -588,14 +589,14 @@ return this; | ||
Utilities.addHiddenUnwritableMethodToInstance(instance, "saved", function () { | ||
return opts.changes.length === 0; | ||
return instRtd.changes.length === 0; | ||
}); | ||
Utilities.addHiddenUnwritableMethodToInstance(instance, "removeSync", function () { | ||
if (opts.is_new) | ||
if (instRtd.isNew) | ||
return; | ||
var conditions = {}; | ||
for (let i = 0; i < opts.keys.length; i++) { | ||
conditions[opts.keys[i]] = opts.data[opts.keys[i]]; | ||
for (let i = 0; i < instRtd.keys.length; i++) { | ||
conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]]; | ||
} | ||
let removeErr = null; | ||
Utilities.attachOnceTypedHookRefToInstance(instance, 'remove', {}); | ||
Hook.wait(instance, opts.hooks.beforeRemove, function (err) { | ||
Hook.wait(instance, instRtd.hooks.beforeRemove, function (err) { | ||
if (err) { | ||
@@ -608,4 +609,4 @@ emitCallbackStyleEvent("remove", err, instance); | ||
Utilities.filterWhereConditionsInput(conditions, instance.model()); | ||
const syncResponse = Utilities.catchBlocking(() => opts.driver.remove(opts.table, conditions)); | ||
Hook.trigger(instance, opts.hooks.afterRemove, !syncResponse.error); | ||
const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.remove(instRtd.table, conditions)); | ||
Hook.trigger(instance, instRtd.hooks.afterRemove, !syncResponse.error); | ||
emitCallbackStyleEvent("remove", syncResponse.error, instance); | ||
@@ -619,4 +620,4 @@ removeErr = syncResponse.error; | ||
Utilities.addHiddenUnwritableMethodToInstance(instance, "remove", function (cb) { | ||
const syncReponse = Utilities.catchBlocking(() => instance.removeSync()); | ||
Utilities.takeAwayResult(syncReponse, { callback: cb }); | ||
const syncReponse = FxORMCore.catchBlocking(() => instance.removeSync()); | ||
FxORMCore.takeAwayResult(syncReponse, { callback: cb }); | ||
return this; | ||
@@ -627,12 +628,12 @@ }); | ||
if (propName != undefined) { | ||
opts.changes.push(propName); | ||
instRtd.changes.push(propName); | ||
} | ||
}); | ||
Utilities.addHiddenReadonlyPropertyToInstance(instance, "dirtyProperties", function () { return opts.changes; }); | ||
Utilities.addHiddenReadonlyPropertyToInstance(instance, "dirtyProperties", function () { return instRtd.changes; }); | ||
Utilities.addHiddenPropertyToInstance(instance, "isInstance", true); | ||
Utilities.addHiddenUnwritableMethodToInstance(instance, "isPersisted", function () { | ||
return !opts.is_new; | ||
return !instRtd.isNew; | ||
}); | ||
Utilities.addHiddenPropertyToInstance(instance, "isShell", function () { | ||
return opts.isShell; | ||
return instRtd.isShell; | ||
}); | ||
@@ -646,12 +647,14 @@ Utilities.addHiddenUnwritableMethodToInstance(instance, "validateSync", function () { | ||
Utilities.addHiddenPropertyToInstance(instance, "__singleton_uid", function () { | ||
return opts.uid; | ||
return instRtd.uid; | ||
}); | ||
Utilities.addHiddenPropertyToInstance(instance, "__opts", opts); | ||
Utilities.addHiddenPropertyToInstance(instance, "__instRtd", instRtd); | ||
/* just for compat */ | ||
Utilities.addHiddenPropertyToInstance(instance, "__opts", instRtd); | ||
Utilities.addHiddenPropertyToInstance(instance, "model", function () { | ||
return Model; | ||
}); | ||
for (let i = 0; i < opts.keyProperties.length; i++) { | ||
var prop = opts.keyProperties[i]; | ||
if (!(prop.name in opts.data)) { | ||
opts.changes = Object.keys(opts.data); | ||
for (let i = 0; i < instRtd.keyProperties.length; i++) { | ||
var prop = instRtd.keyProperties[i]; | ||
if (!(prop.name in instRtd.data)) { | ||
instRtd.changes = Object.keys(instRtd.data); | ||
break; | ||
@@ -661,8 +664,8 @@ } | ||
rememberKeys(); | ||
opts.setupAssociations(instance); | ||
for (let i = 0; i < opts.one_associations.length; i++) { | ||
var asc = opts.one_associations[i]; | ||
instRtd.__setupAssociations(instance); | ||
for (let i = 0; i < instRtd.one_associations.length; i++) { | ||
var asc = instRtd.one_associations[i]; | ||
if (!asc.reversed && !asc.__for_extension) { | ||
for (let k in asc.field) { | ||
if (!opts.data.hasOwnProperty(k)) { | ||
if (!instRtd.data.hasOwnProperty(k)) { | ||
addInstanceProperty(k); | ||
@@ -672,4 +675,4 @@ } | ||
} | ||
if (asc.name in opts.data) { | ||
var d = opts.data[asc.name]; | ||
if (asc.name in instRtd.data) { | ||
var d = instRtd.data[asc.name]; | ||
var mapper = function (obj) { | ||
@@ -684,21 +687,21 @@ return obj.isInstance ? obj : new asc.model(obj); | ||
} | ||
delete opts.data[asc.name]; | ||
delete instRtd.data[asc.name]; | ||
} | ||
} | ||
for (let i = 0; i < opts.many_associations.length; i++) { | ||
var aName = opts.many_associations[i].name; | ||
opts.associations[aName] = { | ||
changed: false, data: opts.many_associations[i] | ||
for (let i = 0; i < instRtd.many_associations.length; i++) { | ||
var aName = instRtd.many_associations[i].name; | ||
instRtd.associations[aName] = { | ||
changed: false, data: instRtd.many_associations[i] | ||
}; | ||
if (Array.isArray(opts.data[aName])) { | ||
instance[aName] = opts.data[aName]; | ||
delete opts.data[aName]; | ||
if (Array.isArray(instRtd.data[aName])) { | ||
instance[aName] = instRtd.data[aName]; | ||
delete instRtd.data[aName]; | ||
} | ||
} | ||
Object.keys(opts.events).forEach((evtName) => { | ||
if (typeof opts.events[evtName] !== 'function') | ||
Object.keys(instRtd.events).forEach((evtName) => { | ||
if (typeof instRtd.events[evtName] !== 'function') | ||
throw new ORMError("INVALID_EVENT_HANDLER", 'PARAM_MISMATCH'); | ||
instance.on(evtName, opts.events[evtName]); | ||
instance.on(evtName, instRtd.events[evtName]); | ||
}); | ||
Hook.wait(instance, opts.hooks.afterLoad, function (err) { | ||
Hook.wait(instance, instRtd.hooks.afterLoad, function (err) { | ||
process.nextTick(() => { | ||
@@ -705,0 +708,0 @@ emitCallbackStyleEvent("ready", err, instance); |
@@ -51,3 +51,3 @@ /// <reference lib="es2017" /> | ||
} | ||
var found_assoc = false; | ||
let found_assoc = false; | ||
for (let k in data) { | ||
@@ -87,3 +87,3 @@ if (k === "extra_field") | ||
}; | ||
const setupAssociations = function (instance) { | ||
const __setupAssociations = function (instance) { | ||
const genHookHandlerForInstance = Utilities.hookHandlerDecorator({ thisArg: instance }); | ||
@@ -97,3 +97,3 @@ OneAssociation.extend(model, instance, m_opts.driver, one_associations, { assoc_opts, genHookHandlerForInstance }); | ||
keys: m_opts.keys, | ||
is_new: inst_opts.is_new || false, | ||
isNew: inst_opts.isNew || false, | ||
isShell: inst_opts.isShell || false, | ||
@@ -113,3 +113,3 @@ data: data, | ||
association_properties: association_properties, | ||
setupAssociations: setupAssociations, | ||
__setupAssociations: __setupAssociations, | ||
fieldToPropertyMap: fieldToPropertyMap, | ||
@@ -135,3 +135,3 @@ keyProperties: keyProperties, | ||
if (data.length == m_opts.keys.length) { | ||
var data2 = {}; | ||
const data2 = {}; | ||
for (let i = 0; i < m_opts.keys.length; i++) { | ||
@@ -149,3 +149,3 @@ data2[m_opts.keys[i]] = data[i++]; | ||
else if (typeof data === "number" || typeof data === "string") { | ||
var data2 = {}; | ||
const data2 = {}; | ||
data2[m_opts.keys[0]] = data; | ||
@@ -157,3 +157,3 @@ return createInstanceSync(data2, { isShell: true }); | ||
} | ||
var isNew = false; | ||
let isNew = false; | ||
for (let i = 0; i < m_opts.keys.length; i++) { | ||
@@ -169,3 +169,3 @@ if (!data.hasOwnProperty(m_opts.keys[i])) { | ||
return createInstanceSync(data, { | ||
is_new: isNew, | ||
isNew, | ||
autoSave: m_opts.autoSave, | ||
@@ -304,6 +304,6 @@ cascadeRemove: m_opts.cascadeRemove | ||
const chainOrRunSync = function () { | ||
var conditions = null; | ||
var options = {}; | ||
var order = null; | ||
var merges = []; | ||
let conditions = null; | ||
let options = {}; | ||
let order = null; | ||
let merges = []; | ||
Helpers.selectArgs(arguments, (arg_type, arg) => { | ||
@@ -390,3 +390,3 @@ switch (arg_type) { | ||
} | ||
var chain = new ChainFind(model, { | ||
return new ChainFind(model, { | ||
only: options.only || model_fields, | ||
@@ -428,3 +428,2 @@ keys: m_opts.keys, | ||
}); | ||
return chain; | ||
}; | ||
@@ -631,3 +630,3 @@ model.findSync = function (...args) { | ||
const item = createInstanceSync(data, { | ||
is_new: true, | ||
isNew: true, | ||
autoSave: m_opts.autoSave, | ||
@@ -634,0 +633,0 @@ // not fetch associated instance on its creation. |
{ | ||
"name": "@fxjs/orm", | ||
"version": "1.14.0", | ||
"version": "1.14.1", | ||
"description": "", | ||
@@ -37,8 +37,8 @@ "main": "lib", | ||
"@fibjs/enforce": "^0.1.1", | ||
"@fxjs/db-driver": "^0.4.0", | ||
"@fxjs/knex": "^0.4.0", | ||
"@fxjs/orm-core": "^0.3.0", | ||
"@fxjs/orm-property": "^0.3.0", | ||
"@fxjs/sql-ddl-sync": "^0.10.0", | ||
"@fxjs/sql-query": "^0.9.0", | ||
"@fxjs/db-driver": "^0.4.1", | ||
"@fxjs/knex": "^0.4.1", | ||
"@fxjs/orm-core": "^0.3.1", | ||
"@fxjs/orm-property": "^0.3.1", | ||
"@fxjs/sql-ddl-sync": "^0.10.1", | ||
"@fxjs/sql-query": "^0.9.1", | ||
"@types/knex": "^0.16.1", | ||
@@ -52,3 +52,3 @@ "fib-pool": "^1.6.0", | ||
"devDependencies": { | ||
"@fibjs/types": "^0.34.1", | ||
"@fibjs/types": "^0.35.0", | ||
"@types/lodash.clonedeep": "^4.5.6", | ||
@@ -60,3 +60,3 @@ "@types/lodash.flatten": "^4.4.6", | ||
"fib-pool": "^1.6.0", | ||
"fib-typify": "^0.10.0", | ||
"fib-typify": "^0.11.3", | ||
"lodash": "^4.17.11", | ||
@@ -79,3 +79,3 @@ "rimraf": "^3.0.2", | ||
}, | ||
"gitHead": "ec15a409369c2b4c7c88ef6cbf63cc5ebff0ffed" | ||
"gitHead": "69f661603b0536b59c87bdf74460ac0f32cac3d1" | ||
} |
@@ -1,2 +0,1 @@ | ||
/// <reference types="@fibjs/types" /> | ||
import type { FxOrmHook } from "./hook"; | ||
@@ -83,18 +82,18 @@ import type { FxOrmModel } from "./model"; | ||
beforeSet?: { | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
afterSet?: { | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
beforeRemove?: { | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
afterRemove?: { | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
beforeAdd?: { | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookActionCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
afterAdd?: { | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Fibjs.AnyObject): any; | ||
(func: FxOrmHook.HookResultCallback, opts?: FxOrmHook.HookPatchOptions & Record<string, any>): any; | ||
}; | ||
@@ -101,0 +100,0 @@ } |
@@ -123,3 +123,3 @@ /// <reference types="@fibjs/types" /> | ||
}; | ||
execQuerySync: (query: string, opt: Fibjs.AnyObject) => any; | ||
execQuerySync: (query: string, opt: Record<string, any>) => any; | ||
[ext_key: string]: any; | ||
@@ -126,0 +126,0 @@ } |
@@ -1,2 +0,1 @@ | ||
/// <reference types="@fibjs/types" /> | ||
import { FxOrmCommon } from './_common'; | ||
@@ -20,3 +19,3 @@ import type { FxOrmInstance } from './instance'; | ||
export interface HookWait<CTX_SELF = FxOrmInstance.Instance, TNEXT_THIS = any> { | ||
(self: CTX_SELF, cur: FxOrmCommon.Arraible<HookActionCallback | FxOrmCommon.Arraible<HookActionCallback>>, next: FxOrmCommon.GenericCallback<TNEXT_THIS>, opts?: Fibjs.AnyObject): void; | ||
(self: CTX_SELF, cur: FxOrmCommon.Arraible<HookActionCallback | FxOrmCommon.Arraible<HookActionCallback>>, next: FxOrmCommon.GenericCallback<TNEXT_THIS>, opts?: Record<string, any>): void; | ||
} | ||
@@ -23,0 +22,0 @@ export interface HookPatchOptions { |
/// <reference types="@fibjs/types" /> | ||
import { FxOrmAssociation } from "./assoc"; | ||
import { FxOrmDMLDriver } from "./DMLDriver"; | ||
import type { FxOrmAssociation } from "./assoc"; | ||
import type { FxOrmDMLDriver } from "./DMLDriver"; | ||
import type { FxOrmHook } from "./hook"; | ||
import type { FxOrmModel } from "./model"; | ||
import type { FxOrmProperty } from "./property"; | ||
import { FxOrmValidators } from "./Validators"; | ||
import { FxOrmCommon } from "./_common"; | ||
import { FxOrmSynchronous } from "./synchronous"; | ||
import type { FxOrmValidators } from "./Validators"; | ||
import type { FxOrmCommon } from "./_common"; | ||
import type { FxOrmError } from "./Error"; | ||
export declare namespace FxOrmInstance { | ||
@@ -19,3 +19,3 @@ interface InstanceDataPayload { | ||
uid?: string; | ||
is_new?: boolean; | ||
isNew?: boolean; | ||
isShell?: boolean; | ||
@@ -29,3 +29,2 @@ autoSave?: boolean; | ||
} | ||
type InstanceChangeRecords = string[]; | ||
interface InstanceConstructorOptions { | ||
@@ -36,3 +35,6 @@ table: string; | ||
data?: InstanceDataPayload; | ||
changes?: InstanceChangeRecords; | ||
/** | ||
* @description changes records | ||
*/ | ||
changes?: string[]; | ||
extra?: string[] | Record<string, FxOrmProperty.NormalizedProperty>; | ||
@@ -45,3 +47,3 @@ extra_info?: { | ||
}; | ||
is_new?: boolean; | ||
isNew?: boolean; | ||
isShell?: boolean; | ||
@@ -62,6 +64,10 @@ autoSave?: FxOrmModel.ModelConstructorOptions['autoSave']; | ||
driver: FxOrmDMLDriver.DMLDriver; | ||
setupAssociations: { | ||
(instance: Instance): void; | ||
}; | ||
/** | ||
* @internal | ||
*/ | ||
__setupAssociations: (instance: Instance) => void; | ||
fieldToPropertyMap: FxOrmProperty.FieldToPropertyMapType; | ||
/** | ||
* @internal | ||
*/ | ||
events?: { | ||
@@ -71,16 +77,11 @@ [k: string]: FxOrmCommon.GenericCallback<any>; | ||
} | ||
interface InnerInstanceOptions extends InstanceConstructorOptions { | ||
interface InnerInstanceRuntimeData extends InstanceConstructorOptions { | ||
associations?: { | ||
[key: string]: FxOrmAssociation.InstanceAssociationItemInformation; | ||
}; | ||
extrachanges: InstanceChangeRecords; | ||
extrachanges: string[]; | ||
} | ||
type InstanceConstructor = new (model: FxOrmModel.Model, opts: InstanceConstructorOptions) => FxOrmInstance.Instance; | ||
type InstanceEventType = 'ready' | 'save' | 'beforeRemove' | 'remove'; | ||
interface Instance extends FxOrmSynchronous.SynchronizedInstance { | ||
saved(): boolean; | ||
remove(callback: FxOrmCommon.VoidCallback): Instance; | ||
validate: { | ||
(callback: FxOrmCommon.ValidatorCallback): void; | ||
}; | ||
interface Instance { | ||
on(event: InstanceEventType | string, callback: FxOrmCommon.GenericCallback<any>): Instance; | ||
@@ -90,7 +91,11 @@ $on: Class_EventEmitter['on']; | ||
$emit: Class_EventEmitter['emit']; | ||
validate(callback: FxOrmCommon.ValidatorCallback): void; | ||
validateSync(): false | FxOrmError.ExtendedError[]; | ||
save(callback?: FxOrmCommon.VoidCallback): Instance; | ||
save(data: InstanceDataPayload, callback?: FxOrmCommon.VoidCallback): Instance; | ||
save(data: InstanceDataPayload, options: SaveOptions, callback?: FxOrmCommon.VoidCallback): Instance; | ||
saveSync(data?: FxOrmInstance.InstanceDataPayload, options?: FxOrmInstance.SaveOptions): FxOrmInstance.Instance; | ||
saved(): boolean; | ||
remove(callback?: FxOrmCommon.VoidCallback): Instance; | ||
removeSync(): void; | ||
/** | ||
@@ -117,2 +122,3 @@ * @noenum | ||
/** | ||
* @internal | ||
* @noenum | ||
@@ -122,5 +128,6 @@ */ | ||
/** | ||
* @internal | ||
* @noenum | ||
*/ | ||
__opts?: InnerInstanceOptions; | ||
__instRtd?: InnerInstanceRuntimeData; | ||
/** | ||
@@ -147,4 +154,9 @@ * @noenum | ||
}; | ||
/** | ||
* other patched synchronous version method | ||
* - lazyLoad: getXXXSync, setXXXSync, removeXXXSync | ||
* - association: [addAccessor]Sync, [getAccessor]Sync, [hasAccessor]Sync, [setAccessor]Sync, [removeAccessor]Sync | ||
*/ | ||
[extraProperty: string]: any; | ||
} | ||
} |
@@ -71,7 +71,5 @@ /// <reference types="@fibjs/types" /> | ||
type FibORM = ORM; | ||
type FibOrmDB = ORM; | ||
interface FibORMIConnectionOptions extends FxDbDriverNS.ConnectionInputArgs { | ||
timezone: string; | ||
} | ||
type InstanceOptions = FxOrmInstance.InnerInstanceOptions; | ||
type OrigAggreteGenerator = (...args: any[]) => FxOrmQuery.IAggregated; | ||
@@ -78,0 +76,0 @@ interface FibOrmFindLikeQueryObject { |
@@ -5,3 +5,2 @@ import type { FxOrmCommon } from './_common'; | ||
import type { FxOrmModel } from "./model"; | ||
import type { FxOrmError } from "./Error"; | ||
import type { FxSqlQuerySubQuery } from "@fxjs/sql-query"; | ||
@@ -52,19 +51,2 @@ export declare namespace FxOrmSynchronous { | ||
} | ||
interface SynchronizedInstance { | ||
saveSync: { | ||
(data?: FxOrmInstance.InstanceDataPayload, options?: FxOrmInstance.SaveOptions): FxOrmInstance.Instance; | ||
}; | ||
removeSync: { | ||
(): void; | ||
}; | ||
validateSync: { | ||
(): false | FxOrmError.ExtendedError[]; | ||
}; | ||
/** | ||
* other patched synchronous version method | ||
* - lazyLoad: getXXXSync, setXXXSync, removeXXXSync | ||
* - association: [addAccessor]Sync, [getAccessor]Sync, [hasAccessor]Sync, [setAccessor]Sync, [removeAccessor]Sync | ||
*/ | ||
[ek: string]: any; | ||
} | ||
interface SynchronizedIChainFind { | ||
@@ -71,0 +53,0 @@ } |
@@ -1,2 +0,1 @@ | ||
/// <reference types="@fibjs/types" /> | ||
import FxORMCore = require('@fxjs/orm-core'); | ||
@@ -140,6 +139,6 @@ import { FxSqlQuery, FxSqlQuerySubQuery } from '@fxjs/sql-query'; | ||
association_ids?: any[]; | ||
removeConditions?: Fibjs.AnyObject; | ||
$ref: Fibjs.AnyObject; | ||
removeConditions?: Record<string, any>; | ||
$ref: Record<string, any>; | ||
useChannel?: Function; | ||
}): Fibjs.AnyObject; | ||
}): Record<string, any>; | ||
export declare function hookHandlerDecorator({ thisArg, onlyOnce }?: { | ||
@@ -150,6 +149,6 @@ thisArg?: any; | ||
export declare function reusableChannelGenerator(): () => FxOrmHook.HookChannelResults<Function>; | ||
export declare const createHookHelper: (hooks: Fibjs.AnyObject, hook: keyof FxOrmModel.Hooks | keyof FxOrmAssociation.InstanceAssociationItem['hooks'], { initialHooks }?: Fibjs.AnyObject) => (cb: FxOrmHook.HookActionCallback | FxOrmHook.HookResultCallback, opts?: FxOrmModel.ModelHookPatchOptions) => any; | ||
export declare function attachOnceTypedHookRefToInstance(instance: FxOrmInstance.Instance, type: 'save' | 'create' | 'remove', typedHookRef: Fibjs.AnyObject): void; | ||
export declare const createHookHelper: (hooks: Record<string, any>, hook: keyof FxOrmModel.Hooks | keyof FxOrmAssociation.InstanceAssociationItem['hooks'], { initialHooks }?: Record<string, any>) => (cb: FxOrmHook.HookActionCallback | FxOrmHook.HookResultCallback, opts?: FxOrmModel.ModelHookPatchOptions) => any; | ||
export declare function attachOnceTypedHookRefToInstance(instance: FxOrmInstance.Instance, type: 'save' | 'create' | 'remove', typedHookRef: Record<string, any>): void; | ||
export declare function arraify<T = any>(item: T | T[]): T[]; | ||
export declare function isKeyProperty(prop: FxOrmProperty.NormalizedProperty): boolean; | ||
export declare function isKeyPrimaryProperty(prop: FxOrmProperty.NormalizedProperty): boolean; |
396204
8620
Updated@fxjs/db-driver@^0.4.1
Updated@fxjs/knex@^0.4.1
Updated@fxjs/orm-core@^0.3.1
Updated@fxjs/orm-property@^0.3.1
Updated@fxjs/sql-ddl-sync@^0.10.1
Updated@fxjs/sql-query@^0.9.1