|
| 1 | +const mysql = require('mysql2/promise'); |
| 2 | + |
| 3 | +async function insertAllFormData() { |
| 4 | + const connection = await mysql.createConnection({ |
| 5 | + host: 'localhost', |
| 6 | + user: 'root', |
| 7 | + password: 'your_password', // Add if needed |
| 8 | + database: 'your_db' |
| 9 | + }); |
| 10 | + |
| 11 | + try { |
| 12 | + await connection.beginTransaction(); |
| 13 | + |
| 14 | + // 1. Insert into forms |
| 15 | + const [formResult] = await connection.execute( |
| 16 | + `INSERT INTO forms ( |
| 17 | + farmer_name, age, mobile, district, block, panchayat, hamlet, |
| 18 | + id_type, id_number, gender, spouse, type_of_households, h_members, |
| 19 | + hh_occupation, special_catog, caste, house_owner, type_of_house, |
| 20 | + drinking_water, potability, domestic_water, toilet_avail, toilet_cond, |
| 21 | + household_education, user_id, created_at, lat, lon, status, form_type |
| 22 | + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| 23 | + [2, 'FARMER_NAME', 'AGE', 9876543210, 'DISTRICT', 'BLOCK', 'PANCHAYAT', 'HAMLET', |
| 24 | + 'ID_TYPE', 'ID_NUMBER', 'GENDER', 'SPOUSE', 'HOUSEHOLD_TYPE', 5, |
| 25 | + 'OCCUPATION', 'SPECIAL_CAT', 'CASTE', 'OWNER', 'HOUSE_TYPE', |
| 26 | + 'DRINK_WATER', 'POTABILITY', 'DOMESTIC_WATER', 'TOILET_AVAIL', 'TOILET_COND', |
| 27 | + 'EDUCATION', 1, '2025-04-16', 'LAT', 'LON', 'MCODE', 1] |
| 28 | + ); |
| 29 | + |
| 30 | + const formId = formResult.insertId; |
| 31 | + |
| 32 | + // 2. Insert into bank_details |
| 33 | + await connection.execute( |
| 34 | + `INSERT INTO bank_details ( |
| 35 | + form_id, account_holder_name, account_number, bank_name, |
| 36 | + branch, ifsc_code, farmer_ack |
| 37 | + ) VALUES (?, ?, ?, ?, ?, ?, ?)`, |
| 38 | + [formId, 'ACCOUNT_HOLDER', 123456789012, 'BANK_NAME', |
| 39 | + 'BRANCH_NAME', 'IFSC00001', 'ACKNOWLEDGED'] |
| 40 | + ); |
| 41 | + |
| 42 | + // 3. Insert into files |
| 43 | + await connection.execute( |
| 44 | + `INSERT INTO files ( |
| 45 | + form_id, identity, geotag, patta, fmb, photo, passbook |
| 46 | + ) VALUES (?, ?, ?, ?, ?, ?, ?)`, |
| 47 | + [formId, 'IDENTITY_DOC', 'GEOTAG_URL', 'PATTA_DOC', 'FMB_DOC', 'PHOTO_URL', 'PASSBOOK_DOC'] |
| 48 | + ); |
| 49 | + |
| 50 | + // 4. Insert into farm_pond_details |
| 51 | + await connection.execute( |
| 52 | + `INSERT INTO farm_pond_details ( |
| 53 | + form_id, ownership, well_irrigation, area_irrigated, irrigated_lands, |
| 54 | + patta, total_area, taluk, firka, revenue, crop_season, livestocks, |
| 55 | + sf_number, soil_type, land_to_benefit, date_of_ins, length, breadth, |
| 56 | + depth, volume, p_contribution, f_contribution, total_est, area_benefited, |
| 57 | + field_insp, site_app, date_of_app |
| 58 | + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| 59 | + [formId, 'OWNERSHIP', 'WELL_IRR', 'AREA_IRR', 'IRRIG_LAND', |
| 60 | + 'PATTA', 'TOT_AREA', 'TALUK', 'FIRKA', 'REVENUE', 'CROP_SEASON', 'LIVESTOCKS', |
| 61 | + 'SF_NO', 'SOIL', 'LAND_BENEFIT', 'DATE_INS', '10', '20', |
| 62 | + '5', '1000', 'P_CONTRI', 'F_CONTRI', 'EST_TOTAL', 'AREA_BENEFITED', |
| 63 | + 'INSP', 'SITE_APP', 'APP_DATE', 'LEN_PF', 'BRE_PF', 'DEP_PF', 'VOL_PF', |
| 64 | + 'AREA_BENEFITED_PF', 'VERIFIER'] |
| 65 | + ); |
| 66 | + |
| 67 | + await connection.commit(); |
| 68 | + console.log("All data inserted successfully with form_id:", formId); |
| 69 | + } catch (err) { |
| 70 | + await connection.rollback(); |
| 71 | + console.error("Transaction failed:", err); |
| 72 | + } finally { |
| 73 | + await connection.end(); |
| 74 | + } |
| 75 | +} |
0 commit comments