npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sqlite-data-generator

v1.0.1

Published

A CLI for generating SQLite database files with example data

Downloads

10

Readme

sqlite-data-generator

A CLI for generating SQLite database files with example data

Configuration

To change the configuration edit the config/tables.js file. The configuration is an array of objects, each object represents a table in the database. The object has the following properties:

  • name: The name of the table

  • vars: An object with key-value pairs. The value can a value or a function with the signature () -> value. The function is evaluated before the table is generated. The variables can be accessed in a generator function using the variables parameter.

  • rowVars: An object with key-value pairs. The value can a value or a function with the signature () -> value. The function is evaluated for each row generated. The variables can be accessed through variables.row in a generator function using the variables parameter.

  • fields: An array of objects, each object represents a field in the table. The object has the following properties:

    • name: The name of the field. Omit for foreign key constraints.

    • type: The type of the field, options are the sqlite3 types (INTEGER, TEXT, REAL, BLOB, NULL) followed by an optional constraint (NOT NULL, PRIMARY KEY, UNIQUE, CHECK, DEFAULT, COLLATE). The constraint is optional and can be omitted. The type and constraint are separated by a space. You can combine multiple constraints by separating them with a space. For more information please refer to the SQLite documentation.

      Examples:

      • INTEGER NOT NULL
      • TEXT PRIMARY KEY
      • REAL NOT NULL
      • TEXT UNIQUE NOT NULL
      • TEXT DEFAULT 'Hello' CHECK(columnName <> 'World')
      • 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.
    • 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.

  • rows: The number of rows to generate for the table or an object with the following properties:

    • sourceTable: The name of the source table to generate rows from. The source table must be defined before the table that uses it.
    • filter: A string that can be used to filter the rows of the source table. The filter is a SQL WHERE clause without the WHERE keyword.

For an example configuration see the config/tables.js file.

Usage

To generate a database file run the following command:

npm run start

Or use it as an NPM package:

npm install sqlite-data-generator
const db = new SQLiteDataGenerator("example.db");

async function main() {
  try {
    await db.connect();
    await db.generate(tables);
  } catch (error) {
    console.error(error);
  } finally {
    await db.disconnect();
  }
}

main()
  .then(() => console.log("Database generated successfully!"))
  .catch((error) => console.error(error));

API

constructor(filename: string [, sqlite3: sqlite3 package instance, debug: debug instance]): Creates a new instance of the SQLiteDataGenerator class. The filename is the name of the database file to generate.

async connect(): Connects to the database.

async enableForeignKeySupport: Enables foreign keys on the connection.

async disconnect(): Disconnects from the database.

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.

async getRandomRowFromTable(tableName: string [, filter: string = ""]): Returns a random row from the table. The filter parameter is a string that can be used to filter the rows of the table. The filter is a SQL WHERE clause without the WHERE keyword.

async getRowFromTable(tableName: string [, filter: string = "", order: string = ""]): Returns a row from the table. The filter parameter is a string that can be used to filter the rows of the table. The filter is a SQL WHERE clause without the WHERE keyword. The order parameter is a string that can be used to order the rows of the table. The order is a SQL ORDER BY clause without the ORDER BY keyword.

Debugging

To enable debugging set the DEBUG environment variable to sqlite-data-generator

Roadmap

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

Known Issues

  • N/A