@eggjs/dal-runtime 3.33.0 → 3.33.1

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.
@@ -0,0 +1,150 @@
1
+ {% macro newDataLogic(columns, old, new) %}
2
+ let tmp;
3
+ {% for column in table.columns %}
4
+ // empty-line
5
+ tmp = {{ old }}.{{ column.propertyName }};
6
+ if (tmp !== undefined) {
7
+ {{ new }}.${{ column.propertyName }} = tmp;
8
+ }
9
+ {% endfor %}
10
+ {% endmacro %}
11
+
12
+ {% macro findLogic(funcName, sqlName, idx, uniq) %}
13
+ public async {{ funcName }}(
14
+ {% for key in idx.keys %}
15
+ ${{ key.propertyName }}: {{columnMap[key.propertyName].type.type | dbTypeToTSType}}{% if loop.last !== true %},{% endif%}
16
+ {% endfor %}
17
+ ): Promise<{{ clazzName }}{{ '| null' if uniq else '[]' }}> {
18
+ return this.dataSource.{{ 'executeScalar' if uniq else 'execute' }}('{{ sqlName }}', {
19
+ {% for key in idx.keys %}
20
+ ${{ key.propertyName }},
21
+ {% endfor %}
22
+ });
23
+ }
24
+ {% endmacro %}
25
+
26
+ {% macro generatePrimaryType(primary) %}
27
+ {% if (primary.keys | length) === 1 %}
28
+ {{primary.keys[0].propertyName}}: {{ columnMap[primary.keys[0].propertyName].type.type | dbTypeToTSType}}
29
+ {% else %}
30
+ primary: {
31
+ {% for key in primary.keys %}
32
+ {{ key.propertyName }}: {{columnMap[key.propertyName].type.type | dbTypeToTSType}}{% if loop.last !== true %},{% endif%}
33
+ {% endfor %}
34
+ }
35
+ {% endif %}
36
+ {% endmacro %}
37
+
38
+ {% macro generateUpdateValue(primary) %}
39
+ {% if (primary.keys | length) === 1 %}
40
+ const newData: Record<string, any> = {
41
+ primary: {
42
+ {{primary.keys[0].propertyName}},
43
+ },
44
+ };
45
+ {% else %}
46
+ const newData: Record<string, any> = {
47
+ primary,
48
+ };
49
+ {% endif %}
50
+ {% endmacro %}
51
+
52
+ {% macro generateDeleteValue(primary) %}
53
+ {% if (primary.keys | length) === 1 %}
54
+ {
55
+ {{primary.keys[0].propertyName}},
56
+ }
57
+
58
+ {% else %}
59
+ primary
60
+ {% endif %}
61
+ {% endmacro %}
62
+
63
+ {% macro generateInsertType(primary) %}
64
+ Optional<{{clazzName}},
65
+ {% for key in primary.keys %}
66
+ '{{ key.propertyName }}'{% if loop.last !== true %}|{% endif%}
67
+ {% endfor %}
68
+ >
69
+ {% endmacro %}
70
+
71
+ import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
72
+ import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
73
+ import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
74
+ import { {{ clazzName }} } from '{{ tableModelPath }}';
75
+ // empty-line
76
+ type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
77
+ /**
78
+ * 自动生成的 {{ clazzName }}DAO 基类
79
+ * @class Base{{ clazzName }}DAO
80
+ * @classdesc 该文件由 @eggjs/tegg 自动生成,请**不要**修改它!
81
+ */
82
+ /* istanbul ignore next */
83
+ @SingletonProto({
84
+ accessLevel: AccessLevel.PUBLIC,
85
+ })
86
+ export class Base{{ clazzName }}DAO {
87
+ static clazzModel = {{ clazzName }};
88
+
89
+ @Inject({
90
+ name: DataSourceInjectName,
91
+ })
92
+ @DataSourceQualifier('{{moduleName}}.{{ table.dataSourceName }}.{{ clazzName }}')
93
+ protected readonly dataSource: DataSource<{{clazzName}}>;
94
+
95
+ // empty-line
96
+ {# insert: 插入 #}
97
+ public async insert(raw: {{generateInsertType(primaryIndex)}}): Promise<InsertResult> {
98
+ const data: Record<string, any> = {};
99
+
100
+ {{ newDataLogic(columns, 'raw', 'data') }}
101
+
102
+ // empty-line
103
+ return this.dataSource.executeRawScalar('insert', data);
104
+ }
105
+
106
+ // empty-line
107
+ {# update: 更新 #}
108
+ public async update({{generatePrimaryType(primaryIndex)}}, data: Partial<{{ ((clazzName)) }}>): Promise<UpdateResult> {
109
+ // empty-line
110
+ {{ generateUpdateValue(primaryIndex) }}
111
+
112
+ {{ newDataLogic(columns, 'data', 'newData') }}
113
+
114
+ // empty-line
115
+ return this.dataSource.executeRawScalar('update', newData);
116
+ }
117
+
118
+ {% for funcName in [ 'delete', 'del' ] %}
119
+ // empty-line
120
+ {# delete: 删除 #}
121
+ public async {{ funcName }}({{generatePrimaryType(primaryIndex)}}): Promise<DeleteResult> {
122
+ return this.dataSource.executeRawScalar('delete', {{generateDeleteValue(primaryIndex)}});
123
+ }
124
+ {% endfor %}
125
+
126
+ {% for idx in table.indices %}
127
+ // empty-line
128
+ {# 某个索引 #}
129
+ {% set tmpName = ((idx.keys[0].propertyName if (idx.keys | length) === 1 else idx.name) | pascalCase) %}
130
+ {% set findName = 'findBy' + tmpName %}
131
+ {% set findOneName = 'findOneBy' + tmpName %}
132
+ {{ findLogic(findName, findName, idx, false) }}
133
+ // empty-line
134
+ {{ findLogic(findOneName, findOneName, idx, true) }}
135
+ {% endfor %}
136
+
137
+ // empty-line
138
+ {# 某个索引 #}
139
+ {% if primaryIndex %}
140
+ {% set tmpName = ((primaryIndex.keys[0].propertyName if (primaryIndex.keys | length) === 1 else primaryIndex.name) | pascalCase) %}
141
+ {% set findName = 'findBy' + tmpName %}
142
+ {% set findOneName = 'findOneBy' + tmpName %}
143
+ {{ findLogic(findName, findName, primaryIndex, true) }}
144
+ {% if (primaryIndex.keys | length) === 1 %}
145
+ // empty-line
146
+ {{ findLogic('findByPrimary', findName, primaryIndex, true) }}
147
+ {% endif %}
148
+ {% endif %}
149
+ }
150
+ // empty-line
@@ -0,0 +1,17 @@
1
+ import { SingletonProto, AccessLevel } from '@eggjs/tegg';
2
+ import { Base{{ clazzName }}DAO } from './base/Base{{ clazzName }}DAO';
3
+ // empty-line
4
+ /**
5
+ * {{ clazzName }}DAO 类
6
+ {% if user.name %} * @author {{ user.name }} {% if user.email %}<{{ user.email }}>{% endif %}
7
+ {% endif %} * @class {{ clazzName }}DAO
8
+ * @classdesc 在此扩展关于 {{ clazzName }} 数据的一切操作
9
+ * @extends Base{{ clazzName }}DAO
10
+ */
11
+ @SingletonProto({
12
+ accessLevel: AccessLevel.PUBLIC,
13
+ })
14
+ export default class {{ clazzName }}DAO extends Base{{ clazzName }}DAO {
15
+ // empty-line
16
+ }
17
+ // empty-line
@@ -0,0 +1,17 @@
1
+ import { SqlMap } from '@eggjs/tegg/dal';
2
+ // empty-line
3
+ /**
4
+ * Define Custom SQLs
5
+ *
6
+ * import { SqlMap, SqlType } from '@eggjs/tegg/dal';
7
+ *
8
+ * export default {
9
+ * findByName: {
10
+ * type: SqlType.SELECT,
11
+ * sql: 'SELECT {{ allColumns }} from foo where name = {{ name }}'
12
+ * },
13
+ * }
14
+ */
15
+ export default {
16
+ // empty-line
17
+ } as Record<string, SqlMap>;
@@ -0,0 +1,150 @@
1
+ {% macro newDataLogic(columns, old, new) %}
2
+ let tmp;
3
+ {% for column in table.columns %}
4
+ // empty-line
5
+ tmp = {{ old }}.{{ column.propertyName }};
6
+ if (tmp !== undefined) {
7
+ {{ new }}.${{ column.propertyName }} = tmp;
8
+ }
9
+ {% endfor %}
10
+ {% endmacro %}
11
+
12
+ {% macro findLogic(funcName, sqlName, idx, uniq) %}
13
+ public async {{ funcName }}(
14
+ {% for key in idx.keys %}
15
+ ${{ key.propertyName }}: {{columnMap[key.propertyName].type.type | dbTypeToTSType}}{% if loop.last !== true %},{% endif%}
16
+ {% endfor %}
17
+ ): Promise<{{ clazzName }}{{ '| null' if uniq else '[]' }}> {
18
+ return this.dataSource.{{ 'executeScalar' if uniq else 'execute' }}('{{ sqlName }}', {
19
+ {% for key in idx.keys %}
20
+ ${{ key.propertyName }},
21
+ {% endfor %}
22
+ });
23
+ }
24
+ {% endmacro %}
25
+
26
+ {% macro generatePrimaryType(primary) %}
27
+ {% if (primary.keys | length) === 1 %}
28
+ {{primary.keys[0].propertyName}}: {{ columnMap[primary.keys[0].propertyName].type.type | dbTypeToTSType}}
29
+ {% else %}
30
+ primary: {
31
+ {% for key in primary.keys %}
32
+ {{ key.propertyName }}: {{columnMap[key.propertyName].type.type | dbTypeToTSType}}{% if loop.last !== true %},{% endif%}
33
+ {% endfor %}
34
+ }
35
+ {% endif %}
36
+ {% endmacro %}
37
+
38
+ {% macro generateUpdateValue(primary) %}
39
+ {% if (primary.keys | length) === 1 %}
40
+ const newData: Record<string, any> = {
41
+ primary: {
42
+ {{primary.keys[0].propertyName}},
43
+ },
44
+ };
45
+ {% else %}
46
+ const newData: Record<string, any> = {
47
+ primary,
48
+ };
49
+ {% endif %}
50
+ {% endmacro %}
51
+
52
+ {% macro generateDeleteValue(primary) %}
53
+ {% if (primary.keys | length) === 1 %}
54
+ {
55
+ {{primary.keys[0].propertyName}},
56
+ }
57
+
58
+ {% else %}
59
+ primary
60
+ {% endif %}
61
+ {% endmacro %}
62
+
63
+ {% macro generateInsertType(primary) %}
64
+ Optional<{{clazzName}},
65
+ {% for key in primary.keys %}
66
+ '{{ key.propertyName }}'{% if loop.last !== true %}|{% endif%}
67
+ {% endfor %}
68
+ >
69
+ {% endmacro %}
70
+
71
+ import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
72
+ import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
73
+ import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
74
+ import { {{ clazzName }} } from '{{ tableModelPath }}';
75
+ // empty-line
76
+ type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
77
+ /**
78
+ * 自动生成的 {{ clazzName }}DAO 基类
79
+ * @class Base{{ clazzName }}DAO
80
+ * @classdesc 该文件由 @eggjs/tegg 自动生成,请**不要**修改它!
81
+ */
82
+ /* istanbul ignore next */
83
+ @SingletonProto({
84
+ accessLevel: AccessLevel.PUBLIC,
85
+ })
86
+ export class Base{{ clazzName }}DAO {
87
+ static clazzModel = {{ clazzName }};
88
+
89
+ @Inject({
90
+ name: DataSourceInjectName,
91
+ })
92
+ @DataSourceQualifier('{{moduleName}}.{{ table.dataSourceName }}.{{ clazzName }}')
93
+ protected readonly dataSource: DataSource<{{clazzName}}>;
94
+
95
+ // empty-line
96
+ {# insert: 插入 #}
97
+ public async insert(raw: {{generateInsertType(primaryIndex)}}): Promise<InsertResult> {
98
+ const data: Record<string, any> = {};
99
+
100
+ {{ newDataLogic(columns, 'raw', 'data') }}
101
+
102
+ // empty-line
103
+ return this.dataSource.executeRawScalar('insert', data);
104
+ }
105
+
106
+ // empty-line
107
+ {# update: 更新 #}
108
+ public async update({{generatePrimaryType(primaryIndex)}}, data: Partial<{{ ((clazzName)) }}>): Promise<UpdateResult> {
109
+ // empty-line
110
+ {{ generateUpdateValue(primaryIndex) }}
111
+
112
+ {{ newDataLogic(columns, 'data', 'newData') }}
113
+
114
+ // empty-line
115
+ return this.dataSource.executeRawScalar('update', newData);
116
+ }
117
+
118
+ {% for funcName in [ 'delete', 'del' ] %}
119
+ // empty-line
120
+ {# delete: 删除 #}
121
+ public async {{ funcName }}({{generatePrimaryType(primaryIndex)}}): Promise<DeleteResult> {
122
+ return this.dataSource.executeRawScalar('delete', {{generateDeleteValue(primaryIndex)}});
123
+ }
124
+ {% endfor %}
125
+
126
+ {% for idx in table.indices %}
127
+ // empty-line
128
+ {# 某个索引 #}
129
+ {% set tmpName = ((idx.keys[0].propertyName if (idx.keys | length) === 1 else idx.name) | pascalCase) %}
130
+ {% set findName = 'findBy' + tmpName %}
131
+ {% set findOneName = 'findOneBy' + tmpName %}
132
+ {{ findLogic(findName, findName, idx, false) }}
133
+ // empty-line
134
+ {{ findLogic(findOneName, findOneName, idx, true) }}
135
+ {% endfor %}
136
+
137
+ // empty-line
138
+ {# 某个索引 #}
139
+ {% if primaryIndex %}
140
+ {% set tmpName = ((primaryIndex.keys[0].propertyName if (primaryIndex.keys | length) === 1 else primaryIndex.name) | pascalCase) %}
141
+ {% set findName = 'findBy' + tmpName %}
142
+ {% set findOneName = 'findOneBy' + tmpName %}
143
+ {{ findLogic(findName, findName, primaryIndex, true) }}
144
+ {% if (primaryIndex.keys | length) === 1 %}
145
+ // empty-line
146
+ {{ findLogic('findByPrimary', findName, primaryIndex, true) }}
147
+ {% endif %}
148
+ {% endif %}
149
+ }
150
+ // empty-line
@@ -0,0 +1,17 @@
1
+ import { SingletonProto, AccessLevel } from '@eggjs/tegg';
2
+ import { Base{{ clazzName }}DAO } from './base/Base{{ clazzName }}DAO';
3
+ // empty-line
4
+ /**
5
+ * {{ clazzName }}DAO 类
6
+ {% if user.name %} * @author {{ user.name }} {% if user.email %}<{{ user.email }}>{% endif %}
7
+ {% endif %} * @class {{ clazzName }}DAO
8
+ * @classdesc 在此扩展关于 {{ clazzName }} 数据的一切操作
9
+ * @extends Base{{ clazzName }}DAO
10
+ */
11
+ @SingletonProto({
12
+ accessLevel: AccessLevel.PUBLIC,
13
+ })
14
+ export default class {{ clazzName }}DAO extends Base{{ clazzName }}DAO {
15
+ // empty-line
16
+ }
17
+ // empty-line
@@ -0,0 +1,17 @@
1
+ import { SqlMap } from '@eggjs/tegg/dal';
2
+ // empty-line
3
+ /**
4
+ * Define Custom SQLs
5
+ *
6
+ * import { SqlMap, SqlType } from '@eggjs/tegg/dal';
7
+ *
8
+ * export default {
9
+ * findByName: {
10
+ * type: SqlType.SELECT,
11
+ * sql: 'SELECT {{ allColumns }} from foo where name = {{ name }}'
12
+ * },
13
+ * }
14
+ */
15
+ export default {
16
+ // empty-line
17
+ } as Record<string, SqlMap>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/dal-runtime",
3
- "version": "3.33.0",
3
+ "version": "3.33.1",
4
4
  "description": "tegg dal decorator",
5
5
  "keywords": [
6
6
  "egg",
@@ -12,14 +12,16 @@
12
12
  "main": "dist/index.js",
13
13
  "files": [
14
14
  "dist/**/*.js",
15
- "dist/**/*.d.ts"
15
+ "dist/**/*.d.ts",
16
+ "dist/**/*.njk"
16
17
  ],
17
18
  "typings": "dist/index.d.ts",
18
19
  "scripts": {
19
20
  "test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
20
21
  "clean": "tsc -b --clean",
21
22
  "tsc": "npm run clean && tsc -p ./tsconfig.json",
22
- "tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
23
+ "tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json && npm run cp:template",
24
+ "cp:template": "cp -r src/templates dist/src/templates",
23
25
  "prepublishOnly": "npm run tsc:pub"
24
26
  },
25
27
  "author": "killagu <killa123@126.com>",
@@ -37,7 +39,7 @@
37
39
  "node": ">=14.0.0"
38
40
  },
39
41
  "dependencies": {
40
- "@eggjs/dal-decorator": "^3.33.0",
42
+ "@eggjs/dal-decorator": "^3.33.1",
41
43
  "@eggjs/rds": "^1.0.0",
42
44
  "js-beautify": "^1.15.1",
43
45
  "lodash": "^4.17.21",
@@ -49,7 +51,7 @@
49
51
  "access": "public"
50
52
  },
51
53
  "devDependencies": {
52
- "@eggjs/tegg": "^3.33.0",
54
+ "@eggjs/tegg": "^3.33.1",
53
55
  "@types/lodash": "^4.17.0",
54
56
  "@types/mocha": "^10.0.1",
55
57
  "@types/node": "^20.2.4",
@@ -59,5 +61,5 @@
59
61
  "ts-node": "^10.9.1",
60
62
  "typescript": "^5.0.4"
61
63
  },
62
- "gitHead": "8415595364405be14730f6151ded8763bfb12454"
64
+ "gitHead": "0c382d6aeb23cc50bc1f082402901d6ba9f03686"
63
65
  }