Skip to content

Commit

Permalink
feat: implement model.$original and model.$attributes
Browse files Browse the repository at this point in the history
Refs: #72
  • Loading branch information
targos committed Sep 23, 2021
1 parent 3e06ec7 commit 952a139
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
3 changes: 3 additions & 0 deletions adonis-typings/odm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ declare module '@ioc:Zakodium/Mongodb/Odm' {
readonly createdAt: Date;
readonly updatedAt: Date;

readonly $original: ModelAttributes<this>;
readonly $attributes: ModelAttributes<this>;

/**
* `true` if the entry has been persisted to the database.
*/
Expand Down
59 changes: 30 additions & 29 deletions src/Model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,16 @@ export class BaseModel {
public readonly createdAt: Date;
public readonly updatedAt: Date;

public $original: Record<string, unknown>;
public $attributes: Record<string, unknown>;

public $isPersisted = false;
public $isLocal = true;
public $isDeleted = false;

protected $collection: Collection<
ModelAttributes<MongodbDocument<unknown>>
> | null = null;
protected $originalData: Record<string, unknown>;
protected $currentData: Record<string, unknown>;
protected $options: InternalModelConstructorOptions;

public constructor(
Expand All @@ -273,11 +274,11 @@ export class BaseModel {
alreadyExists = false,
) {
if (dbObj) {
this.$originalData = alreadyExists === true ? cloneDeep(dbObj) : {};
this.$currentData = dbObj;
this.$original = alreadyExists === true ? cloneDeep(dbObj) : {};
this.$attributes = dbObj;
} else {
this.$originalData = {};
this.$currentData = {};
this.$original = {};
this.$attributes = {};
}

if (options !== undefined) {
Expand Down Expand Up @@ -535,10 +536,10 @@ export class BaseModel {

public [Symbol.for('nodejs.util.inspect.custom')](): unknown {
return {
model: this.constructor.name,
originalData: this.$originalData,
currentData: this.$currentData,
isDirty: this.$isDirty,
Model: this.constructor.name,
$original: this.$original,
$attributes: this.$attributes,
$isDirty: this.$isDirty,
};
}

Expand All @@ -547,10 +548,10 @@ export class BaseModel {
}

public get $dirty(): Partial<ModelAttributes<this>> {
return pickBy(this.$currentData, (value, key) => {
return pickBy(this.$attributes, (value, key) => {
return (
this.$originalData[key] === undefined ||
!isEqual(this.$originalData[key], value)
this.$original[key] === undefined ||
!isEqual(this.$original[key], value)
);
}) as Partial<ModelAttributes<this>>;
}
Expand Down Expand Up @@ -588,11 +589,11 @@ export class BaseModel {
// which shouldn't reset the createdAt field.
const toSet = {} as DataToSet;
const now = new Date();
if (this.$currentData.createdAt === undefined) {
this.$currentData.createdAt = now;
if (this.$attributes.createdAt === undefined) {
this.$attributes.createdAt = now;
toSet.createdAt = now;
}
this.$currentData.updatedAt = now;
this.$attributes.updatedAt = now;
toSet.updatedAt = now;

for (const [dirtyKey, dirtyValue] of dirtyEntries) {
Expand All @@ -602,15 +603,15 @@ export class BaseModel {
}

public get id() {
return this.$currentData._id;
return this.$attributes._id;
}

public get $isDirty(): boolean {
return Object.keys(this.$dirty).length > 0;
}

public toJSON(): unknown {
return this.$currentData;
return this.$attributes;
}

public async save(
Expand All @@ -627,16 +628,16 @@ export class BaseModel {
};
if (!this.$isPersisted) {
const result = await collection.insertOne(toSet, driverOptions);
this.$currentData._id = result.insertedId;
this.$attributes._id = result.insertedId;
this.$isPersisted = true;
} else {
await collection.updateOne(
{ _id: this.$currentData._id },
{ _id: this.$attributes._id },
{ $set: toSet },
driverOptions,
);
}
this.$originalData = cloneDeep(this.$currentData);
this.$original = cloneDeep(this.$attributes);
return true;
}

Expand All @@ -651,7 +652,7 @@ export class BaseModel {
};
const result = await collection.deleteOne(
{
_id: this.$currentData._id,
_id: this.$attributes._id,
},
driverOptions,
);
Expand All @@ -663,19 +664,19 @@ export class BaseModel {
values: NoExtraProperties<Partial<Omit<ModelAttributes<this>, '_id'>>, T>,
): this {
Object.entries(values).forEach(([key, value]) => {
this.$currentData[key] = value;
this.$attributes[key] = value;
});
return this;
}

public fill<T extends Partial<Omit<ModelAttributes<this>, '_id'>>>(
values: NoExtraProperties<Partial<Omit<ModelAttributes<this>, '_id'>>, T>,
) {
const createdAt = this.$currentData.createdAt;
this.$currentData = {
const createdAt = this.$attributes.createdAt;
this.$attributes = {
_id: this.id,
};
if (createdAt) this.$currentData.createdAt = createdAt;
if (createdAt) this.$attributes.createdAt = createdAt;
return this.merge(values);
}
}
Expand Down Expand Up @@ -710,16 +711,16 @@ export class BaseAutoIncrementModel extends BaseModel {
assert(doc.value, 'upsert should always create a document');
toSet._id = doc.value.count;
await collection.insertOne(toSet, driverOptions);
this.$currentData._id = doc.value.count;
this.$attributes._id = doc.value.count;
this.$isPersisted = true;
} else {
await collection.updateOne(
{ _id: this.$currentData._id },
{ _id: this.$attributes._id },
{ $set: toSet },
driverOptions,
);
}
this.$originalData = cloneDeep(this.$currentData);
this.$original = cloneDeep(this.$attributes);
return true;
}
}

0 comments on commit 952a139

Please sign in to comment.