Files
Web_BAI_Manage_ApiServer/script/pocketbase.ensure-cart-order-autogen-id.js
XuJiacheng ec6b59b4fa feat: 添加购物车相关迁移和索引功能
- 在 package.json 中添加迁移脚本 `migrate:cart-active-unique-index`。
- 修改 `pocketbase.cart-order.js` 文件,更新 `cart_id` 和 `cart_product_id` 字段的必填属性,并添加唯一索引 `idx_tbl_cart_owner_product_active_unique`。
- 在 `pocketbase.ensure-cart-order-autogen-id.js` 中,调整 `cart_id` 字段的必填属性为可选,并确保 `order_id` 字段为必填。
- 在 `pocketbase.product-list.js` 中,新增 `prod_list_barcode` 字段。
- 新增 `make-openapi-standalone.cjs` 脚本,用于处理 OpenAPI 文档。
- 新增 `pocketbase.cart-active-unique-index.js` 脚本,处理购物车的唯一索引和去重逻辑。
2026-04-09 14:49:12 +08:00

200 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createRequire } from 'module';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import PocketBase from 'pocketbase';
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let runtimeConfig = {};
try {
runtimeConfig = require('../pocket-base/bai_api_pb_hooks/bai_api_shared/config/runtime.js');
} catch (_error) {
runtimeConfig = {};
}
function readEnvFile(filePath) {
if (!fs.existsSync(filePath)) return {};
const content = fs.readFileSync(filePath, 'utf8');
const result = {};
for (const rawLine of content.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line || line.startsWith('#')) continue;
const index = line.indexOf('=');
if (index === -1) continue;
const key = line.slice(0, index).trim();
const value = line.slice(index + 1).trim();
result[key] = value;
}
return result;
}
const backendEnv = readEnvFile(path.resolve(__dirname, '..', 'back-end', '.env'));
const PB_URL = String(
process.env.PB_URL
|| backendEnv.POCKETBASE_API_URL
|| runtimeConfig.POCKETBASE_API_URL
|| 'http://127.0.0.1:8090'
).replace(/\/+$/, '');
const AUTH_TOKEN = process.env.POCKETBASE_AUTH_TOKEN || runtimeConfig.POCKETBASE_AUTH_TOKEN || '';
if (!AUTH_TOKEN) {
console.error('❌ 缺少 POCKETBASE_AUTH_TOKEN无法执行 cart/order 业务 ID 自动生成迁移。');
process.exit(1);
}
const pb = new PocketBase(PB_URL);
const TARGETS = [
{
collectionName: 'tbl_cart',
fieldName: 'cart_id',
autogeneratePattern: 'CART-[0-9]{13}-[A-Za-z0-9]{6}',
required: false,
},
{
collectionName: 'tbl_order',
fieldName: 'order_id',
autogeneratePattern: 'ORDER-[0-9]{13}-[A-Za-z0-9]{6}',
required: true,
},
];
function normalizeFieldPayload(field, overrides) {
const payload = Object.assign({}, field, overrides || {});
if (payload.type === 'number') {
if (Object.prototype.hasOwnProperty.call(payload, 'onlyInt')) payload.onlyInt = !!payload.onlyInt;
}
if (payload.type === 'autodate') {
payload.onCreate = typeof payload.onCreate === 'boolean' ? payload.onCreate : true;
payload.onUpdate = typeof payload.onUpdate === 'boolean' ? payload.onUpdate : false;
}
if (payload.type === 'file') {
payload.maxSelect = typeof payload.maxSelect === 'number' ? payload.maxSelect : 0;
payload.maxSize = typeof payload.maxSize === 'number' ? payload.maxSize : 0;
payload.mimeTypes = Array.isArray(payload.mimeTypes) ? payload.mimeTypes : null;
}
return payload;
}
function buildCollectionPayload(collection, fields) {
return {
name: collection.name,
type: collection.type,
listRule: collection.listRule,
viewRule: collection.viewRule,
createRule: collection.createRule,
updateRule: collection.updateRule,
deleteRule: collection.deleteRule,
fields,
indexes: collection.indexes || [],
};
}
async function ensureAutoGenerateField(target) {
const collection = await pb.collections.getOne(target.collectionName);
const fields = Array.isArray(collection.fields) ? collection.fields : [];
const existingField = fields.find((field) => field && field.name === target.fieldName);
if (!existingField) {
throw new Error(`${target.collectionName}.${target.fieldName} 不存在`);
}
const currentPattern = String(existingField.autogeneratePattern || '');
if (existingField.type === 'text' && currentPattern === target.autogeneratePattern) {
return {
collectionName: target.collectionName,
fieldName: target.fieldName,
action: 'skipped',
autogeneratePattern: currentPattern,
};
}
const nextFields = fields
.filter((field) => field && field.name !== 'id')
.map((field) => {
if (field.name !== target.fieldName) {
return normalizeFieldPayload(field);
}
return normalizeFieldPayload(field, {
type: 'text',
required: typeof target.required === 'boolean' ? target.required : true,
autogeneratePattern: target.autogeneratePattern,
});
});
await pb.collections.update(collection.id, buildCollectionPayload(collection, nextFields));
return {
collectionName: target.collectionName,
fieldName: target.fieldName,
action: 'updated',
autogeneratePattern: target.autogeneratePattern,
};
}
async function verifyTargets() {
const result = [];
for (const target of TARGETS) {
const collection = await pb.collections.getOne(target.collectionName);
const field = (collection.fields || []).find((item) => item && item.name === target.fieldName);
const pattern = String(field && field.autogeneratePattern || '');
const ok = !!field && field.type === 'text' && pattern === target.autogeneratePattern;
result.push({
collectionName: target.collectionName,
fieldName: target.fieldName,
type: field ? field.type : '',
required: !!(field && field.required),
autogeneratePattern: pattern,
ok,
});
}
const failed = result.filter((item) => !item.ok);
if (failed.length) {
throw new Error(`以下字段未正确启用自动生成: ${JSON.stringify(failed, null, 2)}`);
}
return result;
}
async function main() {
try {
console.log(`🔄 正在连接 PocketBase: ${PB_URL}`);
pb.authStore.save(AUTH_TOKEN, null);
console.log('✅ 已使用 POCKETBASE_AUTH_TOKEN 载入认证状态。');
const changed = [];
for (const target of TARGETS) {
changed.push(await ensureAutoGenerateField(target));
}
console.log('📝 业务 ID 自动生成处理结果:');
console.log(JSON.stringify(changed, null, 2));
const verified = await verifyTargets();
console.log('📝 校验结果:');
console.log(JSON.stringify(verified, null, 2));
console.log('🎉 cart_id / order_id 自动生成规则已就绪。');
} catch (error) {
console.error('❌ cart/order 业务 ID 自动生成迁移失败:', error.response?.data || error.message || error);
process.exitCode = 1;
}
}
main();