@lunora/seed 1.0.0-alpha.62 → 1.0.0-alpha.63
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/index.mjs +1 -1
- package/dist/packem_shared/{createSeedClient-BgekpXyy.mjs → createSeedClient-Ew_Dt82y.mjs} +1 -1
- package/dist/packem_shared/plan-DRy9bg5D.mjs +1 -0
- package/dist/packem_shared/seedPlan-3bhwPxyX.mjs +1 -0
- package/dist/testing.mjs +1 -1
- package/package.json +2 -2
- package/dist/packem_shared/plan-BJCg_8ro.mjs +0 -1
- package/dist/packem_shared/seedPlan-DH9xL-hi.mjs +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createSeedClient as o}from"./packem_shared/createSeedClient-
|
|
1
|
+
import{createSeedClient as o}from"./packem_shared/createSeedClient-Ew_Dt82y.mjs";import{V as a}from"./packem_shared/plan-DRy9bg5D.mjs";export{o as createSeedClient,a as seedPlan};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{l as
|
|
1
|
+
import{l as k,V as $,w as C}from"./plan-DRy9bg5D.mjs";const S=t=>Array.isArray(t)&&t.length===2&&typeof t[0]=="number"&&typeof t[1]=="number",A=(t,u,l,s)=>{const n=o=>typeof o=="number"?o:C.int([l,u,"count"],{max:o[1],min:o[0]});return t===void 0?{count:s}:typeof t=="number"?{count:t}:typeof t=="function"?{count:t(n)}:S(t)?{count:n(t)}:{count:t.length,partials:t}},M=(t,u,l)=>{const s={};if(t!==void 0){const n=new Set(t.flatMap(o=>Object.keys(o)));for(const o of n)s[o]=c=>t[c.index-l]?.[o]}for(const[n,o]of Object.entries(u??{}))s[n]=o;return s},R=(t,u={})=>{const{defaultCount:l=10,persist:s,seed:n=0}=u,o={},c={},a={},O=async(e,r,d)=>{k(n);const{count:f,partials:y}=A(r,e,n,l),m=a[e]??0,v=$(t,{counts:{[e]:f},existingIds:c,indexOffset:{[e]:m},only:[e],overrides:{[e]:M(y,d?.overrides,m)},seed:n});let h=[];for(const{rows:p,table:i}of v){const g=p.map(x=>x._id);o[i]??=[],o[i].push(...p),c[i]??=[],c[i].push(...g),a[i]=(a[i]??0)+p.length,i===e&&(h=g),s!==void 0&&await s(i,p)}return{[e]:h}};let b=Promise.resolve();const j=(e,r,d)=>{const f=b.then(()=>O(e,r,d));return b=f.then(()=>{},()=>{}),f},w={$ids:c,$reset:()=>{for(const e of Object.keys(o))delete o[e];for(const e of Object.keys(c))delete c[e];for(const e of Object.keys(a))delete a[e]},$store:o};return new Proxy(w,{get(e,r,d){return typeof r=="string"&&!r.startsWith("$")&&Object.hasOwn(t.tables,r)?(f,y)=>j(r,f,y):Reflect.get(e,r,d)}})};export{R as createSeedClient};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as $}from"@lunora/errors";import{faker as m}from"@faker-js/faker";import{optionalInner as C}from"@lunora/values";let x=0;const q=(e,n)=>e<n?-1:e>n?1:0,O=e=>e===void 0?"undefined":typeof e=="bigint"?`${e.toString()}n`:e===null||typeof e!="object"?JSON.stringify(e)??"null":Array.isArray(e)?`[${e.map(n=>O(n)).join(",")}]`:`{${Object.keys(e).toSorted(q).map(n=>`${JSON.stringify(n)}:${O(e[n])}`).join(",")}}`,V=(e,n)=>{let t=3735928559^n,o=1103547991^n;for(let a=0;a<e.length;a+=1){const i=e.codePointAt(a)??0;t=Math.imul(t^i,2654435761),o=Math.imul(o^i,1597334677)}return t=Math.imul(t^t>>>16,2246822507),t^=Math.imul(o^o>>>13,3266489909),o=Math.imul(o^o>>>16,2246822507),o^=Math.imul(t^t>>>13,3266489909),4294967296*(2097151&o)+(t>>>0)},G="\0",H=e=>{const n=typeof e=="string"?G+e:O(e);return V(n,x)%4294967296},K=e=>{if(typeof e=="number"){x=e>>>0;return}if(typeof e=="string"){x=V(e,0)%4294967296;return}let n=0;for(const t of e)n=Math.imul(n^t,2654435761)>>>0;x=n},d=(e,n)=>(m.seed(H(e)),n()),W=(e,n)=>{if(typeof n=="number")return n;const[t,o]=n;return d(["__count__",e],()=>m.number.int({max:o,min:t}))},X=/[a-z]/,Y=/[A-Z]/,ee=/\d/,u={bool(e){return d(e,()=>m.datatype.boolean())},city(e){return d(e,()=>m.location.city())},country(e){return d(e,()=>m.location.country())},dateString(e,n){const t=n?.min??new Date("1980-01-01T00:00:00.000Z"),o=n?.max??new Date("2020-01-01T00:00:00.000Z");return d(e,()=>m.date.between({from:t,to:o}).toISOString())},email(e){return d(e,()=>m.internet.email().toLowerCase())},firstName(e){return d(e,()=>m.person.firstName())},float(e,n){const{fractionDigits:t=2,max:o=1e3,min:a=0}=n??{};return d(e,()=>m.number.float({fractionDigits:t,max:o,min:a}))},fullName(e){return d(e,()=>m.person.fullName())},int(e,n){const{max:t=1e3,min:o=0}=n??{};return d(e,()=>m.number.int({max:t,min:o}))},lastName(e){return d(e,()=>m.person.lastName())},oneOf(e,n){if(n.length!==0)return d(e,()=>m.helpers.arrayElement(n))},paragraph(e){return d(e,()=>m.lorem.paragraph())},password(e){return d(e,()=>m.internet.password())},phoneNumber(e){return d(e,()=>m.phone.number())},scramble(e,n){const t=new Set(n?.preserve);return Array.from({length:e.length},(o,a)=>{const i=e.charAt(a);return t.has(i)?i:X.test(i)?d([e,a,"l"],()=>m.string.alpha({casing:"lower",length:1})):Y.test(i)?d([e,a,"u"],()=>m.string.alpha({casing:"upper",length:1})):ee.test(i)?d([e,a,"d"],()=>String(m.number.int({max:9,min:0}))):i}).join("")},sentence(e,n){return d(e,()=>m.lorem.sentence(n?{max:n.max??8,min:n.min??3}:void 0))},slug(e){return d(e,()=>m.lorem.slug())},streetAddress(e){return d(e,()=>m.location.streetAddress())},times(e,n,t){const o=W(e,n),a=[];for(let i=0;i<o;i+=1)a.push(t([e,i]));return a},url(e){return d(e,()=>m.internet.url())},username(e){return d(e,()=>m.internet.username())},uuid(e){return d(e,()=>m.string.uuid())},word(e){return d(e,()=>m.lorem.word())}},h=e=>e._meta??{},Z=e=>C(e)??e,z=e=>{const{column:n}=h(e);if(n?.defaultValue!==void 0||n?.defaultFn!==void 0)return!0;const t=C(e);return t===void 0?!1:z(t)},ne=(e,n)=>{const t=Z(n),o=h(t);return{fkTable:t.kind==="id"?o.tableName:void 0,hasServerDefault:z(n),kind:t.kind,name:e,nullable:o.column?.notNull===!1,optional:n.kind==="optional",validator:t}},te=e=>{const{tables:n}=e;return Object.entries(n).map(([t,o])=>({fields:Object.entries(o.shape).map(([a,i])=>ne(a,i)),name:t}))},re=(e,n)=>{const t=new Map(e.map(r=>[r.name,r])),o=r=>{const l=t.get(r);if(l===void 0)return new Set;const f=new Set;for(const g of l.fields)g.fkTable!==void 0&&g.fkTable!==r&&n.has(g.fkTable)&&f.add(g.fkTable);return f},a=[],i=new Set,s=[...n].filter(r=>t.has(r));for(;s.length>0;){const r=s.findIndex(g=>[...o(g)].every(w=>i.has(w))),l=r===-1?0:r,[f]=s.splice(l,1);if(f===void 0)break;a.push(f),i.add(f)}return a},oe=(e,n,t=new Set)=>{const o=new Map(e.map(s=>[s.name,s])),a=new Set(n),i=[...a];for(;i.length>0;){const s=i.pop();if(s===void 0)break;const r=o.get(s);if(r!==void 0&&!t.has(s))for(const l of r.fields)l.fkTable!==void 0&&l.fkTable!==s&&o.has(l.fkTable)&&!a.has(l.fkTable)&&(a.add(l.fkTable),i.push(l.fkTable))}return a},ae=e=>h(e).constraints??{},se=[{generate:e=>u.email(e),keywords:["email"]},{generate:e=>u.firstName(e),keywords:["firstname"]},{generate:e=>u.lastName(e),keywords:["lastname","surname"]},{generate:e=>u.username(e),keywords:["username"]},{generate:e=>u.fullName(e),keywords:["name"]},{generate:e=>u.sentence(e,{max:5,min:2}),keywords:["title"]},{generate:e=>u.url(e),keywords:["url","link","image","avatar"]},{generate:e=>u.phoneNumber(e),keywords:["phone"]},{generate:e=>u.paragraph(e),keywords:["description","bio","body","content","text"]},{generate:e=>u.slug(e),keywords:["slug","key","code"]},{generate:e=>u.city(e),keywords:["city"]},{generate:e=>u.country(e),keywords:["country"]},{generate:e=>u.streetAddress(e),keywords:["address","street"]},{generate:e=>u.password(e),keywords:["password","secret","token"]}],ie=new Set(["at","date","deadline","expires","expiry","since","timestamp","until"]),ue=/([a-z\d])([A-Z])/gu,le=/[\s_-]+/u,me=e=>e.replaceAll(ue,"$1 $2").split(le).filter(n=>n!=="").map(n=>n.toLowerCase()),de=4320*60*60*1e3,ce=e=>{const n=me(e).at(-1);return n!==void 0&&ie.has(n)},fe=(e,n)=>n-u.int(e,{max:de,min:0}),ge=(e,n,t)=>{const o=e.toLowerCase(),a=se.find(f=>f.keywords.some(g=>o.includes(g))),i=a===void 0?u.word(n):a.generate(n),{maxLength:s,minLength:r}=t;if(s!==void 0&&r!==void 0&&r>s)throw new $("INTERNAL",`Seed constraint error for field "${e}": minLength (${String(r)}) > maxLength (${String(s)}). Adjust the schema constraints.`);const l=s!==void 0&&i.length>s?i.slice(0,s):i;return r!==void 0&&l.length<r?l.padEnd(r,l.length>0?l:"x"):l},y=(e,n,t,o)=>{const a=Z(e),i=ae(a);if(i.enum!==void 0&&i.enum.length>0)return u.oneOf(t,i.enum);switch(a.kind){case"any":return u.word(t);case"array":{const s=h(a).inner;return s===void 0?[]:u.times(t,[1,3],r=>y(s,n,r,o))}case"bigint":return u.int(t,{max:1e6,min:0});case"boolean":return u.bool(t);case"bytes":return Array.from({length:8},(s,r)=>u.int([t,r],{max:255,min:0}));case"date":case"timestamp":return new Date(u.dateString(t)).getTime();case"from":throw new $("INTERNAL",`@lunora/seed: column "${n}" is a v.from() validator, whose external Standard Schema the seeder cannot introspect to invent a conforming value. Supply one via \`overrides\` (\`{ <table>: { ${n}: … } }\`), restrict the run with \`only\` so the table is skipped, or give the column a concrete v.* type.`);case"id":return u.uuid(t);case"literal":return h(a).value;case"null":return null;case"number":{const{maximum:s,minimum:r}=i;if(s===void 0&&r===void 0&&ce(n))return fe(t,o);if(s!==void 0&&r!==void 0&&r>s)throw new $("INTERNAL",`Seed constraint error for field "${n}": minimum (${String(r)}) > maximum (${String(s)}). Adjust the schema constraints.`);const l=r??0,f=s??1e3;return!Number.isInteger(l)||!Number.isInteger(f)?u.float(t,{max:f,min:l}):u.int(t,{max:f,min:l})}case"object":{const s=h(a).shape??{};return Object.fromEntries(Object.entries(s).map(([r,l])=>[r,y(l,r,[t,r],o)]))}case"record":{const{keyValidator:s,valueValidator:r}=h(a),l=u.times(t,[1,3],f=>{const g=s===void 0?u.word(["k",f]):String(y(s,n,["k",f],o)),w=r===void 0?u.word(["v",f]):y(r,n,["v",f],o);return[g,w]});return Object.fromEntries(l)}case"storage":return`seed/${u.uuid(t)}`;case"string":return ge(n,t,i);case"union":{const s=h(a).members??[],r=u.oneOf(t,s);return r===void 0?u.word(t):y(r,n,[t,"u"],o)}default:return u.word(t)}},pe=(e,n)=>typeof e=="function"?e(n):e,he=(e,n)=>{if(!e.optional)return e.nullable?null:u.uuid(n)},we=(e,n,t,o,a)=>{const i=e.fkTable,s=i===n?(o.get(n)??[]).slice(0,t):o.get(i)??[],r=a[i]??[];return r.length===0?s:[...s,...r]},R=(e,n,t,o)=>[e,n,t,o],be=(e,n,t,o,a,i,s)=>{if(e.fkTable!==void 0){const r=we(e,n,o,a,i);return r.length===0?he(e,t):u.oneOf(t,r)}if(!e.hasServerDefault)return y(e.validator,e.name,t,s)},Se=(e,n={})=>{const{counts:t={},defaultCount:o=10,existingIds:a={},indexOffset:i={},now:s=Date.now(),only:r,overrides:l={},seed:f=0}=n;K(f);const g=te(e);if(r!==void 0){const c=new Set(g.map(b=>b.name));for(const b of r)if(!c.has(b)){const k=g.map(N=>N.name).join(", ");throw new $("BAD_REQUEST",`unknown table "${b}" in seed \`only\` — schema defines: ${k||"(no tables)"}`)}}const w=new Set(r??g.map(c=>c.name)),F=new Set(Object.keys(a).filter(c=>!w.has(c)&&(a[c]??[]).length>0)),J=new Set([...oe(g,w,F)].filter(c=>w.has(c)||(a[c]??[]).length===0)),P=re(g,J),Q=new Map(g.map(c=>[c.name,c])),j=new Map,L={},M=[];for(const c of P){const b=Q.get(c);if(b===void 0)continue;const k=l[c]??{},N=t[c]??o,U=i[c]??0,_=[];j.set(c,_);const T=[];L[c]=T;for(let S=0;S<N;S+=1){const A=U+S,v={},E=(p,B)=>{if(Object.hasOwn(k,p)){const D=pe(k[p],{field:p,index:A,row:v,store:L,table:c});if(D!==void 0){v[p]=D;return}}const I=B();I!==void 0&&(v[p]=I)};E("_id",()=>u.uuid(R(f,c,A,"_id"))),_.push(v._id);for(const p of b.fields)E(p.name,()=>be(p,c,R(f,c,A,p.name),S,j,a,s));T.push(v)}M.push({rows:T,table:c})}return M};export{te as S,Se as V,K as l,u as w};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"@lunora/errors";import{V as a}from"./plan-DRy9bg5D.mjs";export{a as seedPlan};
|
package/dist/testing.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{V as y,S}from"./packem_shared/plan-
|
|
1
|
+
import{V as y,S}from"./packem_shared/plan-DRy9bg5D.mjs";const b=(f,r,a)=>{if(r.size===0&&a.size===0)return f;const n={...f};for(const s of r){const e=n[s];typeof e=="number"&&(n[s]=BigInt(e))}for(const s of a){const e=n[s];Array.isArray(e)&&(n[s]=Uint8Array.from(e).buffer)}return n},z=async(f,r,a={})=>{const n=y(r,a),s=S(r),e=new Map,m=new Map;for(const o of s){const i=new Set(o.fields.filter(t=>t.kind==="bigint").map(t=>t.name)),c=new Set(o.fields.filter(t=>t.kind==="bytes").map(t=>t.name));i.size>0&&e.set(o.name,i),c.size>0&&m.set(o.name,c)}const l={};return await f.run(async o=>{const i=o.db.insert;for(const{rows:c,table:t}of n){const p=[],w=e.get(t)??new Set,u=m.get(t)??new Set;for(const d of c)p.push(await i(t,b(d,w,u),{allowExplicitId:!0}));l[t]=p}}),l};export{z as seed};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/seed",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.63",
|
|
4
4
|
"description": "Schema-driven, deterministic database seeding for Lunora: realistic fake data from defineSchema",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@faker-js/faker": "^10.5.0",
|
|
51
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
51
|
+
"@lunora/errors": "1.0.0-alpha.15"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@lunora/server": ">=1.0.0-alpha.24 <2.0.0-0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as $}from"@lunora/errors";import{faker as u}from"@faker-js/faker";import{optionalInner as Z}from"@lunora/values";let x=0;const G=(e,t)=>e<t?-1:e>t?1:0,O=e=>e===void 0?"undefined":typeof e=="bigint"?`${e.toString()}n`:e===null||typeof e!="object"?JSON.stringify(e)??"null":Array.isArray(e)?`[${e.map(t=>O(t)).join(",")}]`:`{${Object.keys(e).toSorted(G).map(t=>`${JSON.stringify(t)}:${O(e[t])}`).join(",")}}`,z=(e,t)=>{let n=3735928559^t,r=1103547991^t;for(let a=0;a<e.length;a+=1){const s=e.codePointAt(a)??0;n=Math.imul(n^s,2654435761),r=Math.imul(r^s,1597334677)}return n=Math.imul(n^n>>>16,2246822507),n^=Math.imul(r^r>>>13,3266489909),r=Math.imul(r^r>>>16,2246822507),r^=Math.imul(n^n>>>13,3266489909),4294967296*(2097151&r)+(n>>>0)},H="\0",K=e=>{const t=typeof e=="string"?H+e:O(e);return z(t,x)%4294967296},W=e=>{if(typeof e=="number"){x=e>>>0;return}if(typeof e=="string"){x=z(e,0)%4294967296;return}let t=0;for(const n of e)t=Math.imul(t^n,2654435761)>>>0;x=t},m=(e,t)=>(u.seed(K(e)),t()),C=(e,t)=>{if(typeof t=="number")return t;const[n,r]=t;return m(["__count__",e],()=>u.number.int({max:r,min:n}))},X=e=>e.length===0?e:e.charAt(0).toUpperCase()+e.slice(1),Y=/[a-z]/,ee=/[A-Z]/,te=/\d/,l={bool(e){return m(e,()=>u.datatype.boolean())},char(e){return m(e,()=>u.string.alpha(1))},city(e){return m(e,()=>u.location.city())},country(e){return m(e,()=>u.location.country())},countryCode(e){return m(e,()=>u.location.countryCode())},dateString(e,t){const n=t?.min??new Date("1980-01-01T00:00:00.000Z"),r=t?.max??new Date("2020-01-01T00:00:00.000Z");return m(e,()=>u.date.between({from:n,to:r}).toISOString())},digit(e){return m(e,()=>String(u.number.int({max:9,min:0})))},email(e){return m(e,()=>u.internet.email().toLowerCase())},firstName(e){return m(e,()=>u.person.firstName())},float(e,t){const{fractionDigits:n=2,max:r=1e3,min:a=0}=t??{};return m(e,()=>u.number.float({fractionDigits:n,max:r,min:a}))},fullName(e){return m(e,()=>u.person.fullName())},hex(e){return m(e,()=>u.string.hexadecimal({casing:"lower",length:1,prefix:""}))},int(e,t){const{max:n=1e3,min:r=0}=t??{};return m(e,()=>u.number.int({max:n,min:r}))},ipv4(e){return m(e,()=>u.internet.ipv4())},lastName(e){return m(e,()=>u.person.lastName())},mac(e){return m(e,()=>u.internet.mac())},oneOf(e,t){if(t.length!==0)return m(e,()=>u.helpers.arrayElement(t))},paragraph(e){return m(e,()=>u.lorem.paragraph())},password(e){return m(e,()=>u.internet.password())},phoneNumber(e){return m(e,()=>u.phone.number())},scramble(e,t){const n=new Set(t?.preserve);return Array.from({length:e.length},(r,a)=>{const s=e.charAt(a);return n.has(s)?s:Y.test(s)?m([e,a,"l"],()=>u.string.alpha({casing:"lower",length:1})):ee.test(s)?m([e,a,"u"],()=>u.string.alpha({casing:"upper",length:1})):te.test(s)?m([e,a,"d"],()=>String(u.number.int({max:9,min:0}))):s}).join("")},sentence(e,t){return m(e,()=>u.lorem.sentence(t?{max:t.max??8,min:t.min??3}:void 0))},slug(e){return m(e,()=>u.lorem.slug())},someOf(e,t,n){const r=Math.min(C(["__some__",e],t),n.length);return m(e,()=>u.helpers.arrayElements(n,r))},streetAddress(e){return m(e,()=>u.location.streetAddress())},streetName(e){return m(e,()=>u.location.street())},timezone(e){return m(e,()=>u.location.timeZone())},times(e,t,n){const r=C(e,t),a=[];for(let s=0;s<r;s+=1)a.push(n([e,s]));return a},url(e){return m(e,()=>u.internet.url())},username(e){return m(e,()=>u.internet.username())},uuid(e){return m(e,()=>u.string.uuid())},word(e,t){const n=m(e,()=>u.lorem.word());return t?.capitalize===!0?X(n):n},words(e,t){return m(e,()=>u.lorem.words(t?{max:t.max??5,min:t.min??2}:void 0))}},h=e=>e._meta??{},V=e=>Z(e)??e,U=e=>{const{column:t}=h(e);if(t?.defaultValue!==void 0||t?.defaultFn!==void 0)return!0;const n=Z(e);return n===void 0?!1:U(n)},ne=(e,t)=>{const n=V(t),r=h(n);return{fkTable:n.kind==="id"?r.tableName:void 0,hasServerDefault:U(t),kind:n.kind,name:e,nullable:r.column?.notNull===!1,optional:t.kind==="optional",validator:n}},re=e=>{const{tables:t}=e;return Object.entries(t).map(([n,r])=>({fields:Object.entries(r.shape).map(([a,s])=>ne(a,s)),name:n}))},oe=(e,t)=>{const n=new Map(e.map(o=>[o.name,o])),r=o=>{const c=n.get(o);if(c===void 0)return new Set;const f=new Set;for(const g of c.fields)g.fkTable!==void 0&&g.fkTable!==o&&t.has(g.fkTable)&&f.add(g.fkTable);return f},a=[],s=new Set,i=[...t].filter(o=>n.has(o));for(;i.length>0;){const o=i.findIndex(g=>[...r(g)].every(w=>s.has(w))),c=o===-1?0:o,[f]=i.splice(c,1);if(f===void 0)break;a.push(f),s.add(f)}return a},ae=(e,t,n=new Set)=>{const r=new Map(e.map(i=>[i.name,i])),a=new Set(t),s=[...a];for(;s.length>0;){const i=s.pop();if(i===void 0)break;const o=r.get(i);if(o!==void 0&&!n.has(i))for(const c of o.fields)c.fkTable!==void 0&&c.fkTable!==i&&r.has(c.fkTable)&&!a.has(c.fkTable)&&(a.add(c.fkTable),s.push(c.fkTable))}return a},ie=e=>h(e).constraints??{},se=[{generate:e=>l.email(e),keywords:["email"]},{generate:e=>l.firstName(e),keywords:["firstname"]},{generate:e=>l.lastName(e),keywords:["lastname","surname"]},{generate:e=>l.username(e),keywords:["username"]},{generate:e=>l.fullName(e),keywords:["name"]},{generate:e=>l.sentence(e,{max:5,min:2}),keywords:["title"]},{generate:e=>l.url(e),keywords:["url","link","image","avatar"]},{generate:e=>l.phoneNumber(e),keywords:["phone"]},{generate:e=>l.paragraph(e),keywords:["description","bio","body","content","text"]},{generate:e=>l.slug(e),keywords:["slug","key","code"]},{generate:e=>l.city(e),keywords:["city"]},{generate:e=>l.country(e),keywords:["country"]},{generate:e=>l.streetAddress(e),keywords:["address","street"]},{generate:e=>l.password(e),keywords:["password","secret","token"]}],ue=new Set(["at","date","deadline","expires","expiry","since","timestamp","until"]),le=/([a-z\d])([A-Z])/gu,me=/[\s_-]+/u,ce=e=>e.replaceAll(le,"$1 $2").split(me).filter(t=>t!=="").map(t=>t.toLowerCase()),de=4320*60*60*1e3,fe=e=>{const t=ce(e).at(-1);return t!==void 0&&ue.has(t)},ge=(e,t)=>t-l.int(e,{max:de,min:0}),pe=(e,t,n)=>{const r=e.toLowerCase(),a=se.find(f=>f.keywords.some(g=>r.includes(g))),s=a===void 0?l.word(t):a.generate(t),{maxLength:i,minLength:o}=n;if(i!==void 0&&o!==void 0&&o>i)throw new $("INTERNAL",`Seed constraint error for field "${e}": minLength (${String(o)}) > maxLength (${String(i)}). Adjust the schema constraints.`);const c=i!==void 0&&s.length>i?s.slice(0,i):s;return o!==void 0&&c.length<o?c.padEnd(o,c.length>0?c:"x"):c},y=(e,t,n,r)=>{const a=V(e),s=ie(a);if(s.enum!==void 0&&s.enum.length>0)return l.oneOf(n,s.enum);switch(a.kind){case"any":return l.word(n);case"array":{const i=h(a).inner;return i===void 0?[]:l.times(n,[1,3],o=>y(i,t,o,r))}case"bigint":return l.int(n,{max:1e6,min:0});case"boolean":return l.bool(n);case"bytes":return Array.from({length:8},(i,o)=>l.int([n,o],{max:255,min:0}));case"date":case"timestamp":return new Date(l.dateString(n)).getTime();case"from":throw new $("INTERNAL",`@lunora/seed: column "${t}" is a v.from() validator, whose external Standard Schema the seeder cannot introspect to invent a conforming value. Supply one via \`overrides\` (\`{ <table>: { ${t}: … } }\`), restrict the run with \`only\` so the table is skipped, or give the column a concrete v.* type.`);case"id":return l.uuid(n);case"literal":return h(a).value;case"null":return null;case"number":{const{maximum:i,minimum:o}=s;if(i===void 0&&o===void 0&&fe(t))return ge(n,r);if(i!==void 0&&o!==void 0&&o>i)throw new $("INTERNAL",`Seed constraint error for field "${t}": minimum (${String(o)}) > maximum (${String(i)}). Adjust the schema constraints.`);const c=o??0,f=i??1e3;return!Number.isInteger(c)||!Number.isInteger(f)?l.float(n,{max:f,min:c}):l.int(n,{max:f,min:c})}case"object":{const i=h(a).shape??{};return Object.fromEntries(Object.entries(i).map(([o,c])=>[o,y(c,o,[n,o],r)]))}case"record":{const{keyValidator:i,valueValidator:o}=h(a),c=l.times(n,[1,3],f=>{const g=i===void 0?l.word(["k",f]):String(y(i,t,["k",f],r)),w=o===void 0?l.word(["v",f]):y(o,t,["v",f],r);return[g,w]});return Object.fromEntries(c)}case"storage":return`seed/${l.uuid(n)}`;case"string":return pe(t,n,s);case"union":{const i=h(a).members??[],o=l.oneOf(n,i);return o===void 0?l.word(n):y(o,t,[n,"u"],r)}default:return l.word(n)}},he=(e,t)=>typeof e=="function"?e(t):e,we=(e,t)=>{if(!e.optional)return e.nullable?null:l.uuid(t)},be=(e,t,n,r,a)=>{const s=e.fkTable,i=s===t?(r.get(t)??[]).slice(0,n):r.get(s)??[],o=a[s]??[];return o.length===0?i:[...i,...o]},R=(e,t,n,r)=>[e,t,n,r],ye=(e,t,n,r,a,s,i)=>{if(e.fkTable!==void 0){const o=be(e,t,r,a,s);return o.length===0?we(e,n):l.oneOf(n,o)}if(!e.hasServerDefault)return y(e.validator,e.name,n,i)},xe=(e,t={})=>{const{counts:n={},defaultCount:r=10,existingIds:a={},indexOffset:s={},now:i=Date.now(),only:o,overrides:c={},seed:f=0}=t;W(f);const g=re(e);if(o!==void 0){const d=new Set(g.map(b=>b.name));for(const b of o)if(!d.has(b)){const k=g.map(N=>N.name).join(", ");throw new $("BAD_REQUEST",`unknown table "${b}" in seed \`only\` — schema defines: ${k||"(no tables)"}`)}}const w=new Set(o??g.map(d=>d.name)),F=new Set(Object.keys(a).filter(d=>!w.has(d)&&(a[d]??[]).length>0)),J=new Set([...ae(g,w,F)].filter(d=>w.has(d)||(a[d]??[]).length===0)),P=oe(g,J),Q=new Map(g.map(d=>[d.name,d])),j=new Map,_={},M=[];for(const d of P){const b=Q.get(d);if(b===void 0)continue;const k=c[d]??{},N=n[d]??r,B=s[d]??0,L=[];j.set(d,L);const A=[];_[d]=A;for(let S=0;S<N;S+=1){const T=B+S,v={},E=(p,q)=>{if(Object.hasOwn(k,p)){const D=he(k[p],{field:p,index:T,row:v,store:_,table:d});if(D!==void 0){v[p]=D;return}}const I=q();I!==void 0&&(v[p]=I)};E("_id",()=>l.uuid(R(f,d,T,"_id"))),L.push(v._id);for(const p of b.fields)E(p.name,()=>ye(p,d,R(f,d,T,p.name),S,j,a,i));A.push(v)}M.push({rows:A,table:d})}return M};export{re as S,xe as V,l as k,W as l};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"@lunora/errors";import{V as a}from"./plan-BJCg_8ro.mjs";export{a as seedPlan};
|