Skip to content

Commit

Permalink
[db] Replace faker with @faker-js/faker
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Feb 11, 2022
1 parent 651bccf commit b1f0ed4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint
yarn prettier --check .

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Features

- Monorepo project structure powered by Yarn v2 with PnP
- Monorepo project structure powered by Yarn with PnP
- GraphQL API using code-first development approach (TypeScript, GraphQL.js, Knex, PostgreSQL)
- Stateless JWT cookie-based authentication (supporting SSR, OAuth 2.0 via Google, Facebook, etc.)
- Database tooling — seed files, migrations, Knex.js REPL shell, etc.
Expand Down
2 changes: 1 addition & 1 deletion api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ assistance.

## Tech Stack

- [Node.js](https://nodejs.org/) `v16`, [Yarn](https://yarnpkg.com/) `v2`, [TypeScript](https://www.typescriptlang.org/), [Babel](https://babeljs.io/), [Prettier](https://prettier.io/), [ESLint](https://eslint.org/) — core platform and dev tools
- [Node.js](https://nodejs.org/) `v16`, [Yarn](https://yarnpkg.com/), [TypeScript](https://www.typescriptlang.org/), [Babel](https://babeljs.io/), [Prettier](https://prettier.io/), [ESLint](https://eslint.org/) — core platform and dev tools
- [GraphQL.js](https://github.com/graphql/graphql-js), [GraphQL.js Relay](https://github.com/graphql/graphql-relay-js), [DataLoader](https://github.com/graphql/dataloader), [Validator.js](https://github.com/validatorjs/validator.js)[GraphQL](https://graphql.org/) schema and API endpoint(s)
- [PostgreSQL](https://www.postgresql.org/), [Knex.js](https://knexjs.org/), [`pg`](https://node-postgres.com/), [`@google-cloud/storage`](https://googleapis.dev/nodejs/storage/latest) — data access
- [`jswonwebtoken`](https://github.com/auth0/node-jsonwebtoken), [`google-auth-library`](https://github.com/googleapis/google-auth-library-nodejs) — stateless JWT-based sessions and authentication
Expand Down
11 changes: 5 additions & 6 deletions db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@
"db:migrate": "knex migrate:latest",
"db:rollback": "knex migrate:rollback",
"db:seed": "knex seed:run",
"db:reset": "babel-node --root-mode=upward -x .ts ./scripts/reset.ts",
"db:reset": "babel-node --root-mode=upward -o .,../api -x .ts ./scripts/reset.ts",
"db:reset-test": "knex migrate:rollback --env=test 001_initial && knex migrate:latest --env=test && yarn db:restore --env=test",
"db:reset-prod": "knex migrate:rollback --env=prod 001_initial && knex migrate:latest --env=prod && yarn db:restore --env=prod",
"db:backup": "babel-node --root-mode=upward -x .ts ./scripts/backup.ts",
"db:restore": "babel-node --root-mode=upward -x .ts ./scripts/restore.ts",
"db:import-seeds": "babel-node --root-mode=upward -x .ts ./scripts/import-seeds.ts",
"db:update-types": "babel-node --root-mode=upward -x .ts ./scripts/update-types.ts",
"db:import-seeds": "babel-node --root-mode=upward -o .,../api -x .ts ./scripts/import-seeds.ts",
"db:update-types": "babel-node --root-mode=upward -o .,../api -x .ts ./scripts/update-types.ts",
"db:repl": "babel-node --root-mode=upward -o .,../api -x .ts --experimental-repl-await ./scripts/repl.ts",
"db:psql": "babel-node --root-mode=upward -x .ts ./scripts/psql.ts"
"db:psql": "babel-node --root-mode=upward -o .,../api -x .ts ./scripts/psql.ts"
},
"devDependencies": {
"@babel/core": "^7.17.2",
"@babel/node": "^7.16.8",
"@babel/register": "^7.17.0",
"@faker-js/faker": "^6.0.0-alpha.6",
"@types/babel__core": "^7.1.18",
"@types/cross-spawn": "^6.0.2",
"@types/faker": "^6.6.9",
"@types/lodash": "^4.14.178",
"@types/minimist": "^1.2.2",
"@types/prettier": "^2.4.4",
"chalk": "^4.1.2",
"cross-spawn": "^7.0.3",
"envars": "^0.4.0",
"faker": "^6.6.6",
"knex": "^1.0.3",
"knex-types": "^0.3.2",
"lodash": "^4.17.21",
Expand Down
5 changes: 3 additions & 2 deletions db/seeds/01_users.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */

import { date, image, internet, name, random } from "faker";
import faker from "@faker-js/faker";
import { type Knex } from "knex";
import nanoid from "nanoid";
import fs from "node:fs/promises";
Expand All @@ -11,6 +11,7 @@ import prettier from "prettier";
// https://zelark.github.io/nano-id-cc/
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
const newUserId = nanoid.customAlphabet(alphabet, 6);
const { date, image, internet, name, random } = faker;

function stringify(obj: Record<string, unknown>) {
return prettier.format(JSON.stringify(obj), { parser: "json" });
Expand Down Expand Up @@ -62,5 +63,5 @@ export async function seed(db: Knex) {
await fs.writeFile(jsonFile, stringify(users));
}

await db.table("user").insert(users);
await db.table("user").insert(users).onConflict(["id"]).ignore();
}
4 changes: 3 additions & 1 deletion scripts/api-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const args = minimist(process.argv.slice(2));
process.env.NODE_ENV = "production";
process.env.APP_ENV = args.env ?? process.env.APP_ENV ?? "test";
envars.config({ env: process.env.APP_ENV });
delete process.env.PGSSLMODE;

// Load the list of environment variables required by the app (api/env.ts)
/** @type {import("../api/env").default} */
Expand Down Expand Up @@ -47,7 +48,8 @@ await $`gcloud functions deploy ${name} ${[
// `--signature-type=http`,
`--source=./dist`,
`--timeout=30`,
`--trigger-http`,
`--set-env-vars=NODE_OPTIONS=--require=./.pnp.cjs --require=source-map-support/register --no-warnings`,
...Object.keys(env).map((key) => `--set-env-vars=${key}=${env[key]}`),
`--max-instances=4`,
`--trigger-http`,
]}`;
26 changes: 8 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,13 @@ __metadata:
languageName: node
linkType: hard

"@faker-js/faker@npm:^6.0.0-alpha.6":
version: 6.0.0-alpha.6
resolution: "@faker-js/faker@npm:6.0.0-alpha.6"
checksum: 7da62a22a8e1bfbcd8b6517a6f3aa74cf468a681cea4b3cd306a285ec670dc591d48cb5191fc1131fba9fc669ba12f9c1b4849eb47f9be50c8476f72705bcc99
languageName: node
linkType: hard

"@gar/promisify@npm:^1.0.1":
version: 1.1.2
resolution: "@gar/promisify@npm:1.1.2"
Expand Down Expand Up @@ -3020,15 +3027,6 @@ __metadata:
languageName: node
linkType: hard

"@types/faker@npm:^6.6.9":
version: 6.6.9
resolution: "@types/faker@npm:6.6.9"
dependencies:
faker: "*"
checksum: 8fb069b6f2bf2cefec42a214395f49695b881d70e651a215a61c2fa7494bb173d1a598c895882ff89df3702ab51c49c23433b3d4bcfd8323cf4f608ed9fb6ce8
languageName: node
linkType: hard

"@types/fs-extra@npm:^8.0.1":
version: 8.1.2
resolution: "@types/fs-extra@npm:8.1.2"
Expand Down Expand Up @@ -5612,16 +5610,15 @@ __metadata:
"@babel/core": ^7.17.2
"@babel/node": ^7.16.8
"@babel/register": ^7.17.0
"@faker-js/faker": ^6.0.0-alpha.6
"@types/babel__core": ^7.1.18
"@types/cross-spawn": ^6.0.2
"@types/faker": ^6.6.9
"@types/lodash": ^4.14.178
"@types/minimist": ^1.2.2
"@types/prettier": ^2.4.4
chalk: ^4.1.2
cross-spawn: ^7.0.3
envars: ^0.4.0
faker: ^6.6.6
knex: ^1.0.3
knex-types: ^0.3.2
lodash: ^4.17.21
Expand Down Expand Up @@ -6708,13 +6705,6 @@ __metadata:
languageName: node
linkType: hard

"faker@npm:*, faker@npm:^6.6.6":
version: 6.6.6
resolution: "faker@npm:6.6.6"
checksum: bfb0d581ab4d76095f2c6bff213fe1735ce077f0f8df849c144ad26035e105bce1b7a704cc030366338c52e25b636ac097a712426a78c9b8804af78f6f2bffe7
languageName: node
linkType: hard

"fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3":
version: 3.1.3
resolution: "fast-deep-equal@npm:3.1.3"
Expand Down

0 comments on commit b1f0ed4

Please sign in to comment.