Skip to content

Commit

Permalink
fix: casts
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddyuchina committed Mar 25, 2024
1 parent 55bb282 commit 99b0e31
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ class Builder {
async getModels(...columns) {
columns = flatten(columns);
if (columns.length > 0) {
if (this.query._statements.filter(item => item.grouping == 'columns').length > 0 && columns[0] !== '*') {
if (this.query._statements.filter(item => item.grouping == 'columns').length == 0 && columns[0] !== '*') {
this.query.select(...columns);
}
}
Expand Down Expand Up @@ -1061,8 +1061,7 @@ class Builder {
return item;
}

const model = this.model.newInstance(item, true);
model.syncOriginal();
const model = this.model.newFromBuilder(item);

return model;
}));
Expand Down
35 changes: 27 additions & 8 deletions src/concerns/has-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
getSetterMethod,
} = require('../utils');
const CastsAttributes = require('../casts-attributes');
const collect = require('collect.js');

const HasAttributes = (Model) => {
return class extends Model {
Expand Down Expand Up @@ -112,6 +113,16 @@ const HasAttributes = (Model) => {
setAttributes(attributes) {
this.attributes = { ...attributes };
}

setRawAttributes(attributes, sync = false) {
this.attributes = attributes;

if (sync) {
this.syncOriginal();
}

return this;
}

getAttributes() {
return { ...this.attributes };
Expand Down Expand Up @@ -144,14 +155,18 @@ const HasAttributes = (Model) => {
const casts = this.getCasts();
const castType = casts[key];

if (this.isCustomCast()) {
return castType.set(this, key, value, this.attributes);
if (this.isCustomCast(castType)) {
value = castType.set(this, key, value, this.attributes);
}

if (castType === 'json') {
value = JSON.stringify(value);
}

if (castType === 'collection') {
value = JSON.stringify(value);
}

if (value !== null && this.isDateAttribute(key)) {
value = this.fromDateTime(value);
}
Expand Down Expand Up @@ -225,12 +240,16 @@ const HasAttributes = (Model) => {
case 'object':
case 'json':
try {
return typeof value === 'string' ? JSON.parse(value) : value;
return JSON.parse(value);
} catch (e) {
return null;
}
case 'collection':
return new Collection(typeof value === 'string' ? JSON.parse(value) : value);
try {
return collect(JSON.parse(value));
} catch (e) {
return collect([]);
}
case 'date':
return this.asDate(value);
case 'datetime':
Expand All @@ -239,8 +258,8 @@ const HasAttributes = (Model) => {
case 'timestamp':
return this.asTimestamp(value);
}

if (this.isCustomCast()) {
if (this.isCustomCast(castType)) {
return castType.get(this, key, value, this.attributes);
}

Expand Down Expand Up @@ -371,9 +390,9 @@ const HasAttributes = (Model) => {
}

hasCast(key, types = []) {
if (this.casts[key] !== undefined) {
if (key in this.casts) {
types = flatten(types);
return types ? types.includes(this.getCastType(key)) : true;
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
}

return false;
Expand Down
8 changes: 8 additions & 0 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class Model extends BaseModel {
return model;
}

newFromBuilder(attributes = {}, connection = null) {
const model = this.newInstance({}, true);
model.setRawAttributes(attributes, true);
model.setConnection(connection || this.getConnectionName());

return model;
}

asProxy () {
const handler = {
get: function (target, prop) {
Expand Down

0 comments on commit 99b0e31

Please sign in to comment.