gina-sequelize 0.0.1 → 0.0.3
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/{gina.ts → lib/gina.ts} +4 -60
- package/main.ts +58 -0
- package/package.json +3 -3
package/{gina.ts → lib/gina.ts}
RENAMED
|
@@ -1,56 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
dotenv.config();
|
|
3
|
-
|
|
4
|
-
import { DataTypes, Sequelize, Transaction } from 'sequelize';
|
|
5
|
-
import path from 'path';
|
|
1
|
+
import { DataTypes, Sequelize } from 'sequelize';
|
|
6
2
|
import fs from 'fs/promises';
|
|
7
3
|
import moment from 'moment';
|
|
8
4
|
|
|
9
|
-
const modelsDir = path.join(__dirname + `/models`);
|
|
10
|
-
|
|
11
|
-
const POOL_MAX_CONNECTIONS = 50;
|
|
12
|
-
const POOL_IDLE_TIME = 12000;
|
|
13
|
-
const POOL_ACQUIRE_TIME = 30000;
|
|
14
|
-
|
|
15
|
-
const sequelize = new Sequelize({
|
|
16
|
-
host: process.env.DB_HOST,
|
|
17
|
-
database: process.env.DB_NAME,
|
|
18
|
-
username: process.env.DB_USERNAME,
|
|
19
|
-
password: process.env.DB_PASSWORD,
|
|
20
|
-
dialect: 'mysql',
|
|
21
|
-
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED,
|
|
22
|
-
pool: {
|
|
23
|
-
max: POOL_MAX_CONNECTIONS,
|
|
24
|
-
min: 0,
|
|
25
|
-
idle: POOL_IDLE_TIME,
|
|
26
|
-
evict: POOL_IDLE_TIME - 2000,
|
|
27
|
-
acquire: POOL_ACQUIRE_TIME
|
|
28
|
-
},
|
|
29
|
-
logging: console.info
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
async function initializeDb() {
|
|
33
|
-
|
|
34
|
-
const files = (await fs.readdir(modelsDir)).filter(file => !file.includes('.d.ts') && !file.includes('.js.map'));
|
|
35
|
-
const associations: (() => void)[] = [];
|
|
36
|
-
|
|
37
|
-
for (const file of files) {
|
|
38
|
-
const filePath = path.join(modelsDir, file);
|
|
39
|
-
const model = await import(filePath.replace('.js', ''));
|
|
40
|
-
associations.push(model.initializeModel(sequelize));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
for (const association of associations) {
|
|
44
|
-
association();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
5
|
function runMigrations() {
|
|
50
|
-
|
|
6
|
+
return;
|
|
51
7
|
}
|
|
52
8
|
|
|
53
|
-
async function createMigration() {
|
|
9
|
+
async function createMigration(sequelize: Sequelize) {
|
|
54
10
|
const models = Object.keys(sequelize.models);
|
|
55
11
|
const queryInterface = sequelize.getQueryInterface();
|
|
56
12
|
const tables = await queryInterface.showAllTables();
|
|
@@ -181,7 +137,7 @@ ${downTables}
|
|
|
181
137
|
};`;
|
|
182
138
|
|
|
183
139
|
|
|
184
|
-
await fs.writeFile(
|
|
140
|
+
await fs.writeFile('./migrations/' + moment().format('YYYYMMDDHHmmss') + '.ts', migration);
|
|
185
141
|
|
|
186
142
|
|
|
187
143
|
}
|
|
@@ -192,15 +148,3 @@ const gina = {
|
|
|
192
148
|
};
|
|
193
149
|
|
|
194
150
|
export default gina;
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// (async () => {
|
|
198
|
-
// try {
|
|
199
|
-
// await initializeDb();
|
|
200
|
-
// await createMigration();
|
|
201
|
-
// process.exit();
|
|
202
|
-
// } catch (err) {
|
|
203
|
-
// console.error(err);
|
|
204
|
-
// process.exit(1);
|
|
205
|
-
// }
|
|
206
|
-
// })();
|
package/main.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
dotenv.config();
|
|
3
|
+
|
|
4
|
+
import { Sequelize, Transaction } from 'sequelize';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import fs from 'fs/promises';
|
|
7
|
+
import gina from './lib/gina';
|
|
8
|
+
|
|
9
|
+
const modelsDir = path.join(__dirname + `/models`);
|
|
10
|
+
|
|
11
|
+
const POOL_MAX_CONNECTIONS = 50;
|
|
12
|
+
const POOL_IDLE_TIME = 12000;
|
|
13
|
+
const POOL_ACQUIRE_TIME = 30000;
|
|
14
|
+
|
|
15
|
+
const sequelize = new Sequelize({
|
|
16
|
+
host: process.env.DB_HOST,
|
|
17
|
+
database: process.env.DB_NAME,
|
|
18
|
+
username: process.env.DB_USERNAME,
|
|
19
|
+
password: process.env.DB_PASSWORD,
|
|
20
|
+
dialect: 'mysql',
|
|
21
|
+
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED,
|
|
22
|
+
pool: {
|
|
23
|
+
max: POOL_MAX_CONNECTIONS,
|
|
24
|
+
min: 0,
|
|
25
|
+
idle: POOL_IDLE_TIME,
|
|
26
|
+
evict: POOL_IDLE_TIME - 2000,
|
|
27
|
+
acquire: POOL_ACQUIRE_TIME
|
|
28
|
+
},
|
|
29
|
+
logging: console.info
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
async function initializeDb() {
|
|
33
|
+
|
|
34
|
+
const files = (await fs.readdir(modelsDir)).filter(file => !file.includes('.d.ts') && !file.includes('.js.map'));
|
|
35
|
+
const associations: (() => void)[] = [];
|
|
36
|
+
|
|
37
|
+
for (const file of files) {
|
|
38
|
+
const filePath = path.join(modelsDir, file);
|
|
39
|
+
const model = await import(filePath.replace('.js', ''));
|
|
40
|
+
associations.push(model.initializeModel(sequelize));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const association of associations) {
|
|
44
|
+
association();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
(async () => {
|
|
50
|
+
try {
|
|
51
|
+
await initializeDb();
|
|
52
|
+
await gina.createMigration(sequelize);
|
|
53
|
+
process.exit();
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.error(err);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gina-sequelize",
|
|
3
|
-
"main": "gina.ts",
|
|
4
|
-
"version": "0.0.
|
|
3
|
+
"main": "lib/gina.ts",
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "Gina is a tool to auto-generate and run migrations based on Sequelize ORM.",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"dev": "ts-node
|
|
7
|
+
"dev": "ts-node main.ts"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|