react-context-orm - v1.0.0

React Context ORM

React Context ORM is a flexible lightweight ORM (Object-Relational Mapping) library for React applications, enabling easy and efficient data management using models.

To install the library, run:

npm install react-context-orm

Here's a simple example to get you started:

import React from 'react';
import {
  Model,
  StoreProvider,
  useStoreState,
  useDispatch,
} from 'react-context-orm';

class User extends Model {
  static entity = 'users';

  static fields() {
    return {
      id: this.attr(null),
      name: this.attr(''),
    };
  }
}

const App = () => {
  const state = useStoreState();
  const dispatch = useDispatch();

  React.useEffect(() => {
    User.init(state, dispatch);
    User.create({ data: { id: 1, name: 'John Doe' } });
  }, [state, dispatch]);

  return (
    
      
{JSON.stringify(state.users)}
); }; export default App;

Define your models by extending the Model class:

class Post extends Model {
  static entity = 'posts';

  static fields() {
    return {
      id: this.attr(null),
      title: this.attr(''),
      content: this.attr(''),
      userId: this.attr(null),
      user: this.belongsTo(User, 'userId'),
    };
  }
}

Perform CRUD operations using the static methods:

await User.create({ data: { id: 1, name: 'John Doe' } });
const user = User.find(1);
await User.update({ data: { id: 1, name: 'Jane Doe' } });
await User.delete(1);

Define and query relationships:

class Comment extends Model {
  static entity = 'comments';

  static fields() {
    return {
      id: this.attr(null),
      content: this.attr(''),
      postId: this.attr(null),
      post: this.belongsTo(Post, 'postId'),
    };
  }
}

const post = Post.query().with('comments').find(1);

Use the query builder for complex queries:

const posts = Post.query()
  .with(['user', 'comments'])
  .orderBy('title', 'asc')
  .all();

We welcome contributions! Please read our contributing guidelines for more information.

Use belongsTo and hasMany methods to define relationships in your model fields.

Use the create, insert, insertOrUpdate, and delete methods with an array of data.

MIT License