admindb 2.1.0 → 2.1.1
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/README.md +1 -1
- package/dist/app.d.ts +1 -69
- package/dist/app.js +1 -257
- package/dist/auth/config.d.ts +1 -19
- package/dist/auth/config.js +1 -147
- package/dist/auth/crypto.d.ts +1 -27
- package/dist/auth/crypto.js +1 -133
- package/dist/auth/index.d.ts +1 -5
- package/dist/auth/index.js +1 -22
- package/dist/auth/middleware.d.ts +1 -14
- package/dist/auth/middleware.js +1 -100
- package/dist/auth/routes.d.ts +1 -7
- package/dist/auth/routes.js +1 -78
- package/dist/auth/types.d.ts +1 -35
- package/dist/auth/types.js +1 -11
- package/dist/cli/args.d.ts +1 -45
- package/dist/cli/args.js +1 -328
- package/dist/cli/config.d.ts +1 -56
- package/dist/cli/config.js +1 -133
- package/dist/cli/index.d.ts +1 -4
- package/dist/cli/index.js +1 -21
- package/dist/cli/runner.d.ts +1 -1
- package/dist/cli/runner.js +1 -222
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +1 -5
- package/dist/data/datasets.d.ts +1 -17
- package/dist/data/datasets.js +1 -133
- package/dist/data/detector.d.ts +1 -30
- package/dist/data/detector.js +1 -472
- package/dist/data/engine.d.ts +1 -10
- package/dist/data/engine.js +1 -211
- package/dist/data/index.d.ts +1 -5
- package/dist/data/index.js +1 -22
- package/dist/data/strategies.d.ts +1 -30
- package/dist/data/strategies.js +1 -343
- package/dist/data/types.d.ts +1 -73
- package/dist/data/types.js +1 -8
- package/dist/db/database.d.ts +1 -119
- package/dist/db/database.js +1 -471
- package/dist/db/export.d.ts +1 -12
- package/dist/db/export.js +1 -42
- package/dist/db/filters.d.ts +1 -22
- package/dist/db/filters.js +1 -158
- package/dist/db/index.d.ts +1 -8
- package/dist/db/index.js +1 -25
- package/dist/db/introspection.d.ts +1 -6
- package/dist/db/introspection.js +1 -172
- package/dist/db/manager.d.ts +1 -81
- package/dist/db/manager.js +1 -397
- package/dist/db/migrations.d.ts +1 -5
- package/dist/db/migrations.js +1 -110
- package/dist/db/postgres.d.ts +1 -85
- package/dist/db/postgres.js +1 -745
- package/dist/db/types.d.ts +1 -206
- package/dist/db/types.js +1 -7
- package/dist/index.d.ts +1 -23
- package/dist/index.js +1 -66
- package/dist/public/js/api.js +1 -47
- package/dist/public/js/app.js +1 -262
- package/dist/public/js/browse.js +1 -1371
- package/dist/public/js/databases.js +1 -235
- package/dist/public/js/designer.js +1 -258
- package/dist/public/js/forms.js +1 -826
- package/dist/public/js/icons.js +1 -51
- package/dist/public/js/inspector.js +1 -638
- package/dist/public/js/query.js +1 -322
- package/dist/public/js/schema.js +1 -447
- package/dist/public/js/seed.js +1 -1090
- package/dist/routes/api/helpers.d.ts +1 -42
- package/dist/routes/api/helpers.js +1 -133
- package/dist/routes/api/import-export.d.ts +1 -3
- package/dist/routes/api/import-export.js +1 -85
- package/dist/routes/api/index.d.ts +1 -4
- package/dist/routes/api/index.js +1 -37
- package/dist/routes/api/query.d.ts +1 -3
- package/dist/routes/api/query.js +1 -76
- package/dist/routes/api/rows.d.ts +1 -3
- package/dist/routes/api/rows.js +1 -373
- package/dist/routes/api/seed.d.ts +1 -3
- package/dist/routes/api/seed.js +1 -82
- package/dist/routes/api/tables.d.ts +1 -3
- package/dist/routes/api/tables.js +1 -160
- package/dist/routes/databases.d.ts +1 -26
- package/dist/routes/databases.js +1 -230
- package/dist/routes/index.d.ts +1 -3
- package/dist/routes/index.js +1 -20
- package/dist/routes/pages.d.ts +1 -9
- package/dist/routes/pages.js +1 -390
- package/dist/serverless.d.ts +1 -40
- package/dist/serverless.js +1 -210
- package/dist/sql/classifier.d.ts +1 -19
- package/dist/sql/classifier.js +1 -37
- package/dist/sql/generator.d.ts +1 -81
- package/dist/sql/generator.js +1 -348
- package/dist/sql/index.d.ts +1 -2
- package/dist/sql/index.js +1 -19
- package/dist/types/api.d.ts +1 -412
- package/dist/types/api.js +1 -9
- package/dist/utils/colors.d.ts +1 -40
- package/dist/utils/colors.js +1 -64
- package/dist/utils/common.d.ts +1 -50
- package/dist/utils/common.js +1 -229
- package/dist/utils/csv.d.ts +1 -15
- package/dist/utils/csv.js +1 -80
- package/dist/utils/datatype.d.ts +1 -49
- package/dist/utils/datatype.js +1 -222
- package/dist/utils/icons.d.ts +1 -11
- package/dist/utils/icons.js +1 -72
- package/dist/utils/index.d.ts +1 -4
- package/dist/utils/index.js +1 -21
- package/dist/utils/logger.d.ts +1 -10
- package/dist/utils/logger.js +1 -40
- package/dist/views/layouts/main.hbs +0 -4
- package/dist/views/pages/databases.hbs +0 -18
- package/dist/views/pages/designer.hbs +0 -10
- package/dist/views/pages/error.hbs +0 -2
- package/dist/views/pages/form.hbs +0 -5
- package/dist/views/pages/home.hbs +0 -8
- package/dist/views/pages/login.hbs +0 -13
- package/dist/views/pages/query.hbs +0 -9
- package/dist/views/pages/schema.hbs +0 -20
- package/dist/views/pages/seed.hbs +0 -31
- package/dist/views/pages/table.hbs +0 -14
- package/dist/views/partials/icon.hbs +0 -1
- package/dist/views/partials/navbar.hbs +0 -6
- package/dist/views/partials/sidebar.hbs +0 -8
- package/package.json +3 -2
- package/dist/app.js.map +0 -1
- package/dist/auth/config.js.map +0 -1
- package/dist/auth/crypto.js.map +0 -1
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/middleware.js.map +0 -1
- package/dist/auth/routes.js.map +0 -1
- package/dist/auth/types.js.map +0 -1
- package/dist/cli/args.js.map +0 -1
- package/dist/cli/config.js.map +0 -1
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/runner.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/data/datasets.js.map +0 -1
- package/dist/data/detector.js.map +0 -1
- package/dist/data/engine.js.map +0 -1
- package/dist/data/index.js.map +0 -1
- package/dist/data/strategies.js.map +0 -1
- package/dist/data/types.js.map +0 -1
- package/dist/db/database.js.map +0 -1
- package/dist/db/export.js.map +0 -1
- package/dist/db/filters.js.map +0 -1
- package/dist/db/index.js.map +0 -1
- package/dist/db/introspection.js.map +0 -1
- package/dist/db/manager.js.map +0 -1
- package/dist/db/migrations.js.map +0 -1
- package/dist/db/postgres.js.map +0 -1
- package/dist/db/types.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/public/css/input.css +0 -302
- package/dist/routes/api/helpers.js.map +0 -1
- package/dist/routes/api/import-export.js.map +0 -1
- package/dist/routes/api/index.js.map +0 -1
- package/dist/routes/api/query.js.map +0 -1
- package/dist/routes/api/rows.js.map +0 -1
- package/dist/routes/api/seed.js.map +0 -1
- package/dist/routes/api/tables.js.map +0 -1
- package/dist/routes/databases.js.map +0 -1
- package/dist/routes/index.js.map +0 -1
- package/dist/routes/pages.js.map +0 -1
- package/dist/serverless.js.map +0 -1
- package/dist/sql/classifier.js.map +0 -1
- package/dist/sql/generator.js.map +0 -1
- package/dist/sql/index.js.map +0 -1
- package/dist/types/api.js.map +0 -1
- package/dist/utils/colors.js.map +0 -1
- package/dist/utils/common.js.map +0 -1
- package/dist/utils/csv.js.map +0 -1
- package/dist/utils/datatype.js.map +0 -1
- package/dist/utils/icons.js.map +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/logger.js.map +0 -1
package/dist/data/engine.js
CHANGED
|
@@ -1,211 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateRows = generateRows;
|
|
4
|
-
exports.buildSeedInsertSql = buildSeedInsertSql;
|
|
5
|
-
const generator_1 = require("../sql/generator");
|
|
6
|
-
const types_1 = require("./types");
|
|
7
|
-
const detector_1 = require("./detector");
|
|
8
|
-
const strategies_1 = require("./strategies");
|
|
9
|
-
/** Distinct non-null values of a foreign key's referenced column. */
|
|
10
|
-
async function sampleFkValues(db, fk) {
|
|
11
|
-
if (!fk)
|
|
12
|
-
return [];
|
|
13
|
-
try {
|
|
14
|
-
const ref = await db.getTableInfo(fk.table);
|
|
15
|
-
if (!ref.success || !ref.data || !ref.data.columns.length)
|
|
16
|
-
return [];
|
|
17
|
-
const refCol = fk.to || ref.data.primaryKey[0] || ref.data.columns[0].name;
|
|
18
|
-
if (!refCol)
|
|
19
|
-
return [];
|
|
20
|
-
const r = await db.all(`SELECT DISTINCT ${(0, generator_1.quoteIdentifier)(refCol)} AS v FROM ${(0, generator_1.quoteIdentifier)(fk.table)} WHERE ${(0, generator_1.quoteIdentifier)(refCol)} IS NOT NULL LIMIT 1000`);
|
|
21
|
-
return (r.data ?? []).map((row) => row.v).filter((v) => v != null);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
return [];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function stringifyKey(v) {
|
|
28
|
-
if (v instanceof Uint8Array || Buffer.isBuffer(v))
|
|
29
|
-
return `buf:${Buffer.from(v).toString('hex')}`;
|
|
30
|
-
return `${typeof v}:${String(v)}`;
|
|
31
|
-
}
|
|
32
|
-
function uniqueSuffix(base, seen) {
|
|
33
|
-
if (typeof base === 'number' && Number.isFinite(base)) {
|
|
34
|
-
let n = Number(base);
|
|
35
|
-
while (seen.has(stringifyKey(n)))
|
|
36
|
-
n += 1;
|
|
37
|
-
return n;
|
|
38
|
-
}
|
|
39
|
-
let i = 1;
|
|
40
|
-
let cand = `${String(base)}-${i}`;
|
|
41
|
-
while (seen.has(stringifyKey(cand))) {
|
|
42
|
-
i += 1;
|
|
43
|
-
cand = `${String(base)}-${i}`;
|
|
44
|
-
}
|
|
45
|
-
return cand;
|
|
46
|
-
}
|
|
47
|
-
function orderedValue(value, base, dir, orEqual) {
|
|
48
|
-
const v = value;
|
|
49
|
-
const b = base;
|
|
50
|
-
if (typeof v === 'string' && typeof b === 'string') {
|
|
51
|
-
const tv = Date.parse(v);
|
|
52
|
-
const tb = Date.parse(b);
|
|
53
|
-
if (Number.isFinite(tv) && Number.isFinite(tb)) {
|
|
54
|
-
if (dir === 'after') {
|
|
55
|
-
if (orEqual ? tv < tb : tv <= tb)
|
|
56
|
-
return new Date(tb + 1000).toISOString();
|
|
57
|
-
}
|
|
58
|
-
else if (orEqual ? tv > tb : tv >= tb) {
|
|
59
|
-
return new Date(tb - 1000).toISOString();
|
|
60
|
-
}
|
|
61
|
-
return v;
|
|
62
|
-
}
|
|
63
|
-
if (/^\d{4}-\d{2}-\d{2}$/.test(v) && /^\d{4}-\d{2}-\d{2}$/.test(b)) {
|
|
64
|
-
const dv = Date.parse(`${v}T00:00:00Z`);
|
|
65
|
-
const db = Date.parse(`${b}T00:00:00Z`);
|
|
66
|
-
if (dir === 'after') {
|
|
67
|
-
if (orEqual ? dv < db : dv <= db)
|
|
68
|
-
return new Date(db + 86400_000).toISOString().slice(0, 10);
|
|
69
|
-
}
|
|
70
|
-
else if (orEqual ? dv > db : dv >= db) {
|
|
71
|
-
return new Date(db - 86400_000).toISOString().slice(0, 10);
|
|
72
|
-
}
|
|
73
|
-
return v;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (typeof v === 'number' && typeof b === 'number') {
|
|
77
|
-
if (dir === 'after')
|
|
78
|
-
return orEqual ? Math.max(v, b) : Math.max(v, b + 1);
|
|
79
|
-
return orEqual ? Math.min(v, b) : Math.min(v, b - 1);
|
|
80
|
-
}
|
|
81
|
-
return v;
|
|
82
|
-
}
|
|
83
|
-
function applyOrdering(fields, ordering) {
|
|
84
|
-
if (!ordering.size)
|
|
85
|
-
return;
|
|
86
|
-
const byCol = new Map(fields.map((f) => [f.column, f]));
|
|
87
|
-
for (const f of fields) {
|
|
88
|
-
const rel = ordering.get(f.column);
|
|
89
|
-
if (!rel)
|
|
90
|
-
continue;
|
|
91
|
-
if (rel.after && f.value != null) {
|
|
92
|
-
const other = byCol.get(rel.after.column);
|
|
93
|
-
if (other && other.value != null)
|
|
94
|
-
f.value = orderedValue(f.value, other.value, 'after', rel.after.orEqual);
|
|
95
|
-
}
|
|
96
|
-
if (rel.before && f.value != null) {
|
|
97
|
-
const other = byCol.get(rel.before.column);
|
|
98
|
-
if (other && other.value != null)
|
|
99
|
-
f.value = orderedValue(f.value, other.value, 'before', rel.before.orEqual);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Generate `count` rows for a table with structured previews.
|
|
105
|
-
*/
|
|
106
|
-
async function generateRows(db, info, configs, count, plan = {}) {
|
|
107
|
-
const fkByColumn = new Map();
|
|
108
|
-
for (const fk of info.foreignKeys)
|
|
109
|
-
fkByColumn.set(fk.from, fk);
|
|
110
|
-
const uniqueCols = new Set();
|
|
111
|
-
for (const cfg of configs)
|
|
112
|
-
if (cfg.unique)
|
|
113
|
-
uniqueCols.add(cfg.name);
|
|
114
|
-
const checkInfo = (0, detector_1.parseChecks)(info.sql ?? null);
|
|
115
|
-
const warnings = [];
|
|
116
|
-
const prepared = [];
|
|
117
|
-
const fkPools = new Map();
|
|
118
|
-
const fkOmit = new Set();
|
|
119
|
-
for (const col of info.columns) {
|
|
120
|
-
const cfg = configs.find((c) => c.name === col.name);
|
|
121
|
-
const raw = plan[col.name];
|
|
122
|
-
const p = raw && raw.strategy ? raw : (cfg?.defaultPlan ?? { strategy: 'skip' });
|
|
123
|
-
if (p.strategy === 'list' && (!p.values || !p.values.length)) {
|
|
124
|
-
const allowed = (0, detector_1.findInMap)(checkInfo.columnAllowed, col.name);
|
|
125
|
-
if (allowed && allowed.length) {
|
|
126
|
-
p.values = [...allowed];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (!cfg || !cfg.strategies.some((s) => s.id === p.strategy)) {
|
|
130
|
-
throw new Error(`Unsupported generator strategy "${p.strategy}" for column "${col.name}".`);
|
|
131
|
-
}
|
|
132
|
-
if (p.strategy === 'list' && !(p.values ?? []).some((v) => String(v).trim() !== '')) {
|
|
133
|
-
warnings.push(`Column "${col.name}": the list is empty, so it will be skipped (DB default applies).`);
|
|
134
|
-
}
|
|
135
|
-
if ((p.strategy === 'skip' || p.strategy === 'null') && !col.pk && col.notnull && col.dflt_value == null) {
|
|
136
|
-
warnings.push(`Column "${col.name}" is NOT NULL without a default — "${p.strategy}" rows may fail to insert.`);
|
|
137
|
-
}
|
|
138
|
-
if (p.strategy === 'fk') {
|
|
139
|
-
const pool = await sampleFkValues(db, fkByColumn.get(col.name) ?? null);
|
|
140
|
-
fkPools.set(col.name, pool);
|
|
141
|
-
const refTable = fkByColumn.get(col.name)?.table ?? '?';
|
|
142
|
-
if (!pool.length) {
|
|
143
|
-
if (col.notnull && !col.pk) {
|
|
144
|
-
fkOmit.add(col.name);
|
|
145
|
-
warnings.push(`Column "${col.name}" is a NOT NULL foreign key to "${refTable}", which has no rows. It is omitted from generated rows — seed "${refTable}" first so the insert can reference real values.`);
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
warnings.push(`Column "${col.name}": "${refTable}" is empty, so NULL will be inserted.`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
prepared.push({ col, plan: p });
|
|
153
|
-
}
|
|
154
|
-
const rows = [];
|
|
155
|
-
const previewRows = [];
|
|
156
|
-
const uniqueSeen = new Map();
|
|
157
|
-
for (let i = 0; i < count; i++) {
|
|
158
|
-
const fields = [];
|
|
159
|
-
const previewRow = {};
|
|
160
|
-
for (const { col, plan: p } of prepared) {
|
|
161
|
-
if (p.strategy === 'fk' && fkOmit.has(col.name)) {
|
|
162
|
-
previewRow[col.name] = null;
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
let v = (0, strategies_1.generateOne)(col, p, fkPools.get(col.name), i);
|
|
166
|
-
if (v === types_1.SKIP) {
|
|
167
|
-
previewRow[col.name] = '(default)';
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (uniqueCols.has(col.name)) {
|
|
171
|
-
const seen = uniqueSeen.get(col.name) ?? new Set();
|
|
172
|
-
let attempts = 0;
|
|
173
|
-
while (seen.has(stringifyKey(v)) && attempts < 20) {
|
|
174
|
-
v = (0, strategies_1.generateOne)(col, p, fkPools.get(col.name), i);
|
|
175
|
-
attempts += 1;
|
|
176
|
-
}
|
|
177
|
-
if (seen.has(stringifyKey(v))) {
|
|
178
|
-
warnings.push(`Column "${col.name}" is UNIQUE but the generator kept colliding — appended a suffix to keep rows insertable.`);
|
|
179
|
-
v = uniqueSuffix(v, seen);
|
|
180
|
-
}
|
|
181
|
-
seen.add(stringifyKey(v));
|
|
182
|
-
uniqueSeen.set(col.name, seen);
|
|
183
|
-
}
|
|
184
|
-
fields.push({ column: col.name, value: v });
|
|
185
|
-
previewRow[col.name] = v;
|
|
186
|
-
}
|
|
187
|
-
// Honor CHECK ordering constraints
|
|
188
|
-
applyOrdering(fields, checkInfo.ordering);
|
|
189
|
-
for (const f of fields) {
|
|
190
|
-
previewRow[f.column] = f.value;
|
|
191
|
-
}
|
|
192
|
-
rows.push(fields);
|
|
193
|
-
if (previewRows.length < 50) {
|
|
194
|
-
previewRows.push(previewRow);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return { rows, previewRows, warnings };
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Render generated rows as a sequence of INSERT statements for preview.
|
|
201
|
-
*/
|
|
202
|
-
function buildSeedInsertSql(table, rows) {
|
|
203
|
-
return rows
|
|
204
|
-
.map((fields) => {
|
|
205
|
-
const cols = fields.map((f) => (0, generator_1.quoteIdentifier)(f.column));
|
|
206
|
-
const vals = fields.map((f) => (0, generator_1.sqlValue)(f.value));
|
|
207
|
-
return `INSERT INTO ${(0, generator_1.quoteIdentifier)(table)} (${cols.join(', ')}) VALUES (${vals.join(', ')});`;
|
|
208
|
-
})
|
|
209
|
-
.join('\n');
|
|
210
|
-
}
|
|
211
|
-
//# sourceMappingURL=engine.js.map
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.generateRows=async function(db,info,configs,count,plan={}){const fkByColumn=new Map;for(const fk of info.foreignKeys)fkByColumn.set(fk.from,fk);const uniqueCols=new Set;for(const cfg of configs)cfg.unique&&uniqueCols.add(cfg.name);const checkInfo=(0,detector_1.parseChecks)(info.sql??null),warnings=[],prepared=[],fkPools=new Map,fkOmit=new Set;for(const col of info.columns){const cfg=configs.find(c=>c.name===col.name),raw=plan[col.name],p=raw&&raw.strategy?raw:cfg?.defaultPlan??{strategy:"skip"};if(!("list"!==p.strategy||p.values&&p.values.length)){const allowed=(0,detector_1.findInMap)(checkInfo.columnAllowed,col.name);allowed&&allowed.length&&(p.values=[...allowed])}if(!cfg||!cfg.strategies.some(s=>s.id===p.strategy))throw new Error(`Unsupported generator strategy "${p.strategy}" for column "${col.name}".`);if("list"!==p.strategy||(p.values??[]).some(v=>""!==String(v).trim())||warnings.push(`Column "${col.name}": the list is empty, so it will be skipped (DB default applies).`),"skip"!==p.strategy&&"null"!==p.strategy||col.pk||!col.notnull||null!=col.dflt_value||warnings.push(`Column "${col.name}" is NOT NULL without a default — "${p.strategy}" rows may fail to insert.`),"fk"===p.strategy){const pool=await sampleFkValues(db,fkByColumn.get(col.name)??null);fkPools.set(col.name,pool);const refTable=fkByColumn.get(col.name)?.table??"?";pool.length||(col.notnull&&!col.pk?(fkOmit.add(col.name),warnings.push(`Column "${col.name}" is a NOT NULL foreign key to "${refTable}", which has no rows. It is omitted from generated rows — seed "${refTable}" first so the insert can reference real values.`)):warnings.push(`Column "${col.name}": "${refTable}" is empty, so NULL will be inserted.`))}prepared.push({col:col,plan:p})}const rows=[],previewRows=[],uniqueSeen=new Map;for(let i=0;i<count;i++){const fields=[],previewRow={};for(const{col:col,plan:p}of prepared){if("fk"===p.strategy&&fkOmit.has(col.name)){previewRow[col.name]=null;continue}let v=(0,strategies_1.generateOne)(col,p,fkPools.get(col.name),i);if(v!==types_1.SKIP){if(uniqueCols.has(col.name)){const seen=uniqueSeen.get(col.name)??new Set;let attempts=0;for(;seen.has(stringifyKey(v))&&attempts<20;)v=(0,strategies_1.generateOne)(col,p,fkPools.get(col.name),i),attempts+=1;seen.has(stringifyKey(v))&&(warnings.push(`Column "${col.name}" is UNIQUE but the generator kept colliding — appended a suffix to keep rows insertable.`),v=uniqueSuffix(v,seen)),seen.add(stringifyKey(v)),uniqueSeen.set(col.name,seen)}fields.push({column:col.name,value:v}),previewRow[col.name]=v}else previewRow[col.name]="(default)"}applyOrdering(fields,checkInfo.ordering);for(const f of fields)previewRow[f.column]=f.value;rows.push(fields),previewRows.length<50&&previewRows.push(previewRow)}return{rows:rows,previewRows:previewRows,warnings:warnings}},exports.buildSeedInsertSql=function(table,rows){return rows.map(fields=>{const cols=fields.map(f=>(0,generator_1.quoteIdentifier)(f.column)),vals=fields.map(f=>(0,generator_1.sqlValue)(f.value));return`INSERT INTO ${(0,generator_1.quoteIdentifier)(table)} (${cols.join(", ")}) VALUES (${vals.join(", ")});`}).join("\n")};const generator_1=require("../sql/generator"),types_1=require("./types"),detector_1=require("./detector"),strategies_1=require("./strategies");async function sampleFkValues(db,fk){if(!fk)return[];try{const ref=await db.getTableInfo(fk.table);if(!ref.success||!ref.data||!ref.data.columns.length)return[];const refCol=fk.to||ref.data.primaryKey[0]||ref.data.columns[0].name;if(!refCol)return[];return((await db.all(`SELECT DISTINCT ${(0,generator_1.quoteIdentifier)(refCol)} AS v FROM ${(0,generator_1.quoteIdentifier)(fk.table)} WHERE ${(0,generator_1.quoteIdentifier)(refCol)} IS NOT NULL LIMIT 1000`)).data??[]).map(row=>row.v).filter(v=>null!=v)}catch{return[]}}function stringifyKey(v){return v instanceof Uint8Array||Buffer.isBuffer(v)?`buf:${Buffer.from(v).toString("hex")}`:`${typeof v}:${String(v)}`}function uniqueSuffix(base,seen){if("number"==typeof base&&Number.isFinite(base)){let n=Number(base);for(;seen.has(stringifyKey(n));)n+=1;return n}let i=1,cand=`${String(base)}-${i}`;for(;seen.has(stringifyKey(cand));)i+=1,cand=`${String(base)}-${i}`;return cand}function orderedValue(value,base,dir,orEqual){const v=value,b=base;if("string"==typeof v&&"string"==typeof b){const tv=Date.parse(v),tb=Date.parse(b);if(Number.isFinite(tv)&&Number.isFinite(tb)){if("after"===dir){if(orEqual?tv<tb:tv<=tb)return new Date(tb+1e3).toISOString()}else if(orEqual?tv>tb:tv>=tb)return new Date(tb-1e3).toISOString();return v}if(/^\d{4}-\d{2}-\d{2}$/.test(v)&&/^\d{4}-\d{2}-\d{2}$/.test(b)){const dv=Date.parse(`${v}T00:00:00Z`),db=Date.parse(`${b}T00:00:00Z`);if("after"===dir){if(orEqual?dv<db:dv<=db)return new Date(db+864e5).toISOString().slice(0,10)}else if(orEqual?dv>db:dv>=db)return new Date(db-864e5).toISOString().slice(0,10);return v}}return"number"==typeof v&&"number"==typeof b?"after"===dir?orEqual?Math.max(v,b):Math.max(v,b+1):orEqual?Math.min(v,b):Math.min(v,b-1):v}function applyOrdering(fields,ordering){if(!ordering.size)return;const byCol=new Map(fields.map(f=>[f.column,f]));for(const f of fields){const rel=ordering.get(f.column);if(rel){if(rel.after&&null!=f.value){const other=byCol.get(rel.after.column);other&&null!=other.value&&(f.value=orderedValue(f.value,other.value,"after",rel.after.orEqual))}if(rel.before&&null!=f.value){const other=byCol.get(rel.before.column);other&&null!=other.value&&(f.value=orderedValue(f.value,other.value,"before",rel.before.orEqual))}}}}
|
package/dist/data/index.d.ts
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
export * from './types';
|
|
2
|
-
export * from './datasets';
|
|
3
|
-
export * from './strategies';
|
|
4
|
-
export * from './detector';
|
|
5
|
-
export * from './engine';
|
|
1
|
+
export * from './types';export * from './datasets';export * from './strategies';export * from './detector';export * from './engine';
|
package/dist/data/index.js
CHANGED
|
@@ -1,22 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
18
|
-
__exportStar(require("./datasets"), exports);
|
|
19
|
-
__exportStar(require("./strategies"), exports);
|
|
20
|
-
__exportStar(require("./detector"), exports);
|
|
21
|
-
__exportStar(require("./engine"), exports);
|
|
22
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var desc=Object.getOwnPropertyDescriptor(m,k);desc&&!("get"in desc?!m.__esModule:desc.writable||desc.configurable)||(desc={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,desc)}:function(o,m,k,k2){void 0===k2&&(k2=k),o[k2]=m[k]}),__exportStar=this&&this.__exportStar||function(m,exports){for(var p in m)"default"===p||Object.prototype.hasOwnProperty.call(exports,p)||__createBinding(exports,m,p)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./types"),exports),__exportStar(require("./datasets"),exports),__exportStar(require("./strategies"),exports),__exportStar(require("./detector"),exports),__exportStar(require("./engine"),exports);
|
|
@@ -1,30 +1 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type ColumnPlan, type StrategyDescriptor } from './types';
|
|
3
|
-
export declare const randInt: (min: number, max: number) => number;
|
|
4
|
-
export declare const randFloat: (min: number, max: number) => number;
|
|
5
|
-
export declare const pick: <T>(arr: T[]) => T;
|
|
6
|
-
export declare const makeSlug: () => string;
|
|
7
|
-
export declare const makeEmail: () => string;
|
|
8
|
-
export declare const makeUsername: () => string;
|
|
9
|
-
export declare const makePhone: () => string;
|
|
10
|
-
export declare const makeAddress: () => string;
|
|
11
|
-
export declare const makeIp: () => string;
|
|
12
|
-
export declare function makeMacAddress(): string;
|
|
13
|
-
export declare const makeCreditCard: () => string;
|
|
14
|
-
export declare const makeAvatar: (seedVal?: string) => string;
|
|
15
|
-
export declare const makeColor: (format?: string) => string;
|
|
16
|
-
export declare function makeUuid(): string;
|
|
17
|
-
export declare function makeWords(minLen: number, maxLen: number): string;
|
|
18
|
-
export declare function makeSentences(minLen: number, maxLen: number): string;
|
|
19
|
-
export declare const makeParagraph: () => string;
|
|
20
|
-
export declare function formatPattern(pattern: string): string;
|
|
21
|
-
export declare function randomDate(from?: string, to?: string, kind?: 'date' | 'datetime'): string;
|
|
22
|
-
export declare const randomTime: () => string;
|
|
23
|
-
export declare function randomUnixTimestamp(format?: string, from?: string, to?: string): number;
|
|
24
|
-
export declare function randomAlnum(minLen: number, maxLen: number): string;
|
|
25
|
-
export declare function makeJsonObject(keys?: string[], jsonValues?: Record<string, string[]>): string;
|
|
26
|
-
export declare function isTextishType(type: string): boolean;
|
|
27
|
-
export declare function strategiesFor(col: ColumnInfo, fk: ForeignKeyInfo | null): StrategyDescriptor[];
|
|
28
|
-
export declare function fallbackPlanFor(col: ColumnInfo): ColumnPlan;
|
|
29
|
-
export declare function generateOne(col: ColumnInfo, p: ColumnPlan, fkPool?: unknown[], rowIndex?: number): unknown;
|
|
30
|
-
export declare function generateSampleValue(col: ColumnInfo, plan: ColumnPlan, fkPool?: unknown[]): unknown;
|
|
1
|
+
import type {ColumnInfo,ForeignKeyInfo} from '../db/index';import{type ColumnPlan,type StrategyDescriptor} from './types';export declare const randInt:(min:number,max:number)=>number;export declare const randFloat:(min:number,max:number)=>number;export declare const pick:<T>(arr:T[])=>T;export declare const makeSlug:()=>string;export declare const makeEmail:()=>string;export declare const makeUsername:()=>string;export declare const makePhone:()=>string;export declare const makeAddress:()=>string;export declare const makeIp:()=>string;export declare function makeMacAddress():string;export declare const makeCreditCard:()=>string;export declare const makeAvatar:(seedVal?:string)=>string;export declare const makeColor:(format?:string)=>string;export declare function makeUuid():string;export declare function makeWords(minLen:number,maxLen:number):string;export declare function makeSentences(minLen:number,maxLen:number):string;export declare const makeParagraph:()=>string;export declare function formatPattern(pattern:string):string;export declare function randomDate( from ?:string,to?:string,kind?:'date'|'datetime'):string;export declare const randomTime:()=>string;export declare function randomUnixTimestamp(format?:string, from ?:string,to?:string):number;export declare function randomAlnum(minLen:number,maxLen:number):string;export declare function makeJsonObject(keys?:string[],jsonValues?:Record<string,string[]>):string;export declare function isTextishType(type:string):boolean;export declare function strategiesFor(col:ColumnInfo,fk:ForeignKeyInfo|null):StrategyDescriptor[];export declare function fallbackPlanFor(col:ColumnInfo):ColumnPlan;export declare function generateOne(col:ColumnInfo,p:ColumnPlan,fkPool?:unknown[],rowIndex?:number):unknown;export declare function generateSampleValue(col:ColumnInfo,plan:ColumnPlan,fkPool?:unknown[]):unknown;
|