Litestone guide / Misc
Coming from Prisma
A Prisma schema reads into .lite almost line for line, and the client keeps Prisma's verb names. convert does the schema half and records every construct it could not carry.
import { convert } from '@frontierjs/litestone/import'
const source = `
model User {
id Int @id @default(autoincrement())
email String @unique @db.VarChar(255)
posts Post[]
}
model Post {
id String @id @default(cuid())
price Decimal @db.Decimal(10, 2)
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId Int
@@map("posts")
}
`
const { lite, gaps } = convert({ source, format: 'prisma', label: 'schema.prisma' })
if (!lite.includes('price Int @scale(2)')) throw new Error('expected Decimal(10, 2) to become Int @scale(2)')
if (!lite.includes('@relation(fields: [authorId], references: [id], onDelete: Cascade)')) throw new Error('expected the relation unchanged')
if (gaps.map(g => g.kind).join() !== 'native-type') throw new Error('expected one lost @db.VarChar')
The converted schema is the same text with the money column re-spelled, and it builds as is:
model User {
id Int (autoincrement())
email String
posts Post[]
}
model Post {
id String (cuid())
price Int (2)
author User (fields: [authorId], references: [id], onDelete: Cascade)
authorId Int
("posts")
}
const u = await db.user.create({ data: { email: 'a@x.co' } })
const p = await db.post.create({ data: { price: 1250, authorId: u.id } })
if (p.price !== 1250 || typeof p.id !== 'string') throw new Error('price is whole minor units; the id is a cuid string')
What else it can do
- The command is
litestone import schema.prisma --out db/schema.lite, and--strictexits 1 if anything changed meaning - Enums,
@id,@unique,@updatedAt,@map,@@map,@@index,@@unique,@@idand@relationcarry over unchanged Decimalbecomes anIntwith@scalein whole minor units, so12.50is written1250; with no@db.Decimal(p, s)the scale 2 is a flagged guess@db.*native types are dropped,@@fulltextbecomes@@fts, andUnsupportedand@ignorefields are skipped- A model that declares no
@idgetsid String @id @default(cuid()), flagged - Each gap is graded
changed,lostornoted; achangedone is marked on its own line in the output - The client verbs
findMany,findUnique,findFirst,create,createMany,update,updateMany,upsert,deleteMany,count,aggregateandgroupBykeep their names include,select,_count, nestedcreateandconnect,increment,OR/AND/NOTandsome/every/nonefilters read the sametakeandskiparelimitandoffset, and the Prisma names are refused by namedeleteis a physical delete;removeis the soft delete on a@@softDeletemodelmode: 'insensitive'is refused, because SQLite'sLIKEalready ignores ASCII case$queryRawisdb.sql, or$rawwith thesqltag inside awhereprisma generate,migrate dev,db pushandstudioarelitestone types,migrate dev,db pushandstudio- Prisma has nowhere to say who may read or write; an imported schema stays open until the first
@@gateor@@allow
In depth: Importing a schema, § The output is not the whole answer, § After the import