Skip to content

Commit

Permalink
Fix record update
Browse files Browse the repository at this point in the history
  • Loading branch information
Henus321 committed Aug 23, 2024
1 parent 76ef458 commit 4b82097
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-sqlite-explorer",
"version": "0.1.3",
"version": "0.1.4",
"description": "Explorer for sqlite database inside react native app",
"source": "./src/index.tsx",
"main": "./lib/commonjs/index.js",
Expand Down
15 changes: 7 additions & 8 deletions src/components/Table/TableModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TableModalProps = {
tableData: TableSignature;
modalType: ModalType;
onClose: () => void;
onSubmit: (model: TableSignatureValue) => void;
onSubmit: (model: TableSignatureValue, initialModel: TableSignatureValue,) => void;
};

const TableModal = ({
Expand All @@ -24,14 +24,13 @@ const TableModal = ({
}: TableModalProps) => {
const [model, setModel] = useState<TableSignatureValue>({});
const [errors, setErrors] = useState<TableSignatureValue>({});
const initialModel = getInitialModel(
tableData.fields,
modalType === 'update' ? checkedRowValue : null
);

useEffect(() => {
setModel(
getInitialModel(
tableData.fields,
modalType === 'update' ? checkedRowValue : null
)
);
setModel(initialModel);
setErrors({});
}, [modalType]);

Expand Down Expand Up @@ -65,7 +64,7 @@ const TableModal = ({

if (!!errorLength) return;

onSubmit(model);
onSubmit(model, initialModel);
};

return (
Expand Down
10 changes: 4 additions & 6 deletions src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ const Table = ({ tableData, onActionSuccess }: TableProps) => {
await DB.addRecord(tableData.name, model);
};

const onUpdate = async (model: TableSignatureValue) => {
const onUpdate = async (model: TableSignatureValue, initModel: TableSignatureValue) => {
if (!checkedRowValue) return;

await DB.editRecord(tableData.name, model)
//await DB.deleteRecord(tableData.name, checkedRowValue);
//await DB.addRecord(tableData.name, model);
await DB.editRecord(tableData.name, model, initModel)
};

const onModalSubmit = async (model: TableSignatureValue) => {
const onModalSubmit = async (model: TableSignatureValue, initModel: TableSignatureValue) => {
if (modalType === 'add') await onAdd(model);
if (modalType === 'update') await onUpdate(model);
if (modalType === 'update') await onUpdate(model, initModel);

await onActionSuccess(tableData);
setModalType(null);
Expand Down
32 changes: 25 additions & 7 deletions src/models/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Database {

addRecord = async (
tableName: string,
model: Record<string, string>
model: TableSignatureValue
): Promise<[ResultSet] | undefined | null> => {
try {
return await this.DB?.executeSql(
Expand All @@ -199,15 +199,33 @@ class Database {

editRecord = async (
tableName: string,
model: Record<string, string>
model: TableSignatureValue,
initModel: Record<string, any>
): Promise<[ResultSet] | undefined | null> => {
try {
const where = Object.keys(initModel).reduce((acc, key, index) => {
const value = initModel[key];

if (!value || (typeof value === 'string' && value.length > 255) || typeof value === 'object')
return acc;

return (
acc +
`${index !== 0 ? ' AND ' : ''}${key}=${typeof value === 'string' ? "'" + value + "'" : value}`
);
}, '');

const dataToSet = Object.keys(model).reduce((acc, key, index, arr) => {
const rawValue = model[key];
if (typeof rawValue === 'object' || rawValue === null) return acc;

const value = typeof rawValue === "string" ? `'${rawValue}'` : rawValue;

return acc + ` ${key} = ${value} ` + (index + 1 === arr.length ? "" : ",");
}, "");

return await this.DB?.executeSql(
`INSERT OR REPLACE INTO ${tableName} (${Object.keys(model).join(',')})
VALUES (${Object.keys(model)
.map(() => '?')
.join(',')})`,
Object.values(model)
`UPDATE ${tableName} SET ${dataToSet} WHERE ${where}`,
);
} catch (err) {
const message = getErrorText(err);
Expand Down

0 comments on commit 4b82097

Please sign in to comment.