db-crud-api
v0.2.0
Published
CRUD api for database tables
Downloads
16
Readme
db-crud-api 💡
CRUD api for database tables.
Installation
npm install db-crud-api
Usage
import dbCrudApi from "db-crud-api";
const apiFactory = dbCrudApi(mySchema);
const mySchema = {
servers: {
server1: {
realName: "server1.mydomain.com",
type: "ms-sql",
instance: "DEFAULT",
user: "User1",
password: "mypassword",
options: {
appName: "myAppName",
...
},
databases: {
db1: {
realName: "db_orders",
tables: {
table1: {
realName: "tb_orders_head",
idField: "Id",
autoId: true,
fields: {
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
CustomerId: { type: "string", lengh: undefined, canBeNull: false, description: "Customer Id", defaultValue: undefined },
...
}
},
table2: {
realName: "tb_orders_detail",
idField: "Id",
autoId: true,
fields: {
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
OrderId: { type: "uuid", lengh: undefined, canBeNull: false, description: "Order Id", defaultValue: undefined },
RowSeq: { type: "numeric", lengh: undefined, canBeNull: false, description: "Row sequence", defaultValue: undefined },
...
}
},
...
}
},
...
}
},
...
}
};
const apiOrder = apiFactory.newROApi("server1.db1.table1"); // Read Only api
const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
const apiExecuteSP = apiFactory.newExecuteApi("my_storeproc @Company, @OrderNumber"); // execute my_storeproc
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
console.log(await apiOrder.getByFilter()); // this get all rows
console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
console.log(await apiExecuteSP.execute({params: {Company: "XXX", OrderNumber: "12345"}}})); // execute
License
MIT License
Copyright (c) 2022
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.