Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 4d3eaea

Browse files
committed
feat(web): add migration script example
1 parent 95dca49 commit 4d3eaea

File tree

5 files changed

+372
-0
lines changed

5 files changed

+372
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"kind": "collectionType",
3+
"collectionName": "up_users",
4+
"info": {
5+
"name": "user",
6+
"description": "",
7+
"singularName": "user",
8+
"pluralName": "users",
9+
"displayName": "User"
10+
},
11+
"options": {
12+
"draftAndPublish": false
13+
},
14+
"attributes": {
15+
"username": {
16+
"type": "string",
17+
"minLength": 3,
18+
"unique": true,
19+
"configurable": false,
20+
"required": true
21+
},
22+
"email": {
23+
"type": "email",
24+
"minLength": 6,
25+
"configurable": false,
26+
"required": true
27+
},
28+
"provider": {
29+
"type": "string",
30+
"configurable": false
31+
},
32+
"password": {
33+
"type": "password",
34+
"minLength": 6,
35+
"configurable": false,
36+
"private": true,
37+
"searchable": false
38+
},
39+
"resetPasswordToken": {
40+
"type": "string",
41+
"configurable": false,
42+
"private": true,
43+
"searchable": false
44+
},
45+
"confirmationToken": {
46+
"type": "string",
47+
"configurable": false,
48+
"private": true,
49+
"searchable": false
50+
},
51+
"confirmed": {
52+
"type": "boolean",
53+
"default": false,
54+
"configurable": false
55+
},
56+
"blocked": {
57+
"type": "boolean",
58+
"default": false,
59+
"configurable": false
60+
},
61+
"role": {
62+
"type": "relation",
63+
"relation": "manyToOne",
64+
"target": "plugin::users-permissions.role",
65+
"inversedBy": "users",
66+
"configurable": false
67+
},
68+
"old_pw_hash": {
69+
"type": "string"
70+
},
71+
"migrated_pw": {
72+
"type": "boolean",
73+
"default": false
74+
}
75+
}
76+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = plugin => {
2+
const x =
3+
};

scripts/user-migration/package-lock.json

Lines changed: 207 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/user-migration/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "user-migration",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "user-migration.js",
6+
"scripts": {
7+
"migrate": "node user-migration.js"
8+
},
9+
"author": "izzy",
10+
"license": "ISC",
11+
"dependencies": {
12+
"axios": "^1.6.8",
13+
"mysql2": "^3.9.3"
14+
}
15+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const strapiUrl = "http://localhost:1337/api";
2+
3+
const axios = require("axios");
4+
5+
async function getUsersFromStrapi() {
6+
try {
7+
// Make a GET request to the Strapi API to fetch users
8+
const response = await axios.get(`${strapiUrl}/users`);
9+
10+
// Extract user data from the response
11+
const users = response.data;
12+
13+
// Output user data
14+
console.log("Users:");
15+
console.log(users);
16+
} catch (error) {
17+
console.error("Error fetching users:", error.message);
18+
}
19+
}
20+
21+
async function createUser(username, email, password, old_password) {
22+
try {
23+
const response = await fetch(`${strapiUrl}/auth/local/register`, {
24+
method: "POST",
25+
headers: {
26+
"Content-Type": "application/json",
27+
Accept: "application/json",
28+
},
29+
body: JSON.stringify({
30+
username: username,
31+
email: email,
32+
password: password,
33+
old_pw_hash: old_password,
34+
}),
35+
});
36+
console.log(response);
37+
38+
return await response.json();
39+
} catch (e) {
40+
console.log("something went wrong");
41+
console.log(e);
42+
return null;
43+
}
44+
}
45+
46+
// Get the client
47+
const mysql = require("mysql2");
48+
// Create the connection to database
49+
const connection = mysql.createConnection({
50+
host: "localhost",
51+
user: "vim",
52+
database: "vim",
53+
password: "super-secret",
54+
});
55+
56+
// A simple SELECT query
57+
connection.query("SELECT * FROM `vs_users`", function (err, results, fields) {
58+
console.log(results); // results contains rows returned by server
59+
60+
results.forEach((user) => {
61+
createUser(user.user_name, user.email, "123gege321", "old_hash")
62+
.then((a) => {
63+
console.log(a);
64+
})
65+
.catch((err) => {
66+
console.log(err);
67+
});
68+
});
69+
70+
console.log(fields); // fields contains extra meta data about results, if available
71+
});

0 commit comments

Comments
 (0)