(schema: string, additionalSchemas?: Record<string, string>)
| 141 | } |
| 142 | |
| 143 | export async function loadSchema(schema: string, additionalSchemas?: Record<string, string>) { |
| 144 | if (!schema.includes('datasource ')) { |
| 145 | schema = `${makePrelude('sqlite')}\n\n${schema}`; |
| 146 | } |
| 147 | |
| 148 | // create a temp folder |
| 149 | const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'zenstack-schema')); |
| 150 | |
| 151 | // create a temp file |
| 152 | const tempFile = path.join(tempDir, `schema.zmodel`); |
| 153 | fs.writeFileSync(tempFile, schema); |
| 154 | |
| 155 | if (additionalSchemas) { |
| 156 | for (const [fileName, content] of Object.entries(additionalSchemas)) { |
| 157 | let name = fileName; |
| 158 | if (!name.endsWith('.zmodel')) { |
| 159 | name += '.zmodel'; |
| 160 | } |
| 161 | const filePath = path.join(tempDir, name); |
| 162 | fs.writeFileSync(filePath, content); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | const r = await loadDocumentWithPlugins(tempFile); |
| 167 | expect(r).toSatisfy( |
| 168 | (r) => r.success, |
| 169 | `Failed to load schema: ${(r as any).errors?.map((e: any) => e.toString()).join(', ')}`, |
| 170 | ); |
| 171 | invariant(r.success); |
| 172 | return r.model; |
| 173 | } |
| 174 | |
| 175 | export async function loadSchemaWithError(schema: string, error: string | RegExp) { |
| 176 | if (!schema.includes('datasource ')) { |
no test coverage detected