@wzyjs/next 0.2.40
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/Base.zmodel +51 -0
- package/dist/index.js +81 -0
- package/dist/useHabitGroupCRUD.ts +118 -0
- package/package.json +23 -0
package/dist/Base.zmodel
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
generator client {
|
|
2
|
+
provider = "prisma-client-js"
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
datasource db {
|
|
6
|
+
provider = "mysql"
|
|
7
|
+
url = env("DATABASE_URL")
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
plugin prisma {
|
|
11
|
+
provider = '@core/prisma'
|
|
12
|
+
output = './schema.prisma'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
plugin enhancer {
|
|
16
|
+
provider = '@core/enhancer'
|
|
17
|
+
output = '../src/api/generated/enhancer'
|
|
18
|
+
preserveTsFiles = true
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
plugin zod {
|
|
22
|
+
provider = '@core/zod'
|
|
23
|
+
output = '../src/api/generated/zod'
|
|
24
|
+
compile = false
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
plugin trpc {
|
|
28
|
+
provider = '@zenstackhq/trpc'
|
|
29
|
+
output = '../src/api/generated/trpc'
|
|
30
|
+
version = 'v11'
|
|
31
|
+
generateClientHelpers = 'next'
|
|
32
|
+
importCreateRouter = "@/api/trpc/trpc"
|
|
33
|
+
importProcedure = "@/api/trpc/procedures"
|
|
34
|
+
zodSchemasImport = '@/api/generated/zod'
|
|
35
|
+
generateModelActions = 'findMany, findUnique, create, update, delete, deleteMany'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
plugin store {
|
|
39
|
+
provider = '@wzyjs/next'
|
|
40
|
+
title = 'Generate useModelStore'
|
|
41
|
+
output = 'src/api/generated/store'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
abstract model Base {
|
|
45
|
+
id String @id @default(cuid())
|
|
46
|
+
createdAt DateTime @default(now())
|
|
47
|
+
updatedAt DateTime? @updatedAt
|
|
48
|
+
isDeleted Boolean? @default(false) @omit
|
|
49
|
+
|
|
50
|
+
@@allow('all', !isDeleted)
|
|
51
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.name = void 0;
|
|
40
|
+
exports.default = run;
|
|
41
|
+
var fs = require("node:fs");
|
|
42
|
+
var path = require("node:path");
|
|
43
|
+
exports.name = 'Generate useModelStore';
|
|
44
|
+
var config = {
|
|
45
|
+
template: {
|
|
46
|
+
content: fs.readFileSync(path.resolve(__dirname, './useHabitGroupCRUD.ts'), 'utf-8'),
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
function run(model, options) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
var modelNames, indexContent;
|
|
52
|
+
return __generator(this, function (_a) {
|
|
53
|
+
// 检查配置
|
|
54
|
+
if (!options.output) {
|
|
55
|
+
throw new Error('store output is required');
|
|
56
|
+
}
|
|
57
|
+
modelNames = model.declarations.filter(function (item) { return item.$type === 'DataModel'; });
|
|
58
|
+
// 创建目录
|
|
59
|
+
if (modelNames.length) {
|
|
60
|
+
if (!fs.existsSync(options.output)) {
|
|
61
|
+
fs.mkdirSync(options.output);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// 生成模板
|
|
65
|
+
modelNames.forEach(function (_a) {
|
|
66
|
+
var ModelName = _a.name;
|
|
67
|
+
var templateContent = config.template.content;
|
|
68
|
+
var modelName = ModelName.replace(/^./, function (s) { return s.toLowerCase(); });
|
|
69
|
+
templateContent = templateContent.replace(/habitGroup/g, modelName);
|
|
70
|
+
templateContent = templateContent.replace(/HabitGroup/g, ModelName);
|
|
71
|
+
fs.writeFileSync(options.output + "/use".concat(ModelName, "Store.ts"), templateContent);
|
|
72
|
+
});
|
|
73
|
+
indexContent = modelNames.map(function (_a) {
|
|
74
|
+
var ModelName = _a.name;
|
|
75
|
+
return "export * from './use".concat(ModelName, "Store'");
|
|
76
|
+
}).join('\n');
|
|
77
|
+
fs.writeFileSync(options.output + '/index.ts', indexContent + '\n');
|
|
78
|
+
return [2 /*return*/];
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import { message } from '@/components'
|
|
5
|
+
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import { api } from '@/api/react'
|
|
8
|
+
|
|
9
|
+
interface Option {
|
|
10
|
+
showTip?: boolean,
|
|
11
|
+
list?: false | {
|
|
12
|
+
query?: Parameters<typeof api.habitGroup.findMany.useQuery>[0],
|
|
13
|
+
option?: Parameters<typeof api.habitGroup.findMany.useQuery>[1],
|
|
14
|
+
},
|
|
15
|
+
create?: Parameters<typeof api.habitGroup.create.useMutation>[0],
|
|
16
|
+
update?: Parameters<typeof api.habitGroup.update.useMutation>[0],
|
|
17
|
+
remove?: Parameters<typeof api.habitGroup.update.useMutation>[0],
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const useHabitGroupCRUD = (option: Option = {}) => {
|
|
21
|
+
const { list, create, update, remove, showTip = true } = option
|
|
22
|
+
|
|
23
|
+
const apiUtils = api.useUtils()
|
|
24
|
+
|
|
25
|
+
const listState = api.habitGroup.findMany.useQuery(list ? list.query : {}, {
|
|
26
|
+
...list.option,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const onSuccess = (tip: string) => {
|
|
30
|
+
message.destroy()
|
|
31
|
+
if (showTip) {
|
|
32
|
+
message.success(tip)
|
|
33
|
+
}
|
|
34
|
+
if (list) {
|
|
35
|
+
listState.refetch()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const baseCreateState = api.habitGroup.create.useMutation({
|
|
40
|
+
...create,
|
|
41
|
+
onSuccess: (...args) => {
|
|
42
|
+
if (create?.onSuccess) {
|
|
43
|
+
create.onSuccess(...args)
|
|
44
|
+
}
|
|
45
|
+
onSuccess('创建成功')
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const createState = {
|
|
50
|
+
...baseCreateState as unknown as typeof baseCreateState,
|
|
51
|
+
mutate: (data: Parameters<typeof baseCreateState.mutate>[0]['data']) => {
|
|
52
|
+
return baseCreateState.mutate({ data })
|
|
53
|
+
},
|
|
54
|
+
mutateAsync: (data: Parameters<typeof baseCreateState.mutateAsync>[0]['data']) => {
|
|
55
|
+
return baseCreateState.mutateAsync({ data })
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const baseUpdateState = api.habitGroup.update.useMutation({
|
|
60
|
+
...update,
|
|
61
|
+
onSuccess: (...args) => {
|
|
62
|
+
if (update?.onSuccess) {
|
|
63
|
+
update.onSuccess(...args)
|
|
64
|
+
}
|
|
65
|
+
onSuccess('更新成功')
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const updateState = {
|
|
70
|
+
...baseUpdateState as unknown as typeof baseUpdateState,
|
|
71
|
+
mutate: (id: string, data: Parameters<typeof baseUpdateState.mutate>[0]['data']) => {
|
|
72
|
+
return baseUpdateState.mutate({
|
|
73
|
+
where: { id },
|
|
74
|
+
data,
|
|
75
|
+
})
|
|
76
|
+
},
|
|
77
|
+
mutateAsync: (id: string, data: Parameters<typeof baseUpdateState.mutateAsync>[0]['data']) => {
|
|
78
|
+
return baseUpdateState.mutateAsync({
|
|
79
|
+
where: { id },
|
|
80
|
+
data,
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const baseRemoveState = api.habitGroup.update.useMutation({
|
|
86
|
+
...remove,
|
|
87
|
+
onSuccess: (...args) => {
|
|
88
|
+
if (remove?.onSuccess) {
|
|
89
|
+
remove.onSuccess(...args)
|
|
90
|
+
}
|
|
91
|
+
onSuccess('删除成功')
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const removeState = {
|
|
96
|
+
...baseRemoveState as unknown as typeof baseRemoveState,
|
|
97
|
+
mutate: (id: string) => {
|
|
98
|
+
return baseRemoveState.mutate({
|
|
99
|
+
where: { id },
|
|
100
|
+
data: { isDeleted: true },
|
|
101
|
+
})
|
|
102
|
+
},
|
|
103
|
+
mutateAsync: (id: string) => {
|
|
104
|
+
return baseRemoveState.mutateAsync({
|
|
105
|
+
where: { id },
|
|
106
|
+
data: { isDeleted: true },
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
listState,
|
|
113
|
+
createState,
|
|
114
|
+
removeState,
|
|
115
|
+
updateState,
|
|
116
|
+
apiUtils,
|
|
117
|
+
}
|
|
118
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wzyjs/next",
|
|
3
|
+
"version": "0.2.40",
|
|
4
|
+
"description": "description",
|
|
5
|
+
"author": "wzy",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build:plugin": "tsc src/plugin/store/index.ts --outDir dist --skipLibCheck --module CommonJS",
|
|
9
|
+
"cp1": "cp src/prisma/Base.zmodel dist/Base.zmodel",
|
|
10
|
+
"cp2": "cp src/plugin/store/useHabitGroupCRUD.ts dist/useHabitGroupCRUD.ts",
|
|
11
|
+
"build": "npm run build:plugin && npm run cp1 && npm run cp2"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@zenstackhq/sdk": "^2.12.3"
|
|
18
|
+
},
|
|
19
|
+
"gitHead": "272513ecd0b28d577a444785b05f7a9ec962e79d",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
}
|
|
23
|
+
}
|