( casing: Casing, out: string, breakpoints: boolean, credentials: MysqlCredentials, tablesFilter: string[], prefix: Prefix, )
| 305 | }; |
| 306 | |
| 307 | export const introspectMysql = async ( |
| 308 | casing: Casing, |
| 309 | out: string, |
| 310 | breakpoints: boolean, |
| 311 | credentials: MysqlCredentials, |
| 312 | tablesFilter: string[], |
| 313 | prefix: Prefix, |
| 314 | ) => { |
| 315 | const { connectToMySQL } = await import('../connections'); |
| 316 | const { db, database } = await connectToMySQL(credentials); |
| 317 | |
| 318 | const matchers = tablesFilter.map((it) => { |
| 319 | return new Minimatch(it); |
| 320 | }); |
| 321 | |
| 322 | const filter = (tableName: string) => { |
| 323 | if (matchers.length === 0) return true; |
| 324 | |
| 325 | let flags: boolean[] = []; |
| 326 | |
| 327 | for (let matcher of matchers) { |
| 328 | if (matcher.negate) { |
| 329 | if (!matcher.match(tableName)) { |
| 330 | flags.push(false); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (matcher.match(tableName)) { |
| 335 | flags.push(true); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (flags.length > 0) { |
| 340 | return flags.every(Boolean); |
| 341 | } |
| 342 | return false; |
| 343 | }; |
| 344 | |
| 345 | const progress = new IntrospectProgress(); |
| 346 | const res = await renderWithTask( |
| 347 | progress, |
| 348 | fromMysqlDatabase(db, database, filter, (stage, count, status) => { |
| 349 | progress.update(stage, count, status); |
| 350 | }), |
| 351 | ); |
| 352 | |
| 353 | const schema = { id: originUUID, prevId: '', ...res } as MySqlSchema; |
| 354 | const ts = mysqlSchemaToTypeScript(schema, casing); |
| 355 | const relationsTs = relationsToTypeScript(schema, casing); |
| 356 | const { internal, ...schemaWithoutInternals } = schema; |
| 357 | |
| 358 | const schemaFile = join(out, 'schema.ts'); |
| 359 | writeFileSync(schemaFile, ts.file); |
| 360 | const relationsFile = join(out, 'relations.ts'); |
| 361 | writeFileSync(relationsFile, relationsTs.file); |
| 362 | console.log(); |
| 363 | |
| 364 | const { snapshots, journal } = prepareOutFolder(out, 'mysql'); |
no test coverage detected