@cowris/userdb-client
v1.1.2
Published
This is a Prisma Client package for the Cowris PostgreSQL UserDB. It provides a simple interface to interact with the UserDB, allowing you to perform CRUD operations on user data and relared tables.
Readme
@cowris/userdb-client
A Prisma Client package for the Cowris PostgreSQL UserDB.
Description
This package provides a centralized Prisma client for the Cowris PostgreSQL UserDB. It simplifies database access and ensures consistency by sharing the Prisma configuration and client instance. It offers a simple interface to interact with the UserDB, allowing you to perform CRUD operations on user data and related tables.
Installation
npm install @cowris/userdb-clientUsage
Import the Prisma Client:
JavaScript
const prisma = require('@cowris/userdb-client');
async function main() {
const allUsers = await prisma.user.findMany();
console.log(allUsers);
}
main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});Ensure Database Connection
Make sure the microservice has the USER_DATABASE_URL environment variable set, pointing to the PostgreSQL database.
Setup the database in microservice
Add this under scripts in package.json in the microservice
"setup-prisma:userdb": "node node_modules/@cowris/userdb-client/scripts/migrate.js || echo 'Migration failed. Please install the cowris userdb client. Run `npm install @cowris/userdb-client` to install it.'"File Structure
@cowris/userdb-client/
├── prisma/
│ ├── migrations/
│ ├── schema/
│ │ ├── user.prisma
│ │ └── schema.prisma // Main schema file
│ └── .env
├── node_modules/
├── package.json
├── package-lock.json
└── index.jsprisma/schema/: Contains individual Prisma model files (user.prisma, etc.) and the mainschema.prismafile that combines them.prisma/migrations/: Stores Prisma migration files (if used).prisma/.env: Holds the database connection string (DATABASE_URL).index.js: Exports the Prisma client instance.
Schema Definition
The Prisma schema is defined in the prisma/schema directory. Each model has its own .prisma file, and index.prisma imports and combines them.
Example user.prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}Updating the Schema
Modify Model Files:
Make changes to the relevant .prisma files in the prisma/schema directory.
Generate Prisma Client:
Run the following command in the package's root directory:
npx prisma generatePublish a New Version:
- Update the package version in
package.json. - Publish the updated package to your private npm registry:
npm publish --access restrictedUpdate in Microservices:
Update the package in each microservice:
npm update @cowris/userdb-clientSecurity Considerations
- Database Credentials: Do not hardcode database credentials. Use environment variables or secure credential management systems.
- Private Registry: Ensure your private npm registry is secure.
Contributing
Contributions are welcome. Please follow the standard pull request process.
License
MIT License
