Skip to content

Commit

Permalink
feat: 🎸 add quote table and seed quote data
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 22, 2022
1 parent c7d8a8d commit e3044a1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Api similar to [dummy-api](https://dummyapi.io/docs)

Api for playing around with dummy data. Feel free to use it in your demo projects, tutorials, or testing tasks.

All authorization headers are optional.

documentation: <https://documenter.getpostman.com/view/3827865/Uyr8kHHD>

swagger url: <https://dummy-api-nestjs.herokuapp.com/api>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@nestjs/platform-express": "^8.0.0",
"@nestjs/swagger": "^5.2.1",
"@prisma/client": "^3.12.0",
"axios": "^0.26.1",
"bcryptjs": "^2.4.3",
"compression": "^1.7.4",
"dayjs": "^1.11.1",
Expand Down
26 changes: 26 additions & 0 deletions prisma/migrations/20220422024338_create_quote/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- CreateTable
CREATE TABLE "quote" (
"id" UUID NOT NULL,
"content" TEXT,
"author" TEXT,
"tags" TEXT[],
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "quote_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "index_quote_on_created_at" ON "quote"("created_at");

-- CreateIndex
CREATE INDEX "index_quote_on_content" ON "quote"("content");

-- CreateIndex
CREATE INDEX "index_quote_on_author" ON "quote"("author");

-- CreateIndex
CREATE INDEX "index_quote_on_tags" ON "quote"("tags");

-- CreateIndex
CREATE INDEX "index_quote_on_updated_at" ON "quote"("updated_at");
15 changes: 15 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ model todo {
@@index([users_id], map: "index_todo_on_users_id")
}

model quote {
id String @id @default(uuid()) @db.Uuid
content String?
author String?
tags String[]
created_at DateTime @default(now())
updated_at DateTime @default(now())
@@index([created_at], map: "index_quote_on_created_at")
@@index([content], map: "index_quote_on_content")
@@index([author], map: "index_quote_on_author")
@@index([tags], map: "index_quote_on_tags")
@@index([updated_at], map: "index_quote_on_updated_at")
}

enum Title {
mr
ms
Expand Down
27 changes: 27 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PrismaClient } from '@prisma/client';
import _ from 'lodash';
import { faker } from '@faker-js/faker';
import bcrypt from 'bcryptjs';
import axios from 'axios';

const prisma = new PrismaClient();

Expand All @@ -12,6 +13,7 @@ const prisma = new PrismaClient();
await createTags();
await createComments();
await createTodos();
await createQuotes();
})();

async function createUsers() {
Expand Down Expand Up @@ -166,3 +168,28 @@ async function createTodos() {
});
}
}

async function createQuotes() {
const rootUrl = `https://api.quotable.io`;
const response = await axios.get(`${rootUrl}/quotes`, {
params: { limit: 100 },
});
if (response) {
const responseData = response.data;
console.log('responseData = ', responseData);

if (responseData) {
const resultsList = responseData.results;
for (let index = 0; index < resultsList.length; index++) {
const item = resultsList[index];
await prisma.quote.create({
data: {
content: item.content,
author: item.author,
tags: item.tags,
},
});
}
}
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

axios@0.26.1:
axios@0.26.1, axios@^0.26.1:
version "0.26.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
Expand Down

0 comments on commit e3044a1

Please sign in to comment.