sasat 0.19.37 → 0.19.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ import * as mysql from 'mysql2';
3
3
  export declare class MysqlClient extends DBClient {
4
4
  readonly connectionOption?: Partial<mysql.ConnectionOptions> | undefined;
5
5
  private readonly pool;
6
+ logger: (sql: string) => void;
6
7
  constructor(connectionOption?: Partial<mysql.ConnectionOptions> | undefined, poolOption?: Partial<mysql.PoolOptions>);
7
8
  transaction(): Promise<SQLTransaction>;
8
9
  release(): Promise<void>;
@@ -8,6 +8,7 @@ export class MysqlClient extends DBClient {
8
8
  constructor(connectionOption, poolOption) {
9
9
  super();
10
10
  this.connectionOption = connectionOption;
11
+ this.logger = () => { };
11
12
  this.pool = mysql.createPool({
12
13
  ...defaultConfig,
13
14
  ...connectionOption,
@@ -28,6 +29,7 @@ export class MysqlClient extends DBClient {
28
29
  this._released = true;
29
30
  }
30
31
  execSql(sql) {
32
+ this.logger(sql);
31
33
  return promisify(this.pool.query).bind(this.pool)(sql);
32
34
  }
33
35
  }
@@ -28,12 +28,12 @@ export const orderToSQL = (order) => order
28
28
  .join(', ');
29
29
  const mergeWhereClause = (whereClauses) => {
30
30
  const result = [];
31
- whereClauses.forEach(it => {
31
+ for (const it of whereClauses) {
32
32
  if (Array.isArray(it))
33
33
  result.push(...it);
34
34
  else
35
35
  result.push(it);
36
- });
36
+ }
37
37
  return result;
38
38
  };
39
39
  export const createSQLString = (sql) => {
@@ -4,14 +4,14 @@ type StrOrNum = string | number;
4
4
  type ValueType = string | boolean | number | null;
5
5
  export declare const QExpr: {
6
6
  readonly field: (table: string, name: string, alias?: string) => Field;
7
- readonly fn: (fnName: string, args: Value[]) => Fn;
7
+ readonly fn: (fnName: string, args: Value[], alias?: string) => Fn;
8
8
  readonly paren: (expression: BooleanValueExpression) => ParenthesisExpression;
9
9
  readonly table: (name: string, joins: Join[], alias: string) => QueryTable;
10
10
  readonly subQueryTable: (query: Query, joins: Join[], alias: string) => QueryTable;
11
11
  readonly join: (table: QueryTable, conditions: BooleanValueExpression, type?: JoinType) => Join;
12
12
  readonly value: (value: ValueType) => Literal;
13
- readonly sort: (field: Field | Fn, direction?: SortDirection) => Sort;
14
- readonly order: (field: Field | Fn, direction?: SortDirection) => Sort;
13
+ readonly sort: (field: Field | Fn | Identifier, direction?: SortDirection) => Sort;
14
+ readonly order: (field: Field | Fn | Identifier, direction?: SortDirection) => Sort;
15
15
  readonly ident: (identifier: string) => Identifier;
16
16
  readonly raw: (sql: string) => RawExpression;
17
17
  readonly simpleWhere: (tableNameOrAlias: string, where: {
@@ -28,11 +28,11 @@ export declare const QExpr: {
28
28
  readonly comparison: (left: Value, operator: ComparisonOperators, right: Value) => ComparisonExpression;
29
29
  readonly contains: (left: Value, right: string) => BooleanValueExpression;
30
30
  readonly notContains: (left: Value, right: string) => BooleanValueExpression;
31
- readonly statsWiths: (left: Value, right: string) => BooleanValueExpression;
32
- readonly notStatsWiths: (left: Value, right: string) => BooleanValueExpression;
33
- readonly endsWiths: (left: Value, right: string) => BooleanValueExpression;
34
- readonly notEndsWiths: (left: Value, right: string) => BooleanValueExpression;
35
- readonly In: (left: Value, right: StrOrNum[] | Query | RawExpression) => BooleanValueExpression;
31
+ readonly startsWith: (left: Value, right: string) => BooleanValueExpression;
32
+ readonly notStartsWith: (left: Value, right: string) => BooleanValueExpression;
33
+ readonly endsWith: (left: Value, right: string) => BooleanValueExpression;
34
+ readonly notEndsWith: (left: Value, right: string) => BooleanValueExpression;
35
+ readonly in: (left: Value, right: StrOrNum[] | Query | RawExpression) => BooleanValueExpression;
36
36
  readonly notIn: (left: Value, values: StrOrNum[]) => InExpression;
37
37
  readonly between: (left: Value, begin: Value, end: Value) => BetweenExpression;
38
38
  readonly isNull: (expr: Value) => IsNullExpression;
@@ -53,11 +53,11 @@ export declare const QExpr: {
53
53
  comparison: (left: Value, operator: ComparisonOperators, right: Value) => ComparisonExpression;
54
54
  contains: (left: Value, right: string) => BooleanValueExpression;
55
55
  notContains: (left: Value, right: string) => BooleanValueExpression;
56
- statsWiths: (left: Value, right: string) => BooleanValueExpression;
57
- notStatsWiths: (left: Value, right: string) => BooleanValueExpression;
58
- endsWiths: (left: Value, right: string) => BooleanValueExpression;
59
- notEndsWiths: (left: Value, right: string) => BooleanValueExpression;
60
- In: (left: Value, right: StrOrNum[] | Query | RawExpression) => BooleanValueExpression;
56
+ startsWith: (left: Value, right: string) => BooleanValueExpression;
57
+ notStartsWith: (left: Value, right: string) => BooleanValueExpression;
58
+ endsWith: (left: Value, right: string) => BooleanValueExpression;
59
+ notEndsWith: (left: Value, right: string) => BooleanValueExpression;
60
+ in: (left: Value, right: StrOrNum[] | Query | RawExpression) => BooleanValueExpression;
61
61
  notIn: (left: Value, values: StrOrNum[]) => InExpression;
62
62
  between: (left: Value, begin: Value, end: Value) => BetweenExpression;
63
63
  isNull: (expr: Value) => IsNullExpression;
@@ -29,10 +29,11 @@ const field = (table, name, alias) => ({
29
29
  name: name,
30
30
  alias,
31
31
  });
32
- const fn = (fnName, args) => ({
32
+ const fn = (fnName, args, alias) => ({
33
33
  kind: QueryNodeKind.Function,
34
34
  fnName,
35
35
  args,
36
+ alias,
36
37
  });
37
38
  const paren = (expression) => ({
38
39
  kind: QueryNodeKind.Parenthesis,
@@ -103,11 +104,11 @@ const conditions = {
103
104
  comparison: (left, operator, right) => comparison(operator)(left, right),
104
105
  contains: containsExpr(false, 'contains'),
105
106
  notContains: containsExpr(true, 'contains'),
106
- statsWiths: containsExpr(false, 'start'),
107
- notStatsWiths: containsExpr(true, 'start'),
108
- endsWiths: containsExpr(false, 'end'),
109
- notEndsWiths: containsExpr(true, 'end'),
110
- In,
107
+ startsWith: containsExpr(false, 'start'),
108
+ notStartsWith: containsExpr(true, 'start'),
109
+ endsWith: containsExpr(false, 'end'),
110
+ notEndsWith: containsExpr(true, 'end'),
111
+ in: In,
111
112
  notIn,
112
113
  between,
113
114
  isNull: isNull(false),
@@ -20,7 +20,7 @@ export declare enum QueryNodeKind {
20
20
  }
21
21
  export type LockMode = 'FOR UPDATE' | 'FOR SHARE';
22
22
  export type Query = {
23
- select: Select;
23
+ select: SelectExpr[];
24
24
  from: QueryTable;
25
25
  where?: BooleanValueExpression;
26
26
  groupBy?: GroupByExpr;
@@ -34,13 +34,11 @@ export type GroupByExpr = {
34
34
  kind: QueryNodeKind.GroupBy;
35
35
  cols: (Field | Identifier)[];
36
36
  };
37
- type Select = SelectExpr[];
38
- export declare const NO_ALIAS: "__SASAT_NO_ALIAS";
39
37
  export type Field = {
40
38
  kind: QueryNodeKind.Field;
41
39
  table: string;
42
40
  name: string;
43
- alias?: string | typeof NO_ALIAS;
41
+ alias?: string;
44
42
  };
45
43
  export type Fn = {
46
44
  kind: QueryNodeKind.Function;
@@ -48,13 +46,13 @@ export type Fn = {
48
46
  args: Value[];
49
47
  alias?: string;
50
48
  };
51
- export type SelectExpr = Field | Fn;
49
+ export type SelectExpr = Field | Fn | Identifier;
52
50
  export type QueryTable = {
53
51
  kind: QueryNodeKind.Table;
54
52
  alias: string;
55
53
  joins: Join[];
56
54
  } & ({
57
- subquery: false;
55
+ subquery?: false;
58
56
  name: string;
59
57
  } | {
60
58
  subquery: true;
@@ -137,4 +135,3 @@ export type Sort = {
137
135
  direction?: SortDirection;
138
136
  };
139
137
  export type QueryNode = Field | Fn | QueryTable | Literal | BooleanValueExpression | Join | Sort;
140
- export {};
@@ -18,4 +18,3 @@ export var QueryNodeKind;
18
18
  QueryNodeKind[QueryNodeKind["Raw"] = 15] = "Raw";
19
19
  QueryNodeKind[QueryNodeKind["GroupBy"] = 16] = "GroupBy";
20
20
  })(QueryNodeKind || (QueryNodeKind = {}));
21
- export const NO_ALIAS = '__SASAT_NO_ALIAS';
@@ -1,13 +1,13 @@
1
1
  import { SELECT_ALIAS_SEPARATOR } from './nodeToSql.js';
2
2
  const rowToObjs = (row) => {
3
3
  const objs = {};
4
- Object.entries(row).forEach(([key, value]) => {
4
+ for (const [key, value] of Object.entries(row)) {
5
5
  const [table, column] = key.split(SELECT_ALIAS_SEPARATOR);
6
6
  if (!objs[table]) {
7
7
  objs[table] = {};
8
8
  }
9
9
  objs[table][column] = value;
10
- });
10
+ }
11
11
  return objs;
12
12
  };
13
13
  const getUnique = (obj, info) => info.keyAliases.map(it => obj[it]).join('_~_');
@@ -44,9 +44,9 @@ const execTable = (info, objs, current) => {
44
44
  result = currentTarget;
45
45
  }
46
46
  if (currentTarget !== null) {
47
- info.joins.forEach(it => {
47
+ for (const it of info.joins) {
48
48
  currentTarget[it.property] = execTable(it, objs, currentTarget[it.property]);
49
- });
49
+ }
50
50
  }
51
51
  return result;
52
52
  };
@@ -54,17 +54,17 @@ export const hydrate = (data, info) => {
54
54
  const result = [];
55
55
  const t0mapper = {};
56
56
  info.isArray = false;
57
- data.forEach(row => {
57
+ for (const row of data) {
58
58
  const objs = rowToObjs(row);
59
59
  const currentObj = objs[info.tableAlias];
60
60
  const unique = getUnique(currentObj, info);
61
61
  if (t0mapper[unique] === undefined) {
62
62
  t0mapper[unique] = result.length;
63
63
  result.push(execTable(info, objs, currentObj));
64
- return;
64
+ continue;
65
65
  }
66
66
  const base = result[t0mapper[unique]];
67
67
  execTable(info, objs, base);
68
- });
68
+ }
69
69
  return result;
70
70
  };
@@ -3,7 +3,16 @@ import { SqlString } from '../../../sql/sqlString.js';
3
3
  import { queryToSql } from './queryToSql.js';
4
4
  export const SELECT_ALIAS_SEPARATOR = '__';
5
5
  export const Sql = {
6
- select: (expr) => expr.kind === QueryNodeKind.Field ? Sql.fieldInSelect(expr) : Sql.fn(expr),
6
+ select: (expr) => {
7
+ switch (expr.kind) {
8
+ case QueryNodeKind.Field:
9
+ return Sql.fieldInSelect(expr);
10
+ case QueryNodeKind.Identifier:
11
+ return Sql.identifier(expr);
12
+ case QueryNodeKind.Function:
13
+ return Sql.fn(expr);
14
+ }
15
+ },
7
16
  literal: (literal) => SqlString.escape(literal.value),
8
17
  fieldInCondition: (identifier) => SqlString.escapeId(identifier.table) +
9
18
  '.' +
@@ -20,7 +29,7 @@ export const Sql = {
20
29
  identifier: (ident) => {
21
30
  return SqlString.escapeId(ident.identifier);
22
31
  },
23
- fn: (fn) => `${fn.fnName}(${fn.args.map(Sql.value).join(',')})`,
32
+ fn: (fn) => `${fn.fnName}(${fn.args.map(Sql.value).join(',')})${fn.alias ? ` AS ${fn.alias}` : ''}`,
24
33
  value: (v) => {
25
34
  if (v.kind === QueryNodeKind.Function)
26
35
  return Sql.fn(v);
@@ -5,9 +5,9 @@ export const selectionSetToField = (selections, number) => {
5
5
  tableAlias: 't' + number,
6
6
  };
7
7
  let num = number;
8
- selections.forEach(it => {
8
+ for (const it of selections) {
9
9
  if (it.kind !== 'Field')
10
- return;
10
+ continue;
11
11
  if (it.selectionSet) {
12
12
  num += 1;
13
13
  const field = selectionSetToField(it.selectionSet.selections, num);
@@ -15,9 +15,10 @@ export const selectionSetToField = (selections, number) => {
15
15
  num = field[1];
16
16
  }
17
17
  else {
18
- result.fields.push(it.name.value);
18
+ if (it.name.value !== '__typename')
19
+ result.fields.push(it.name.value);
19
20
  }
20
- });
21
+ }
21
22
  return [result, num];
22
23
  };
23
24
  export const gqlResolveInfoToField = (info) => {
@@ -4,7 +4,7 @@ import { RelationMap, TableInfo } from '../dsl/query/createQueryResolveInfo.js';
4
4
  import { Fields } from '../field.js';
5
5
  import { QueryResolveInfo } from '../dsl/query/sql/hydrate.js';
6
6
  import { QueryOptions } from '../sasatDBDatasource.js';
7
- export declare const createQuery: (baseTableName: string, fields: Fields<unknown>, options: QueryOptions | undefined, tableInfo: TableInfo, relationMap: RelationMap, context?: unknown) => Query;
7
+ export declare const createQuery: (baseTableName: string, fields: Fields<any>, options: QueryOptions | undefined, tableInfo: TableInfo, relationMap: RelationMap, context?: unknown) => Query;
8
8
  export type PagingOption = {
9
9
  numberOfItem: number;
10
10
  where?: BooleanValueExpression;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasat",
3
- "version": "0.19.37",
3
+ "version": "0.19.39",
4
4
  "repository": "https://github.com/nin138/sasat.git",
5
5
  "author": "nin138 <ninian138@gmail.com>",
6
6
  "license": "MIT",