-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ee7fe3
commit 9b67547
Showing
7 changed files
with
252 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
cli/introspection/src/prompt/utils/templates/__tests__/exampleScript.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { exampleScript } from '../script' | ||
|
||
test('exampleScript', async () => { | ||
const schema = `datasource db { | ||
provider = "sqlite" | ||
url = "file:dev.db" | ||
default = true | ||
} | ||
generator photon { | ||
provider = "photonjs" | ||
output = "@generated/photon" | ||
transpile = false | ||
} | ||
model User { | ||
id String @id @default(uuid()) | ||
username String | ||
posts Post[] | ||
} | ||
model Post { | ||
id String @id @default(uuid()) | ||
data String | ||
user User? | ||
}` | ||
|
||
expect(await exampleScript({ datamodel: schema, typescript: true })).toMatchInlineSnapshot(` | ||
"/** | ||
* | ||
* This code was auto-generated based on the introspection result. | ||
* Consider it a playground to explore the Photon API. | ||
* Feel free to change it as much as you like or delete it altogether. | ||
* | ||
* The model \`User\` was randomly selected to demonstrate some API calls. | ||
* | ||
* Tip: Use the auto-completion of your editor to explore available API operations. | ||
* | ||
*/ | ||
import Photon from '@generated/photon' | ||
const photon = new Photon() | ||
async function main() { | ||
// Tip: Explore some arguments to \`findMany\` | ||
const allUsers = await photon.users.findMany() | ||
console.log(\`Retrieved all published users: \`, allUsers) | ||
// Comment out the lines below to create a new User | ||
// ATTENTION: This code creates a new record in your database | ||
// const newUser = await photon.users.create({ | ||
// data: { | ||
// // add some values here | ||
// }, | ||
// }) | ||
// console.log(\`Created a new User: \`, newUser) | ||
} | ||
main() | ||
.catch(e => console.error(e)) | ||
.finally(async () => { | ||
await photon.disconnect() | ||
})" | ||
`) | ||
|
||
expect(await exampleScript({ datamodel: schema, typescript: false })).toMatchInlineSnapshot(` | ||
"/** | ||
* | ||
* This code was auto-generated based on the introspection result. | ||
* Consider it a playground to explore the Photon API. | ||
* Feel free to change it as much as you like or delete it altogether. | ||
* | ||
* The model \`User\` was randomly selected to demonstrate some API calls. | ||
* | ||
* Tip: Use the auto-completion of your editor to explore available API operations. | ||
* | ||
*/ | ||
const Photon = require('@generated/photon') | ||
const photon = new Photon() | ||
async function main() { | ||
// Tip: Explore some arguments to \`findMany\` | ||
const allUsers = await photon.users.findMany() | ||
console.log(\`Retrieved all published users: \`, allUsers) | ||
// Comment out the lines below to create a new User | ||
// ATTENTION: This code creates a new record in your database | ||
// const newUser = await photon.users.create({ | ||
// data: { | ||
// // add some values here | ||
// }, | ||
// }) | ||
// console.log(\`Created a new User: \`, newUser) | ||
} | ||
main() | ||
.catch(e => console.error(e)) | ||
.finally(async () => { | ||
await photon.disconnect() | ||
})" | ||
`) | ||
}) |
60 changes: 60 additions & 0 deletions
60
cli/introspection/src/prompt/utils/templates/__tests__/sortModels.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { sortModels } from '../sortModels' | ||
import { DMMF } from '@prisma/photon/dist/runtime/dmmf-types' | ||
|
||
test('sortModels', () => { | ||
const models: DMMF.Model[] = [ | ||
{ | ||
name: 'Car', | ||
fields: [], | ||
isEmbedded: false, | ||
dbName: null, | ||
}, | ||
{ | ||
name: 'Bike', | ||
fields: [], | ||
isEmbedded: false, | ||
dbName: null, | ||
}, | ||
{ | ||
name: 'Post', | ||
fields: [], | ||
isEmbedded: false, | ||
dbName: null, | ||
}, | ||
{ | ||
name: 'User', | ||
fields: [], | ||
isEmbedded: false, | ||
dbName: null, | ||
}, | ||
] | ||
|
||
expect(sortModels(models)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"dbName": null, | ||
"fields": Array [], | ||
"isEmbedded": false, | ||
"name": "User", | ||
}, | ||
Object { | ||
"dbName": null, | ||
"fields": Array [], | ||
"isEmbedded": false, | ||
"name": "Post", | ||
}, | ||
Object { | ||
"dbName": null, | ||
"fields": Array [], | ||
"isEmbedded": false, | ||
"name": "Bike", | ||
}, | ||
Object { | ||
"dbName": null, | ||
"fields": Array [], | ||
"isEmbedded": false, | ||
"name": "Car", | ||
}, | ||
] | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
cli/introspection/src/prompt/utils/templates/sortModels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DMMF } from '@prisma/photon/dist/runtime/dmmf-types' | ||
|
||
const modelOrder = ['User', 'Player', 'Customer', 'Product', 'Order', 'Article', 'Post', 'Message'] | ||
|
||
export function sortModels(models: DMMF.Model[]): DMMF.Model[] { | ||
return models.sort((a, b) => { | ||
if (modelOrder.includes(a.name) && modelOrder.includes(b.name)) { | ||
const aIndex = modelOrder.indexOf(a.name) | ||
const bIndex = modelOrder.indexOf(b.name) | ||
return aIndex < bIndex ? -1 : 1 | ||
} | ||
|
||
if (modelOrder.includes(a.name)) { | ||
return -1 | ||
} | ||
|
||
if (modelOrder.includes(b.name)) { | ||
return 1 | ||
} | ||
|
||
return a.name < b.name ? -1 : 1 | ||
}) | ||
} |