[wip] ActiveRecord in node
yarn add tinyrecord
# npm i tinyrecord --save
# sqlite
yarn add better-sqlite3
# mysql
yarn add mysql2
const { Base } = require('tinyrecord');
// yarn add sqlite3
Base.establishConnection({
adapter: 'sqlite3',
database: ':memory:'
});
// yarn add mysql2
Base.establishConnection({
adapter: 'mysql2',
host: '127.0.0.1',
user: 'root',
password: '',
database: 'tinyrecord'
});
const { Base } = require('tinyrecord');
class Post extends Base {}
Post.tableName = 'posts';
module.exports = Post;
const post = await Post.new({ title: 'hello' });
await post.save();
const post = await Post.create({ title: 'hello' });
await post.update({ title: 'world' });
const post = await Post.find(1);
const post = await Post.findBy({ title: 'hello' });
const post = await Post.findOrCreateBy({ title: 'hello' });
const sql = await Post.order({ id: 'asc' }).toSql();
const posts = await Post.limit(10).records();
const { Migration } = require('tinyrecord');
class CreatePosts extends Migration {
async change() {
await this.createTable('posts', {}, t => {
t.string('title');
t.text('content');
t.timestamps();
});
}
}
tiny db:create
tiny db:drop
tiny db:migrate
tiny db:migrate:reset
tiny migration:create AddTitleToPosts title:string
tiny model:create Post title:string
MIT