openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 4.32 kB
JavaScript
import{u as e}from"./openclaw-state-read-sqlite-post-commit-CsXPYVOo.mjs";import{_ as t,a as n,b as r,g as i,h as a,m as o,p as s,v as c,x as l,y as u}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";function getJsonObjectArgs(e,n){let i=[];for(let{selection:a}of e.selections??[])if(t.is(a)&&c.is(a.column))i.push(colName(a.column.column.name),colRef(n,a.column.column.name));else if(c.is(a))i.push(colName(a.column.name),colRef(n,a.column.name));else if(r.is(a)&&l.is(a.alias))i.push(colName(a.alias.name),colRef(n,a.alias.name));else throw Error(`can't extract column names from the select query node`);return i}function colName(e){return new a(i.createImmediate(e))}function colRef(e,n){return new a(t.create(c.create(n),u.create(e)))}function jsonArrayFrom$1(e){return s`(select coalesce(json_group_array(json_object(${s.join(getSqliteJsonObjectArgs(e.toOperationNode(),`agg`))})), '[]') from ${e} as agg)`}function getSqliteJsonObjectArgs(e,t){try{return getJsonObjectArgs(e,t)}catch{throw Error(`SQLite jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery.`)}}function jsonArrayFrom(t,n,r){if(!e(t))return jsonArrayFrom$1(n);let i=o(),a=r.length?s`order by ${s.join(r.map(e=>s`${i.ref(`agg.${e}`)} asc nulls first`))}`:s``;return s`(select coalesce(json_agg(agg ${a}), '[]')::text from ${n} as agg)`}function jsonArrayValue(t,n){return s`${s.raw(e(t)?`json_build_array`:`json_array`)}(${s.join(n)})`}function jsonObjectValue(t,n){return s`${s.raw(e(t)?`json_build_object`:`json_object`)}(${s.join(Object.entries(n).flatMap(([e,t])=>[s.lit(e),t]))})`}function jsonObjectTextEntries(t,r){let i=e(t);return n(t).fn(i?`json_each_text`:`json_each`,[i?s`${r}::json`:s.val(r)])}function jsonArrayTextElement(t,r,i){return e(t)?n(t).fn(`json_extract_path_text`,[s`${r}::json`,s.val(String(i))]):n(t).fn(`json_extract`,[r,s.val(`$[${i}]`)])}function jsonKeyPath(e){return`$.${/^[A-Za-z_][A-Za-z0-9_]*$/u.test(e)?e:JSON.stringify(e)}`}function jsonExtractText(t,n,r){let i=o();return e(t)?i.fn(`json_extract_path_text`,[s`${n}::json`,i.val(r)]):i.fn(`json_extract`,[n,i.val(jsonKeyPath(r))])}function jsonOrderedTextArray(t,n,r){return e(t)?s`coalesce(array_to_json(array_agg(${n} order by ${r}))::text, '[]')`:o().fn(`json_group_array`,[n])}function jsonRemoveKey(t,n,r){return e(t)?s`(${n}::jsonb - ${r}::text)::text`:o().fn(`json_remove`,[n,o().val(jsonKeyPath(r))])}function objectPath(e){return s`string_to_array(substr(${e}, 3), '.')`}function jsonPathValue(t,n,r,i=`text`){if(!e(t))return s`json_extract(${n}, ${r})`;let a=s`(${n}::jsonb ${s.raw(i===`json`?`#>`:`#>>`)} ${objectPath(r)})`;return s`${a}${s.raw(i===`number`?`::double precision`:i===`json`?`::text`:``)}`}function jsonValid(t,n){return e(t)?s`pg_input_is_valid(${n}, 'json')`:s`json_valid(${n})`}function jsonType(t,n){return e(t)?s`jsonb_typeof(${n}::jsonb)`:s`json_type(${n})`}function jsonRemovePaths(t,n,r){if(!e(t))return s`json_remove(${n}, ${s.join(r)})`;let i=s`${n}::jsonb`;for(let e of r)i=s`(${i} #- ${objectPath(e)})`;return s`${i}::text`}function jsonSetPath(t,n,r,i,a=!1){if(!e(t))return s`json_set(${n}, ${r}, ${i})`;let o=a?s`${i}::jsonb`:s`to_jsonb(${i}::text)`;return s`jsonb_set(${n}::jsonb, ${objectPath(r)}, coalesce(${o}, 'null'::jsonb))::text`}function jsonPickObject(t,n,r){return e(t)?s`(SELECT coalesce(jsonb_object_agg(key, value), '{}'::jsonb)::text
FROM jsonb_each(CASE WHEN jsonb_typeof(${n}::jsonb) = 'object' THEN ${n}::jsonb ELSE '{}'::jsonb END)
WHERE key IN (${s.join(r)}))`:s`(SELECT json_group_object(key, CASE type
WHEN 'object' THEN json(value) WHEN 'array' THEN json(value)
WHEN 'true' THEN json('true') WHEN 'false' THEN json('false')
ELSE value END) FROM json_each(${n}) WHERE key IN (${s.join(r)}))`}function jsonNativeFieldsValid(t,n){return e(t)?s`coalesce(pg_input_is_valid(${n}, 'jsonb'), false)`:s`coalesce(json_valid(${n}), 0)`}export{jsonNativeFieldsValid as a,jsonOrderedTextArray as c,jsonRemoveKey as d,jsonRemovePaths as f,jsonValid as h,jsonExtractText as i,jsonPathValue as l,jsonType as m,jsonArrayTextElement as n,jsonObjectTextEntries as o,jsonSetPath as p,jsonArrayValue as r,jsonObjectValue as s,jsonArrayFrom as t,jsonPickObject as u};