Data & Services

Database

Use SQLite or MySQL through the database manager, query builder, models, and migrations.

Beta · v0.2.0Lanjit APIs may change before the stable release. Review the release notes and upgrade guide before updating.

The db container binding provides connections, queries, and transactions. The query builder supports filtering, ordering, limits, reads, inserts, updates, and deletes. Active-record models provide all, find, create, and save; schema migrations support common column types and modifiers.

Examples

DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lanjit
DB_USERNAME=root
DB_PASSWORD=
const posts = await app.make('db')
  .table('posts')
  .where('published', true)
  .orderBy('created_at', 'desc')
  .limit(20)
  .get();
const id = await app.make('db').table('posts').insert({
  title: 'Halo Lanjit',
  published: true,
});

await app.make('db').table('posts').where('id', id).update({ title: 'Judul baru' });
import { Model } from '../../framework/Database/Model.js';

export class Post extends Model {
  static table = 'posts';
}
const post = new Post({ title: 'Dokumentasi Lanjit' });
await post.save();

const existing = await Post.find(1);
const recent = await Post.query().orderBy('created_at', 'desc').get();
node artisan make:migration create_posts_table
export async function up(schema) {
  await schema.create('posts', (table) => {
    table.id();
    table.string('title');
    table.text('body').nullable();
    table.boolean('published');
    table.timestamps();
  });
}

export async function down(schema) {
  await schema.dropIfExists('posts');
}
node artisan migrate
node artisan migrate:status
node artisan migrate:rollback
node artisan migrate:fresh --seed
await app.make('db').transaction(async (connection) => {
  await connection.statement('UPDATE "accounts" SET "balance" = ? WHERE "id" = ?', [900, 1]);
  await connection.statement('UPDATE "accounts" SET "balance" = ? WHERE "id" = ?', [1100, 2]);
});
Edit this page on GitHub

Type to search all documentation.