Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 50 additions & 30 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class Builder {
'select', 'from', 'where', 'orWhere', 'whereColumn', 'whereRaw',
'whereNot', 'orWhereNot', 'whereIn', 'orWhereIn', 'whereNotIn', 'orWhereNotIn', 'whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull', 'whereExists', 'orWhereExists',
'whereNotExists', 'orWhereNotExists', 'whereBetween', 'orWhereBetween', 'whereNotBetween', 'orWhereNotBetween',
'whereLike', 'orWhereLike', 'whereILike', 'orWhereILike',
'whereJsonObject', 'whereJsonPath', 'whereJsonSupersetOf', 'whereJsonSubsetOf',
'join', 'joinRaw', 'leftJoin', 'leftOuterJoin', 'rightJoin', 'rightOuterJoin', 'crossJoin',
'whereLike', 'orWhereLike', 'whereILike', 'orWhereILike',
'whereJsonObject', 'whereJsonPath', 'whereJsonSupersetOf', 'whereJsonSubsetOf',
'join', 'joinRaw', 'leftJoin', 'leftOuterJoin', 'rightJoin', 'rightOuterJoin', 'crossJoin',
'transacting', 'groupBy', 'groupByRaw', 'returning',
'having', 'havingRaw', 'havingBetween',
'having', 'havingRaw', 'havingBetween',
'limit', 'offset', 'orderBy', 'orderByRaw', // 'inRandomOrder',
'union', 'insert', 'forUpdate', 'forShare', 'distinct',
'clearOrder', 'clear', 'clearSelect', 'clearWhere', 'clearHaving', 'clearGroup',
Expand Down Expand Up @@ -102,12 +102,32 @@ class Builder {
return new Proxy(this, handler)
}

where(...args) {
if (typeof args[0] === 'function') {
const callback = args[0];
this.query.where((query) => {
const originalQuery = this.query;
this.query = query;
callback(this);
this.query = originalQuery;
});

return this;
}

this.query.where(...args);

return this;
}

orWhere(...args) {
if (typeof args[0] === 'function') {
const callback = args[0];
this.query.orWhere((query) => {
const originalQuery = this.query;
this.query = query;
callback(this);
this.query = originalQuery;
});

return this;
Expand Down Expand Up @@ -160,7 +180,7 @@ class Builder {
builder.globalScopes = { ...this.globalScopes };
builder.localMacros = { ...this.localMacros };
builder.eagerLoad = { ...this.eagerLoad };

return builder;
}

Expand Down Expand Up @@ -196,8 +216,8 @@ class Builder {
}

addUpdatedAtColumn(values) {
if (! this.model.usesTimestamps()
|| this.model.getUpdatedAtColumn() === null) {
if (!this.model.usesTimestamps()
|| this.model.getUpdatedAtColumn() === null) {
return values;
}

Expand Down Expand Up @@ -256,7 +276,7 @@ class Builder {
} else {
this.query = this.query?.table(this.model.getTable());
}

return this;
}

Expand Down Expand Up @@ -327,7 +347,7 @@ class Builder {
if (typeof scope !== 'string') {
scope = scope.constructor.name;
}

unset(this.globalScopes, scope);

return this;
Expand Down Expand Up @@ -362,7 +382,7 @@ class Builder {
if (relations.length === 0) {
return this;
}

for (const relation of relations) {
let eagerLoad;
if (typeof relation === 'string') {
Expand All @@ -377,7 +397,7 @@ class Builder {
}

this.eagerLoad = merge(this.eagerLoad, this.parseWithRelations(eagerLoads));

return this;
}

Expand Down Expand Up @@ -495,13 +515,13 @@ class Builder {
const db = this.model.getConnection();

return this.where(
db.raw('(' + query.toSQL().sql +')'),
db.raw('(' + query.toSQL().sql + ')'),
operator,
typeof count ==='number' ? db.raw(count) : count,
typeof count === 'number' ? db.raw(count) : count,
boolean
);
}

withAggregate(relations, column, action = null) {
if (relations.length === 0) {
return this;
Expand Down Expand Up @@ -594,9 +614,9 @@ class Builder {

const wheres = from.getQuery()._single.table !== this.getQuery()._single.table
? this.requalifyWhereTables(
from.getQuery().wheres,
from.getQuery().from,
this.getModel().getTable()
from.getQuery().wheres,
from.getQuery().from,
this.getModel().getTable()
) : from.getQuery().wheres;

return this.where(
Expand Down Expand Up @@ -631,7 +651,7 @@ class Builder {
if (query.query._single.table !== this.query._single.table) {
const databaseName = query.query._single.table;

if (! query.query._single.table.startsWith(databaseName) && ! query.query._single.table.contains('.')) {
if (!query.query._single.table.startsWith(databaseName) && !query.query._single.table.contains('.')) {
query.from(databaseName + '.' + query.from);
}
}
Expand Down Expand Up @@ -694,7 +714,7 @@ class Builder {
const last = progress.join('.');

if (results[last] === undefined) {
results[last] = () => {};
results[last] = () => { };
}
});

Expand All @@ -705,15 +725,15 @@ class Builder {
let preparedRelationships = {};

if (prefix !== '') {
prefix += '.';
prefix += '.';
}

for (const key in relations) {
const value = relations[key];
if (isString(value) || isFinite(parseInt(value))) {
continue;
}

const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);

preparedRelationships = merge(
Expand All @@ -723,7 +743,7 @@ class Builder {
},
this.prepareNestedWithRelationships(value, `${prefix}${attribute}`),
);

unset(relations, key);
}

Expand All @@ -734,10 +754,10 @@ class Builder {
if (isString(value)) {
[attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
}

preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([
attributeSelectConstraint,
preparedRelationships[`${prefix}${attribute}`] || (() => {}),
preparedRelationships[`${prefix}${attribute}`] || (() => { }),
]);
}

Expand Down Expand Up @@ -766,7 +786,7 @@ class Builder {
if (column.includes('.')) {
return column;
}

return query instanceof BelongsToMany
? query.related.getTable() + '.' + column
: column;
Expand Down Expand Up @@ -800,13 +820,13 @@ class Builder {
if (models.length > 0) {
models = await this.eagerLoadRelations(models);
}

return models[0] || null;
}

async firstOrFail(...columns) {
const data = await this.first(...columns);

if (data === null) {
throw (new ModelNotFoundError).setModel(this.model.constructor.name);
}
Expand Down Expand Up @@ -996,7 +1016,7 @@ class Builder {
this.query.select(...columns);
}
}

return this.hydrate(
await this.query.get()
).all();
Expand All @@ -1018,7 +1038,7 @@ class Builder {

return relation.asProxy();
}

relationsNestedUnder(relation) {
const nested = {};

Expand Down Expand Up @@ -1054,7 +1074,7 @@ class Builder {
for (const name in this.eagerLoad) {
const constraints = this.eagerLoad[name];

if (! name.includes('.')) {
if (!name.includes('.')) {
models = await this.eagerLoadRelation(models, name, constraints);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ async function prepareDatabase(migrator) {
}

async function setupConnection(config) {
const sutando = require('./sutando');
const Sutando = require('./sutando');
const MigrationRepository = require('./migrations/migration-repository');
const Migrator = require('./migrations/migrator');

const table = config?.migration?.table || 'migrations';

const sutando = new Sutando;

sutando.addConnection(config, 'default');

Object.entries(config.connections || {}).forEach(([name, connection]) => {
sutando.addConnection(connection, name);
sutando.addConnection(connection, name);
});

const repository = new MigrationRepository(sutando, table);
Expand Down Expand Up @@ -99,7 +100,7 @@ async function migrateStatus(config, options = {}, destroyAll = false) {
if (destroyAll) {
await sutando.destroyAll();
}

return migrations;
}

Expand Down
Loading