Comprehensive Guide to akira-orm for Seamless Database Management in 2023

Introduction to Akira-ORM

Akira-ORM is a powerful, modern object-relational mapper designed to simplify database interactions for developers. Its rich set of APIs makes CRUD operations, migrations, and relationships effortless. In this guide, we’ll delve into a variety of useful Akira-ORM APIs with practical code snippets to help you master this tool quickly.

Installation

  
    npm install akira-orm
  

Connecting to a Database

  
    const { Akira } = require('akira-orm');

    const akira = new Akira({
      type: 'sqlite',
      database: 'database.sqlite'
    });

    akira.connect();
  

Defining a Model

  
    const { Model } = require('akira-orm');

    class User extends Model {
      static get tableName() {
        return 'users';
      }

      static get idColumn() {
        return 'id';
      }
    }
  

Creating Records

  
    const createUser = async () => {
      const user = await User.query().insert({
        username: 'akiraDev',
        email: 'akira@example.com'
      });
      console.log('User Created:', user);
    };

    createUser();
  

Reading Records

  
    const fetchUsers = async () => {
      const users = await User.query();
      console.log('Users:', users);
    };

    fetchUsers();
  

Updating Records

  
    const updateUser = async (id) => {
      const user = await User.query().patchAndFetchById(id, {
        email: 'new-email@example.com'
      });
      console.log('Updated User:', user);
    };

    updateUser(1);
  

Deleting Records

  
    const deleteUser = async (id) => {
      await User.query().deleteById(id);
      console.log('User Deleted');
    };

    deleteUser(1);
  

Handling Migrations

  
    const { Migration } = require('akira-orm');

    class CreateUsersTable extends Migration {
      async up() {
        await this.schema.createTable('users', (table) => {
          table.increments('id');
          table.string('username');
          table.string('email');
          table.timestamps();
        });
      }

      async down() {
        await this.schema.dropTable('users');
      }
    }

    new CreateUsersTable().run();
  

Relationships

  
    class Post extends Model {
      static get tableName() {
        return 'posts';
      }

      static get relationMappings() {
        return {
          author: {
            relation: Model.BelongsToOneRelation,
            modelClass: User,
            join: {
              from: 'posts.userId',
              to: 'users.id'
            }
          }
        };
      }
    }
  

Full Example Application

  
    const { Akira, Model } = require('akira-orm');

    const akira = new Akira({
      type: 'sqlite',
      database: 'app.sqlite'
    });

    akira.connect();

    class User extends Model {
      static get tableName() {
        return 'users';
      }

      static get idColumn() {
        return 'id';
      }
    }

    class Post extends Model {
      static get tableName() {
        return 'posts';
      }

      static get relationMappings() {
        return {
          author: {
            relation: Model.BelongsToOneRelation,
            modelClass: User,
            join: {
              from: 'posts.userId',
              to: 'users.id'
            }
          }
        };
      }
    }

    const runApp = async () => {
      await akira.schema.createTable('users', (table) => {
        table.increments('id');
        table.string('username');
        table.string('email');
        table.timestamps();
      });

      await akira.schema.createTable('posts', (table) => {
        table.increments('id');
        table.string('title');
        table.text('content');
        table.integer('userId').references('id').inTable('users');
        table.timestamps();
      });

      const user = await User.query().insert({
        username: 'johnDoe',
        email: 'john@example.com'
      });

      const post = await Post.query().insert({
        title: 'Awesome Post',
        content: 'This is the content of the awesome post.',
        userId: user.id
      });

      console.log('User:', user);
      console.log('Post:', post);
    };

    runApp();
  

Start using Akira-ORM today, and simplify your database management with its elegant and powerful APIs.

Hash: 4bd95719e0718539120465a9f17f1c5848ba323568a680012992e90090e88381

Leave a Reply

Your email address will not be published. Required fields are marked *