-actmst interface
db change
This commit is contained in:
@@ -13,10 +13,6 @@ CREATE TABLE IF NOT EXISTS dbo.prjmst
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
-- Table: dbo.prjmst
|
|
||||||
|
|
||||||
-- DROP TABLE IF EXISTS dbo.prjmst;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS dbo.trnmst
|
CREATE TABLE IF NOT EXISTS dbo.trnmst
|
||||||
(
|
(
|
||||||
trnseq integer NOT NULL, -- เลขที่รายการ หรือ เลข บิล
|
trnseq integer NOT NULL, -- เลขที่รายการ หรือ เลข บิล
|
||||||
@@ -33,3 +29,29 @@ TABLESPACE pg_default;
|
|||||||
|
|
||||||
ALTER TABLE IF EXISTS dbo.trnmst
|
ALTER TABLE IF EXISTS dbo.trnmst
|
||||||
OWNER to postgres;
|
OWNER to postgres;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION dbo.translatebdg(
|
||||||
|
p_bdgcod text
|
||||||
|
)
|
||||||
|
RETURNS text
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $BODY$
|
||||||
|
DECLARE
|
||||||
|
resultName TEXT;
|
||||||
|
BEGIN
|
||||||
|
SELECT bdgnam
|
||||||
|
INTO resultName
|
||||||
|
FROM dbo.bdgmst
|
||||||
|
WHERE bdgcod = p_bdgcod
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
RETURN resultName;
|
||||||
|
END;
|
||||||
|
$BODY$;
|
||||||
|
|
||||||
|
ALTER FUNCTION dbo.translatebdg(text)
|
||||||
|
OWNER TO postgres;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { GeneralService } from '../../share/generalservice.js'
|
||||||
|
|
||||||
|
export class ActmstInterface {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.general = new GeneralService()
|
||||||
|
this.table = 'actmst'
|
||||||
|
this.pk = ['actseq', 'actnum'] // ⭐ PK ตาม CREATE TABLE
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveInterface(database, data) {
|
||||||
|
const method = data.methods.toLowerCase()
|
||||||
|
const { methods, ...payload } = data
|
||||||
|
|
||||||
|
if (method === 'put') return this.update(database, payload)
|
||||||
|
if (method === 'post') return this.insert(database, payload)
|
||||||
|
if (method === 'delete') return this.remove(database, payload)
|
||||||
|
|
||||||
|
throw new Error(`Unknown method: ${method}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async insert(database, payload) {
|
||||||
|
const cols = Object.keys(payload)
|
||||||
|
const vals = Object.values(payload)
|
||||||
|
const placeholders = cols.map((_, i) => `$${i + 1}`).join(', ')
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
INSERT INTO ${database}.${this.table} (${cols.join(', ')})
|
||||||
|
VALUES (${placeholders})
|
||||||
|
RETURNING *
|
||||||
|
`
|
||||||
|
return await this.general.executeQueryParam(database, sql, vals)
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(database, payload) {
|
||||||
|
const where = {}
|
||||||
|
const update = {}
|
||||||
|
|
||||||
|
for (const col in payload) {
|
||||||
|
if (this.pk.includes(col)) where[col] = payload[col]
|
||||||
|
else update[col] = payload[col]
|
||||||
|
}
|
||||||
|
|
||||||
|
const setCols = Object.keys(update)
|
||||||
|
.map((col, i) => `${col} = $${i + 1}`)
|
||||||
|
.join(', ')
|
||||||
|
|
||||||
|
const whereCols = Object.keys(where)
|
||||||
|
.map((col, i) => `${col} = $${i + 1 + Object.keys(update).length}`)
|
||||||
|
.join(' AND ')
|
||||||
|
|
||||||
|
const params = [...Object.values(update), ...Object.values(where)]
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
UPDATE ${database}.${this.table}
|
||||||
|
SET ${setCols}
|
||||||
|
WHERE ${whereCols}
|
||||||
|
RETURNING *
|
||||||
|
`
|
||||||
|
|
||||||
|
return await this.general.executeQueryParam(database, sql, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(database, payload) {
|
||||||
|
const where = {}
|
||||||
|
|
||||||
|
this.pk.forEach(pk => {
|
||||||
|
if (!payload[pk]) throw new Error(`Missing PK: ${pk}`)
|
||||||
|
where[pk] = payload[pk]
|
||||||
|
})
|
||||||
|
|
||||||
|
const whereCols = Object.keys(where)
|
||||||
|
.map((col, i) => `${col} = $${i + 1}`)
|
||||||
|
.join(' AND ')
|
||||||
|
|
||||||
|
const params = Object.values(where)
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
DELETE FROM ${database}.${this.table}
|
||||||
|
WHERE ${whereCols}
|
||||||
|
RETURNING *
|
||||||
|
`
|
||||||
|
|
||||||
|
return await this.general.executeQueryParam(database, sql, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user