Skip to content

Commit

Permalink
added:readme-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
broisnischal committed Jan 26, 2024
1 parent 23ee5e6 commit 8c21009
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 60 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# prisma-fns

## 1.1.4

### Patch Changes

- Updated README.md

## 1.1.3

### Patch Changes
Expand Down
112 changes: 71 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,83 @@ npm install prisma-fns

## Basic Usage

#### Get Values

```ts
import { PrismaClient } from "@prisma/client";
import { log, exists, getValues, generateSlug, save } from "prisma-fns";

// Create a new instance of PrismaClient extended with various functions
const prisma = new PrismaClient()
.$extends(log)
.$extends(exists)
.$extends(getValues)
.$extends(generateSlug)
.$extends(save);

// Now you can use the prisma instance with the added functions

// Example: Check if a user with a specific ID exists
async function doesUserExist(userId: number) {
const userExists = await prisma.$exists.user({ id: userId });
prisma.$log(
`User with ID ${userId} ${userExists ? "exists" : "does not exist"}`
);
}
// getValues (field, where)
const prisma = new PrismaClient().$extends(getValues);

const user = await prisma.user.getValues("email", {
email: {
contains: "@",
},
});
```

// Example: Get values from the database
async function getAllUsers() {
const users = await prisma.$getValues.user.findMany();
prisma.$log(`All users: ${JSON.stringify(users)}`);
}
#### Log

```ts
// console.log
const prisma = new PrismaClient().$extends(log);

const user = await prisma.user.findFirst();

prisma.$log(user);
```

#### Exists

```ts
const prisma = new PrismaClient().$extends(exists);

// returns boolean
const user = await prisma.user.exists({
id: 1,
});
```

#### logPerf

```ts
const prisma = new PrismaClient().$extends(logPerf);

// Example: Generate a slug
const title = "Example Blog Post Title";
const slug = prisma.$generateSlug(title);
prisma.$log(`Generated slug for "${title}": ${slug}`);

// Example: Save data to the database
const newData = {
/* Your data here */
};
const savedData = await prisma.$save.modelName.create({ data: newData });
prisma.$log(`Saved data: ${JSON.stringify(savedData)}`);

// Example: Closing the Prisma client
async function closePrisma() {
await prisma.$disconnect();
prisma.$log("Prisma client disconnected.");
const user = await prisma.user.findFirst();


// logs in console
{
model: 'User',
operation: 'findFirst',
args: {},
time: 25.3997129797935
}
```

#### remember

```ts
// creates singleton
const prisma = new PrismaClient().$extends(remember);
```

#### save

```ts
// saves object
const prisma = new PrismaClient().$extends(save);
```

#### generate slug

```ts
const prisma = new PrismaClient().$extends(generateSlug);

const user = await prisma.user.findFirst();

// returns slug
user.slug;
```

## Support

If you like the project, please consider supporting us by giving a ⭐️ on Github.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-fns",
"version": "1.1.3",
"version": "1.1.4",
"description": "",
"main": "./dist/index.js",
"private": false,
Expand Down
24 changes: 7 additions & 17 deletions script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
generateSlug,
save,
remember,
pagination,
log,
logPerf,
// pagination,
} from "./src";

const prisma = new PrismaClient()
Expand All @@ -14,25 +16,13 @@ const prisma = new PrismaClient()
.$extends(generateSlug)
.$extends(save)
.$extends(remember)
.$extends(pagination);
.$extends(log)
.$extends(logPerf);

async function main() {
// const user = await prisma.user.create({
// data: {
// name: "Alice",
// email: "[email protected]",
// },
// });
// const prisma = new PrismaClient().$extends(logPerf);

// const user = await prisma.user.getValues("email", {
// email: {
// contains: "@",
// },
// });

const user = await prisma.user.findMany({
take: 5,
});
const user = await prisma.user.findFirst();

console.log(user);
}
Expand Down
2 changes: 1 addition & 1 deletion src/fn/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default Prisma.defineExtension((prisma) => {
return prisma.$extends({
name: "log",
client: {
$log: (s: string) => console.log(s),
$log: <T>(s: T) => console.log(s) as never,
// async $totalQueries() {
// const index_prisma_client_queries_total = 0;
// const metricsCounters = await (
Expand Down

0 comments on commit 8c21009

Please sign in to comment.