npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magicdog-introspection

v1.0.2

Published

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.

Downloads

2

Readme

prisma-db-introspection

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.

Convenient Shorthand usage

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.

Detailed Usage

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 Internals

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 Database Introspection Internals

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.

Normalization Pipeline

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:

  1. Copy Enum definitions from the existing base model.
  2. Remove all relation names which are not needed, unless they are explicitly given by the base model.
  3. Normalize all type and field names. If the type or field is present in the base model, copy the name and directives, as they might be known to prisma, but not to the database.
  4. Re-order all models according to the base model. For new types and enums, order alphabetically.
  5. Hide all reserved fields, like updatedAt, createdAt, id, unless they are present in the base model.
  6. Adjust the cardinality of relations which use a join table according to the base model, since we cannot guess them from the database.
  7. Remove all back relations for inine relations, except they are given in the datamodel. This is especially important for self-relations, which would otherwise generate duplicated fields.

Test Environment

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