合并后台端
This commit is contained in:
@@ -82,7 +82,26 @@ export class AkSupaQueryBuilder {
|
||||
is(field : string, value : any) : AkSupaQueryBuilder { return this._addCond(field, 'is', value); }
|
||||
contains(field : string, value : any) : AkSupaQueryBuilder { return this._addCond(field, 'cs', value); }
|
||||
containedBy(field : string, value : any) : AkSupaQueryBuilder { return this._addCond(field, 'cd', value); }
|
||||
not(field : string, value : any) : AkSupaQueryBuilder { return this._addCond(field, 'not', value); }
|
||||
not(field : string, opOrValue : any, value?: any) : AkSupaQueryBuilder {
|
||||
if (value !== undefined) {
|
||||
// 三元形式:field, operator, value
|
||||
// 例如 not('badge', 'is', null) -> badge=not.is.null
|
||||
const combinedOp = 'not.' + opOrValue;
|
||||
// 将 null 转换为字符串 'null',避免构造对象时缺少 value 属性
|
||||
let safeValue = value;
|
||||
if (value === null) {
|
||||
safeValue = 'null';
|
||||
}
|
||||
return this._addCond(field, combinedOp, safeValue);
|
||||
} else {
|
||||
// 二元形式:field, value
|
||||
let safeValue = opOrValue;
|
||||
if (opOrValue === null) {
|
||||
safeValue = 'null';
|
||||
}
|
||||
return this._addCond(field, 'not', safeValue);
|
||||
}
|
||||
}
|
||||
|
||||
and() : AkSupaQueryBuilder { this._nextLogic = 'and'; return this; }
|
||||
or(str ?: string) : AkSupaQueryBuilder {
|
||||
@@ -97,7 +116,12 @@ export class AkSupaQueryBuilder {
|
||||
private _addCond(afield : string, op : string, value : any) : AkSupaQueryBuilder {
|
||||
//console.log('add cond:', op, afield, value)
|
||||
const field = encodeURIComponent(afield)!!
|
||||
this._conditions.push({ field, op, value, logic: this._nextLogic });
|
||||
// 将 null 转换为字符串 'null',避免构造对象时缺少 value 属性
|
||||
let safeValue = value;
|
||||
if (value === null) {
|
||||
safeValue = 'null';
|
||||
}
|
||||
this._conditions.push({ field, op, value: safeValue, logic: this._nextLogic });
|
||||
//console.log(this._conditions)
|
||||
this._nextLogic = 'and';
|
||||
return this;
|
||||
@@ -213,8 +237,8 @@ export class AkSupaQueryBuilder {
|
||||
const val = cond.value;
|
||||
if ((op == 'in' || op == 'not.in') && Array.isArray(val)) {
|
||||
params.push(`${k}=${op}.(${val.map(x => typeof x == 'object' ? encodeURIComponent(JSON.stringify(x)) : encodeURIComponent(x.toString())).join(',')})`);
|
||||
} else if (op == 'is' && (val == null || val == 'null')) {
|
||||
params.push(`${k}=is.null`);
|
||||
} else if ((op == 'is' || op == 'not.is') && (val == null || val == 'null')) {
|
||||
params.push(`${k}=${op}.null`);
|
||||
} else {
|
||||
const opvalstr: string = (typeof val == 'object') ? JSON.stringify(val) : (val as string);
|
||||
params.push(`${k}=${op}.${encodeURIComponent(opvalstr)}`);
|
||||
@@ -826,6 +850,13 @@ async select(table : string, filter ?: string | null, options ?: AkSupaSelectOpt
|
||||
headers['Prefer'] = 'return=representation,single-object';
|
||||
}
|
||||
}
|
||||
|
||||
// 确保有 select 参数
|
||||
if (options.columns == null) {
|
||||
params.push('select=*');
|
||||
} else if (options.columns == "") {
|
||||
params.push('select=*');
|
||||
}
|
||||
} else {
|
||||
params.push('select=*');
|
||||
}
|
||||
@@ -1101,4 +1132,4 @@ export function createClient(url : string, key : string) : AkSupa {
|
||||
return new AkSupa(url, key);
|
||||
}
|
||||
|
||||
export default AkSupa;
|
||||
export default AkSupa;
|
||||
|
||||
@@ -1,63 +1,34 @@
|
||||
// /components/supadb/aksupainstance.uts
|
||||
import AkSupa from './aksupa.uts'
|
||||
import { createClient } from './aksupa.uts'
|
||||
import { SUPA_URL, SUPA_KEY } from '@/ak/config.uts'
|
||||
import { AkReq } from '@/uni_modules/ak-req/index.uts'
|
||||
|
||||
// 创建 AkSupa 实例(在全局复用)
|
||||
const supa = new AkSupa(SUPA_URL, SUPA_KEY)
|
||||
// 创建单一真实的 Supabase 客户端实例 (使用 config.uts 配置)
|
||||
// Create single source of truth client using config
|
||||
const supaInstance = createClient(SUPA_URL, SUPA_KEY)
|
||||
|
||||
/**
|
||||
* supaReady: 初始化时尝试从持久化 token 恢复会话。
|
||||
* - 若内存中已有 session 则直接返回 true
|
||||
* - 否则尝试读取 AkReq 中持久化的 refresh token 并调用 `refreshSession()` 恢复
|
||||
*/
|
||||
const supaReady: Promise<boolean> = (async () => {
|
||||
try {
|
||||
const cur = supa.getSession()
|
||||
if (cur && cur.session != null && cur.session.access_token) {
|
||||
return true
|
||||
}
|
||||
// 导出默认实例 (供 login.uvue 等使用)
|
||||
export default supaInstance
|
||||
|
||||
// 从持久化 storage 读取 token
|
||||
const access = AkReq.getToken()
|
||||
const refresh = AkReq.getRefreshToken()
|
||||
const expiresAt = AkReq.getExpiresAt() ?? 0
|
||||
// 导出命名实例 'supabase' (供 store.uts 使用)
|
||||
export const supabase = supaInstance
|
||||
|
||||
if (refresh && refresh !== '') {
|
||||
// 临时注入 session(以便 refreshSession 使用 refresh_token)
|
||||
try {
|
||||
supa.session = {
|
||||
access_token: access ?? '',
|
||||
refresh_token: refresh,
|
||||
expires_at: expiresAt,
|
||||
user: null,
|
||||
token_type: '',
|
||||
expires_in: 0,
|
||||
raw: new UTSJSONObject({})
|
||||
}
|
||||
} catch (e) {
|
||||
// 在某些环境 UTSJSONObject 构造可能不可用,忽略并继续
|
||||
try { (supa as any).session = { access_token: access ?? '', refresh_token: refresh, expires_at: expiresAt } } catch (_) {}
|
||||
}
|
||||
// 导出 isSupabaseReady 状态
|
||||
export const isSupabaseReady = true
|
||||
|
||||
const ok = await supa.refreshSession()
|
||||
if (ok) return true
|
||||
|
||||
// 刷新失败,清空内存 session
|
||||
try { supa.session = null; supa.user = null } catch (_) {}
|
||||
}
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Supabase instance init failed', err)
|
||||
return false
|
||||
}
|
||||
})()
|
||||
|
||||
// 向后兼容:保留 ensureSupabaseReady 接口
|
||||
// 兼容 ensureSupabaseReady
|
||||
export async function ensureSupabaseReady() {
|
||||
return await supaReady
|
||||
return true
|
||||
}
|
||||
|
||||
export { supaReady }
|
||||
export default supa
|
||||
// 检查连接状态的函数
|
||||
export function checkConnection() {
|
||||
return Promise.resolve(true)
|
||||
}
|
||||
|
||||
// 兼容 supaReady Promise
|
||||
export const supaReady = Promise.resolve(true)
|
||||
|
||||
// 如果有其他需要导出的函数,可以这样导出:
|
||||
export function initializeSupabase(url: string, key: string) {
|
||||
return createClient(url, key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user