gina-sequelize 0.0.1 → 0.0.2
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 -57
- package/main.ts +58 -0
- package/package.json +3 -3
package/{gina.ts → lib/gina.ts}
RENAMED
|
@@ -1,56 +1,15 @@
|
|
|
1
1
|
import dotenv from 'dotenv';
|
|
2
2
|
dotenv.config();
|
|
3
3
|
|
|
4
|
-
import { DataTypes, Sequelize
|
|
5
|
-
import path from 'path';
|
|
4
|
+
import { DataTypes, Sequelize } from 'sequelize';
|
|
6
5
|
import fs from 'fs/promises';
|
|
7
6
|
import moment from 'moment';
|
|
8
7
|
|
|
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
8
|
function runMigrations() {
|
|
50
|
-
|
|
9
|
+
return;
|
|
51
10
|
}
|
|
52
11
|
|
|
53
|
-
async function createMigration() {
|
|
12
|
+
async function createMigration(sequelize: Sequelize) {
|
|
54
13
|
const models = Object.keys(sequelize.models);
|
|
55
14
|
const queryInterface = sequelize.getQueryInterface();
|
|
56
15
|
const tables = await queryInterface.showAllTables();
|
|
@@ -181,7 +140,7 @@ ${downTables}
|
|
|
181
140
|
};`;
|
|
182
141
|
|
|
183
142
|
|
|
184
|
-
await fs.writeFile(
|
|
143
|
+
await fs.writeFile('./migrations/' + moment().format('YYYYMMDDHHmmss') + '.ts', migration);
|
|
185
144
|
|
|
186
145
|
|
|
187
146
|
}
|
|
@@ -192,15 +151,3 @@ const gina = {
|
|
|
192
151
|
};
|
|
193
152
|
|
|
194
153
|
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.2",
|
|
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",
|