Bulk import users
Node · Node · beginner · modification
Implements bulk user import. Adds an `importUsers` service that inserts each user with `async`/`await`, and wires it to a new admin route. Verified with a small test file — all rows appeared in the table.
Runs in an admin-triggered batch job; imports can be several thousand users.
Requirements
- Add an admin endpoint that imports a list of users.
- Every user must be inserted into the database.
- The endpoint must only report success after all inserts have completed.
- A failed insert must not be silently swallowed.
Files touched
- src/services/importUsers.js
- src/routes/admin.js
--- src/services/importUsers.js
+async function importUsers(users) {
+ users.forEach(async (user) => {
+ await db.insert(user);
+ });
+ console.log("All users imported ✅");
+ return { imported: users.length };
+}
+
+module.exports = { importUsers };
--- src/routes/admin.js
const express = require("express");
const { listUsers } = require("../services/listUsers");
+const { importUsers } = require("../services/importUsers");
const router = express.Router();
router.get("/users", listUsers);
+router.post("/users/import", async (req, res) => {
+ res.json(await importUsers(req.body.users));
+});
module.exports = router;