vona-module-a-ormdialect 5.0.13

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016-present Vona
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,57 @@
1
+ /** databaseDialect: begin */
2
+ export * from '../bean/databaseDialect.mysql.ts';
3
+ export * from '../bean/databaseDialect.mysql3.ts';
4
+ export * from '../bean/databaseDialect.pg.ts';
5
+ import 'vona';
6
+ declare module 'vona' {
7
+ interface IDatabaseDialectRecord {
8
+ 'a-ormdialect:mysql': never;
9
+ 'a-ormdialect:mysql3': never;
10
+ 'a-ormdialect:pg': never;
11
+ }
12
+ }
13
+ declare module 'vona-module-a-ormdialect' {
14
+ interface DatabaseDialectMysql {
15
+ }
16
+ interface DatabaseDialectMysql {
17
+ get $beanFullName(): 'a-ormdialect.databaseDialect.mysql';
18
+ get $onionName(): 'a-ormdialect:mysql';
19
+ }
20
+ interface DatabaseDialectPg {
21
+ }
22
+ interface DatabaseDialectPg {
23
+ get $beanFullName(): 'a-ormdialect.databaseDialect.pg';
24
+ get $onionName(): 'a-ormdialect:pg';
25
+ }
26
+ }
27
+ /** databaseDialect: end */
28
+ /** databaseDialect: begin */
29
+ import type { DatabaseDialectMysql } from '../bean/databaseDialect.mysql.ts';
30
+ import type { DatabaseDialectMysql3 } from '../bean/databaseDialect.mysql3.ts';
31
+ import type { DatabaseDialectPg } from '../bean/databaseDialect.pg.ts';
32
+ import 'vona';
33
+ declare module 'vona' {
34
+ interface IBeanRecordGeneral {
35
+ 'a-ormdialect.databaseDialect.mysql': DatabaseDialectMysql;
36
+ 'a-ormdialect.databaseDialect.mysql3': DatabaseDialectMysql3;
37
+ 'a-ormdialect.databaseDialect.pg': DatabaseDialectPg;
38
+ }
39
+ }
40
+ /** databaseDialect: end */
41
+ /** scope: begin */
42
+ import { BeanScopeBase, type BeanScopeUtil } from 'vona';
43
+ export declare class ScopeModuleAOrmdialect extends BeanScopeBase {
44
+ }
45
+ export interface ScopeModuleAOrmdialect {
46
+ util: BeanScopeUtil;
47
+ }
48
+ import 'vona';
49
+ declare module 'vona' {
50
+ interface IBeanScopeRecord {
51
+ 'a-ormdialect': ScopeModuleAOrmdialect;
52
+ }
53
+ interface IBeanScopeContainer {
54
+ ormdialect: ScopeModuleAOrmdialect;
55
+ }
56
+ }
57
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-ormdialect";
2
+ export { ScopeModuleAOrmdialect as ScopeModule } from './index.ts';
@@ -0,0 +1,13 @@
1
+ import type { Knex } from 'knex';
2
+ import type { ConfigDatabaseClient, IFetchDatabasesResultItem, IFetchIndexesResultItem, TableIdentity } from 'vona-module-a-orm';
3
+ import { BeanDatabaseDialectBase } from 'vona-module-a-orm';
4
+ export declare class DatabaseDialectMysql extends BeanDatabaseDialectBase {
5
+ getConfigBase(): ConfigDatabaseClient | undefined;
6
+ fetchDatabases(schemaBuilder: Knex.SchemaBuilder, databasePrefix: string): Promise<IFetchDatabasesResultItem[]>;
7
+ createDatabase(schemaBuilder: Knex.SchemaBuilder, databaseName: string): Promise<void>;
8
+ dropDatabase(schemaBuilder: Knex.SchemaBuilder, databaseName: string): Promise<void>;
9
+ fetchIndexes(schemaBuilder: Knex.SchemaBuilder, tableName: string): Promise<IFetchIndexesResultItem[]>;
10
+ insert(builder: Knex.QueryBuilder): Promise<TableIdentity[]>;
11
+ query(result: any): any;
12
+ viewDependents(builder: Knex.QueryBuilder, viewName: string): Promise<string[]>;
13
+ }
@@ -0,0 +1,5 @@
1
+ import type { ConfigDatabaseClient } from 'vona-module-a-orm';
2
+ import { DatabaseDialectMysql } from './databaseDialect.mysql.ts';
3
+ export declare class DatabaseDialectMysql3 extends DatabaseDialectMysql {
4
+ getConfigBase(): ConfigDatabaseClient | undefined;
5
+ }
@@ -0,0 +1,13 @@
1
+ import type { Knex } from 'knex';
2
+ import type { ConfigDatabaseClient, IFetchDatabasesResultItem, IFetchIndexesResultItem, TableIdentity } from 'vona-module-a-orm';
3
+ import { BeanDatabaseDialectBase } from 'vona-module-a-orm';
4
+ export declare class DatabaseDialectPg extends BeanDatabaseDialectBase {
5
+ getConfigBase(): ConfigDatabaseClient | undefined;
6
+ fetchDatabases(schemaBuilder: Knex.SchemaBuilder, databasePrefix: string): Promise<IFetchDatabasesResultItem[]>;
7
+ createDatabase(schemaBuilder: Knex.SchemaBuilder, databaseName: string): Promise<void>;
8
+ dropDatabase(schemaBuilder: Knex.SchemaBuilder, databaseName: string): Promise<void>;
9
+ fetchIndexes(schemaBuilder: Knex.SchemaBuilder, tableName: string): Promise<IFetchIndexesResultItem[]>;
10
+ insert(builder: Knex.QueryBuilder): Promise<TableIdentity[]>;
11
+ query(result: any): any;
12
+ viewDependents(builder: Knex.QueryBuilder, viewName: string): Promise<string[]>;
13
+ }
@@ -0,0 +1 @@
1
+ export * from './.metadata/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,183 @@
1
+ import { BeanInfo, Virtual, BeanScopeBase } from 'vona';
2
+ import { promisify } from 'node:util';
3
+ import { DatabaseDialect, BeanDatabaseDialectBase } from 'vona-module-a-orm';
4
+ import { Scope } from 'vona-module-a-bean';
5
+
6
+ var _dec$3, _dec2$3, _class$3;
7
+ let DatabaseDialectMysql = (_dec$3 = DatabaseDialect(), _dec2$3 = BeanInfo({
8
+ module: "a-ormdialect"
9
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class DatabaseDialectMysql extends BeanDatabaseDialectBase {
10
+ getConfigBase() {
11
+ return {
12
+ pool: {
13
+ afterCreate(conn, done) {
14
+ mysql_afterCreate(conn).then(done).catch(done);
15
+ }
16
+ }
17
+ };
18
+ }
19
+ async fetchDatabases(schemaBuilder, databasePrefix) {
20
+ const res = await schemaBuilder.raw(`show databases like '${databasePrefix}%'`);
21
+ let dbs = res[0];
22
+ dbs = dbs.map(db => {
23
+ const name = db[Object.keys(db)[0]];
24
+ return {
25
+ name
26
+ };
27
+ });
28
+ return dbs;
29
+ }
30
+ async createDatabase(schemaBuilder, databaseName) {
31
+ await schemaBuilder.raw(`CREATE DATABASE \`${databaseName}\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci`);
32
+ }
33
+ async dropDatabase(schemaBuilder, databaseName) {
34
+ await schemaBuilder.raw(`drop database \`${databaseName}\``);
35
+ }
36
+ async fetchIndexes(schemaBuilder, tableName) {
37
+ const res = await schemaBuilder.raw(`show index from ${tableName}`);
38
+ let items = res[0];
39
+ items = items.map(item => {
40
+ return {
41
+ indexName: item.Key_name
42
+ };
43
+ });
44
+ return items;
45
+ }
46
+ async insert(builder) {
47
+ const items = await builder;
48
+ return items;
49
+ }
50
+ query(result) {
51
+ return result[0];
52
+ }
53
+ async viewDependents(builder, viewName) {
54
+ const sqlViews = `
55
+ SELECT T.TABLE_SCHEMA ref_schema,
56
+ T.TABLE_NAME ref_name,
57
+ V.TABLE_NAME dep_name,
58
+ T.TABLE_TYPE type
59
+ FROM INFORMATION_SCHEMA.TABLES T
60
+ INNER JOIN INFORMATION_SCHEMA.VIEWS V
61
+ ON V.TABLE_SCHEMA = T.TABLE_SCHEMA
62
+ AND V.VIEW_DEFINITION LIKE CONCAT('%\`',T.TABLE_NAME,'\`%')
63
+ WHERE T.TABLE_SCHEMA = DATABASE()
64
+ `;
65
+ const items = await builder.distinct('dep_name').fromRaw(`(${sqlViews}) as viewDependents`).where({
66
+ ref_name: viewName
67
+ });
68
+ return items.map(item => item.dep_name);
69
+ }
70
+ }) || _class$3) || _class$3);
71
+ async function mysql_afterCreate(conn) {
72
+ await _executeQuery(conn, 'SET SESSION explicit_defaults_for_timestamp=ON');
73
+ await _executeQuery(conn, "SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO'");
74
+ }
75
+ async function _executeQuery(conn, sql) {
76
+ const queryAsync = promisify(cb => conn.query(sql, cb));
77
+ return await queryAsync();
78
+ }
79
+
80
+ var _dec$2, _dec2$2, _dec3, _class$2;
81
+ let DatabaseDialectMysql3 = (_dec$2 = DatabaseDialect(), _dec2$2 = Virtual(), _dec3 = BeanInfo({
82
+ module: "a-ormdialect"
83
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = _dec3(_class$2 = class DatabaseDialectMysql3 extends DatabaseDialectMysql {
84
+ getConfigBase() {
85
+ return super.getConfigBase();
86
+ // const configBase = super.getConfigBase();
87
+ // return Object.assign({}, configBase, {
88
+ // connection: {
89
+ // // typeCast(field, next) {
90
+ // // if (field.type === 'JSON') {
91
+ // // return field.string('utf-8'); // utf8 https://github.com/sidorares/node-mysql2/pull/1662
92
+ // // }
93
+ // // return next();
94
+ // // },
95
+ // },
96
+ // });
97
+ }
98
+ }) || _class$2) || _class$2) || _class$2);
99
+
100
+ var _dec$1, _dec2$1, _class$1;
101
+ let DatabaseDialectPg = (_dec$1 = DatabaseDialect(), _dec2$1 = BeanInfo({
102
+ module: "a-ormdialect"
103
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class DatabaseDialectPg extends BeanDatabaseDialectBase {
104
+ getConfigBase() {
105
+ return {
106
+ connection: {
107
+ // types: {
108
+ // getTypeParser: (oid: number, format: string): any => {
109
+ // if (oid === 114) return pgTypes.getTypeParser(25, 'text');
110
+ // return pgTypes.getTypeParser(oid, cast(format));
111
+ // },
112
+ // },
113
+ }
114
+ };
115
+ }
116
+ async fetchDatabases(schemaBuilder, databasePrefix) {
117
+ const res = await schemaBuilder.raw(`select datname from pg_database where datname like '${databasePrefix}%'`);
118
+ let dbs = res.rows;
119
+ dbs = dbs.map(db => {
120
+ return {
121
+ name: db.datname
122
+ };
123
+ });
124
+ return dbs;
125
+ }
126
+ async createDatabase(schemaBuilder, databaseName) {
127
+ await schemaBuilder.raw(`CREATE DATABASE "${databaseName}" encoding=UTF8`);
128
+ }
129
+ async dropDatabase(schemaBuilder, databaseName) {
130
+ await schemaBuilder.raw(`DROP DATABASE "${databaseName}"`);
131
+ }
132
+ async fetchIndexes(schemaBuilder, tableName) {
133
+ const res = await schemaBuilder.raw(`select indexname,indexdef from pg_indexes where tablename='${tableName}'`);
134
+ let items = res.rows;
135
+ items = items.map(item => {
136
+ return {
137
+ indexName: item.indexname
138
+ };
139
+ });
140
+ return items;
141
+ }
142
+ async insert(builder) {
143
+ builder.returning('id');
144
+ const items = await builder;
145
+ return items.map(item => item.id);
146
+ }
147
+ query(result) {
148
+ return result.rows;
149
+ }
150
+ async viewDependents(builder, viewName) {
151
+ const sqlViews = `
152
+ select
153
+ ref_nsp.nspname ref_schema, ref_cl.relname ref_name,
154
+ rwr_cl.relkind dep_type,
155
+ rwr_nsp.nspname dep_schema,
156
+ rwr_cl.relname dep_name
157
+ from pg_depend dep
158
+ join pg_class ref_cl on dep.refobjid = ref_cl.oid
159
+ join pg_namespace ref_nsp on ref_cl.relnamespace = ref_nsp.oid
160
+ join pg_rewrite rwr on dep.objid = rwr.oid
161
+ join pg_class rwr_cl on rwr.ev_class = rwr_cl.oid
162
+ join pg_namespace rwr_nsp on rwr_cl.relnamespace = rwr_nsp.oid
163
+ where
164
+ dep.deptype = 'n'
165
+ and dep.classid = 'pg_rewrite'::regclass
166
+ `;
167
+ let items = await builder.distinct('dep_name').fromRaw(`(${sqlViews}) as depend_view`).where({
168
+ ref_name: viewName
169
+ });
170
+ items = items.map(item => item.dep_name);
171
+ items = items.filter(item => item.toLowerCase() !== viewName.toLowerCase());
172
+ return items;
173
+ }
174
+ }) || _class$1) || _class$1);
175
+
176
+ var _dec, _dec2, _class;
177
+ let ScopeModuleAOrmdialect = (_dec = Scope(), _dec2 = BeanInfo({
178
+ module: "a-ormdialect"
179
+ }), _dec(_class = _dec2(_class = class ScopeModuleAOrmdialect extends BeanScopeBase {}) || _class) || _class);
180
+
181
+ /** scope: end */
182
+
183
+ export { DatabaseDialectMysql, DatabaseDialectMysql3, DatabaseDialectPg, ScopeModuleAOrmdialect };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "vona-module-a-ormdialect",
3
+ "type": "module",
4
+ "version": "5.0.13",
5
+ "title": "a-ormdialect",
6
+ "vonaModule": {
7
+ "dependencies": {}
8
+ },
9
+ "description": "",
10
+ "author": "",
11
+ "keywords": [
12
+ "Vona Module"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": [
17
+ "./src/index.ts",
18
+ "./dist/index.d.ts"
19
+ ],
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "static"
27
+ ],
28
+ "dependencies": {
29
+ "mysql2": "^3.12.0",
30
+ "pg": "^8.13.3",
31
+ "pg-types": "^4.0.2"
32
+ },
33
+ "devDependencies": {
34
+ "clean-package": "^2.2.0",
35
+ "rimraf": "^6.0.1"
36
+ },
37
+ "scripts": {
38
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
39
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
40
+ }
41
+ }