2.0.7 • Published 2 years ago

africa.js v2.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

:earth_africa: África.js

África is a fulfilled ORM and query builder that runs over SQL Server, SQLite, MySQL, MariaDB and PostgreSQL.

CLI

To install the CLI, you need to run on your bash:

$ npm install -g africa.js

Init

The init command, starts the CLI creating a .env file and the folders for migrations and seeds. You can change it after the .env file is created.

$ africa init <database_client> <host> <user> <password> <database_name>

Or, if you already have a .env file, you can just run:

$ africa init

The .env file

Heres a example of how the .env file show be like:

AFRICA_MIGRATIONS='migrations/'
AFRICA_SEEDS='seeds/'

DB_CLIENT='mysql|mariadb|postgresql|sqlite|sqlserver'
DB_HOST='localhost'
DB_USER='root'
DB_PASS=''
DB_NAME='africa'

Create Migration

To create a migration, you need to run:

$ africa create-migration example_migration
'20010911_084600-example_migration' migration file created with success!

Create Seeder

To create a seeder, you can run:

$ africa create-seeder example_seed
'20010911_101010-example_seed' seed file created with success!

Migrate

To run the migrations in the folder, you need to run:

$ africa migrate

Seed

To run the seeders in the folder, you need to run:

$ africa seed

Rollback

To rollback the migrations (in the database), you can run:

$ africa rollback

The API

The API follows the SQL syntax, so we get the same API methods for every database client.

Lets start with the connection over a MySQL client as a example:

import { Africa, MariaDB, MySQL, PostgreSQL, SQLite, SQLServer } from 'africa.js';

const mysql = new MySQL('host', 'user', 'password', 'database');
const mariadb = new MariaDB('host', 'user', 'password', 'database');
const postgre = new PostgreSQL('host', 'user', 'password', 'database');
const sqlserver = new SQLServer('host', 'user', 'password', 'database');
const sqlite = new SQLite('path/to/database_filename');

The Querie Builder

Gets SQL value of the query:

  await mysql
    .create('news', {
      'id': new Africa().int().null(false).primary_key().auto_increment(),
      'name': new Africa().varchar(255).null(false),
      'age': new Africa().varchar(255).null(false),
      'email': new Africa().varchar(255).null(false)
    });

Running queries

Create a new table:

  await mysql
    .create('news', {
      'id': new Africa().int().null(false).primary_key().auto_increment(),
      'name': new Africa().varchar(255).null(false),
      'age': new Africa().varchar(255).null(false),
      'email': new Africa().varchar(255).null(false)
    });

Read table from database:

  await mysql
    .select() // default brings all records
    .from('news');

Insert into table:

  await mysql
    .insert('news',
      {
        name: 'John Doe',
        age: 21,
        email: 'john@doe.com'
      }
    );

Update record in a table:

  await mysql
    .update('news', {
      name: 'Jane Doe'
    })
    .where('name', '=', 'John Doe')
    .where('email', '=', 'john@doe.com');

Delete in table:

  await mysql
    .delete('news')
    .cascade();

Read table from database with clausules:

  await mysql
    .select('id, name')
    .from('news')
    .where('collumn', 'operator', 'value');

Read table from database with order by clausules:

  await mysql
    .select('id, name')
    .from('news')
    .where('collumn', 'operator', 'value')
    .order_by('collumn')
    .desc();

Read table from database with INNER JOIN:

  await mysql
    .select('id, name')
    .from('news')
    .join('authors', {
      author_name: 'author'
    }); // { id: 1, title: 'example', author: 'Author Name' }

Read table from database with LEFT JOIN:

  await mysql
    .select('News.id, News.title, Authors.name')
    .as('id', 'title', 'author')
    .from('news')
    .left_join('authors', {
      'News.author': 'Authors.id'
    }); // { 'id: 1, title: 'example', author: 'Author Name' }

Read table from database with RIGHT JOIN:

  await mysql
    .select('News.id, News.title, News.content')
    .as('id', 'title', 'content')
    .from('news_categories')
    .right_join('news', {
      'NewsCategories.id': 'News.category_id'
    }); // { 'id: 1, title: 'example', author: 'Author Name' }

Read table from database with FULL OUTER JOIN:

  await mysql
    .select() // all collumns by default
    .from('table1')
    .outer_join('table2', {
      'table1.collumn': 'table2.collumn'
    });

RAW SQL:

  await mysql
    .raw('SELECT * FROM news');

COPYRIGHT

MIT License.

Made with :hearts: by John.

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.9.9

2 years ago

1.9.8

2 years ago

1.9.7

2 years ago

1.9.6

2 years ago

1.9.5

2 years ago

1.9.4

2 years ago

1.9.3

2 years ago

1.9.2

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.9-ALPHA

2 years ago

1.8.9-BETA

2 years ago

1.8.9

2 years ago

1.8.8

2 years ago

1.8.7

2 years ago

1.8.6-ALPHA

2 years ago

1.8.6-BETA

2 years ago

1.8.6

2 years ago

1.8.5-ALPHA

2 years ago

1.8.5-BETA

2 years ago

1.8.5

2 years ago

1.8.4

2 years ago

1.8.3

2 years ago

1.8.2

2 years ago

1.8.1-ALPHA

2 years ago

1.8.1-BETA

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.7.9-ALPHA-7

2 years ago

1.7.9-ALPHA-6

2 years ago

1.7.9-ALPHA-5

2 years ago

1.7.9-ALPHA-3

2 years ago

1.7.9-ALPHA-2

2 years ago

1.7.9-ALPHA

2 years ago

1.7.9-BETA

2 years ago

1.7.9

2 years ago

1.7.8-BETA

2 years ago

1.7.7

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

2 years ago

1.6.0

2 years ago

1.5.4

2 years ago

1.5.1

2 years ago