@drizzle-team/studio
Advanced tools
Comparing version 0.0.17 to 0.0.18
137
index.d.ts
@@ -1,132 +0,5 @@ | ||
import { Relations, SQL, TablesRelationalConfig } from "drizzle-orm"; | ||
import { AnyMySqlTable, MySqlDatabase } from "drizzle-orm/mysql-core"; | ||
import { AnyPgTable, PgDatabase } from "drizzle-orm/pg-core"; | ||
import { AnySQLiteTable, BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"; | ||
export type FilterOp = [string, string, any]; | ||
export type Filter = { | ||
and?: Filter[]; | ||
or?: Filter[]; | ||
op?: FilterOp; | ||
}; | ||
export declare function filtersToSQL(filters: Filter): SQL | undefined; | ||
export type Setup = { | ||
type: "sqlite"; | ||
db: BaseSQLiteDatabase<"async" | "sync", any, Record<string, never>>; | ||
schema: Record<string, AnySQLiteTable | Relations>; | ||
} | { | ||
type: "mysql"; | ||
db: MySqlDatabase<any, any>; | ||
schema: Record<string, AnyMySqlTable | Relations>; | ||
} | { | ||
type: "pg"; | ||
db: PgDatabase<any, any>; | ||
schema: Record<string, AnyPgTable | Relations>; | ||
}; | ||
export declare const queryEngineForSetup: (setup: Setup) => Promise<{ | ||
query: (params: { | ||
table: string; | ||
columns: string[] | undefined; | ||
where: Filter | undefined; | ||
orderBy: { | ||
column: string; | ||
type: "ASC" | "DESC"; | ||
} | undefined; | ||
limit: number; | ||
offset: number; | ||
}) => Promise<{ | ||
[x: string]: any; | ||
}[]>; | ||
delete: (params: { | ||
table: string; | ||
where: Filter; | ||
}) => Promise<any>; | ||
insert: (params: { | ||
table: string; | ||
data: Record<string, any>; | ||
}) => Promise<any>; | ||
update: (params: { | ||
table: string; | ||
data: Record<string, any>; | ||
where: Filter; | ||
}) => Promise<any>; | ||
dropTable: (params: { | ||
table: string; | ||
isCascade?: boolean; | ||
}) => Promise<any>; | ||
truncateTable: (params: { | ||
table: string; | ||
isCascade?: boolean; | ||
}) => Promise<any>; | ||
tables: (options?: { | ||
withoutCount?: boolean; | ||
}) => Promise<{ | ||
name: string; | ||
columns: { | ||
name: string; | ||
type: string; | ||
_type: string; | ||
}[]; | ||
count: number | undefined; | ||
primaryKeys: string[]; | ||
}[]>; | ||
tablesConfig: { | ||
tables: TablesRelationalConfig; | ||
tableNamesMap: Record<string, string>; | ||
}; | ||
} | { | ||
query: (params: { | ||
table: string; | ||
columns: string[] | undefined; | ||
where: Filter | undefined; | ||
orderBy: { | ||
column: string; | ||
type: "ASC" | "DESC"; | ||
} | undefined; | ||
limit: number; | ||
offset: number; | ||
}) => any; | ||
delete: (params: { | ||
table: string; | ||
where: Filter; | ||
}) => any; | ||
insert: (params: { | ||
table: string; | ||
data: Record<string, any>; | ||
}) => any; | ||
update: (params: { | ||
table: string; | ||
data: Record<string, any>; | ||
where: Filter; | ||
}) => any; | ||
dropTable: (params: { | ||
table: string; | ||
isCascade?: boolean; | ||
}) => Promise<any>; | ||
tables: (options?: { | ||
withoutCount?: boolean; | ||
}) => Promise<{ | ||
name: string; | ||
columns: { | ||
name: string; | ||
type: string; | ||
_type: string; | ||
}[]; | ||
count: number | undefined; | ||
primaryKeys: string[]; | ||
}[]>; | ||
tablesConfig: { | ||
tables: TablesRelationalConfig; | ||
tableNamesMap: Record<string, string>; | ||
}; | ||
truncateTable?: undefined; | ||
}>; | ||
export declare const extractRelations: (tablesConfig: { | ||
tables: TablesRelationalConfig; | ||
tableNamesMap: Record<string, string>; | ||
}) => { | ||
name: string; | ||
table: string; | ||
colmns: string[]; | ||
refTable: string; | ||
refColumns: string[]; | ||
}[]; | ||
import { SQLiteSetup } from "./sqlite"; | ||
import { PgSetup } from "./pg"; | ||
import { MySqlSetup } from "./mysql"; | ||
export type Setup = SQLiteSetup | MySqlSetup | PgSetup; | ||
export declare const queryEngineForSetup: (setup: Setup) => Promise<import("./common").QueryEngine>; |
988
index.js
@@ -10,7 +10,7 @@ "use strict"; | ||
}; | ||
var __copyProps = (to, from, except, desc2) => { | ||
var __copyProps = (to, from, except, desc4) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc2 = __getOwnPropDesc(from, key)) || desc2.enumerable }); | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc4 = __getOwnPropDesc(from, key)) || desc4.enumerable }); | ||
} | ||
@@ -24,26 +24,12 @@ return to; | ||
__export(src_exports, { | ||
extractRelations: () => extractRelations, | ||
filtersToSQL: () => filtersToSQL, | ||
queryEngineForSetup: () => queryEngineForSetup | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/sqlite.ts | ||
var import_drizzle_orm2 = require("drizzle-orm"); | ||
var import_sqlite_core = require("drizzle-orm/sqlite-core"); | ||
// src/common.ts | ||
var import_drizzle_orm = require("drizzle-orm"); | ||
var import_mysql_core = require("drizzle-orm/mysql-core"); | ||
var import_pg_core = require("drizzle-orm/pg-core"); | ||
var import_sqlite_core = require("drizzle-orm/sqlite-core"); | ||
function assertSQLiteTable(value) { | ||
if (!(0, import_drizzle_orm.is)(value, import_sqlite_core.SQLiteTable)) { | ||
throw new Error(); | ||
} | ||
} | ||
function assertPgTable(value) { | ||
if (!(0, import_drizzle_orm.is)(value, import_pg_core.PgTable)) { | ||
throw new Error(); | ||
} | ||
} | ||
function assertMysqlTable(value) { | ||
if (!(0, import_drizzle_orm.is)(value, import_mysql_core.MySqlTable)) { | ||
throw new Error(); | ||
} | ||
} | ||
function filtersToSQL(filters) { | ||
@@ -73,4 +59,44 @@ return Object.entries(filters).reduce( | ||
} | ||
var convertToTsColumns = (data, columnNamesMap) => { | ||
return Object.fromEntries( | ||
Object.entries(data).map((it) => { | ||
const { name: tsName, mapper } = columnNamesMap[it[0]]; | ||
if (!tsName) | ||
throw new Error(`No column name '${it[0]}'`); | ||
return [tsName, mapper(it[1])]; | ||
}) | ||
); | ||
}; | ||
var extractRelations = (tablesConfig) => { | ||
const rels = Object.values(tablesConfig.tables).map( | ||
(it) => Object.entries(it.relations).map((it1) => { | ||
const normalized = (0, import_drizzle_orm.normalizeRelation)( | ||
tablesConfig.tables, | ||
tablesConfig.tableNamesMap, | ||
it1[1] | ||
); | ||
const rel = it1[1]; | ||
const refTable = rel.referencedTableName; | ||
const fields = normalized.fields.map((it2) => it2.name).flat(); | ||
const refColumns = normalized.references.map((it2) => it2.name).flat(); | ||
return { | ||
name: it1[0], | ||
table: it.dbName, | ||
colmns: fields, | ||
refTable, | ||
refColumns | ||
}; | ||
}) | ||
).flat(); | ||
return rels; | ||
}; | ||
// src/sqlite.ts | ||
function assertSQLiteTable(value) { | ||
if (!(0, import_drizzle_orm2.is)(value, import_sqlite_core.SQLiteTable)) { | ||
throw new Error(); | ||
} | ||
} | ||
var columnMapper = (column) => { | ||
if ((0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteTimestamp)) { | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteTimestamp)) { | ||
return (input) => { | ||
@@ -82,38 +108,27 @@ if (input === void 0 || input === null) | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_pg_core.PgTimestamp) || (0, import_drizzle_orm.is)(column, import_pg_core.PgDate)) { | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return new Date(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_mysql_core.MySqlTimestamp) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlDate)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return new Date(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSmallInt) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSmallSerial) || (0, import_drizzle_orm.is)(column, import_pg_core.PgDoublePrecision) || (0, import_drizzle_orm.is)(column, import_pg_core.PgReal) || (0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteInteger) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlTinyInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlSmallInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlMediumInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlFloat) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlDouble)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return Number(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_pg_core.PgBigInt53) || (0, import_drizzle_orm.is)(column, import_pg_core.PgBigSerial53)) { | ||
return (input) => Number(input); | ||
return (input) => input; | ||
}; | ||
var typeForTable = (column) => { | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteBoolean)) { | ||
return "boolean"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_pg_core.PgBigInt64) || (0, import_drizzle_orm.is)(column, import_pg_core.PgBigSerial64)) { | ||
return (input) => BigInt(input); | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger)) { | ||
return "number"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_mysql_core.MySqlBigInt53)) { | ||
return (input) => Number(input); | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteTimestamp)) { | ||
return "date"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_mysql_core.MySqlBigInt64)) { | ||
return (input) => BigInt(input); | ||
if ((0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteBlobJson) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteTextJson)) { | ||
return "json"; | ||
} | ||
return (input) => input; | ||
return "string"; | ||
}; | ||
var queryForSqliteAsync = (db, tablesMap) => (params) => { | ||
var queryFn = (db, tablesMap) => async (params) => { | ||
const table = tablesMap[params.table]; | ||
@@ -133,5 +148,5 @@ const config = (0, import_sqlite_core.getTableConfig)(table); | ||
const orderByColumn = params.orderBy ? columns[params.orderBy.column] : void 0; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm.asc)(orderByColumn)] : [(0, import_drizzle_orm.desc)(orderByColumn)] : []; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm2.asc)(orderByColumn)] : [(0, import_drizzle_orm2.desc)(orderByColumn)] : []; | ||
const q = hasPartial ? db.select(partialSelect) : db.select(); | ||
return q.from(table).where(params.where ? filtersToSQL(params.where) : void 0).orderBy(...orderByExpr).limit(params.limit).offset(params.offset).all(); | ||
return await q.from(table).where(params.where ? filtersToSQL(params.where) : void 0).orderBy(...orderByExpr).limit(params.limit).offset(params.offset); | ||
}; | ||
@@ -160,3 +175,9 @@ var dropRelationTablesForSqliteAsync = async (db, tableName, tablesMap, relations) => { | ||
}; | ||
var dropTableForSqliteAsync = (db, tablesMap, tablesConfig) => async (params) => { | ||
var insertFn = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.insert(table).values(data).run(); | ||
}; | ||
var dropTableFn = (db, tablesMap, tablesConfig) => async (params) => { | ||
const { isCascade, table } = params; | ||
@@ -171,71 +192,159 @@ const sqliteTable = tablesMap[params.table]; | ||
} | ||
return await db.run(import_drizzle_orm.sql`DROP TABLE IF EXISTS ${sqliteTable}`); | ||
return await db.run(import_drizzle_orm2.sql`DROP TABLE IF EXISTS ${sqliteTable}`); | ||
}; | ||
var queryForSqliteSync = (db, tablesMap) => (params) => { | ||
var deleteFn = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const config = (0, import_sqlite_core.getTableConfig)(table); | ||
const hasPartial = (params.columns && params.columns.length > 0) ?? false; | ||
const columns = (config.columns || []).reduce((acc, it) => { | ||
acc[it.name] = it; | ||
return acc; | ||
}, {}); | ||
const partialSelect = (config.columns || []).reduce((acc, it) => { | ||
if (params.columns?.includes(it.name)) { | ||
acc[it.name] = it; | ||
} | ||
return acc; | ||
}, {}); | ||
const orderByColumn = params.orderBy ? columns[params.orderBy.column] : void 0; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm.asc)(orderByColumn)] : [(0, import_drizzle_orm.desc)(orderByColumn)] : []; | ||
const q = hasPartial ? db.select(partialSelect) : db.select(); | ||
return Promise.resolve( | ||
q.from(table).where(params.where ? filtersToSQL(params.where) : void 0).orderBy(...orderByExpr).limit(params.limit).offset(params.offset).all() | ||
); | ||
return db.delete(table).where(filtersToSQL(params.where)).run(); | ||
}; | ||
var dropRelationTablesForSqliteSync = (db, tableName, tablesMap, relations) => { | ||
const filteredRelations = relations.filter( | ||
(relation) => relation.table === tableName | ||
var updateFn = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.update(table).set(data).where(filtersToSQL(params.where)).run(); | ||
}; | ||
var sqliteQueryEngine = async (setup) => { | ||
const { db } = setup; | ||
const schemaToTables = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm2.is)(it, import_sqlite_core.SQLiteTable)) { | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
acc[config.name] = it; | ||
} | ||
return acc; | ||
}, {}); | ||
return [key, mapped]; | ||
}) | ||
); | ||
const relationTables = filteredRelations.map((relation) => relation.refTable); | ||
for (const table of relationTables) { | ||
dropRelationTablesForSqliteSync(db, table, tablesMap, filteredRelations); | ||
const sqliteTable = tablesMap[table]; | ||
if (!sqliteTable) { | ||
throw new Error(`Table ${table} not found`); | ||
const schmaToTablesToColumnNames = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm2.is)(it, import_sqlite_core.SQLiteTable)) { | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm2.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
return [key, mapped]; | ||
}) | ||
); | ||
const tablesFn = async (schema, options) => { | ||
return Promise.all( | ||
Object.values(setup.schema[schema]).filter((it) => { | ||
return (0, import_drizzle_orm2.is)(it, import_sqlite_core.SQLiteTable); | ||
}).map(async (it) => { | ||
assertSQLiteTable(it); | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => { | ||
return { | ||
name: it2.name, | ||
type: typeForTable(it2), | ||
_type: it2.getSQLType() | ||
}; | ||
}); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = schemaToTables[schema][tableName]; | ||
const result = await setup.db.select({ | ||
count: import_drizzle_orm2.sql`count(*)` | ||
}).from(table).all(); | ||
count = result[0].count; | ||
} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
); | ||
}; | ||
const schemasToTableConfig = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const config = (0, import_drizzle_orm2.extractTablesRelationalConfig)( | ||
val, | ||
import_drizzle_orm2.createTableRelationsHelpers | ||
); | ||
return [key, config]; | ||
}) | ||
); | ||
return { | ||
query: (schema) => queryFn(db, schemaToTables[schema]), | ||
insert: (schema) => insertFn(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
update: (schema) => updateFn(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
delete: (schema) => deleteFn(db, schemaToTables[schema]), | ||
dropTable: (schema) => dropTableFn(db, schemaToTables[schema], schemasToTableConfig[schema]), | ||
truncate: (_) => { | ||
throw new Error("Truncate is not available in SQLite"); | ||
}, | ||
tables: tablesFn, | ||
tablesConfig: (schema) => { | ||
return schemasToTableConfig[schema]; | ||
} | ||
try { | ||
db.delete(sqliteTable).run(); | ||
} catch (error) { | ||
} | ||
} | ||
}; | ||
}; | ||
var dropTableForSqliteSync = (db, tablesMap, tablesConfig) => (params) => { | ||
const { isCascade, table } = params; | ||
const sqliteTable = tablesMap[params.table]; | ||
if (!sqliteTable) { | ||
throw new Error(`Table ${params.table} not found`); | ||
// src/pg.ts | ||
var import_pg_core = require("drizzle-orm/pg-core"); | ||
var import_drizzle_orm3 = require("drizzle-orm"); | ||
function assertPgTable(value) { | ||
if (!(0, import_drizzle_orm3.is)(value, import_pg_core.PgTable)) { | ||
throw new Error(); | ||
} | ||
if (isCascade) { | ||
const relations = extractRelations(tablesConfig); | ||
dropRelationTablesForSqliteSync(db, table, tablesMap, relations); | ||
} | ||
var columnMapper2 = (column) => { | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgTimestamp) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgDate)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return new Date(input); | ||
}; | ||
} | ||
return Promise.resolve(db.run(import_drizzle_orm.sql`DROP TABLE IF EXISTS ${sqliteTable}`)); | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSmallInt) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSmallSerial) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgDoublePrecision) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgReal)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return Number(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgBigInt53) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgBigSerial53)) { | ||
return (input) => Number(input); | ||
} | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgBigInt64) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgBigSerial64)) { | ||
return (input) => BigInt(input); | ||
} | ||
return (input) => input; | ||
}; | ||
var truncateTableForSqliteSync = (db, tablesMap, tablesConfig) => (params) => { | ||
const { isCascade, table } = params; | ||
const sqliteTable = tablesMap[params.table]; | ||
if (!sqliteTable) { | ||
throw new Error(`Table ${params.table} not found`); | ||
var typeForTable2 = (column) => { | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgBoolean)) { | ||
return "boolean"; | ||
} | ||
if (isCascade) { | ||
const relations = extractRelations(tablesConfig); | ||
dropRelationTablesForSqliteSync(db, table, tablesMap, relations); | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgBigInt53) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgBigSerial53) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgReal) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSmallInt) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgSmallSerial) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgDoublePrecision)) { | ||
return "number"; | ||
} | ||
return Promise.resolve( | ||
db.run(import_drizzle_orm.sql`TRUNCATE TABLE IF EXISTS ${sqliteTable}`) | ||
); | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgTimestamp) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgTimestampString) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgDate) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgDateString)) { | ||
return "date"; | ||
} | ||
if ((0, import_drizzle_orm3.is)(column, import_pg_core.PgArray) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgJson) || (0, import_drizzle_orm3.is)(column, import_pg_core.PgJsonb)) { | ||
return "json"; | ||
} | ||
return "string"; | ||
}; | ||
var queryForPg = (db, schema) => (params) => { | ||
var queryFn2 = (db, schema) => (params) => { | ||
const tablesMap = Object.values(schema).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_pg_core.PgTable)) { | ||
if ((0, import_drizzle_orm3.is)(it, import_pg_core.PgTable)) { | ||
const config2 = (0, import_pg_core.getTableConfig)(it); | ||
@@ -263,7 +372,7 @@ acc[config2.name] = it; | ||
const orderByColumn = params.orderBy ? columns[params.orderBy.column] : void 0; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm.asc)(orderByColumn)] : [(0, import_drizzle_orm.desc)(orderByColumn)] : []; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm3.asc)(orderByColumn)] : [(0, import_drizzle_orm3.desc)(orderByColumn)] : []; | ||
const q = hasPartial ? db.select(partialSelect) : db.select(); | ||
return q.from(table).where(params.where ? filtersToSQL(params.where) : void 0).orderBy(...orderByExpr).limit(params.limit).offset(params.offset).execute(); | ||
}; | ||
var dropTableForPg = (db, tablesMap) => (params) => { | ||
var dropTableFn2 = (db, tablesMap) => (params) => { | ||
const { isCascade } = params; | ||
@@ -275,7 +384,7 @@ const pgTable = tablesMap[params.table]; | ||
if (isCascade) { | ||
return db.execute(import_drizzle_orm.sql`DROP TABLE IF EXISTS ${pgTable} CASCADE`); | ||
return db.execute(import_drizzle_orm3.sql`DROP TABLE IF EXISTS ${pgTable} CASCADE`); | ||
} | ||
return db.execute(import_drizzle_orm.sql`DROP TABLE IF EXISTS ${pgTable}`); | ||
return db.execute(import_drizzle_orm3.sql`DROP TABLE IF EXISTS ${pgTable}`); | ||
}; | ||
var truncateTableForPg = (db, tablesMap) => (params) => { | ||
var truncateTableFn = (db, tablesMap) => (params) => { | ||
const { isCascade } = params; | ||
@@ -287,8 +396,162 @@ const pgTable = tablesMap[params.table]; | ||
if (isCascade) { | ||
return db.execute(import_drizzle_orm.sql`TRUNCATE TABLE ${pgTable} CASCADE`); | ||
return db.execute(import_drizzle_orm3.sql`TRUNCATE TABLE ${pgTable} CASCADE`); | ||
} | ||
return db.execute(import_drizzle_orm.sql`TRUNCATE TABLE ${pgTable}`); | ||
return db.execute(import_drizzle_orm3.sql`TRUNCATE TABLE ${pgTable}`); | ||
}; | ||
var queryForMysql = (db, tablesMap) => (params) => { | ||
var deleteFn2 = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
return db.delete(table).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var insertFn2 = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.insert(table).values(data).execute(); | ||
}; | ||
var updateFn2 = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.update(table).set(data).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var pgQueryEngine = async (setup) => { | ||
const { db } = setup; | ||
const schemaToTables = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm3.is)(it, import_pg_core.PgTable)) { | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
acc[config.name] = it; | ||
} | ||
return acc; | ||
}, {}); | ||
return [key, mapped]; | ||
}) | ||
); | ||
const schmaToTablesToColumnNames = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm3.is)(it, import_pg_core.PgTable)) { | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm3.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper2(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
return [key, mapped]; | ||
}) | ||
); | ||
const tables = async (schema, options) => { | ||
return Promise.all( | ||
Object.values(setup.schema).filter((it) => { | ||
return (0, import_drizzle_orm3.is)(it, import_pg_core.PgTable); | ||
}).map(async (it) => { | ||
assertPgTable(it); | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => ({ | ||
name: it2.name, | ||
type: typeForTable2(it2), | ||
_type: it2.getSQLType() | ||
})); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = schemaToTables[schema][tableName]; | ||
const result = await setup.db.select({ | ||
count: import_drizzle_orm3.sql`count(*)`.mapWith((it2) => Number(it2)) | ||
}).from(table).execute(); | ||
count = result[0].count; | ||
} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
); | ||
}; | ||
const schemasToTableConfig = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const config = (0, import_drizzle_orm3.extractTablesRelationalConfig)( | ||
val, | ||
import_drizzle_orm3.createTableRelationsHelpers | ||
); | ||
return [key, config]; | ||
}) | ||
); | ||
return { | ||
query: (schema) => queryFn2(db, schemaToTables[schema]), | ||
delete: (schema) => deleteFn2(db, schemaToTables[schema]), | ||
insert: (schema) => insertFn2(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
update: (schema) => updateFn2(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
dropTable: (schema) => dropTableFn2(db, schemaToTables[schema]), | ||
truncate: (schema) => truncateTableFn(db, schemaToTables[schema]), | ||
tables, | ||
tablesConfig: (schema) => { | ||
return schemasToTableConfig[schema]; | ||
} | ||
}; | ||
}; | ||
// src/mysql.ts | ||
var import_drizzle_orm4 = require("drizzle-orm"); | ||
var import_mysql_core = require("drizzle-orm/mysql-core"); | ||
var typeForTable3 = (column) => { | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlBoolean)) { | ||
return "boolean"; | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlTinyInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlSmallInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlMediumInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlFloat) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlBigInt53) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlDouble)) { | ||
return "number"; | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlTimestamp) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlTimestampString) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlDate) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlDateString)) { | ||
return "date"; | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlJson)) { | ||
return "json"; | ||
} | ||
return "string"; | ||
}; | ||
function assertMysqlTable(value) { | ||
if (!(0, import_drizzle_orm4.is)(value, import_mysql_core.MySqlTable)) { | ||
throw new Error(); | ||
} | ||
} | ||
var columnMapper3 = (column) => { | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlTimestamp) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlDate)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return new Date(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlTinyInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlSmallInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlMediumInt) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlFloat) || (0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlDouble)) { | ||
return (input) => { | ||
if (input === void 0 || input === null) | ||
return null; | ||
return Number(input); | ||
}; | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlBigInt53)) { | ||
return (input) => Number(input); | ||
} | ||
if ((0, import_drizzle_orm4.is)(column, import_mysql_core.MySqlBigInt64)) { | ||
return (input) => BigInt(input); | ||
} | ||
return (input) => input; | ||
}; | ||
var queryFn3 = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const config = (0, import_mysql_core.getTableConfig)(table); | ||
@@ -307,7 +570,7 @@ const hasPartial = (params.columns && params.columns.length > 0) ?? false; | ||
const orderByColumn = params.orderBy ? columns[params.orderBy.column] : void 0; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm.asc)(orderByColumn)] : [(0, import_drizzle_orm.desc)(orderByColumn)] : []; | ||
const orderByExpr = orderByColumn ? params.orderBy.type === "ASC" ? [(0, import_drizzle_orm4.asc)(orderByColumn)] : [(0, import_drizzle_orm4.desc)(orderByColumn)] : []; | ||
const q = hasPartial ? db.select(partialSelect) : db.select(); | ||
return q.from(table).where(params.where ? filtersToSQL(params.where) : void 0).orderBy(...orderByExpr).limit(params.limit).offset(params.offset).execute(); | ||
}; | ||
var dropRelationTablesForMysql = (db, tableName, tablesMap, relations) => { | ||
var dropRelationTablesFn = (db, tableName, tablesMap, relations) => { | ||
const filteredRelations = relations.filter( | ||
@@ -318,3 +581,3 @@ (relation) => relation.table === tableName | ||
for (const table of relationTables) { | ||
dropRelationTablesForMysql(db, table, tablesMap, filteredRelations); | ||
dropRelationTablesFn(db, table, tablesMap, filteredRelations); | ||
const mysqlTable = tablesMap[table]; | ||
@@ -324,6 +587,6 @@ if (!mysqlTable) { | ||
} | ||
db.execute(import_drizzle_orm.sql`TRUNCATE TABLE IF EXISTS ${mysqlTable}`); | ||
db.execute(import_drizzle_orm4.sql`TRUNCATE TABLE IF EXISTS ${mysqlTable}`); | ||
} | ||
}; | ||
var dropTableForMysql = (db, tablesMap, tablesConfig) => (params) => { | ||
var dropTableFn3 = (db, tablesMap, tablesConfig) => (params) => { | ||
const { isCascade, table } = params; | ||
@@ -336,416 +599,123 @@ const mysqlTable = tablesMap[params.table]; | ||
const relations = extractRelations(tablesConfig); | ||
dropRelationTablesForMysql(db, table, tablesMap, relations); | ||
dropRelationTablesFn(db, table, tablesMap, relations); | ||
} | ||
return db.execute(import_drizzle_orm.sql`DROP TABLE IF EXISTS ${mysqlTable}`); | ||
return db.execute(import_drizzle_orm4.sql`DROP TABLE IF EXISTS ${mysqlTable}`); | ||
}; | ||
var deleteForSqliteAsync = (db, tablesMap) => (params) => { | ||
var deleteFn3 = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
return db.delete(table).where(filtersToSQL(params.where)).run(); | ||
}; | ||
var deleteForSqliteSync = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
return Promise.resolve( | ||
db.delete(table).where(filtersToSQL(params.where)).run() | ||
); | ||
}; | ||
var deleteForPg = (db, tablesMap) => (params) => { | ||
const table = tablesMap[params.table]; | ||
return db.delete(table).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var deleteForMysql = (db, tablesMap) => (params) => { | ||
var insertFn3 = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
return db.delete(table).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var insertForSqliteAsync = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.insert(table).values(data).run(); | ||
}; | ||
var insertForSqliteSync = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return Promise.resolve(db.insert(table).values(data).run()); | ||
}; | ||
var insertForPg = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.insert(table).values(data).execute(); | ||
}; | ||
var insertForMysql = (db, tablesMap, tableToColumnNames) => (params) => { | ||
var updateFn3 = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.insert(table).values(data).execute(); | ||
}; | ||
var updateForSqliteAsync = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.update(table).set(data).where(filtersToSQL(params.where)).run(); | ||
}; | ||
var updateForSqliteSync = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return Promise.resolve( | ||
db.update(table).set(data).where(filtersToSQL(params.where)).run() | ||
); | ||
}; | ||
var updateForPg = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.update(table).set(data).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var updateForMysql = (db, tablesMap, tableToColumnNames) => (params) => { | ||
const table = tablesMap[params.table]; | ||
const columnNamesMap = tableToColumnNames[params.table]; | ||
const data = convertToTsColumns(params.data, columnNamesMap); | ||
return db.update(table).set(data).where(filtersToSQL(params.where)).execute(); | ||
}; | ||
var convertToTsColumns = (data, columnNamesMap) => Object.fromEntries( | ||
Object.entries(data).map((it) => { | ||
const { name: tsName, mapper } = columnNamesMap[it[0]]; | ||
if (!tsName) | ||
throw new Error(`No column name '${it[0]}'`); | ||
return [tsName, mapper(it[1])]; | ||
}) | ||
); | ||
var typeForTable = (column) => { | ||
if ((0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteBoolean) || (0, import_drizzle_orm.is)(column, import_pg_core.PgBoolean) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlBoolean)) { | ||
return "boolean"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteInteger) || (0, import_drizzle_orm.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm.is)(column, import_pg_core.PgBigInt53) || (0, import_drizzle_orm.is)(column, import_pg_core.PgBigSerial53) || (0, import_drizzle_orm.is)(column, import_pg_core.PgReal) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSmallInt) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSmallSerial) || (0, import_drizzle_orm.is)(column, import_pg_core.PgDoublePrecision) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlTinyInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlSmallInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlMediumInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlFloat) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlBigInt53) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlDouble)) { | ||
return "number"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteTimestamp) || (0, import_drizzle_orm.is)(column, import_pg_core.PgTimestamp) || (0, import_drizzle_orm.is)(column, import_pg_core.PgTimestampString) || (0, import_drizzle_orm.is)(column, import_pg_core.PgDate) || (0, import_drizzle_orm.is)(column, import_pg_core.PgDateString) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlTimestamp) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlTimestampString) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlDate) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlDateString)) { | ||
return "date"; | ||
} | ||
if ((0, import_drizzle_orm.is)(column, import_pg_core.PgArray) || (0, import_drizzle_orm.is)(column, import_pg_core.PgJson) || (0, import_drizzle_orm.is)(column, import_pg_core.PgJsonb) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlJson) || (0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteBlobJson) || (0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteTextJson)) { | ||
return "json"; | ||
} | ||
return "string"; | ||
}; | ||
var queryEngineForSetup = async (setup) => { | ||
if (setup.type === "sqlite") { | ||
const { db } = setup; | ||
const isSync = (0, import_drizzle_orm.is)( | ||
db, | ||
import_sqlite_core.BaseSQLiteDatabase | ||
); | ||
let result = []; | ||
if (isSync) { | ||
result = db.select({ | ||
name: import_drizzle_orm.sql`name` | ||
}).from(import_drizzle_orm.sql`sqlite_master`).where(import_drizzle_orm.sql`type = 'table'`).all(); | ||
} | ||
const isAsync = (0, import_drizzle_orm.is)( | ||
db, | ||
import_sqlite_core.BaseSQLiteDatabase | ||
); | ||
if (isAsync) { | ||
result = await db.select({ name: import_drizzle_orm.sql`name` }).from(import_drizzle_orm.sql`sqlite_master`).where(import_drizzle_orm.sql`type = 'table'`).all(); | ||
} | ||
const dbTableNames = result.map((it) => it.name); | ||
const tablesMap = Object.values(setup.schema).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_sqlite_core.SQLiteTable)) { | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
if (dbTableNames.includes(config.name)) { | ||
var mysqlQueryEngine = async (setup) => { | ||
const { db } = setup; | ||
const schemaToTables = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm4.is)(it, import_mysql_core.MySqlTable)) { | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
acc[config.name] = it; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
const tableToColumnNames = Object.values(setup.schema).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_sqlite_core.SQLiteTable)) { | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
const tables = async (options) => { | ||
const result2 = await db.select({ | ||
name: import_drizzle_orm.sql`name` | ||
}).from(import_drizzle_orm.sql`sqlite_master`).where(import_drizzle_orm.sql`type = 'table'`); | ||
const dbTableNames2 = result2.map((it) => it.name); | ||
console.log(dbTableNames2); | ||
return Promise.all( | ||
Object.values(setup.schema).filter((it) => { | ||
if (!(0, import_drizzle_orm.is)(it, import_sqlite_core.SQLiteTable)) { | ||
return false; | ||
}, {}); | ||
return [key, mapped]; | ||
}) | ||
); | ||
const schmaToTablesToColumnNames = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const mapped = Object.values(val).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm4.is)(it, import_mysql_core.MySqlTable)) { | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm4.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper3(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
return dbTableNames2.includes(config.name); | ||
}).map(async (it) => { | ||
assertSQLiteTable(it); | ||
const config = (0, import_sqlite_core.getTableConfig)(it); | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => { | ||
return { | ||
name: it2.name, | ||
type: typeForTable(it2), | ||
_type: it2.getSQLType() | ||
}; | ||
}); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = tablesMap[tableName]; | ||
const result3 = await setup.db.select({ | ||
count: import_drizzle_orm.sql`count(*)` | ||
}).from(table).all(); | ||
count = result3[0].count; | ||
} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
return acc; | ||
}, | ||
{} | ||
); | ||
}; | ||
const tablesConfig = (0, import_drizzle_orm.extractTablesRelationalConfig)( | ||
setup.schema, | ||
import_drizzle_orm.createTableRelationsHelpers | ||
); | ||
if (isSync) { | ||
return { | ||
query: queryForSqliteSync(db, tablesMap), | ||
delete: deleteForSqliteSync(db, tablesMap), | ||
insert: insertForSqliteSync(db, tablesMap, tableToColumnNames), | ||
update: updateForSqliteSync(db, tablesMap, tableToColumnNames), | ||
dropTable: dropTableForSqliteSync(db, tablesMap, tablesConfig), | ||
truncateTable: truncateTableForSqliteSync(db, tablesMap, tablesConfig), | ||
tables, | ||
tablesConfig | ||
}; | ||
} | ||
if (isAsync) { | ||
return { | ||
query: queryForSqliteAsync(db, tablesMap), | ||
delete: deleteForSqliteAsync(db, tablesMap), | ||
insert: insertForSqliteAsync(db, tablesMap, tableToColumnNames), | ||
update: updateForSqliteAsync(db, tablesMap, tableToColumnNames), | ||
dropTable: dropTableForSqliteAsync(db, tablesMap, tablesConfig), | ||
tables, | ||
tablesConfig | ||
}; | ||
} | ||
throw new Error("never"); | ||
} | ||
if (setup.type === "pg") { | ||
const { db } = setup; | ||
const result = await db.select({ | ||
name: import_drizzle_orm.sql`table_name` | ||
}).from(import_drizzle_orm.sql`information_schema.tables`).where(import_drizzle_orm.sql`table_schema = 'public'`).execute(); | ||
const dbTableNames = result.map((it) => it.name); | ||
const tablesMap = Object.values(setup.schema).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_pg_core.PgTable)) { | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
if (dbTableNames.includes(config.name)) { | ||
acc[config.name] = it; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
const tableToColumnNames = Object.values(setup.schema).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_pg_core.PgTable)) { | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
const tables = async (options) => { | ||
const result2 = await db.select({ | ||
name: import_drizzle_orm.sql`table_name` | ||
}).from(import_drizzle_orm.sql`information_schema.tables`).where(import_drizzle_orm.sql`table_schema = 'public'`).execute(); | ||
const dbTableNames2 = result2.map((it) => it.name); | ||
return Promise.all( | ||
Object.values(setup.schema).filter((it) => { | ||
if (!(0, import_drizzle_orm.is)(it, import_pg_core.PgTable)) { | ||
return false; | ||
} | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
return dbTableNames2.includes(config.name); | ||
}).map(async (it) => { | ||
assertPgTable(it); | ||
const config = (0, import_pg_core.getTableConfig)(it); | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => ({ | ||
name: it2.name, | ||
type: typeForTable(it2), | ||
_type: it2.getSQLType() | ||
})); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = tablesMap[tableName]; | ||
const result3 = await setup.db.select({ | ||
count: import_drizzle_orm.sql`count(*)`.mapWith((it2) => Number(it2)) | ||
}).from(table).execute(); | ||
count = result3[0].count; | ||
} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
); | ||
}; | ||
const tablesConfig = (0, import_drizzle_orm.extractTablesRelationalConfig)( | ||
setup.schema, | ||
import_drizzle_orm.createTableRelationsHelpers | ||
); | ||
return { | ||
query: queryForPg(db, setup.schema), | ||
delete: deleteForPg(db, tablesMap), | ||
insert: insertForPg(db, tablesMap, tableToColumnNames), | ||
update: updateForPg(db, tablesMap, tableToColumnNames), | ||
dropTable: dropTableForPg(db, tablesMap), | ||
truncateTable: truncateTableForPg(db, tablesMap), | ||
tables, | ||
tablesConfig | ||
}; | ||
} | ||
if (setup.type === "mysql") { | ||
const { db } = setup; | ||
const result = await db.select({ | ||
name: import_drizzle_orm.sql`table_name` | ||
}).from(import_drizzle_orm.sql`information_schema.tables`).where(import_drizzle_orm.sql`table_schema = 'public'`).execute(); | ||
const dbTableNames = result.map((it) => it.name); | ||
const tablesMap = Object.values(setup.schema).reduce((acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_mysql_core.MySqlTable)) { | ||
return [key, mapped]; | ||
}) | ||
); | ||
const tables = async (schema, options) => { | ||
return Promise.all( | ||
Object.values(setup.schema).filter((it) => { | ||
return (0, import_drizzle_orm4.is)(it, import_mysql_core.MySqlTable); | ||
}).map(async (it) => { | ||
assertMysqlTable(it); | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
if (dbTableNames.includes(config.name)) { | ||
acc[config.name] = it; | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => ({ | ||
name: it2.name, | ||
type: typeForTable3(it2), | ||
_type: it2.getSQLType() | ||
})); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = schemaToTables[schema][tableName]; | ||
const result = await setup.db.select({ | ||
count: import_drizzle_orm4.sql`count(*)` | ||
}).from(table).execute(); | ||
count = result[0].count; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
const tableToColumnNames = Object.values(setup.schema).reduce( | ||
(acc, it) => { | ||
if ((0, import_drizzle_orm.is)(it, import_mysql_core.MySqlTable)) { | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
const columnNamesMap = Object.fromEntries( | ||
Object.entries((0, import_drizzle_orm.getTableColumns)(it)).map((col) => [ | ||
col[1].name, | ||
{ | ||
name: col[0], | ||
mapper: columnMapper(col[1]) | ||
} | ||
]) | ||
); | ||
acc[config.name] = columnNamesMap; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
); | ||
const tables = async (options) => { | ||
const result2 = await db.select({ | ||
name: import_drizzle_orm.sql`table_name` | ||
}).from(import_drizzle_orm.sql`information_schema.tables`).where(import_drizzle_orm.sql`table_schema = 'public'`).execute(); | ||
const dbTableNames2 = result2.map((it) => it.name); | ||
return Promise.all( | ||
Object.values(setup.schema).filter((it) => { | ||
if (!(0, import_drizzle_orm.is)(it, import_mysql_core.MySqlTable)) { | ||
return false; | ||
} | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
return dbTableNames2.includes(config.name); | ||
}).map(async (it) => { | ||
assertMysqlTable(it); | ||
const config = (0, import_mysql_core.getTableConfig)(it); | ||
const primaryKeys = config.primaryKeys.map((it2) => it2.columns.map((column) => column.name)).concat(config.columns.map((it2) => it2.primary ? it2.name : "")).filter(Boolean).flat(); | ||
const tableName = config.name; | ||
const columns = config.columns.map((it2) => ({ | ||
name: it2.name, | ||
type: typeForTable(it2), | ||
_type: it2.getSQLType() | ||
})); | ||
let count = void 0; | ||
if (!options?.withoutCount) { | ||
const table = tablesMap[tableName]; | ||
const result3 = await setup.db.select({ | ||
count: import_drizzle_orm.sql`count(*)` | ||
}).from(table).execute(); | ||
count = result3[0].count; | ||
} | ||
return { | ||
name: tableName, | ||
columns, | ||
count, | ||
primaryKeys | ||
}; | ||
}) | ||
}; | ||
const schemasToTableConfig = Object.fromEntries( | ||
Object.entries(setup.schema).map(([key, val]) => { | ||
const config = (0, import_drizzle_orm4.extractTablesRelationalConfig)( | ||
val, | ||
import_drizzle_orm4.createTableRelationsHelpers | ||
); | ||
}; | ||
const tablesConfig = (0, import_drizzle_orm.extractTablesRelationalConfig)( | ||
setup.schema, | ||
import_drizzle_orm.createTableRelationsHelpers | ||
); | ||
return { | ||
query: queryForMysql(db, tablesMap), | ||
delete: deleteForMysql(db, tablesMap), | ||
insert: insertForMysql(db, tablesMap, tableToColumnNames), | ||
update: updateForMysql(db, tablesMap, tableToColumnNames), | ||
dropTable: dropTableForMysql(db, tablesMap, tablesConfig), | ||
tables, | ||
tablesConfig | ||
}; | ||
} | ||
return [key, config]; | ||
}) | ||
); | ||
return { | ||
query: (schema) => queryFn3(db, schemaToTables[schema]), | ||
delete: (schema) => deleteFn3(db, schemaToTables[schema]), | ||
insert: (schema) => insertFn3(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
update: (schema) => updateFn3(db, schemaToTables[schema], schmaToTablesToColumnNames[schema]), | ||
dropTable: (schema) => dropTableFn3(db, schemaToTables[schema], schemasToTableConfig[schema]), | ||
truncate: (schema) => { | ||
throw new Error("truncate not implemented for MySQL"); | ||
}, | ||
tables, | ||
tablesConfig: (schema) => schemasToTableConfig[schema] | ||
}; | ||
}; | ||
// src/index.ts | ||
var queryEngineForSetup = async (setup) => { | ||
if (setup.type === "sqlite") | ||
return sqliteQueryEngine(setup); | ||
if (setup.type === "pg") | ||
return pgQueryEngine(setup); | ||
if (setup.type === "mysql") | ||
return mysqlQueryEngine(setup); | ||
throw new Error("unknown setup"); | ||
}; | ||
var extractRelations = (tablesConfig) => { | ||
const rels = Object.values(tablesConfig.tables).map( | ||
(it) => Object.entries(it.relations).map((it1) => { | ||
const normalized = (0, import_drizzle_orm.normalizeRelation)( | ||
tablesConfig.tables, | ||
tablesConfig.tableNamesMap, | ||
it1[1] | ||
); | ||
const rel = it1[1]; | ||
const refTable = rel.referencedTableName; | ||
const fields = normalized.fields.map((it2) => it2.name).flat(); | ||
const refColumns = normalized.references.map((it2) => it2.name).flat(); | ||
return { | ||
name: it1[0], | ||
table: it.dbName, | ||
colmns: fields, | ||
refTable, | ||
refColumns | ||
}; | ||
}) | ||
).flat(); | ||
return rels; | ||
}; |
{ | ||
"name": "@drizzle-team/studio", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"license": "UNLICENSED", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -10,2 +10,3 @@ import { BaseSchema } from "valibot"; | ||
type: import("valibot").EnumSchema<["sql-schema"], "sql-schema">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").OptionalSchema<import("valibot").ObjectSchema<{ | ||
@@ -19,2 +20,3 @@ count: import("valibot").BooleanSchema<boolean>; | ||
}, { | ||
schema: string; | ||
type: "sql-schema"; | ||
@@ -26,2 +28,3 @@ data?: { | ||
type: import("valibot").EnumSchema<["sql-insert"], "sql-insert">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -45,5 +48,7 @@ table: import("valibot").StringSchema<string>; | ||
}; | ||
schema: string; | ||
type: "sql-insert"; | ||
}>, import("valibot").ObjectSchema<{ | ||
type: import("valibot").EnumSchema<["sql-select"], "sql-select">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -66,5 +71,5 @@ table: import("valibot").StringSchema<string>; | ||
}, { | ||
table: string; | ||
limit: number; | ||
offset: number; | ||
table: string; | ||
columns?: string[] | undefined; | ||
@@ -79,5 +84,5 @@ where?: FilterType | undefined; | ||
data: { | ||
table: string; | ||
limit: number; | ||
offset: number; | ||
table: string; | ||
columns?: string[] | undefined; | ||
@@ -90,5 +95,7 @@ where?: FilterType | undefined; | ||
}; | ||
schema: string; | ||
type: "sql-select"; | ||
}>, import("valibot").ObjectSchema<{ | ||
type: import("valibot").EnumSchema<["sql-update"], "sql-update">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -115,5 +122,7 @@ table: import("valibot").StringSchema<string>; | ||
}; | ||
schema: string; | ||
type: "sql-update"; | ||
}>, import("valibot").ObjectSchema<{ | ||
type: import("valibot").EnumSchema<["sql-delete"], "sql-delete">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -131,5 +140,7 @@ table: import("valibot").StringSchema<string>; | ||
}; | ||
schema: string; | ||
type: "sql-delete"; | ||
}>, import("valibot").ObjectSchema<{ | ||
type: import("valibot").EnumSchema<["sql-truncate"], "sql-truncate">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -147,5 +158,7 @@ table: import("valibot").StringSchema<string>; | ||
}; | ||
schema: string; | ||
type: "sql-truncate"; | ||
}>, import("valibot").ObjectSchema<{ | ||
type: import("valibot").EnumSchema<["sql-drop"], "sql-drop">; | ||
schema: import("valibot").StringSchema<string>; | ||
data: import("valibot").ObjectSchema<{ | ||
@@ -163,4 +176,6 @@ table: import("valibot").StringSchema<string>; | ||
}; | ||
schema: string; | ||
type: "sql-drop"; | ||
}>], { | ||
schema: string; | ||
type: "sql-schema"; | ||
@@ -177,8 +192,9 @@ data?: { | ||
}; | ||
schema: string; | ||
type: "sql-insert"; | ||
} | { | ||
data: { | ||
table: string; | ||
limit: number; | ||
offset: number; | ||
table: string; | ||
columns?: string[] | undefined; | ||
@@ -191,2 +207,3 @@ where?: FilterType | undefined; | ||
}; | ||
schema: string; | ||
type: "sql-select"; | ||
@@ -201,2 +218,3 @@ } | { | ||
}; | ||
schema: string; | ||
type: "sql-update"; | ||
@@ -208,2 +226,3 @@ } | { | ||
}; | ||
schema: string; | ||
type: "sql-delete"; | ||
@@ -215,2 +234,3 @@ } | { | ||
}; | ||
schema: string; | ||
type: "sql-truncate"; | ||
@@ -222,4 +242,5 @@ } | { | ||
}; | ||
schema: string; | ||
type: "sql-drop"; | ||
}>; | ||
export {}; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
799814
9203