(
schema: AnySchema,
config: PrismaConfig & {
isTransaction?: boolean;
},
)
| 134 | } |
| 135 | |
| 136 | export function fromPrisma( |
| 137 | schema: AnySchema, |
| 138 | config: PrismaConfig & { |
| 139 | isTransaction?: boolean; |
| 140 | }, |
| 141 | ): AbstractQuery<AnySchema> { |
| 142 | const { |
| 143 | provider, |
| 144 | prisma, |
| 145 | relationMode = provider === "mongodb" ? "prisma" : "foreign-keys", |
| 146 | db: internalClient, |
| 147 | isTransaction = false, |
| 148 | } = config; |
| 149 | |
| 150 | // replace index with partial index to ignore null values |
| 151 | // see https://github.com/prisma/prisma/issues/3387 |
| 152 | async function initMongoDB() { |
| 153 | if (!internalClient || isTransaction) return; |
| 154 | const db = internalClient.db(); |
| 155 | |
| 156 | async function initCollection(table: AnyTable) { |
| 157 | const collection = db.collection(table.names.mongodb); |
| 158 | const indexes = await collection.indexes(); |
| 159 | |
| 160 | for (const index of indexes) { |
| 161 | if (!index.unique || !index.name || index.sparse) continue; |
| 162 | |
| 163 | await collection.dropIndex(index.name); |
| 164 | await collection.createIndex(index.key, { |
| 165 | name: index.name, |
| 166 | unique: true, |
| 167 | sparse: true, |
| 168 | }); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | await Promise.all(Object.values(schema.tables).map(initCollection)); |
| 173 | } |
| 174 | |
| 175 | let mapped: Map<string, string> | undefined; |
| 176 | |
| 177 | function getPrismaModel(table: AnyTable) { |
| 178 | if (!mapped) { |
| 179 | mapped = new Map(); |
| 180 | for (const key in prisma) { |
| 181 | mapped.set(key.toLowerCase(), key); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | const modelName = mapped.get(table.names.prisma.toLowerCase()); |
| 186 | |
| 187 | if (!modelName) { |
| 188 | throw new Error( |
| 189 | `Prisma client is missing model delegate "${table.names.prisma}" for table "${table.ormName}".`, |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | return prisma[modelName]!; |
no test coverage detected