Skip to content

Commit

Permalink
Converts more queryBuilder things
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Robin committed Aug 11, 2024
1 parent 5c0e939 commit 1c8673f
Show file tree
Hide file tree
Showing 42 changed files with 1,152 additions and 970 deletions.
2 changes: 2 additions & 0 deletions src/constants/regexps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ALIAS_REGEX = /\s+as\s+/i;
export const COUNT_REGEX = /count/i;
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';
export class RelationDoesNotExistError extends Error {
readonly relationName: string;

class RelationDoesNotExistError extends Error {
constructor(relationName) {
constructor(relationName: string) {
super(`unknown relation "${relationName}" in a relation expression`);

this.name = this.constructor.name;
this.relationName = relationName;
}
}

module.exports = {
RelationDoesNotExistError,
};
21 changes: 10 additions & 11 deletions src/model/modelUtils.js → src/model/modelUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const hiddenProps = ['$$queryProps'];
import { nany } from '../ninja.ts';

const staticHiddenProps = [
export const hiddenProps = ['$$queryProps'];

export const staticHiddenProps = [
'$$knex',
'$$validator',
'$$jsonSchema',
Expand All @@ -19,7 +21,11 @@ const staticHiddenProps = [
'$$virtualAttributes',
];

function defineNonEnumerableProperty(obj, prop, value) {
export function defineNonEnumerableProperty(
obj: nany,
prop: string,
value: nany,
) {
Object.defineProperty(obj, prop, {
enumerable: false,
writable: true,
Expand All @@ -28,7 +34,7 @@ function defineNonEnumerableProperty(obj, prop, value) {
});
}

function keyByProps(models, props) {
export function keyByProps(models: nany[], props: string[]) {
const map = new Map();

for (let i = 0, l = models.length; i < l; ++i) {
Expand All @@ -38,10 +44,3 @@ function keyByProps(models, props) {

return map;
}

module.exports = {
hiddenProps,
staticHiddenProps,
defineNonEnumerableProperty,
keyByProps,
};
79 changes: 37 additions & 42 deletions src/queryBuilder/JoinBuilder.js → src/queryBuilder/JoinBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,142 +1,137 @@
'use strict';
import { nany } from '../ninja.ts';
import { QueryBuilderOperationSupport } from './QueryBuilderOperationSupport.ts';
import { KnexOperation } from './operations/KnexOperation.ts';

const { QueryBuilderOperationSupport } = require('./QueryBuilderOperationSupport');
const { KnexOperation } = require('./operations/KnexOperation');

class JoinBuilder extends QueryBuilderOperationSupport {
using(...args) {
export class JoinBuilder extends QueryBuilderOperationSupport<nany> {
using(...args: nany[]) {
return this.addOperation(new KnexOperation('using'), args);
}

on(...args) {
on(...args: nany[]) {
return this.addOperation(new KnexOperation('on'), args);
}

orOn(...args) {
orOn(...args: nany[]) {
return this.addOperation(new KnexOperation('orOn'), args);
}

onBetween(...args) {
onBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('onBetween'), args);
}

onNotBetween(...args) {
onNotBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('onNotBetween'), args);
}

orOnBetween(...args) {
orOnBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnBetween'), args);
}

orOnNotBetween(...args) {
orOnNotBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnNotBetween'), args);
}

onIn(...args) {
onIn(...args: nany[]) {
return this.addOperation(new KnexOperation('onIn'), args);
}

onNotIn(...args) {
onNotIn(...args: nany[]) {
return this.addOperation(new KnexOperation('onNotIn'), args);
}

orOnIn(...args) {
orOnIn(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnIn'), args);
}

orOnNotIn(...args) {
orOnNotIn(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnNotIn'), args);
}

onNull(...args) {
onNull(...args: nany[]) {
return this.addOperation(new KnexOperation('onNull'), args);
}

orOnNull(...args) {
orOnNull(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnNull'), args);
}

onNotNull(...args) {
onNotNull(...args: nany[]) {
return this.addOperation(new KnexOperation('onNotNull'), args);
}

orOnNotNull(...args) {
orOnNotNull(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnNotNull'), args);
}

onExists(...args) {
onExists(...args: nany[]) {
return this.addOperation(new KnexOperation('onExists'), args);
}

orOnExists(...args) {
orOnExists(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnExists'), args);
}

onNotExists(...args) {
onNotExists(...args: nany[]) {
return this.addOperation(new KnexOperation('onNotExists'), args);
}

orOnNotExists(...args) {
orOnNotExists(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnNotExists'), args);
}

type(...args) {
type(...args: nany[]) {
return this.addOperation(new KnexOperation('type'), args);
}

andOn(...args) {
andOn(...args: nany[]) {
return this.addOperation(new KnexOperation('andOn'), args);
}

andOnIn(...args) {
andOnIn(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnIn'), args);
}

andOnNotIn(...args) {
andOnNotIn(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnNotIn'), args);
}

andOnNull(...args) {
andOnNull(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnNull'), args);
}

andOnNotNull(...args) {
andOnNotNull(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnNotNull'), args);
}

andOnExists(...args) {
andOnExists(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnExists'), args);
}

andOnNotExists(...args) {
andOnNotExists(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnNotExists'), args);
}

andOnBetween(...args) {
andOnBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnBetween'), args);
}

andOnNotBetween(...args) {
andOnNotBetween(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnNotBetween'), args);
}

andOnJsonPathEquals(...args) {
andOnJsonPathEquals(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnJsonPathEquals'), args);
}

onVal(...args) {
onVal(...args: nany[]) {
return this.addOperation(new KnexOperation('onVal'), args);
}

andOnVal(...args) {
andOnVal(...args: nany[]) {
return this.addOperation(new KnexOperation('andOnVal'), args);
}

orOnVal(...args) {
orOnVal(...args: nany[]) {
return this.addOperation(new KnexOperation('orOnVal'), args);
}
}

module.exports = {
JoinBuilder,
};
2 changes: 1 addition & 1 deletion src/queryBuilder/RawBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class RawBuilder {
#args: nany[];
#as?: string;

constructor(sql: string, args: nany[]) {
constructor(sql: string, ...args: nany[]) {
this.#sql = `${sql}`;
this.#args = args;
}
Expand Down
2 changes: 1 addition & 1 deletion src/queryBuilder/ReferenceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IModel } from './QueryBuilderOperationSupport.ts';
import { nany } from '../ninja.ts';
import { Knex } from 'knex';

class ReferenceBuilder<T extends IModel> {
export class ReferenceBuilder<T extends IModel> {
#expr: string;
#parsedExpr?: ParsedExpression;
#column?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/queryBuilder/operations/DelegateOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import {
// to to the constructor in `opt.delegate`.
export class DelegateOperation extends QueryBuilderOperation
implements HasAllHooks {
delegate: QueryBuilderOperation;
protected delegate: QueryBuilderOperation;

constructor(
name: string | undefined,
name: string,
opt: { delegate: QueryBuilderOperation },
) {
super(name, opt);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';
import { QueryBuilderOperation } from './QueryBuilderOperation.ts';
import { StaticHookArguments } from '../StaticHookArguments.ts';
import knex from 'knex';
import { nany } from '../../ninja.ts';

const { QueryBuilderOperation } = require('./QueryBuilderOperation');
const { StaticHookArguments } = require('../StaticHookArguments');

class DeleteOperation extends QueryBuilderOperation {
async onBefore2(builder, result) {
export class DeleteOperation extends QueryBuilderOperation {
async onBefore2(builder: nany, result: nany) {
await callBeforeDelete(builder);
return result;
}

onBuildKnex(knexBuilder) {
onBuildKnex(knexBuilder: knex.Knex) {
return knexBuilder.delete();
}

onAfter2(builder, result) {
onAfter2(builder: nany, result: nany) {
return callAfterDelete(builder, result);
}

Expand All @@ -22,20 +22,20 @@ class DeleteOperation extends QueryBuilderOperation {
}
}

function callBeforeDelete(builder) {
function callBeforeDelete(builder: nany) {
return callStaticBeforeDelete(builder);
}

function callStaticBeforeDelete(builder) {
function callStaticBeforeDelete(builder: nany) {
const args = StaticHookArguments.create({ builder });
return builder.modelClass().beforeDelete(args);
}

function callAfterDelete(builder, result) {
function callAfterDelete(builder: nany, result: nany) {
return callStaticAfterDelete(builder, result);
}

async function callStaticAfterDelete(builder, result) {
async function callStaticAfterDelete(builder: nany, result: nany) {
const args = StaticHookArguments.create({ builder, result });
const maybeResult = await builder.modelClass().afterDelete(args);

Expand All @@ -45,7 +45,3 @@ async function callStaticAfterDelete(builder, result) {
return maybeResult;
}
}

module.exports = {
DeleteOperation,
};
37 changes: 0 additions & 37 deletions src/queryBuilder/operations/FindByIdOperation.js

This file was deleted.

Loading

0 comments on commit 1c8673f

Please sign in to comment.