verse-db

Create a connection file to export your database connection

Usage no npm install needed!

<script type="module">
  import verseDb from 'https://cdn.skypack.dev/verse-db';
</script>

README

Verse DB

Getting Started

Create a connection file to export your database connection

// connection.ts

import Database, { ExportedDatabaseTypeDefinition } from verse-db;

const db = new Database({
  type: 'mysql',
  name: 'foo',
  host: '',
  port: 3306,
  username: '',
  password: '',
  database: '',
  schema: '' # useful if connecting to postgres database
});

export default await db.connect<ExportedDatabaseTypeDefinition>();
// foo.ts

import db from './connection.ts';

const lead = await db.models.Leads.findOne();
const result = await db.connection.query(sql, params);

Local Development

  • Run npm i

Adding a new database

  • To automatically generate model of your database schema, run the below script with the appropriate values
npx typeorm-model-generator -h <host> -p <port> -d <database> -u <username> -x <password> -e mysql -o <outputDir> --noConfig
  • Add an entry for your database into the ormconfig.json file

  • To generate type definition files for the models, update the script file with the correct values then run it

./generate-entities-list.sh
  • You may need to set permissions before you can run the script. Run the following to set the proper permissions on the script
chmod +x ./generate-entities-list.sh
  • Export the generated type from index.ts
export { DatabaseEntities as <insert-name-here> } from './path/to/type/defintion';

Testing Locally

  • To test changes locally, build the project:
npm run build
  • Then install it as a dependency of your node app
npm i /path/to/local/verse-db