Registry / database / mysql-table

mysql-table

JSON →
library2.2.7jsnpmunverified

Utility for creating and manipulating SQL tables using KnexJS schema building. Version 2.2.7 is stable. Automates database/table creation and provides a simplified interface with methods like get, getAll, getBy, create, update, upsert, filter, count, and streaming. Works with any database Knex supports: Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, Amazon Redshift. Differentiator: combines automatic schema initialization with a streaming interface, built on top of Knex but abstracts away connection pooling and table creation.

npm install mysql-table
INSTALL
IMPORT
SIG · MYSQL-TABLE
M
mysql-table
databasejavascriptv2.2.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Init
const { Init } = require('mysql-table')
import { Init } from 'mysql-table'
This package supports CommonJS only. ESM import will fail.
Table
const { Table } = require('mysql-table')
const Table = require('mysql-table').Table
Destructuring is correct; assigning to variable works but is not a mistake.
Utils
const { Utils } = require('mysql-table')
import { Utils } from 'mysql-table'
Utils provides paginate and other helpers; only available via CommonJS.

Initializes a MySQL database with a users table, creates a row, and retrieves it.

const { Init, Table } = require('mysql-table'); const config = { database: 'test', host: '127.0.0.1', port: 3306, user: 'root', password: process.env.DB_PASSWORD ?? '' }; function UserTable(con) { const schema = { table: 'users', fields: function(s) { s.uuid('id').notNullable().primary(); s.string('username').unique(); s.integer('age'); s.index('age'); } }; return Table(con, schema).then(table => table); } Init(config, [UserTable]).then(tables => { const users = tables.users; users.create({ id: '123', username: 'jdoe', age: 30 }) .then(() => users.get('123')) .then(row => console.log(row)); });
Debug
Known issues
breakingThe Init function will create the database if it does not exist, which may cause unexpected permissions issues.
fix
Ensure the database user has CREATE DATABASE privileges, or pre-create the database.
affects: >=0.0.0
gotchaTable creation is skipped if table exists; schema updates must be done explicitly via table.alter(schema) and are not automatic.
fix
Call table.alter(schema) after Init if schema changes are needed.
affects: >=0.0.0
deprecatedThe package only supports CommonJS (require). ESM import will throw an error.
fix
Use require('mysql-table') instead of import.
affects: >=2.0.0
gotchatable.upsert does not check for conflicts besides primary key; duplicate unique keys may cause errors.
fix
Ensure unique constraints are handled via schema or use table.update/create with error handling.
affects: >=0.0.0
gotchaThe 'fields' schema function uses Knex schema builder methods directly; incorrect column types or chaining can cause runtime errors.
fix
Refer to Knex schema documentation for valid column methods and chaining.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'mysql-table'
Package not installed or npm cache issue.
fix
Run 'npm install mysql-table' and ensure node_modules includes it.
TypeError: Table is not a constructor
Using 'import Table from 'mysql-table'' instead of destructured require.
fix
Use 'const { Table } = require('mysql-table')'.
Knex: Error: ER_BAD_DB_ERROR: Unknown database 'test'
Database does not exist and Init cannot create it due to missing privileges.
fix
Create the database manually or grant CREATE DATABASE privilege to the user.
UnhandledPromiseRejectionWarning: Error: ER_DUP_ENTRY: Duplicate entry '...'
table.create inserted a row with an existing primary key or unique constraint.
fix
Use table.upsert to update on conflict, or check existence with table.has before create.
Upgrade
Version history
2.2.7latest on npm
Audit
Dependencies
knexrequiredCore dependency for SQL query building and schema management.
Agent activity
2 hits · last 30 days
node
2
Resources
mysql-table — npm install mysql-table · libregistry