Skip to content

Commit

Permalink
acceptable insert algorithm with dynamic defaultAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Aug 22, 2020
1 parent 7047602 commit d934e83
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class MemServerModel {
static _defaultAttributes = {}; // NOTE: probably a decorator here in future
static _embedReferences = {}; // NOTE: serializer concern

static primaryKey: string | null = null; // NOTE: this might be problematic!!
static primaryKey: string | null = null;

static get DB(): Array<InternalModel> {
if (!this._DB[this.name]) {
Expand Down Expand Up @@ -142,33 +142,19 @@ export default class MemServerModel {
this.attributes.push(this.primaryKey);
}

const defaultAttributes = this.attributes.reduce((result, attribute) => {
if (attribute === this.primaryKey) {
result[attribute] = this.primaryKey === "id" ? incrementId(this.DB, this) : generateUUID();

return result;
}

const target = this.defaultAttributes[attribute]; // NOTE: check if I can optimize this earlier
if (!(this.primaryKey in options)) {
options[this.primaryKey] = this.primaryKey === "id" ? incrementId(this.DB, this) : generateUUID();
}

// result[attribute] = typeof target === "function" ? target() : target; // TODO: here we apply the defaultAttribute
result[attribute] = target;
primaryKeyTypeSafetyCheck(this.primaryKey, options[this.primaryKey], this.name);

return result;
}, {});
const defaultTarget = Object.assign(defaultAttributes, options);
const target = Object.keys(defaultTarget).reduce((result, attribute) => {
if (typeof defaultTarget[attribute] === "function") {
result[attribute] = defaultTarget[attribute].apply(defaultTarget); // NOTE: functions will be functions here;
} else {
result[attribute] = defaultTarget[attribute];
const target = this.attributes.reduce((result, attribute) => {
if (typeof result[attribute] === "function") {
result[attribute] = result[attribute].apply(result);
}

return result;
}, defaultTarget);

primaryKeyTypeSafetyCheck(this.primaryKey, target[this.primaryKey], this.name);

}, Object.assign({}, this.defaultAttributes, options));
const existingRecord = target.id ? this.find(target.id) : this.findBy({ uuid: target.uuid });

if (existingRecord) {
Expand Down

0 comments on commit d934e83

Please sign in to comment.