Back to Mikro Orm

@mikro-orm/libsql

packages/libsql/README.md

7.0.142.1 KB
Original Source

@mikro-orm/libsql

MikroORM driver for libSQL and Turso databases, built on top of libsql.

Installation

sh
npm install @mikro-orm/core @mikro-orm/libsql

Usage

typescript
import { defineEntity, p, MikroORM } from '@mikro-orm/libsql';

const AuthorSchema = defineEntity({
  name: 'Author',
  properties: {
    id: p.integer().primary(),
    name: p.string(),
    books: () => p.oneToMany(Book).mappedBy('author'),
  },
});
export class Author extends AuthorSchema.class {}
AuthorSchema.setClass(Author);

const BookSchema = defineEntity({
  name: 'Book',
  properties: {
    id: p.integer().primary(),
    title: p.string(),
    author: () => p.manyToOne(Author).inversedBy('books'),
  },
});
export class Book extends BookSchema.class {}
BookSchema.setClass(Book);

// Local file database
const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: 'my-db.sqlite3',
});

// Or connect to a remote Turso database
const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: 'libsql://your-db.turso.io',
  password: 'your-auth-token',
});

const author = orm.em.create(Author, { name: 'Jon Snow' });
orm.em.create(Book, { title: 'My Life on The Wall', author });
await orm.em.flush();

const authors = await orm.em.find(
  Author,
  { name: { $like: '%Jon%' } },
  {
    populate: ['books'],
  },
);

Features

Documentation

See the official MikroORM documentation and the SQLite usage guide.

License

Copyright © 2018-present Martin Adámek. Licensed under the MIT License.