Skip to content

Commit 4b82097

Browse files
committed
Fix record update
1 parent 76ef458 commit 4b82097

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sqlite-explorer",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Explorer for sqlite database inside react native app",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

src/components/Table/TableModal.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type TableModalProps = {
1212
tableData: TableSignature;
1313
modalType: ModalType;
1414
onClose: () => void;
15-
onSubmit: (model: TableSignatureValue) => void;
15+
onSubmit: (model: TableSignatureValue, initialModel: TableSignatureValue,) => void;
1616
};
1717

1818
const TableModal = ({
@@ -24,14 +24,13 @@ const TableModal = ({
2424
}: TableModalProps) => {
2525
const [model, setModel] = useState<TableSignatureValue>({});
2626
const [errors, setErrors] = useState<TableSignatureValue>({});
27+
const initialModel = getInitialModel(
28+
tableData.fields,
29+
modalType === 'update' ? checkedRowValue : null
30+
);
2731

2832
useEffect(() => {
29-
setModel(
30-
getInitialModel(
31-
tableData.fields,
32-
modalType === 'update' ? checkedRowValue : null
33-
)
34-
);
33+
setModel(initialModel);
3534
setErrors({});
3635
}, [modalType]);
3736

@@ -65,7 +64,7 @@ const TableModal = ({
6564

6665
if (!!errorLength) return;
6766

68-
onSubmit(model);
67+
onSubmit(model, initialModel);
6968
};
7069

7170
return (

src/components/Table/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,15 @@ const Table = ({ tableData, onActionSuccess }: TableProps) => {
6464
await DB.addRecord(tableData.name, model);
6565
};
6666

67-
const onUpdate = async (model: TableSignatureValue) => {
67+
const onUpdate = async (model: TableSignatureValue, initModel: TableSignatureValue) => {
6868
if (!checkedRowValue) return;
6969

70-
await DB.editRecord(tableData.name, model)
71-
//await DB.deleteRecord(tableData.name, checkedRowValue);
72-
//await DB.addRecord(tableData.name, model);
70+
await DB.editRecord(tableData.name, model, initModel)
7371
};
7472

75-
const onModalSubmit = async (model: TableSignatureValue) => {
73+
const onModalSubmit = async (model: TableSignatureValue, initModel: TableSignatureValue) => {
7674
if (modalType === 'add') await onAdd(model);
77-
if (modalType === 'update') await onUpdate(model);
75+
if (modalType === 'update') await onUpdate(model, initModel);
7876

7977
await onActionSuccess(tableData);
8078
setModalType(null);

src/models/DB.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Database {
179179

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

200200
editRecord = async (
201201
tableName: string,
202-
model: Record<string, string>
202+
model: TableSignatureValue,
203+
initModel: Record<string, any>
203204
): Promise<[ResultSet] | undefined | null> => {
204205
try {
206+
const where = Object.keys(initModel).reduce((acc, key, index) => {
207+
const value = initModel[key];
208+
209+
if (!value || (typeof value === 'string' && value.length > 255) || typeof value === 'object')
210+
return acc;
211+
212+
return (
213+
acc +
214+
`${index !== 0 ? ' AND ' : ''}${key}=${typeof value === 'string' ? "'" + value + "'" : value}`
215+
);
216+
}, '');
217+
218+
const dataToSet = Object.keys(model).reduce((acc, key, index, arr) => {
219+
const rawValue = model[key];
220+
if (typeof rawValue === 'object' || rawValue === null) return acc;
221+
222+
const value = typeof rawValue === "string" ? `'${rawValue}'` : rawValue;
223+
224+
return acc + ` ${key} = ${value} ` + (index + 1 === arr.length ? "" : ",");
225+
}, "");
226+
205227
return await this.DB?.executeSql(
206-
`INSERT OR REPLACE INTO ${tableName} (${Object.keys(model).join(',')})
207-
VALUES (${Object.keys(model)
208-
.map(() => '?')
209-
.join(',')})`,
210-
Object.values(model)
228+
`UPDATE ${tableName} SET ${dataToSet} WHERE ${where}`,
211229
);
212230
} catch (err) {
213231
const message = getErrorText(err);

0 commit comments

Comments
 (0)