What is prisma?
Prisma is an open-source database toolkit that includes an ORM (Object-Relational Mapper), migrations, and a query builder for Node.js and TypeScript. It simplifies database access, ensures type safety, and can be used to build GraphQL and REST APIs.
What are prisma's main functionalities?
ORM (Object-Relational Mapping)
Prisma's ORM allows you to interact with your database through an object-oriented model. The code sample demonstrates how to fetch all users from the database using Prisma's ORM.
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function main() {
const allUsers = await prisma.user.findMany();
console.log(allUsers);
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
});
Migrations
Prisma Migrate generates and runs SQL migrations based on your Prisma schema. The code sample shows how to create and apply a new migration called 'init'.
npx prisma migrate dev --name init
Type Safety
Prisma ensures type safety by generating TypeScript types based on your database schema. The code sample demonstrates fetching a user with a specific ID and the result is typed as 'User'.
const user: User = await prisma.user.findUnique({ where: { id: 1 } });
Data Modeling
Prisma uses a schema file to model your database. The code sample shows a Prisma schema definition for a User model with an auto-incrementing ID, name, and unique email.
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
Query Builder
Prisma's query builder allows you to construct complex queries with ease. The code sample demonstrates ordering users by their name in ascending order.
const sortedUsers = await prisma.user.findMany({
orderBy: {
name: 'asc',
},
});
Other packages similar to prisma
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Compared to Prisma, Sequelize has been around for longer and has a more traditional API, but it might not offer the same level of type safety and simplicity in schema management.
typeorm
TypeORM is an ORM that can run in Node.js, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, and Electron platforms and can be used with TypeScript and JavaScript. TypeORM is highly influenced by other ORMs, such as Hibernate, Doctrine, and Entity Framework. It provides a similar level of type safety as Prisma but with a different approach to defining models and running migrations.
knex
Knex.js is a SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift, designed to be flexible, portable, and fun to use. It does not include an ORM by default, but it can be paired with objection.js for ORM functionality. Knex is more of a query builder than a full-fledged ORM and lacks the type safety and model generation features of Prisma.
prisma
A simple string-to-color conversion tool
Installation
Install via npm:
$ npm i prisma --save
and incorporate into your project:
// ES2015
import prisma from 'prisma';
// CommonJS
const prisma = require('prisma');
// UMD
const prisma = window.prisma;
Usage
Simply pass a value to the prisma
function, and it will return an object with the hashed color information:
const colorizedString = prisma('Hello World!');
/* colorizedString is now the object:
{
hex: '#7e7be2',
hsl: 'hsl(242, 64%, 68%)',
hslArray: [242, 0.64, 0.68],
hsla: 'hsla(242, 64%, 68%, 1)',
hslaArray: [242, 0.64, 0.68, 1],
rgb: 'rgb(126, 123, 226)',
rgbArray: [126, 123, 226],
rgba: 'rgba(126, 123, 226, 1)',
rgbaArray: [126, 123, 226, 1],
shouldTextBeDark: true
}
/*
A breakdown of the values:
hex {string}
The hexadecimal color code as a string used in CSS
hsl {string}
The computed values for HSL (hue, saturation, luminance) as a string used in CSS
hslArray {Array<number>}
The computed values for HSL as an array of numbers
hsla {string}
The computed values for HSLA (hue, saturation, luminance, alpha of opacity) as a string used in CSS
hslaArray {Array<number>}
The computed values for HSLA as an array of numbers
rgb {string}
The Computed values for RGB (red, green, blue) as a string used in CSS
rgbArray {Array<number>}
The computed values for RGB as an array of numbers
rgba {string}
The Computed values for RGBA (red, green, blue, alpha of opacity) as a string used in CSS
rgbaArray {Array<number>}
The computed values for RGBA as an array of numbers
shouldTextBeDark {boolean}
Boolean value denoting if using this color as a background-color on an element, should the foreground text color be dark. This is based on the W3C Relative Luminance Definition that is defined in the W3 spec.
How does it work?
Internally the string is hashed using a simple bitwise operation, and the resultant integer is converted into a hexadecimal code that the other values are built from. Because the hash is a bitwise operation based on charCodeAt, the consistency of the hash is guaranteed, and therefore the resultant colors are as well.
Development
Clone the git repository, and run npm install
. From there, you can run any of the following npm scripts:
build
= builds the library with NODE_ENV=development
and with source maps, outputting to the dist
folderbuild-minified
= builds the library with NODE_ENV=production
and minified, outputting tot the dist
folderdev
= runs the playground React application to see prisma
in action. Have fun playing!lint
= runs ESLint against the files in src
prepublish
= runs lint
, transpile
, build
, and build-minified
transpile
= runs babel to transpile the files in src
to an ES5-compliant version in the lib
folder