Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/milroyfraser/sarala
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
https://github.com/milroyfraser/sarala
eloquent javascript json-api laravel orm restful-api
Last synced: about 2 months ago
JSON representation
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
- Host: GitHub
- URL: https://github.com/milroyfraser/sarala
- Owner: milroyfraser
- License: mit
- Created: 2018-02-05T15:01:09.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-17T09:41:31.000Z (about 5 years ago)
- Last Synced: 2024-09-18T01:05:03.618Z (3 months ago)
- Topics: eloquent, javascript, json-api, laravel, orm, restful-api
- Language: JavaScript
- Size: 444 KB
- Stars: 110
- Watchers: 15
- Forks: 8
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Sarala JS
> Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
### [API Documentation](https://sarala-io.github.io/sarala-js-docs/guide/) | [Background Story](https://milroy.me/posts/sarala-laravel-eloquent-like-javascript-orm-to-communicate-with-json-api/1)
## Install
```sh
$ npm i sarala --save
``````sh
$ yarn add sarala
```# Basic Usage
## Model Implementation
##### app/models/BaseModel.js
```javascript
import { Model } from 'sarala';
import axios from 'axios';export default class BaseModel extends Model
{
baseUrl () {
return 'https://sarala-demo.app/api';
}request (config) {
return axios.request(config);
}
}
```##### app/models/Post.js
```javascript
import Model from './BaseModel';
import Tag from './Tag';export default class Post extends Model {
resourceName () {
return 'posts';
}fields () {
return ['title', 'subtitle', 'body', 'slug'];
}relationships () {
return {
tags: new Tag()
};
}
}
```##### app/models/Tag.js
```javascript
import Model from './BaseModel';export default class Tag extends Model {
resourceName () {
return 'tags';
}fields () {
return ['name'];
}
}
```## Fetching data
```javascript
import Post from './../models/Post';const post = new Post();
// makes a GET request to https://sarala-demo.app/api/posts
const fetchAllPosts = async () => {
let posts = await post.with(['tags']).all();
};
```## Insert
##### app/components/MyComponent.js
```javascript
import Tag from './../models/Tag';const tag = new Tag();
tag.name = 'json-api';// makes a POST request to https://sarala-demo.app/api/tags
tag.save();
// or you can directly call tag.create();
```## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
### [API Documentation](https://sarala-io.github.io/sarala-js-docs/guide/) | [Background Story](https://milroy.me/posts/sarala-laravel-eloquent-like-javascript-orm-to-communicate-with-json-api/1)