-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.ts
120 lines (101 loc) · 3.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'react-native-url-polyfill/auto';
import { NativeModules } from 'react-native';
import { atob, btoa } from 'react-native-quick-base64';
global.atob = atob;
global.btoa = btoa;
export { reactiveHooksExtension } from './ReactiveHooksExtension';
export { reactiveQueriesExtension } from './ReactiveQueriesExtension';
declare global {
// eslint-disable-next-line no-var
var __PrismaProxy: PrismaProxy | undefined;
}
const errorResolutions = [
`1. Ensure @prisma/react-native is added to the plugins section in app.json`,
`2. Ensure at least one migration exists:
$ prisma migrate dev`,
`3. Try removing the node_modules/ folder and running the prebuild command:
$ rm -rf node_modules/
$ npx expo prebuild --clean
`,
`4. If in a monorepo, ensure the application's package.json also has the required Prisma dependencies:
$ npm i @prisma/client@latest @prisma/react-native@latest react-native-quick-base64
$ yarn add @prisma/client@latest @prisma/react-native@latest react-native-quick-base64
`,
];
const makeErrorMessage = (message: string) => {
return [message, errorResolutions.join('\n\n')].join('\n\n');
};
// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const PrismaModule = isTurboModuleEnabled
? require('./NativePrisma').default
: NativeModules.Prisma;
if (!PrismaModule) {
throw new Error(
makeErrorMessage('🟥 @prisma/react-native failed to initialize')
);
}
try {
PrismaModule.install();
} catch {
throw new Error(
makeErrorMessage(`🟥 @prisma/react-native failed to install`)
);
}
if (!global.__PrismaProxy) {
throw new Error(
makeErrorMessage('🟥 prisma/react-native C++ bindings failed to initialize')
);
}
// Wrap the create function to stringify the env variables if necessary
const ogCreate = __PrismaProxy!.create;
global.__PrismaProxy = {
...global.__PrismaProxy,
create: (options: PrismaCreateOptions): QueryEngineObject => {
if (typeof options.env !== 'string') {
options.env = JSON.stringify(options.env);
}
if (typeof options.datasourceOverrides !== 'string') {
options.datasourceOverrides = JSON.stringify(options.datasourceOverrides);
}
return ogCreate(options);
},
};
type PrismaCreateOptions = {
datamodel: string;
logLevel: string;
logQueries: boolean;
logCallback: (msg: string) => void;
ignoreEnvVarErrors: boolean;
datasourceOverrides: object | string;
env: object | string;
};
type QueryEngineObject = object;
type PrismaProxy = {
create: (options: PrismaCreateOptions) => QueryEngineObject;
connect: (engine: QueryEngineObject, trace: string) => void;
execute: (
engine: QueryEngineObject,
body: string,
headers: string,
txId: string
) => Promise<string>;
startTransaction: (
engine: QueryEngineObject,
body: string,
hdears: string
) => string;
commitTransaction: (
engine: QueryEngineObject,
txId: string,
headers: string
) => string;
rollbackTransaction: (
engine: QueryEngineObject,
txId: string,
headers: string
) => string;
disconnect: (engine: QueryEngineObject, headers: string) => void;
pushSchema: (engine: QueryEngineObject, schema: string) => void;
applyPendingMigrations: (engine: QueryEngineObject) => void;
};