1.0.9 • Published 1 year ago

api_express_sql v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

API EXPRESS SQL (AES)

AES Adalah sebuah framework yang dibuat guna mempermudah developer pemula yang ingin membuat rest api menggunakan express js dan database mysql packages ini menggabungkan express dan sequelize untuk dapat digunakan dengan lebih mudah.

Authors

INSTALLATION

npm install api_express_sql --save

selanjutnya

const AES = require('api_express_sql');

const app = new AES({
    <!-- database configuration -->
    database: {
    username: "db_username",
    password: "db_password",
    database: "db_name",
    host: "localhost",
    dialect: "mysql",
  },
})

MEMBUAT TABEL

selanjutnya membuat tabel baru di database dengan nama products

const Product = app.db.define("products", {
  id: {
    type: app.datatypes.STRING,
    primaryKey: true,
    autoIncrement: true
  },
  nama: app.datatypes.STRING,
  harga: app.datatypes.INTEGER,
  stok: app.datatypes.INTEGER,
});

// sync model ke database
Product.sync();

MEMBUAT ROUTE

<!-- Gunakan http method (get, post, delete, put, patch) -->

<!-- route untuk menampilkan semua produk -->
app.addRoute("get", "/products", async (req, res, next) => {

  <!-- Mendapatkan semua data di database products -->
  const products = app.findAll(Product);

  <!-- Mengirim respon data dengan format json dan status code 200 (OK) -->
  res.status(200).json({
    products,
  });
});

JALANKAN APLIKASI

- penulisan
app.start(port);

app.start(8000);

CRUD

<!-- CREATE DATA -->

- penulisan fungsi
app.createData(Tabel, data, options);

<!-- id auto increment, set true atau false di membuat tabel  -->
app.createData(Product, {
    nama: "Stock",
    harga: 2500,
    stok: 100,
  })
  .then((res) => console.log(res));

<!-- READ DATA -->

- penulisan fungsi
app.findAll(Tabel, options);

app.findAll(Product)
  .then((res) => console.log(res));

app.findOne(Product, {
    where: {
      id: 1,
    },
  })
  .then((res) => console.log(res));

<!-- UPDATE DATA -->

- penulisan fungsi
app.updateData(Tabel, data, options);

app.updateData(
    Product,
    {
      nama: "Stock",
      harga: 2500,
      stok: 100,
    },
    {
      where: {
        id: 1,
      },
    }
  )
  .then((res) => console.log(res));

<!-- DELETE DATA -->
- penulisan fungsi
app.deleteData(Tabel, options);

- hilangkan options untuk menghapus seluruh data dari tabel product

app
  .deleteData(Product, {
    where: {
      id: 1,
    },
  })
  .then((res) => console.log(res));