Skip to content

Commit

Permalink
Updated Model for Brands
Browse files Browse the repository at this point in the history
  • Loading branch information
singhkunal2050 committed Feb 21, 2025
1 parent a718b03 commit 70ab023
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 65 deletions.
22 changes: 22 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## To update schemas

1. modify schema.prisma

## After changing schema

1. Prisma Generate using `npx prisma generate` [OPTIONAL]
2. Prisma Migrate `npx prisma migrate dev --name <migration-name>`
3.

### .env file

```bash
DATABASE_URL="file:./dev.db"
```

### Testing

0. Modify the sample.ts file for testing
1. Run `npm run build
2. Run `node dist/sample.js`
3. If needed modify services
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "design.email-database",
"version": "1.0.3",
"version": "1.0.5",
"description": "A package to access EmailStencil database",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ model Emails {
}

model Domains {
id String @id @default(cuid())
name String
id Int @id @default(autoincrement())
name String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Expand Down
38 changes: 20 additions & 18 deletions sample.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { db } from './lib/prisma.js';
import { createBrand } from './services/brand.js';
import { getEmailByQuery } from './services/email.js'
import { db } from "./lib/prisma.js";
import { createBrand } from "./services/brand.js";
import { getEmailByQuery } from "./services/email.js";
import { getBrands } from "./services/brand.js";

export * from './services/email.js'
export * from './services/brand.js'
export * from "./services/email.js";
export * from "./services/brand.js";

async function main() {
const email = await createBrand({
name: 'test'
})
console.log(email);
// const email = await createBrand({
// name: "test",
// });

const brands = await getBrands();
console.log({ brands });
}

main()
.then(async () => {
await db.$disconnect()
})
.catch(async (e) => {
console.error(e)
await db.$disconnect()
process.exit(1)
})

.then(async () => {
await db.$disconnect();
})
.catch(async (e) => {
console.error(e);
await db.$disconnect();
process.exit(1);
});

/*
Expand Down
72 changes: 36 additions & 36 deletions services/brand.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { db } from "../lib/prisma.js";

export async function getBrands(offset = 0, limit = 10) {
const brands = await db.domains.findMany({
skip: offset,
take: limit,
})
return brands;
const brands = await db.domains.findMany({
skip: offset,
take: limit,
});
return brands;
}

export async function getBrand(id: string) {
const brand = await db.domains.findUnique({
where: {
id,
},
});
return brand;
export async function getBrand(id: number) {
const brand = await db.domains.findUnique({
where: {
id,
},
});
return brand;
}

export async function getBrandByQuery(query: any) {
const brand = await db.domains.findUnique({
where: {
...query,
},
});
return brand;
const brand = await db.domains.findUnique({
where: {
...query,
},
});
return brand;
}

export async function deleteBrand(id: string) {
const brand = await db.domains.delete({
where: {
id,
},
});
return brand;
export async function deleteBrand(id: number) {
const brand = await db.domains.delete({
where: {
id,
},
});
return brand;
}

export async function updateBrand(id: string, data: any) {
const brand = await db.domains.update({
where: {
id,
},
data,
});
return brand;
export async function updateBrand(id: number, data: any) {
const brand = await db.domains.update({
where: {
id,
},
data,
});
return brand;
}

export async function createBrand(data: any) {
const brand = await db.domains.create({ data });
return brand;
}
const brand = await db.domains.create({ data });
return brand;
}
18 changes: 11 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"declaration": false,
"declaration": false,
"outDir": "./dist",
"rootDir": "./",
"rootDir": "./",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["prisma/**/*.prisma", "./services", "./lib", "index.ts"],
"exclude": [
"node_modules"
]
}
"include": [
"prisma/**/*.prisma",
"./services",
"./lib",
"index.ts",
"sample.ts"
],
"exclude": ["node_modules"]
}

0 comments on commit 70ab023

Please sign in to comment.