The @mincloudx/io
package simplifies CRUD (Create, Read, Update, Delete) operations on database tables, offering a streamlined interface for table management with minimal setup.
Install @mincloudx/io
in your project using npm or pnpm:
npm install @mincloudx/io
# or
pnpm add @mincloudx/io
Begin by importing createIo
and specifying your tables in the tables
option to register them. This setup enables CRUD operations on the registered tables.
const { createIo } = require('@mincloudx/io');
// Registering tables
const tables = ['channel', 'product'];
const io = createIo({ tables });
Access a registered table by appending its name to io
, such as io.channel
or io.product
.
In batch lookup, update, delete, and other operations, the Query object is needed. It is used to describe the characteristics of the data rows that need to be matched.
We can generate and obtain an instance of a Query
object by reading io.query
.
const query = io.product.query;
query.compare('name', '=', 'book');
In most cases of query requests, many scenarios involve comparing values to see if they match. io.getQuery
supports a more convenient way to generate a Query instance. The example code below is equivalent to the one above:
const query = io.getQuery({ name: 'book' });
create a record:
const product = await io.product.create({ name: 'New product' });
create many record:
const dataList = [
{ name: 'Product 1', value: 100 },
{ name: 'Product 2', value: 200 },
{ name: 'Product 3', value: 300 },
];
const { operation_result } = await io.product.createMany(dataList);
console.log('result: ', operation_result);
// or
const result = await io.product.createMany(dataList, { plain: false });
console.log('result: ', result.data.operation_result);
const product = await io.product.update('productId', { name: 'product 2' });
fetch record:
const product = await io.product.get('recordId');
fetch list:
const query = io.query.compare('deleted', '=', false);
const list = await io.channel.find(query, { offset: 0, limit: 20 });
delete a record:
const result = await io.channel.delete('recordId', { offset: 0, limit: 20 });
delete many record
const query = io.query.compare('deleted', '=', true);
const result = await io.channel.deleteMany(query, { offset: 0, limit: 20 });
fetch record count:
const query = io.getQuery({ deleted: false });
const count = await io.channel.deleteMany(query);
# local develop
pnpm watch
# build package
pnpm build
# run test case
pnpm test