prisma-client-php 4.0.2 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/init.js +1 -1
- package/dist/prisma/seed.ts +108 -50
- package/package.json +1 -1
package/dist/init.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import fs from"fs";import path from"path";import{execSync}from"child_process";import{getFileMeta}from"./utils.js";import chalk from"chalk";const{__dirname}=getFileMeta();let projectSettings=null;function checkExcludeFiles(e){return projectSettings?.excludeFilePath?.includes(e.replace(/\\/g,"/"))??!1}function copyRecursiveSync(e,s,
|
|
1
|
+
import fs from"fs";import path from"path";import{execSync}from"child_process";import{getFileMeta}from"./utils.js";import chalk from"chalk";const{__dirname}=getFileMeta();let projectSettings=null;function checkExcludeFiles(e){return projectSettings?.excludeFilePath?.includes(e.replace(/\\/g,"/"))??!1}function copyRecursiveSync(e,s,i=e,n=!1){if(!fs.existsSync(e))return void console.error(`Source folder does not exist: ${e}`);fs.existsSync(s)||fs.mkdirSync(s,{recursive:!0});const o=fs.readdirSync(e,{withFileTypes:!0});for(const r of o){const o=path.join(e,r.name),c=path.join(s,r.name),t=path.relative(i,o),p=path.join(path.basename(i),t);if(checkExcludeFiles(c))return;const a=path.relative(__dirname,o),l=path.normalize("src/Lib/Validator.php");n&&a===l?console.log(`Skipping file: ${p}`):r.isDirectory()?copyRecursiveSync(o,c,i,n):(fs.copyFileSync(o,c),console.log(`Copied file: ${p}`))}}const directoriesToCopy=[{srcFolder:path.join(__dirname,"src"),destFolder:path.join(process.cwd(),"src")},{srcFolder:path.join(__dirname,"settings"),destFolder:path.join(process.cwd(),"settings")},{srcFolder:path.join(__dirname,"prisma"),destFolder:path.join(process.cwd(),"prisma")}];function installNpmDependencies(e){const s=process.cwd(),i=path.join(s,"package.json");let n;if(!fs.existsSync(i))return void console.error("package.json does not exist.");{const s=fs.readFileSync(i,"utf8");n=JSON.parse(s),e||(n.type="module"),fs.writeFileSync(i,JSON.stringify(n,null,2))}let o=[npmPkg("prisma"),npmPkg("@prisma/client"),npmPkg("@prisma/internals")];e||o.push(npmPkg("tsx"),npmPkg("typescript"),npmPkg("@types/node"));const r=o.join(" ");try{console.log(`Installing packages: ${r}`),execSync(`npm install --save-dev ${r}`,{stdio:"inherit",cwd:s}),e||execSync("npx tsc --init",{stdio:"inherit",cwd:s}),console.log("Packages installed successfully.")}catch(e){console.error("Error installing packages:",e)}}function initPrisma(){const e=process.cwd(),s=path.join(e,"prisma");if(fs.existsSync(s))console.warn("Prisma folder already exists. Skipping prisma init.");else try{console.log("Initializing Prisma..."),execSync("npx prisma init",{stdio:"inherit",cwd:e}),console.log("Prisma initialized successfully.")}catch(e){console.error("Error initializing Prisma:",e)}}async function installComposerDependencies(e){const s=process.cwd(),i=path.join(s,"composer.json");if(!fs.existsSync(i))return void console.error("composer.json does not exist.");let n=[composerPkg("calicastle/cuid")];if(e){const e=path.join(s,"prisma-php.json");if(fs.existsSync(e)){const i=readJsonFile(e);let n=[];i.excludeFiles?.map(e=>{const i=path.join(s,e);fs.existsSync(i)&&n.push(i.replace(/\\/g,"/"))}),projectSettings={...i,excludeFiles:i.excludeFiles??[],excludeFilePath:n??[]}}}else n.push(composerPkg("symfony/uid"),composerPkg("brick/math"),composerPkg("tsnc/prisma-php"));try{console.log("Installing Composer dependencies:"),n.forEach(e=>console.log(`- ${chalk.blue(e)}`));const e=`C:\\xampp\\php\\php.exe C:\\ProgramData\\ComposerSetup\\bin\\composer.phar require ${n.join(" ")}`;execSync(e,{stdio:"inherit",cwd:s})}catch(e){console.error("Error installing Composer dependencies:",e)}}const readJsonFile=e=>{const s=fs.readFileSync(e,"utf8");return JSON.parse(s)},npmPinnedVersions={prisma:"7.0.1","@prisma/client":"7.0.1","@prisma/internals":"7.0.1",tsx:"4.20.6",typescript:"5.9.3","@types/node":"24.10.1","better-sqlite3":"12.5.0","@prisma/adapter-better-sqlite3":"7.0.1",mariadb:"3.4.5","@prisma/adapter-mariadb":"7.0.1",pg:"8.16.3","@prisma/adapter-pg":"7.0.1"};function npmPkg(e){return npmPinnedVersions[e]?`${e}@${npmPinnedVersions[e]}`:e}const composerPinnedVersions={"calicastle/cuid":"2.0.0","symfony/uid":"7.3.1","brick/math":"0.14.1","tsnc/prisma-php":"1.0.0"};function composerPkg(e){return composerPinnedVersions[e]?`${e}:${composerPinnedVersions[e]}`:e}async function main(){const e=process.argv.includes("--prisma-php");installNpmDependencies(e),installComposerDependencies(e),initPrisma(),directoriesToCopy.forEach(s=>{copyRecursiveSync(s.srcFolder,s.destFolder,s.srcFolder,e)});const s=path.join(__dirname,"prisma.config.ts"),i=path.join(process.cwd(),"prisma.config.ts");var n,o;n=s,o=i,fs.existsSync(n)?(fs.copyFileSync(n,o),console.log(`Copied file: ${path.basename(n)}`)):console.warn(`File not found (skip): ${n}`),console.log("Finished copying directories.")}main().catch(e=>{console.error("Error during execution:",e)});
|
package/dist/prisma/seed.ts
CHANGED
|
@@ -1,73 +1,131 @@
|
|
|
1
1
|
import { PrismaClient } from "@prisma/client";
|
|
2
2
|
|
|
3
|
+
// ============================================================
|
|
4
|
+
// 1. DATABASE ADAPTER CONFIGURATION
|
|
5
|
+
// Uncomment the section matching your active database
|
|
6
|
+
// ============================================================
|
|
7
|
+
|
|
8
|
+
// --- OPTION A: SQLITE ---------------------------------------
|
|
9
|
+
// import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
10
|
+
|
|
11
|
+
// const adapter = new PrismaBetterSqlite3({
|
|
12
|
+
// url: process.env.DATABASE_URL!,
|
|
13
|
+
// });
|
|
14
|
+
// const prisma = new PrismaClient({ adapter });
|
|
15
|
+
// ------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
// --- OPTION B: POSTGRESQL -----------------------------------
|
|
18
|
+
// import { PrismaPg } from "@prisma/adapter-pg";
|
|
19
|
+
// import { Pool } from "pg";
|
|
20
|
+
|
|
21
|
+
// const connectionString = process.env.DATABASE_URL!;
|
|
22
|
+
// const pool = new Pool({ connectionString });
|
|
23
|
+
// const adapter = new PrismaPg(pool);
|
|
24
|
+
// const prisma = new PrismaClient({ adapter });
|
|
25
|
+
// ------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
// --- OPTION C: MYSQL / MARIADB ------------------------------
|
|
28
|
+
// import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
|
29
|
+
// import { createPool } from "mariadb";
|
|
30
|
+
|
|
31
|
+
// const connectionUrl = new URL(process.env.DATABASE_URL!);
|
|
32
|
+
// const pool = createPool({
|
|
33
|
+
// host: connectionUrl.hostname,
|
|
34
|
+
// port: Number(connectionUrl.port) || 3306,
|
|
35
|
+
// user: connectionUrl.username,
|
|
36
|
+
// password: connectionUrl.password,
|
|
37
|
+
// database: connectionUrl.pathname.slice(1),
|
|
38
|
+
// connectionLimit: 5,
|
|
39
|
+
// });
|
|
40
|
+
// const adapter = new PrismaMariaDb(pool);
|
|
41
|
+
// const prisma = new PrismaClient({ adapter });
|
|
42
|
+
// ------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
// --- DEFAULT (NO ADAPTER) -----------------------------------
|
|
45
|
+
// If you just want standard Prisma without specific drivers:
|
|
3
46
|
const prisma = new PrismaClient();
|
|
47
|
+
// ------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
// ============================================================
|
|
50
|
+
// 2. DATA DEFINITION
|
|
51
|
+
// ============================================================
|
|
4
52
|
|
|
5
53
|
const userRoleData = [
|
|
6
|
-
{
|
|
7
|
-
|
|
8
|
-
name: "Admin",
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
id: 2,
|
|
12
|
-
name: "User",
|
|
13
|
-
},
|
|
54
|
+
{ id: 1, name: "Admin" },
|
|
55
|
+
{ id: 2, name: "User" },
|
|
14
56
|
];
|
|
15
57
|
|
|
16
58
|
const userData = [
|
|
17
59
|
{
|
|
18
60
|
name: "Juan",
|
|
19
61
|
email: "j@gmail.com",
|
|
20
|
-
password: "$2b$10$mgjotYzIXwrK1MCWmu4tgeUVnLcb.qzvqwxOq4FXEL8k2obwXivDi", //
|
|
62
|
+
password: "$2b$10$mgjotYzIXwrK1MCWmu4tgeUVnLcb.qzvqwxOq4FXEL8k2obwXivDi", // bcrypt: 1234
|
|
21
63
|
roleId: 1,
|
|
22
64
|
},
|
|
23
65
|
];
|
|
24
66
|
|
|
67
|
+
// ============================================================
|
|
68
|
+
// 3. EXECUTION LOGIC
|
|
69
|
+
// ============================================================
|
|
70
|
+
|
|
25
71
|
async function main() {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
console.log(`Start seeding ...`);
|
|
73
|
+
|
|
74
|
+
// NOTE: We delete User first, then UserRole to avoid Foreign Key constraints.
|
|
75
|
+
|
|
76
|
+
// --- OPTION A: SQLITE LOGIC ---------------------------------
|
|
77
|
+
/*
|
|
78
|
+
console.log("Seeding for SQLite...");
|
|
79
|
+
|
|
80
|
+
await prisma.user.deleteMany();
|
|
81
|
+
await prisma.userRole.deleteMany();
|
|
82
|
+
// SQLite implies auto-increment reset via internal table if needed,
|
|
83
|
+
// but usually not strictly required for seeds unless using specific IDs.
|
|
84
|
+
|
|
85
|
+
await prisma.userRole.createMany({ data: userRoleData });
|
|
86
|
+
await prisma.user.createMany({ data: userData });
|
|
87
|
+
*/
|
|
88
|
+
// ------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
// --- OPTION B: POSTGRESQL LOGIC -----------------------------
|
|
91
|
+
/*
|
|
92
|
+
console.log("Seeding for PostgreSQL...");
|
|
93
|
+
|
|
94
|
+
await prisma.user.deleteMany();
|
|
95
|
+
await prisma.userRole.deleteMany();
|
|
96
|
+
|
|
97
|
+
// Reset Auto Increment Sequences
|
|
98
|
+
await prisma.$executeRaw`ALTER SEQUENCE "UserRole_id_seq" RESTART WITH 1`;
|
|
99
|
+
await prisma.$executeRaw`ALTER SEQUENCE "User_id_seq" RESTART WITH 1`;
|
|
100
|
+
|
|
101
|
+
await prisma.userRole.createMany({ data: userRoleData });
|
|
102
|
+
await prisma.user.createMany({ data: userData });
|
|
103
|
+
*/
|
|
104
|
+
// ------------------------------------------------------------
|
|
105
|
+
|
|
106
|
+
// --- OPTION C: MYSQL LOGIC ----------------------------------
|
|
107
|
+
/*
|
|
108
|
+
console.log("Seeding for MySQL...");
|
|
109
|
+
|
|
110
|
+
await prisma.user.deleteMany();
|
|
111
|
+
await prisma.userRole.deleteMany();
|
|
112
|
+
|
|
113
|
+
// Reset Auto Increment Counters
|
|
114
|
+
await prisma.$executeRaw`ALTER TABLE UserRole AUTO_INCREMENT = 1`;
|
|
115
|
+
await prisma.$executeRaw`ALTER TABLE User AUTO_INCREMENT = 1`;
|
|
116
|
+
|
|
117
|
+
await prisma.userRole.createMany({ data: userRoleData });
|
|
118
|
+
await prisma.user.createMany({ data: userData });
|
|
119
|
+
*/
|
|
120
|
+
// ------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
console.log(`Seeding finished.`);
|
|
66
123
|
}
|
|
67
124
|
|
|
68
125
|
main()
|
|
69
126
|
.catch((e) => {
|
|
70
|
-
|
|
127
|
+
console.error(e);
|
|
128
|
+
process.exit(1);
|
|
71
129
|
})
|
|
72
130
|
.finally(async () => {
|
|
73
131
|
await prisma.$disconnect();
|
package/package.json
CHANGED