Skip to content
Merged

Dev #90

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
2,070 changes: 1,285 additions & 785 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
"@panates/eslint-config": "^2.1.6",
"@panates/eslint-config-ts": "^2.1.6",
"@panates/tsconfig": "^2.1.6",
"@swc-node/register": "^1.11.1",
"@swc/core": "^1.15.33",
"@swc/helpers": "^0.5.21",
"@swc-node/register": "^1.12.1",
"@swc/core": "^1.15.47",
"@swc/helpers": "^0.5.23",
"@types/mocha": "^10.0.10",
"@types/node": "^25.9.1",
"@types/sinon": "^21.0.1",
"c8": "^11.0.0",
"globals": "^17.6.0",
"dpdm": "^4.2.0",
"mocha": "^11.7.5",
"npm-check-updates": "^22.2.0",
"@types/node": "^26.1.2",
"@types/sinon": "^22.0.0",
"c8": "^12.0.0",
"globals": "^17.9.0",
"dpdm": "^4.3.0",
"mocha": "^11.8.0",
"npm-check-updates": "^23.0.1",
"postgrejs": "^2.23.0",
"prettier": "^3.8.3",
"prettier": "^3.9.6",
"rimraf": "^6.1.3",
"ts-cleanup": "^1.3.0",
"tslib": "^2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/builder",
"description": "Extensible multi-dialect SQL query builder written with TypeScript",
"version": "5.0.4",
"version": "5.0.5",
"author": "Panates",
"private": false,
"license": "Apache-2.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/builder/src/serialize-context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SerializationType } from './enums.js';
import { SerializerRegistry } from './extensions.js';
import { SqlElement } from './serializable.js';
import { Query } from './sql/index.js';
import { isLogicalOperator, isQuery, isSerializable } from './type-guards.js';
import { isLogicalOperator, isQuery, isSqlElement } from './type-guards.js';
import type {
DefaultSerializeFunction,
GenerateOptions,
Expand Down Expand Up @@ -72,6 +71,7 @@ export class SerializeContext implements GenerateOptions {
dialect?: string;
prettyPrint?: boolean;
params?: Record<string, any>;
orgParams?: Record<string, any>;
dialectVersion?: string;
strictParams?: boolean;
serializeHooks?: Function[];
Expand Down Expand Up @@ -119,7 +119,7 @@ export class SerializeContext implements GenerateOptions {
);
}
if (typeof v === 'object') {
if (isSerializable(v)) {
if (isSqlElement(v)) {
const s = v._serialize(this);
return s
? isQuery(v) || isLogicalOperator(v)
Expand Down Expand Up @@ -149,7 +149,7 @@ export class SerializeContext implements GenerateOptions {
this.numberToSQL(v),
);
}
if (v instanceof SqlElement) return v._serialize(this);
if (isSqlElement(v)) return v._serialize(this);
return v;
}

Expand Down
27 changes: 12 additions & 15 deletions packages/builder/src/sql/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SerializationType } from '../enums.js';
import { SerializeContext } from '../serialize-context.js';
import { isRaw } from '../type-guards.js';
import { isRaw, isSqlElement } from '../type-guards.js';
import { Raw } from './elements/raw.js';
import { TableName } from './elements/table-name.js';
import { And } from './operators/and.js';
Expand Down Expand Up @@ -64,24 +64,21 @@ export const Delete = function (
) {
if (!(this instanceof Delete)) return new Delete(tableName);
Query.call(this);
if (
!(
tableName &&
(tableName instanceof TableName ||
typeof tableName === 'string' ||
isRaw(tableName))
)
) {
if (!(
tableName &&
(isSqlElement(tableName, SerializationType.TABLE_NAME) ||
typeof tableName === 'string' ||
isRaw(tableName))
)) {
throw new TypeError(
'String or Raw instance required as first argument (tableName) for Delete',
);
}
this._table =
tableName instanceof TableName
? tableName
: typeof tableName === 'string'
? TableName(tableName)
: tableName;
this._table = isSqlElement(tableName, SerializationType.TABLE_NAME)
? tableName
: typeof tableName === 'string'
? TableName(tableName)
: tableName;
} as DeleteCtor;

Delete.prototype = DeleteClass.prototype;
Expand Down
14 changes: 6 additions & 8 deletions packages/builder/src/sql/elements/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ export const Join = function (
if (!(this instanceof Join)) return new Join(joinType, table);
SqlElement.call(this);
// noinspection SuspiciousTypeOfGuard
if (
!(
isSelect(table) ||
isRaw(table) ||
isTableName(table) ||
typeof table === 'string'
)
) {
if (!(
isSelect(table) ||
isRaw(table) ||
isTableName(table) ||
typeof table === 'string'
)) {
throw new TypeError(
'Table name, select query or raw object required for Join',
);
Expand Down
14 changes: 5 additions & 9 deletions packages/builder/src/sql/insert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SerializationType } from '../enums.js';
import { printArray } from '../helpers.js';
import { SqlElement } from '../serializable.js';
import { SerializeContext } from '../serialize-context.js';
import { isRaw, isSelect, isSerializable } from '../type-guards.js';
import { isRaw, isSelect, isSqlElement } from '../type-guards.js';
import { Raw } from './elements/raw.js';
import { TableName } from './elements/table-name.js';
import { Query } from './query.js';
Expand Down Expand Up @@ -64,7 +63,7 @@ class InsertClass extends ReturningQuery {
*
*/
protected __serializeValues(ctx: SerializeContext): string {
if (isSerializable(this._input)) return this._input._serialize(ctx);
if (isSqlElement(this._input)) return this._input._serialize(ctx);

const arr: string[] = [];
const allValues = this._input;
Expand Down Expand Up @@ -103,9 +102,8 @@ export const Insert = function (
!input ||
!(
(typeof input === 'object' && !Array.isArray(input)) ||
(input instanceof SqlElement &&
(input._type === SerializationType.SELECT_QUERY ||
input._type === SerializationType.RAW))
isSelect(input) ||
isRaw(input)
)
) {
throw new TypeError(
Expand All @@ -127,7 +125,5 @@ export interface Insert extends InsertClass {}
* @param value
*/
export function isInsertQuery(value: any): value is Insert {
return (
isSerializable(value) && value._type === SerializationType.INSERT_QUERY
);
return isSqlElement(value, SerializationType.INSERT_QUERY);
}
17 changes: 11 additions & 6 deletions packages/builder/src/sql/operators/comp-operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SerializationType } from '../../enums.js';
import { SqlElement } from '../../serializable.js';
import { SerializeContext } from '../../serialize-context.js';
import { Field, Param } from '../elements/index.js';
import { isFieldName, isParam, isSqlElement } from '../../type-guards.js';
import { Param } from '../elements/index.js';
import { Operator } from './operator.js';

const EXPRESSION_PATTERN = /^([\w\\.$]+)(\[])?/;
Expand All @@ -25,6 +26,8 @@ class CompOperatorClass extends Operator {
symbol: this._symbol,
left,
right,
orgLeft: this._left,
orgRight: this._right,
};
return this.__serialize(ctx, o);
}
Expand All @@ -35,34 +38,36 @@ class CompOperatorClass extends Operator {
left?: any,
): any {
const isRight = !!left;
if (ctx.strictParams && !(x instanceof SqlElement) && isRight) {
if (ctx.strictParams && !isSqlElement(x) && isRight) {
ctx.strictParamGenId = ctx.strictParamGenId || 0;
const name = 'P$_' + ++ctx.strictParamGenId;
ctx.params = ctx.params || {};
ctx.orgParams = ctx.orgParams || {};
ctx.params[name] = x;
ctx.orgParams[name] = x;
x = Param({
name,
dataType: left?.dataType,
isArray: left?.isArray || Array.isArray(x),
});
}

if (x instanceof SqlElement) {
if (isSqlElement(x)) {
const expression = ctx.anyToSQL(x);
const result: any = {
expression,
};
if (x instanceof Field) {
if (isFieldName(x)) {
result.dataType = x._dataType;
result.isArray = x._isArray;
}
if (x instanceof Param) {
if (isParam(x)) {
let value = ctx.params ? ctx.params[x._name] : undefined;
if (x._isArray && value != null && !Array.isArray(value))
value = [value];
result.value = value;
result.isArray = x._isArray || Array.isArray(value);
result.isParam = true;
result.isParam = ctx.params?.[x._name] != undefined;
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/src/sql/operators/in.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OperatorType } from '../../enums.js';
import type { SqlElement } from '../../serializable.js';
import { SerializeContext } from '../../serialize-context.js';
import { isSerializable } from '../../type-guards.js';
import { isSqlElement } from '../../type-guards.js';
import { CompOperator } from './comp-operator.js';

class InClass extends CompOperator {
Expand Down Expand Up @@ -29,7 +29,7 @@ export const In = function (this: In, left: string | SqlElement, right: any[]) {
this._left = m[1];
this._isArray = !!m[2];
}
this._right = Array.isArray(right) || isSerializable(right) ? right : [right];
this._right = Array.isArray(right) || isSqlElement(right) ? right : [right];
} as InCtor;

In.prototype = InClass.prototype;
Expand Down
1 change: 1 addition & 0 deletions packages/builder/src/sql/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class QueryClass extends SqlElement {
generate(options?: GenerateOptions): GenerateResult {
const ctx = new SerializeContext(this, options);
if (this._params) ctx.params = { ...ctx.params, ...this._params };
ctx.orgParams = { ...ctx.params };
ctx.serializeHooks = this.listeners('serialize');

/* generate output */
Expand Down
8 changes: 4 additions & 4 deletions packages/builder/src/sql/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SerializationType } from '../enums.js';
import { printArray } from '../helpers.js';
import { SqlElement } from '../serializable.js';
import { SerializeContext } from '../serialize-context.js';
import { isJoin } from '../type-guards.js';
import { isJoin, isSelect, isTableName } from '../type-guards.js';
import { Field } from './elements/field.js';
import { GroupColumn } from './elements/group-column.js';
import { Join } from './elements/join.js';
Expand Down Expand Up @@ -67,7 +67,7 @@ class SelectClass extends Query {
for (const arg of table) {
if (!arg) continue;
this._tables.push(
arg instanceof TableName
isTableName(arg)
? arg
: typeof arg === 'string'
? new TableName(arg)
Expand Down Expand Up @@ -228,7 +228,7 @@ class SelectClass extends Query {
const s = ctx.anyToSQL(t);
// t._serialize(ctx);
if (s) {
if (t instanceof SelectClass) {
if (isSelect(t)) {
if (!t._alias)
throw new TypeError('Alias required for sub-select in columns');
arr.push(s + ' ' + t._alias);
Expand All @@ -253,7 +253,7 @@ class SelectClass extends Query {
const s = t._serialize(ctx);
/* istanbul ignore else */
if (s) {
if (t instanceof SelectClass) {
if (isSelect(t)) {
if (!t._alias)
throw new TypeError('Alias required for sub-select in "from"');
arr.push({
Expand Down
33 changes: 14 additions & 19 deletions packages/builder/src/sql/update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SerializationType } from '../enums.js';
import { printArray } from '../helpers.js';
import { SqlElement } from '../serializable.js';
import { SerializeContext } from '../serialize-context.js';
import { isRaw } from '../type-guards.js';
import { isRaw, isSelect, isTableName } from '../type-guards.js';
import { Raw } from './elements/raw.js';
import { TableName } from './elements/table-name.js';
import { And } from './operators/and.js';
Expand Down Expand Up @@ -96,14 +95,12 @@ export const Update = function (
) {
if (!(this instanceof Update)) return new Update(tableName, input);
Query.call(this);
if (
!(
tableName &&
(tableName instanceof TableName ||
typeof tableName === 'string' ||
isRaw(tableName))
)
) {
if (!(
tableName &&
(isTableName(tableName) ||
typeof tableName === 'string' ||
isRaw(tableName))
)) {
throw new TypeError(
'String or Raw instance required as first argument (tableName) for Update',
);
Expand All @@ -112,21 +109,19 @@ export const Update = function (
!input ||
!(
(typeof input === 'object' && !Array.isArray(input)) ||
(input instanceof SqlElement &&
(input._type === SerializationType.SELECT_QUERY ||
input._type === SerializationType.RAW))
isSelect(input) ||
isRaw(input)
)
) {
throw new TypeError(
'Object or Raw instance required as second argument (input) for Update',
);
}
this._table =
tableName instanceof TableName
? tableName
: typeof tableName === 'string'
? new TableName(tableName)
: tableName;
this._table = isTableName(tableName)
? tableName
: typeof tableName === 'string'
? new TableName(tableName)
: tableName;
this._input = input;
} as UpdateCtor;

Expand Down
Loading