sasat 0.19.35 → 0.19.37

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.
@@ -1,5 +1,5 @@
1
1
  export type QueryResponse = Array<{
2
- [key: string]: string;
2
+ [key: string]: SqlValueType;
3
3
  }>;
4
4
  export interface CommandResponse {
5
5
  insertId: number;
@@ -9,6 +9,7 @@ export type Create = {
9
9
  table: string;
10
10
  values: ValueSet[];
11
11
  upsert?: string[];
12
+ ignore?: boolean;
12
13
  };
13
14
  export type Update = {
14
15
  table: string;
@@ -13,7 +13,7 @@ const onDuplicateKeyUpdate = (columns) => {
13
13
  };
14
14
  export const createToSql = (dsl, tableInfo) => {
15
15
  const map = tableInfo[dsl.table].columnMap;
16
- return `INSERT INTO ${escapeId(dsl.table)}(${dsl.values.map(it => escapeId(map[it.field]))}) VALUES(${dsl.values.map(it => escape(it.value))})${onDuplicateKeyUpdate(dsl.upsert)}`;
16
+ return `INSERT ${dsl.ignore ? 'IGNORE ' : ''}INTO ${escapeId(dsl.table)}(${dsl.values.map(it => escapeId(map[it.field]))}) VALUES(${dsl.values.map(it => escape(it.value))})${onDuplicateKeyUpdate(dsl.upsert)}`;
17
17
  };
18
18
  export const updateToSql = (dsl, tableInfo) => {
19
19
  const map = tableInfo[dsl.table].columnMap;
@@ -15,18 +15,25 @@ export declare enum QueryNodeKind {
15
15
  Sort = 12,
16
16
  Identifier = 13,
17
17
  Exists = 14,
18
- Raw = 15
18
+ Raw = 15,
19
+ GroupBy = 16
19
20
  }
20
21
  export type LockMode = 'FOR UPDATE' | 'FOR SHARE';
21
22
  export type Query = {
22
23
  select: Select;
23
24
  from: QueryTable;
24
25
  where?: BooleanValueExpression;
26
+ groupBy?: GroupByExpr;
27
+ having?: BooleanValueExpression;
25
28
  sort?: Sort[];
26
29
  limit?: number;
27
30
  offset?: number;
28
31
  lock?: LockMode;
29
32
  };
33
+ export type GroupByExpr = {
34
+ kind: QueryNodeKind.GroupBy;
35
+ cols: (Field | Identifier)[];
36
+ };
30
37
  type Select = SelectExpr[];
31
38
  export declare const NO_ALIAS: "__SASAT_NO_ALIAS";
32
39
  export type Field = {
@@ -16,5 +16,6 @@ export var QueryNodeKind;
16
16
  QueryNodeKind[QueryNodeKind["Identifier"] = 13] = "Identifier";
17
17
  QueryNodeKind[QueryNodeKind["Exists"] = 14] = "Exists";
18
18
  QueryNodeKind[QueryNodeKind["Raw"] = 15] = "Raw";
19
+ QueryNodeKind[QueryNodeKind["GroupBy"] = 16] = "GroupBy";
19
20
  })(QueryNodeKind || (QueryNodeKind = {}));
20
21
  export const NO_ALIAS = '__SASAT_NO_ALIAS';
@@ -13,6 +13,10 @@ export const queryToSql = (query) => {
13
13
  const select = query.select.map(Sql.select).join(', ');
14
14
  const join = getJoin(query.from).map(Sql.join).join(' ');
15
15
  const where = query.where ? ' WHERE ' + Sql.booleanValue(query.where) : '';
16
+ const groupBy = query.groupBy
17
+ ? ' GROUP BY' + query.groupBy.cols.map(Sql.value).join(',')
18
+ : '';
19
+ const having = query.having ? 'HAVING ' + Sql.booleanValue(query.having) : '';
16
20
  const sort = query.sort && query.sort.length !== 0
17
21
  ? ' ORDER BY ' + Sql.sorts(query.sort)
18
22
  : '';
@@ -23,6 +27,8 @@ export const queryToSql = (query) => {
23
27
  return (`SELECT ${select} FROM ${Sql.table(query.from)}` +
24
28
  join +
25
29
  where +
30
+ groupBy +
31
+ having +
26
32
  sort +
27
33
  limit +
28
34
  offset +
@@ -40,8 +40,11 @@ export declare abstract class SasatDBDatasource<Entity extends EntityType, Ident
40
40
  protected abstract getDefaultValueString(): Partial<{
41
41
  [P in keyof Entity]: Entity[P] | string | null | never;
42
42
  }> | never;
43
- create(entity: Creatable, upsert?: {
44
- updateColumns: string[];
43
+ create(entity: Creatable, option?: {
44
+ ignore?: boolean;
45
+ upsert?: {
46
+ updateColumns: string[];
47
+ };
45
48
  }): Promise<Entity>;
46
49
  upsert<T extends Creatable & Partial<Entity>>(entity: T, updateFields?: (keyof T)[]): Promise<Entity>;
47
50
  update(entity: Updatable): Promise<CommandResponse>;
@@ -10,7 +10,7 @@ export class SasatDBDatasource {
10
10
  this.queryLogger = noop;
11
11
  this.commandLogger = noop;
12
12
  }
13
- async create(entity, upsert) {
13
+ async create(entity, option) {
14
14
  const obj = {
15
15
  ...this.getDefaultValueString(),
16
16
  ...entity,
@@ -21,7 +21,8 @@ export class SasatDBDatasource {
21
21
  field: column,
22
22
  value,
23
23
  })),
24
- upsert: upsert?.updateColumns,
24
+ upsert: option?.upsert?.updateColumns,
25
+ ignore: option?.ignore,
25
26
  };
26
27
  const sql = createToSql(dsl, this.tableInfo);
27
28
  this.commandLogger(sql);
@@ -35,7 +36,9 @@ export class SasatDBDatasource {
35
36
  }
36
37
  async upsert(entity, updateFields = this.primaryKeys) {
37
38
  return this.create(entity, {
38
- updateColumns: this.fieldToColumn(updateFields),
39
+ upsert: {
40
+ updateColumns: this.fieldToColumn(updateFields),
41
+ },
39
42
  });
40
43
  }
41
44
  update(entity) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasat",
3
- "version": "0.19.35",
3
+ "version": "0.19.37",
4
4
  "repository": "https://github.com/nin138/sasat.git",
5
5
  "author": "nin138 <ninian138@gmail.com>",
6
6
  "license": "MIT",