bknd
Version:
Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.
3 lines (2 loc) • 3.87 kB
JavaScript
import{syncTypes as P,syncConfig as O}from"bknd/plugins";import{ModuleManagerEvents as x}from"bknd";function k({enabled:r=!0,includeFirstBoot:c=!1,write:e}){let s=!0;return n=>({name:"bknd-sync-secrets",onBuilt:async()=>{if(!r)return;let t=n.modules;n.emgr.onEvent(x.ModuleManagerSecretsExtractedEvent,async({params:{secrets:a}})=>{await e?.(a)},{id:"sync-secrets"}),s&&c&&(s=!1,await e?.(t.extractSecrets().secrets))}})}import{$console as M}from"bknd/utils";function B(){try{return!0}catch{return!1}}async function y({app:r,...c},e){let s=typeof r=="function"?await r(e):r,n={...c,...s};typeof n.isProduction!="boolean"&&M.warn("You should set `isProduction` option when using managed modes to prevent accidental issues with writing plugins and syncing schema. As fallback, it is set to",B());let t=!1,{typesFilePath:a,configFilePath:f,writer:d,syncSecrets:o}=n,l=n.isProduction??B(),g=n?.options?.plugins??[],h=typeof n.syncSchema=="boolean"?n.syncSchema:!l,S=typeof n.syncSchema=="object"?n.syncSchema:{force:h,drop:h};if(!l){if(a){if(g.some(i=>i.name==="bknd-sync-types"))throw new Error("You have to unregister the `syncTypes` plugin");t=!0,g.push(P({enabled:!0,includeFirstBoot:!0,write:async i=>{try{await n.writer?.(a,i.toString())}catch(p){console.error(`Error writing types to"${a}"`,p)}}}))}if(f){if(g.some(i=>i.name==="bknd-sync-config"))throw new Error("You have to unregister the `syncConfig` plugin");t=!0,g.push(O({enabled:!0,includeFirstBoot:!0,write:async i=>{try{await d?.(f,JSON.stringify(i,null,2))}catch(p){console.error(`Error writing config to "${f}"`,p)}}}))}if(o&&o.enabled!==!1){if(g.some(m=>m.name==="bknd-sync-secrets"))throw new Error("You have to unregister the `syncSecrets` plugin");let i=o.outFile,p=o.format??"env";i||(i=["env",!o.includeSecrets&&"example",p].filter(Boolean).join(".")),t=!0,g.push(k({enabled:!0,includeFirstBoot:!0,write:async m=>{let w=Object.fromEntries(Object.entries(m).map(([u,b])=>[u,o.includeSecrets?b:""]));try{p==="env"?await d?.(i,Object.entries(w).map(([u,b])=>`${u}=${b}`).join(`
`)):await d?.(i,JSON.stringify(w,null,2))}catch(u){console.error(`Error writing secrets to "${i}"`,u)}}}))}}return t&&typeof n.writer!="function"&&M.warn("You must set a `writer` function, attempts to write will fail"),{config:n,isProd:l,plugins:g,syncSchemaOptions:S}}import{$console as A}from"bknd/utils";function _(r){return{...r,app:async c=>{let{config:e,plugins:s,isProd:n,syncSchemaOptions:t}=await y(r,c);return e?.options?.mode&&e?.options?.mode!=="code"&&A.warn("You should not set a different mode than `db` when using code mode"),{...e,options:{...e?.options,mode:"code",plugins:s,manager:{skipValidation:n,onModulesBuilt:async a=>{!n&&t.force&&(A.log("[code] syncing schema"),await a.em.schema().sync(t))},...e?.options?.manager}}}}}}import{getDefaultConfig as v}from"bknd";import{invariant as F,$console as C}from"bknd/utils";function W(r){return{...r,app:async c=>{let{config:e,isProd:s,plugins:n,syncSchemaOptions:t}=await y(r,c),a=e.configFilePath??"bknd-config.json";e?.options?.mode&&e?.options?.mode!=="db"&&C.warn("You should not set a different mode than `db` when using hybrid mode"),F(typeof e.reader=="function","You must set a `reader` option when using hybrid mode");let f=await e.reader?.(a),d=typeof f=="string"?JSON.parse(f):f;return d||(C.warn("No config found, using default config"),d=v(),await e.writer?.(a,JSON.stringify(d,null,2))),{...e,beforeBuild:async o=>{if(o&&!s){let l=o.modules;l.buildSyncConfig=t}await e.beforeBuild?.(o)},config:d,options:{...e?.options,mode:s?"code":"db",plugins:n,manager:{skipValidation:s,secrets:e.secrets,onModulesBuilt:async o=>{o.flags.sync_required&&!s&&t.force&&(C.log("[hybrid] syncing schema"),await o.em.schema().sync(t)),await e?.options?.manager?.onModulesBuilt?.(o)},...e?.options?.manager}}}}}}export{_ as code,W as hybrid,y as makeModeConfig};