albe v1.0.0
Database Connection:
Albe.js utilizes prisma as its default ORM, its shipped as an external plugin so you can replace it as per your own needs however to be able to use prisma you should first create a user in your database with proper permissions.
For local development you want to create a user with the following permissions according to prisma's documentation:
Database | Database user requirements |
---|---|
SQLite | No special requirements. |
MySQL | Database user must have CREATE, ALTER, DROP, REFERENCES ON . privileges |
PostgreSQL | The user must be a super user or have CREATEDB privilege. See CREATE ROLE (PostgreSQL official documentation) |
Microsoft SQL Server | The user must be a site admin or have the SERVER securable. See the official documentation. |
e.g. for a mysql database.
CREATE USER 'example'@'localhost' IDENTIFIED BY 'expassword';
GRANT CREATE, ALTER, DROP, REFERENCES ON *.* TO 'example'@'localhost';
FLUSH PRIVILEGES;
Warning This should only be done in local dev environments, as the leakage of the username and password with said privileges poses a high risk threat to the entire database.
If you are using a cloud hosted provider or if you need more information you can find it in prisma's documentation here.
In the root of the project you will find a .env.example file with the following content:
PORT=3000
HOST=0.0.0.0
#PUBLIC_URL=
DATABASE_URL="mysql://example:expassword@localhost:3306/exampledb"
Go ahead and make a copy with the name .env.
Right now we will only talk about the database url, this is the most important piece of information as it's what allows Albe to the database.
It consists of the protocol (in this case 'mysql') then the username (in this case 'example') then a colon ':' followed by the password of that user(in this case 'expassword') then an at symbol '@' followed by the host(in this case 'localhost') then another colon ':' this time followed by the port through which connection should be established (in this case 3306) and lastly a slash '/' followed by the database name.
It's important to note that prisma requires an existing database, so in case you haven't created it you should go ahead and create one and grant all permissions on it to the user you will be using for connections.
You will also find a folder called prisma which contains a file called schema.prisma with the following information:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
This file contain all of the information about the database, from the connection information to the construction of the tables, and this will be what you will use to make any change to it.
Warning Only replace the client if you know what you're doing!
You can modify both the client and the db sections, for example to use a different database like postgresql.
This file can be either manually modified or you can generate it with the following command:
npx prisma init -y --datasource-provider 'sqlite'
You just have to replace sqlite with the desired provider, available providers are postgresql, mysql, sqlite, sqlserver, mongodb, cockroachdb;
Changing the datasource provider probably means you will have to replace the connection url in the .env file as every protocol has its own peculiarities.
You can find more information about it here.
2 years ago