Skip to content

Commit 1cb51e9

Browse files
authored
Merge pull request #2 from ponsoc/Update-readme-and-example-configuration
Update readme and example configuration
2 parents 298def4 + 79fd047 commit 1cb51e9

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To change the configuration edit the `config/tables.js` file. The configuration
2121
- `REAL NOT NULL`
2222
- `TEXT UNIQUE NOT NULL`
2323
- `TEXT DEFAULT 'Hello' CHECK(columnName <> 'World')`
24-
- `FOREIGN KEY(columnName) REFERENCES tableName(columnName)`. Use this format to create a foreign key constraint. Omit the `name` property when using this format. Add `FOREIGN KEY` constraints to the end of the table definition. Note that the field itself still needs to be added to the table definition. Make sure to order the tables in the configuration so that the referenced table is created first.
24+
- `FOREIGN KEY(columnName) REFERENCES tableName(columnName)`. Use this format to create a foreign key constraint. Omit the `name` property when using this format. Add `FOREIGN KEY` constraints to the end of the table definition. Note that the field itself (where the foreign key refers to) still needs to be added to the table definition. Make sure to order the tables in the configuration so that the referenced table is created first.
2525

2626
- `generator`: A function with the signature `(variables, row) -> value`. The function is evaluated for each row generated. The variables contains the variables defined in the table configuration. The row contains the row data when generating rows based on the source table.
2727

@@ -87,7 +87,6 @@ To enable debugging set the `DEBUG` environment variable to `sqlite-data-generat
8787

8888
## Roadmap
8989

90-
- Extend the example configuration to include more complex examples
9190
- Add the ability to generate multiple rows of data for a single row in the source table
9291

9392
## Known Issues

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.1",
3+
"version": "1.0.2",
44
"description": "A CLI for generating SQLite database files with example data",
55
"main": "src/app.js",
66
"scripts": {

src/app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const { faker } = require("@faker-js/faker");
22
const SQLiteDataGenerator = require("./lib/SQLiteDataGenerator");
33

4+
const db_name = "example.db"
45
// create a new instance of the SQLiteLib class
5-
const db = new SQLiteDataGenerator("example.db");
6+
const db = new SQLiteDataGenerator();
67
// import the TableConfig and pass any dependencies
78
const tables = require("./config/tables.js")({ faker, db });
89

@@ -11,6 +12,7 @@ async function main() {
1112
await db.connect();
1213
await db.enableForeignKeySupport();
1314
await db.generate(tables);
15+
await db.writeToFile(db_name);
1416
} catch (error) {
1517
throw error;
1618
} finally {
@@ -22,4 +24,4 @@ main()
2224
.then(() => console.log("Database generated successfully!"))
2325
.catch((error) => console.error(error));
2426

25-
module.exports = { SQLiteDataGenerator };
27+
module.exports = { SQLiteDataGenerator };

src/config/tables.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,34 @@ module.exports = function (dependencies) {
22
return [
33
{
44
name: "users",
5+
rowVars: {
6+
name: () => dependencies.faker.person.fullName(),
7+
},
58
fields: [
69
{ name: "id", type: "INTEGER PRIMARY KEY" },
7-
{ name: "name", type: "TEXT", generator: () => dependencies.faker.person.fullName() },
8-
{ name: "email", type: "TEXT", generator: () => dependencies.faker.internet.email() },
10+
{ name: "name", type: "TEXT", generator: (variables) => variables.row.name },
11+
{ name: "email", type: "TEXT", generator: (variables) => variables.row.name.replaceAll(/\s/g, "").toLowerCase() + "@example.com" },
912
{ name: "age", type: "INTEGER", generator: () => dependencies.faker.number.int({ min: 18, max: 90 }) },
1013
],
11-
rows: 10,
14+
rows: 8,
1215
},
1316
{
1417
name: "posts",
18+
rowVars: {
19+
state: () => dependencies.faker.helpers.arrayElement(["draf", "review","published"]),
20+
},
1521
fields: [
1622
{ name: "id", type: "INTEGER PRIMARY KEY" },
1723
{ name: "title", type: "TEXT", generator: () => dependencies.faker.lorem.words(3) },
1824
{ name: "content", type: "TEXT", generator: () => dependencies.faker.lorem.paragraph() },
25+
{ name: "created_at", type: "TEXT", generator: () => dependencies.faker.date.past().toISOString() },
26+
{name: "published_at", type: "TEXT", generator: (variables) => {
27+
if (variables.row.state === "published") {
28+
return dependencies.faker.date.past().toISOString()
29+
}
30+
return ""
31+
},
32+
},
1933
{
2034
name: "author_id",
2135
type: "INTEGER",
@@ -30,5 +44,50 @@ module.exports = function (dependencies) {
3044
],
3145
rows: 20,
3246
},
47+
{
48+
name: "post_versions",
49+
rowVars: {
50+
object_id: async () => {
51+
const row = await dependencies.db.getRandomRowFromTable("posts");
52+
return row.id;
53+
},
54+
},
55+
fields: [
56+
{ name: "id", type: "INTEGER PRIMARY KEY" },
57+
{
58+
name: "object_id",
59+
type: "INTEGER",
60+
generator: async (variables) => variables.row.object_id,
61+
},
62+
{
63+
name: "version",
64+
type: "INTEGER",
65+
generator: async (variables) => {
66+
const row = await dependencies.db.getRowFromTable("post_versions", `object_id = ${variables.row.object_id}`, `version DESC`);
67+
return row ? row.version + 1 : 1;
68+
},
69+
},
70+
{
71+
name: "created_at",
72+
type: "TEXT",
73+
generator: async (variables) => {
74+
const versionRow = await dependencies.db.getRowFromTable(
75+
"post_versions",
76+
`object_id = ${variables.row.object_id}`,
77+
`version DESC`
78+
);
79+
if (versionRow) {
80+
return dependencies.faker.date.between({ from: versionRow.created_at, to: new Date() }).toISOString();
81+
}
82+
const objectRow = await dependencies.db.getRowFromTable("posts", `id = ${variables.row.object_id}`);
83+
return objectRow.created_at;
84+
},
85+
},
86+
{
87+
type: "FOREIGN KEY(object_id) REFERENCES posts(id)",
88+
},
89+
],
90+
rows: 35,
91+
},
3392
];
3493
};

src/lib/SQLiteDataGenerator.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ class SQLiteDataGenerator {
55
* @param {string} database - The path to the SQLite database file.
66
* @param {object} sqlite3 - The SQLite3 module.
77
*/
8-
constructor(database, sqlite3 = require("sqlite3").verbose(), debug = require("debug")("sqlite-data-generator")) {
8+
constructor(fs = require("fs"), sqlite3 = require("sqlite3").verbose(), debug = require("debug")("sqlite-data-generator")) {
9+
this.temp_db_name = "sqlite_data_generator.db"
10+
this.fs = fs
911
this.sqlite3 = sqlite3;
10-
this.db = new sqlite3.Database(database);
12+
this.db = new sqlite3.Database(this.temp_db_name);
1113
this.debug = debug;
1214
}
1315

@@ -47,12 +49,22 @@ class SQLiteDataGenerator {
4749
if (err) {
4850
reject(new Error(`Error closing the database connection: ${err.message}`));
4951
} else {
52+
this.fs.unlinkSync(this.temp_db_name)
5053
resolve();
5154
}
5255
});
5356
});
5457
}
5558

59+
/**
60+
* Writes the temporary database to the final location
61+
* @returns {void}
62+
*/
63+
async writeToFile(db_name) {
64+
this.fs.copyFileSync(this.temp_db_name, db_name)
65+
}
66+
67+
5668
/**
5769
* Generates SQLite data for the given tables.
5870
*
@@ -101,7 +113,7 @@ class SQLiteDataGenerator {
101113
}
102114
});
103115
} else {
104-
resolve(null);
116+
reject(new Error(`Error getting row from table ${tableName}: Table doesn't exists.`));
105117
}
106118
});
107119
}

0 commit comments

Comments
 (0)