23
loading...
This website collects cookies to deliver better user experience
Could be there a new approach to create APIs quicker?
class User {
get fillable() {
return ["email", "name"];
}
get validations() {
return {
email: "required|email",
name: "required",
};
}
}
import { Model } from "axe-api";
class User extends Model {
}
export default User;
class User extends Model {
get fillable() {
return {
POST: ["email", "name"],
PUT: ["name"],
};
}
}
class User extends Model {
get fillable() {
return {
POST: ["email", "name"],
PUT: ["name"],
};
}
get validations() {
return {
email: "required|email",
name: "required|max:50",
};
}
}
class User extends Model {
posts() {
return this.hasMany("Post", "id", "user_id");
}
}
class Post extends Model {
user() {
return this.belongsTo("User", "user_id", "id");
}
}
GET api/users
POST api/users
GET api/users/:id
PUT api/users/:id
DELETE api/users/:id
GET api/users/:usedId/posts
POST api/users/:usedId/posts
GET api/users/:usedId/posts/:id
PUT api/users/:usedId/posts/:id
DELETE api/users/:usedId/posts/:id
UserHooks.js
file for the model like this;import bcrypt from "bcrypt";
const onBeforeInsert = async ({ formData }) => {
// Genering salt
formData.salt = bcrypt.genSaltSync(10);
// Hashing the password
formData.password = bcrypt.hashSync(formData.password, salt);
};
export { onBeforeInsert };
GET /api/users
?q=[[{"name": "John"}],[{"$or.age.$gt": 18}, {"$and.id": 666 }]]
&fields:id,name,surname
&sort=surname,-name
&with=posts{comments{id|content}}
&page=2
&per_page=25
name
is "John" OR age
is greater than 18 and the id
is 666.id
, name
, and surname
fields.surname
first (ASC), name
second (DESC).posts
data, with the related comments
data. But in comments
object, just fetch id
and content
fields.Thanks for the cover image