Skip to content

Commit 298def4

Browse files
authored
Merge pull request #1 from ponsoc/introducing-some-improvements
Introducing some improvements
2 parents 4557d2f + 4f842ba commit 298def4

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ main()
7171

7272
`async connect()`: Connects to the database.
7373

74+
`async enableForeignKeySupport`: Enables foreign keys on the connection.
75+
7476
`async disconnect()`: Disconnects from the database.
7577

7678
`async generate(tables: Array)`: Generates the database file based on the configuration. The tables parameter is an array of objects, each object represents a table in the database. The object has properties as described in the configuration section.
@@ -85,10 +87,9 @@ To enable debugging set the `DEBUG` environment variable to `sqlite-data-generat
8587

8688
## Roadmap
8789

88-
- Fix foreign key constraints issue
8990
- Extend the example configuration to include more complex examples
9091
- Add the ability to generate multiple rows of data for a single row in the source table
9192

9293
## Known Issues
9394

94-
- Foreign key constraints can be configured however they are not properly set up in the database.
95+
- N/A

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlite-data-generator",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A CLI for generating SQLite database files with example data",
55
"main": "src/app.js",
66
"scripts": {

src/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const tables = require("./config/tables.js")({ faker, db });
99
async function main() {
1010
try {
1111
await db.connect();
12+
await db.enableForeignKeySupport();
1213
await db.generate(tables);
1314
} catch (error) {
1415
throw error;

src/config/tables.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module.exports = function (dependencies) {
44
name: "users",
55
fields: [
66
{ name: "id", type: "INTEGER PRIMARY KEY" },
7-
{ name: "name", type: "TEXT", generator: () => dependencies.faker.name.findName() },
7+
{ name: "name", type: "TEXT", generator: () => dependencies.faker.person.fullName() },
88
{ name: "email", type: "TEXT", generator: () => dependencies.faker.internet.email() },
99
{ name: "age", type: "INTEGER", generator: () => dependencies.faker.number.int({ min: 18, max: 90 }) },
1010
],
11-
numRows: 10,
11+
rows: 10,
1212
},
1313
{
1414
name: "posts",
@@ -19,13 +19,16 @@ module.exports = function (dependencies) {
1919
{
2020
name: "author_id",
2121
type: "INTEGER",
22-
generator: async () => await dependencies.db.getRandomIdFromTable("users"),
22+
generator: async () => {
23+
const row = await dependencies.db.getRandomRowFromTable("users")
24+
return row.id;
25+
}
2326
},
2427
{
2528
type: "FOREIGN KEY(author_id) REFERENCES users(id)",
2629
},
2730
],
28-
numRows: 20,
31+
rows: 20,
2932
},
3033
];
3134
};

src/lib/SQLiteDataGenerator.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ class SQLiteDataGenerator {
2323
});
2424
}
2525

26+
27+
async enableForeignKeySupport() {
28+
return await new Promise((resolve, reject) => {
29+
this.db.run('PRAGMA foreign_keys = ON', (err) => {
30+
if (err) {
31+
reject(new Error(`Error enabling foreign key support: ${err.message}`));
32+
} else {
33+
resolve();
34+
}
35+
});
36+
})
37+
}
38+
39+
2640
/**
2741
* Disconnects from the database.
2842
* @returns {Promise<void>} A promise that resolves when the disconnection is successful.
2943
*/
3044
async disconnect() {
31-
return await new Promise((resolve) => {
45+
return await new Promise((resolve, reject) => {
3246
this.db.close((err) => {
3347
if (err) {
3448
reject(new Error(`Error closing the database connection: ${err.message}`));
@@ -46,6 +60,7 @@ class SQLiteDataGenerator {
4660
* @returns {Promise} - A promise that resolves when the data generation is complete.
4761
*/
4862
async generate(tables) {
63+
this.debug('Start generating...')
4964
for (const table of tables) {
5065
await this.#createTable(table);
5166
await this.#insertExampleData(table);
@@ -111,9 +126,14 @@ class SQLiteDataGenerator {
111126
* @returns {Promise<void>} - A promise that resolves when the example data is inserted.
112127
*/
113128
async #insertExampleData(table) {
129+
this.debug(`Generating data for table '${table.name}'`);
114130
const variables = await this.#resolveVariables(table.vars);
115131
const fieldsWithName = table.fields.filter((field) => field.name);
116132

133+
this.debug('table', table);
134+
this.debug('variables', variables);
135+
this.debug('fiedsWithName', fieldsWithName);
136+
117137
if (typeof table.rows === "object") {
118138
await this.#eachRow(table.rows.sourceTable, table.name, fieldsWithName, variables, table.rows.filter);
119139
} else {

0 commit comments

Comments
 (0)