Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
prisma-db-introspection
Advanced tools
This module is capable of generating a prisma datamodel for a relational or document databases. Please refer to the `prisma-datamodel` doc for more information on the `ISDL` datamodel structure.
This module is capable of generating a prisma datamodel for a relational or document databases. Please refer to the prisma-datamodel
doc for more information on the ISDL
datamodel structure.
Iintrospection and rendering in one step):
const renderedSdl = await connector
.introspect(schema)
.renderToNormalizdDatamodelString()
Creating a connector:
const connector = Connectors.create(DatabaseType.mysql, client)
const connector = Connectors.create(DatabaseType.postgres, client)
const connector = Connectors.create(DatabaseType.mongo, client)
The database client has to be connected and disconnected by the caller. Please refer to the connector implementation to see the required client library.
Introspect the database:
const introspection = await connector.introspect(schema)
The introspection result caches all database related information and is database specific.
Create an ISDL
structure from the introspection:
const sdl: ISDL = await introspection.getNormalizedDatamodel()
or with an existing reference model:
const sdl: ISDL = await introspection.getNormalizedDatamodel(referenceModel)
it is also possible to get an unnormalized datamodel, basically a raw introspection result from the database. The unnormalized model is most likely not a valid datamodel.
const sdl: ISDL = await introspection.getDatamodel() // Don't use unless you know what you're doing
Rendering can be done using prisma-datamodel
.
With prototype features enabled (V1.1):
const renderer = Renderers.create(introspection.databaseType, true)
const renderedSdl = renderer.render(sdl)
Without prototype features, simply use the shorthand:
const renderer = Renderers.create(introspection.databaseType)
const renderedSdl = renderer.render(sdl)
Which is equivalent to, given the database type for rendering and introspection are the same:
const renderedSdl = introspection.renderToDatamodelString()
Or with an existing reference model:
const renderedSdl = introspection.renderToNormalizedDatamodelString(
referenceModel,
)
Document database introspection works by randomly sampling objects from each collection, inferring a schema for each object and merging these object schemas together. This is done in the class ModelSampler
.
For each field which might be a reference to another collection, we attempt to look up objects in other collections by their primary key. If we find enough referred objects, we mark the field as relation. This is done in RelationResolver
, and can be a somewhat expensive operation.
This approach should work for all forms of document DBs. The MongoConnector
class can be used as a reference implementation.
Relational introspection works by querying the database schema using SQL, and then bringing everything together.
Relations are resolved via their corresponding FK constraints, IDs are resolved via their corresponding PK constraints.
Here, MysqlConnector
, MySqlIntrospectionResult
, PostgresConnector
and PostgresIntrospectionResult
can serve as a reference.
There is a common base class, RelationalConnector
which attempts to unify certain queries using the information_schema
table, which should be standardized. This approach had limited success in practice.
The exact normalization pipeline can be found in the DefaultNormalizer
factory class. In the most complex case, introspecting a relational database with an existing base model, the following steps are applied:
updatedAt
, createdAt
, id
, unless they are present in the base model.In the root directory, there is a docker file to spin up test databases locally.
Here is the corresponding environment:
#!bash
export TEST_PG_DB=prisma
export TEST_PG_HOST=localhost
export TEST_PG_USER=prisma
export TEST_PG_PASSWORD=prisma
export TEST_PG_PORT=5433
export TEST_MONGO_URI=mongodb://prisma:localhost:27018@mongo
export TEST_MONGO_SCHEMA=SchemaIntrospectionTest
export TEST_MYSQL_HOST=localhost
export TEST_MYSQL_PORT=3307
export TEST_MYSQL_USER=root
export TEST_MYSQL_PASSWORD=prisma
FAQs
This module is capable of generating a prisma datamodel for a relational or document databases. Please refer to the `prisma-datamodel` doc for more information on the `ISDL` datamodel structure.
We found that prisma-db-introspection demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.