Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@mrleebo/prisma-ast
Advanced tools
This library uses an abstract syntax tree to parse schema.prisma files into an object in JavaScript. It is similar to [@prisma/sdk](https://github.com/prisma/prisma/tree/master/src/packages/sdk) except that it preserves comments and model attributes.
@mrleebo/prisma-ast is an npm package designed to parse and manipulate Prisma schema files. It provides an Abstract Syntax Tree (AST) representation of Prisma schema files, allowing developers to programmatically read, modify, and generate Prisma schema definitions.
Parsing Prisma Schema
This feature allows you to parse a Prisma schema string into an AST. The code sample demonstrates how to parse a simple Prisma schema and output the resulting AST.
const { parseSchema } = require('@mrleebo/prisma-ast');
const schema = `
model User {
id Int @id @default(autoincrement())
name String
}
`;
const ast = parseSchema(schema);
console.log(JSON.stringify(ast, null, 2));
Modifying Prisma Schema
This feature allows you to modify an existing Prisma schema by manipulating its AST. The code sample shows how to add a new field to the User model and then print the updated schema.
const { parseSchema, printSchema } = require('@mrleebo/prisma-ast');
let schema = `
model User {
id Int @id @default(autoincrement())
name String
}
`;
let ast = parseSchema(schema);
ast.declarations[0].fields.push({
name: 'email',
type: 'String',
attributes: []
});
schema = printSchema(ast);
console.log(schema);
Generating Prisma Schema
This feature allows you to generate a Prisma schema from an AST. The code sample demonstrates how to create an AST for a simple User model and then generate the corresponding Prisma schema string.
const { printSchema } = require('@mrleebo/prisma-ast');
const ast = {
type: 'schema',
declarations: [
{
type: 'model',
name: 'User',
fields: [
{ name: 'id', type: 'Int', attributes: [{ type: 'attribute', name: 'id' }, { type: 'attribute', name: 'default', args: [{ type: 'function', name: 'autoincrement' }] }] },
{ name: 'name', type: 'String', attributes: [] }
]
}
]
};
const schema = printSchema(ast);
console.log(schema);
The official Prisma ORM package. While it does not provide direct AST manipulation capabilities, it offers a comprehensive set of tools for working with databases using Prisma schema files. It focuses more on database interactions and migrations rather than schema parsing and manipulation.
A package that provides a Domain Specific Language (DSL) for generating Prisma schema files. It allows for programmatic creation of Prisma schema definitions but does not offer the same level of AST manipulation as @mrleebo/prisma-ast.
A package that converts Prisma schema files to DBML (Database Markup Language) format. It focuses on schema conversion rather than direct AST manipulation, making it useful for different use cases compared to @mrleebo/prisma-ast.
This library uses an abstract syntax tree to parse schema.prisma files into an object in JavaScript. It also allows you to update your Prisma schema files using a Builder object pattern that is fully implemented in TypeScript.
It is similar to @prisma/sdk except that it preserves comments and model attributes. It also doesn't attempt to validate the correctness of the schema at all; the focus is instead on the ability to parse the schema into an object, manipulate it using JavaScript, and re-print the schema back to a file without losing information that isn't captured by other parsers.
It is probable that a future version of @prisma/sdk will render this library obsolete.
npm install @mrleebo/prisma-ast
produceSchema(source: string, (builder: PrismaSchemaBuilder) => void, printOptions?: PrintOptions): string
produceSchema is the simplest way to interact with prisma-ast; you input your schema source and a producer function to produce modifications to it, and it will output the schema source with your modifications applied.
import { produceSchema } from '@mrleebo/prisma-ast';
const source = `
model User {
id Int @id @default(autoincrement())
name String @unique
}
`;
const output = produceSchema(source, (builder) => {
builder
.model('AppSetting')
.field('key', 'String', [{ name: 'id' }])
.field('value', 'Json');
});
model User {
id Int @id @default(autoincrement())
name String @unique
}
model AppSetting {
key String @id
value Json
}
For more information about what the builder can do, check out the PrismaSchemaBuilder class.
The produceSchema()
utility will construct a builder for you, but you can also create your own instance, which may be useful for more interactive use-cases.
import { createPrismaSchemaBuilder } from '@mrleebo/prisma-ast';
const builder = createPrismaSchemaBuilder();
builder
.model('User')
.field('id', 'Int')
.attribute('id')
.attribute('default', [{ name: 'autoincrement' }])
.field('name', 'String')
.attribute('unique')
.break()
.comment('this is a comment')
.blockAttribute('index', ['name']);
const output = builder.print();
model User {
id Int @id @default(autoincrement())
name String @unique
// this is a comment
@@index([name])
}
prisma-ast can sort the schema for you. The default sort order is ['generator', 'datasource', 'model', 'enum']
and will sort objects of the same type alphabetically.
print(options?: {
sort: boolean,
locales?: string | string[],
sortOrder?: Array<'generator' | 'datasource' | 'model' | 'enum'>
})
You can optionally set your own sort order, or change the locale used by the sort.
// sort with default parameters
builder.print({ sort: true });
// sort with options
builder.print({
sort: true,
locales: 'en-US',
sortOrder: ['datasource', 'generator', 'model', 'enum'],
});
There is a lot that you can do with the schema builder. There are additional sample references available for you to explore.
prisma-ast uses lilconfig to read configuration options which can be located in any of the following files, and in several other variations (see the complete list of search paths):
"prisma-ast"
in package.json
.prisma-astrc
.prisma-astrc.json
.prisma-astrc.js
.config/.prisma-astrc
Configuration options are:
Option | Description | Default Value |
---|---|---|
parser.nodeTrackingLocation | Include the token locations of CST Nodes in the output schema. Disabled by default because it can impact parsing performance. Possible values are "none" , "onlyOffset" , and "full" . | "none" |
Here is an example of how you can customize your configuration options in package.json
.
{
"prisma-ast": {
"parser": {
"nodeTrackingLocation": "full"
}
}
}
The produceSchema
and createPrismaSchemaBuilder
functions are intended to be your interface for interacting with the prisma schema, but you can also get direct access to the AST representation if you need to edit the schema for more advanced usages that aren't covered by the methods above.
The shape of the AST is not fully documented, and it is more likely to change than the builder API.
import { getSchema } from '@mrleebo/prisma-ast';
const source = `
model User {
id Int @id @default(autoincrement())
name String @unique
}
`;
const schema = getSchema(source);
This is what builder.print()
calls internally, and is what you'd use to print if you called getSchema()
.
import { printSchema } from '@mrleebo/prisma-ast';
const source = printSchema(schema);
You can optionally re-sort the schema. The default sort order is ['generator', 'datasource', 'model', 'enum']
, and objects with the same type are sorted alphabetically, but the sort order can be overridden.
const source = printSchema(schema, {
sort: true,
locales: 'en-US',
sortOrder: ['datasource', 'generator', 'model', 'enum'],
});
FAQs
This library uses an abstract syntax tree to parse schema.prisma files into an object in JavaScript. It is similar to [@prisma/sdk](https://github.com/prisma/prisma/tree/master/src/packages/sdk) except that it preserves comments and model attributes.
The npm package @mrleebo/prisma-ast receives a total of 120,640 weekly downloads. As such, @mrleebo/prisma-ast popularity was classified as popular.
We found that @mrleebo/prisma-ast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.