Skip to content

Commit

Permalink
Add Prisma and new folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DemianParkhomenko committed Sep 25, 2023
1 parent 458da3e commit 2a6bee9
Show file tree
Hide file tree
Showing 37 changed files with 690 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"semi": false,
"semi": true,
"trailingComma": "es5",
"singleQuote": true
}
16 changes: 12 additions & 4 deletions api/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
"parserOptions": {
"sourceType": "module"
},
"rules": {
"strict": ["error", "never"]
},
"globals": {
"api": "readonly",
"db": "readonly",
"common": "readonly"
"crud": "readonly",
"common": "readonly",
"domain": "readonly",
"lib": "readonly",
"context": "readonly",
"config": "readonly",
"metarhia": "readonly"
},
"rules": {
"strict": ["error", "never"],
"id-denylist": [2, "api", "db", "common", "console"]
}
}
1 change: 0 additions & 1 deletion api/city.js

This file was deleted.

14 changes: 0 additions & 14 deletions api/country.js

This file was deleted.

35 changes: 7 additions & 28 deletions api/user.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
module.exports = ({ db, common }) => {
const user = db('users')

return {
async read(id) {
return await user.read(id, ['id', 'login'])
},

async create({ login, password }) {
const passwordHash = await common.hash(password)
return await user.create({ login, password: passwordHash })
},

async update(id, { login, password }) {
const passwordHash = await common.hash(password)
return await user.update(id, { login, password: passwordHash })
},

async delete(id) {
return await user.delete(id)
},

async find(mask) {
const sql = 'SELECT login from users where login like $1'
return await user.query(sql, [mask])
},
}
}
({
...crud('user'),
read: async () => {
console.warn('🚨 This is a warning');
return await await db.user.findMany();
},
});
20 changes: 0 additions & 20 deletions config.js

This file was deleted.

22 changes: 22 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
db: {
url: 'postgresql://marcus:marcus@localhost:5432/example',
},
api: {
path: './api',
port: 8001,
transport: 'ws',
},
static: {
port: 8000,
},
load: {
timeout: 5000,
displayErrors: false,
},
logger: { name: 'winston', fsPath: './logs', console: true },
sandbox: {
timeout: 5000,
displayErrors: false,
},
};
53 changes: 0 additions & 53 deletions db.js

This file was deleted.

25 changes: 0 additions & 25 deletions db/data.sql

This file was deleted.

5 changes: 0 additions & 5 deletions db/install.sql

This file was deleted.

3 changes: 0 additions & 3 deletions db/setup.sh

This file was deleted.

41 changes: 0 additions & 41 deletions db/structure.sql

This file was deleted.

14 changes: 0 additions & 14 deletions hash.js

This file was deleted.

35 changes: 35 additions & 0 deletions lib/db/crud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

const crud = (model) => ({
async read(id) {
if (!id) {
return await prisma[model].findMany();
}
return prisma[model].findUnique({
where: { id },
});
},

async create(record) {
return prisma[model].create({
data: record,
});
},

async update(id, record) {
return prisma[model].update({
where: { id },
data: record,
});
},

async delete(id) {
return prisma[model].delete({
where: { id },
});
},
});

module.exports = crud;
14 changes: 14 additions & 0 deletions lib/db/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

/**
* @typedef { import("@prisma/client").User} User
*/
const { PrismaClient } = require('@prisma/client');

const start = async (config) => {
const prisma = new PrismaClient({ datasourceUrl: config.url });
await prisma.$connect();
return prisma;
};

module.exports = start;
24 changes: 12 additions & 12 deletions logger/pino.js → lib/logger/pino.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const pino = require('pino')
const pino = require('pino');

module.exports = (config) => {
const targets = []
const targets = [];
if (config.console) {
targets.push({
level: 'trace',
target: 'pino-pretty',
})
});
}
if (config.fsPath) {
targets.push({
Expand All @@ -15,26 +15,26 @@ module.exports = (config) => {
options: {
destination: `${config.fsPath}/${config.name}/combined.log`,
},
})
});
}
const transport = pino.transport({
targets,
})
const logger = pino(transport)
});
const logger = pino(transport);

return {
log(...args) {
// TODO: issue https://github.com/pinojs/pino-pretty/issues/455
logger.info(...args)
logger.info(...args);
},
info(...args) {
logger.info(...args)
logger.info(...args);
},
warn(...args) {
logger.warn(...args)
logger.warn(...args);
},
error(...args) {
logger.error(...args)
logger.error(...args);
},
}
}
};
};
Loading

0 comments on commit 2a6bee9

Please sign in to comment.