From a517966d20a8e84d0ea48d8b3c8e7f1ab1d258e9 Mon Sep 17 00:00:00 2001 From: aksuharun Date: Mon, 18 Sep 2023 20:17:05 +0300 Subject: [PATCH 1/3] Update week-3/week-3-models-and-services as ES Modules --- week-3/week-3-models-and-services/index.js | 8 +-- .../models/meetup.js | 6 +- .../models/person.js | 4 +- .../package-lock.json | 56 +++++++++++++------ .../week-3-models-and-services/package.json | 3 +- .../services/base-service.js | 8 ++- .../services/meetup-service.js | 11 +++- .../services/person-service.js | 11 +++- 8 files changed, 74 insertions(+), 33 deletions(-) diff --git a/week-3/week-3-models-and-services/index.js b/week-3/week-3-models-and-services/index.js index 1511dd0..5fed547 100644 --- a/week-3/week-3-models-and-services/index.js +++ b/week-3/week-3-models-and-services/index.js @@ -1,7 +1,7 @@ -const Person = require('./models/person') -const Meetup = require('./models/meetup') -const PersonService = require('./services/person-service') -const MeetupService = require('./services/meetup-service') +import Person from './models/person.js' +import Meetup from './models/meetup.js' +import PersonService from './services/person-service.js' +import MeetupService from './services/meetup-service.js' console.log('Hello World!') console.log('Hello World!') diff --git a/week-3/week-3-models-and-services/models/meetup.js b/week-3/week-3-models-and-services/models/meetup.js index 7b58d0f..bc3bfe4 100644 --- a/week-3/week-3-models-and-services/models/meetup.js +++ b/week-3/week-3-models-and-services/models/meetup.js @@ -1,6 +1,6 @@ -const chalk = require('chalk') +import chalk from 'chalk' -module.exports = class Meetup { +const Meetup = class { constructor(name, location, attendees = [], id) { this.name = name this.location = location @@ -16,3 +16,5 @@ module.exports = class Meetup { return new Meetup(name, location, attendees, id) } } + +export default Meetup \ No newline at end of file diff --git a/week-3/week-3-models-and-services/models/person.js b/week-3/week-3-models-and-services/models/person.js index eb37f79..d6b6829 100644 --- a/week-3/week-3-models-and-services/models/person.js +++ b/week-3/week-3-models-and-services/models/person.js @@ -1,4 +1,4 @@ -module.exports = class Person { +const Person = class { constructor(name, age, meetups = [], id) { this.name = name this.age = age @@ -15,3 +15,5 @@ module.exports = class Person { return new Person(name, age, meetups, id); } } + +export default Person diff --git a/week-3/week-3-models-and-services/package-lock.json b/week-3/week-3-models-and-services/package-lock.json index 332c2bc..25d85e4 100644 --- a/week-3/week-3-models-and-services/package-lock.json +++ b/week-3/week-3-models-and-services/package-lock.json @@ -1,61 +1,85 @@ { "name": "week-3-models-and-services", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 3, "requires": true, - "dependencies": { - "ansi-styles": { + "packages": { + "": { + "name": "week-3-models-and-services", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^2.4.2", + "flatted": "^2.0.1" + } + }, + "node_modules/ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "requires": { + "dependencies": { "color-convert": "^1.9.0" + }, + "engines": { + "node": ">=4" } }, - "chalk": { + "node_modules/chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", - "requires": { + "dependencies": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", "supports-color": "^5.3.0" + }, + "engines": { + "node": ">=4" } }, - "color-convert": { + "node_modules/color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", - "requires": { + "dependencies": { "color-name": "1.1.3" } }, - "color-name": { + "node_modules/color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, - "escape-string-regexp": { + "node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "engines": { + "node": ">=0.8.0" + } }, - "flatted": { + "node_modules/flatted": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.1.tgz", "integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==" }, - "has-flag": { + "node_modules/has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "engines": { + "node": ">=4" + } }, - "supports-color": { + "node_modules/supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "requires": { + "dependencies": { "has-flag": "^3.0.0" + }, + "engines": { + "node": ">=4" } } } diff --git a/week-3/week-3-models-and-services/package.json b/week-3/week-3-models-and-services/package.json index 08c74ac..97c513c 100644 --- a/week-3/week-3-models-and-services/package.json +++ b/week-3/week-3-models-and-services/package.json @@ -11,5 +11,6 @@ "dependencies": { "chalk": "^2.4.2", "flatted": "^2.0.1" - } + }, + "type": "module" } diff --git a/week-3/week-3-models-and-services/services/base-service.js b/week-3/week-3-models-and-services/services/base-service.js index 15e1c43..922a6d3 100644 --- a/week-3/week-3-models-and-services/services/base-service.js +++ b/week-3/week-3-models-and-services/services/base-service.js @@ -1,7 +1,7 @@ -const fs = require('fs') -const Flatted = require('flatted/cjs'); +import fs from 'fs' +import Flatted from 'flatted' -module.exports = class Service { +const Service = class { constructor(model, dbPath) { this.model = model this.dbPath = dbPath @@ -66,3 +66,5 @@ module.exports = class Service { }) } } + +export default Service \ No newline at end of file diff --git a/week-3/week-3-models-and-services/services/meetup-service.js b/week-3/week-3-models-and-services/services/meetup-service.js index 41914cd..1eb91cd 100644 --- a/week-3/week-3-models-and-services/services/meetup-service.js +++ b/week-3/week-3-models-and-services/services/meetup-service.js @@ -1,5 +1,10 @@ -const BaseService = require('./base-service') -const MeetupModel = require('../models/meetup') +import BaseService from './base-service.js' +import MeetupModel from '../models/meetup.js' +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); class MeetupService extends BaseService { constructor() { @@ -7,4 +12,4 @@ class MeetupService extends BaseService { } } -module.exports = new MeetupService() +export default new MeetupService() \ No newline at end of file diff --git a/week-3/week-3-models-and-services/services/person-service.js b/week-3/week-3-models-and-services/services/person-service.js index 1a4fbc8..11cf20b 100644 --- a/week-3/week-3-models-and-services/services/person-service.js +++ b/week-3/week-3-models-and-services/services/person-service.js @@ -1,5 +1,10 @@ -const BaseService = require('./base-service') -const PersonModel = require('../models/person') +import BaseService from './base-service.js' +import PersonModel from '../models/person.js' +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); class PersonService extends BaseService { constructor() { @@ -7,4 +12,4 @@ class PersonService extends BaseService { } } -module.exports = new PersonService() +export default new PersonService() From caf0b1659313a5c9795c34da76bc75a3c4b5d21d Mon Sep 17 00:00:00 2001 From: aksuharun Date: Tue, 19 Sep 2023 19:32:05 +0300 Subject: [PATCH 2/3] Migrate functions to use fs/promises in database.js --- .../services/base-service.js | 122 +++++++++--------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/week-3/week-3-models-and-services/services/base-service.js b/week-3/week-3-models-and-services/services/base-service.js index 922a6d3..909e70e 100644 --- a/week-3/week-3-models-and-services/services/base-service.js +++ b/week-3/week-3-models-and-services/services/base-service.js @@ -1,70 +1,64 @@ -import fs from 'fs' +import {promises as fsp} from 'fs' import Flatted from 'flatted' const Service = class { - constructor(model, dbPath) { - this.model = model - this.dbPath = dbPath - } - - - async findAll() { - return new Promise((resolve, reject) => { - fs.readFile(this.dbPath, 'utf8', async (err, file) => { - if (err) { - if (err.code == 'ENOENT') { - await this.saveAll([]) - return resolve([]) - } - - return reject(err) - } - - const items = Flatted.parse(file).map(this.model.create) - - resolve(items) - }) - }) - } - - async add(item) { - const allItems = await this.findAll() - const lastItem = allItems[allItems.length - 1] - const lastItemsId = lastItem && lastItem.id || 0 - item.id = lastItemsId + 1 - - allItems.push(item) - - await this.saveAll(allItems) - - return item - } - - async del(itemId) { - const allItems = await this.findAll() - const itemIndex = allItems.findIndex(p => p.id == itemId) - if (itemIndex < 0) return - - allItems.splice(itemIndex, 1) - - await this.saveAll(allItems) - } - - async find(itemId = 1) { - const allItems = await this.findAll() - - return allItems.find(p => p.id == itemId) - } - - async saveAll(items) { - return new Promise((resolve, reject) => { - fs.writeFile(this.dbPath, Flatted.stringify(items), (err, file) => { - if (err) return reject(err) - - resolve() - }) - }) - } + constructor(model, dbPath) { + this.model = model + this.dbPath = dbPath + } + + + async findAll() { + try{ + const file = await fsp.readFile(this.dbPath, 'utf8'); + const items = Flatted.parse(file).map(this.model.create) + return items + }catch(err){ + if(err.code == 'ENOENT') { + await this.saveAll([]) + return([]) + } + console.log(err) + return err + } + } + + async add(item) { + const allItems = await this.findAll() + const lastItem = allItems[allItems.length - 1] + const lastItemsId = lastItem && lastItem.id || 0 + item.id = lastItemsId + 1 + + allItems.push(item) + + await this.saveAll(allItems) + + return item + } + + async del(itemId) { + const allItems = await this.findAll() + const itemIndex = allItems.findIndex(p => p.id == itemId) + if (itemIndex < 0) return + + allItems.splice(itemIndex, 1) + + await this.saveAll(allItems) + } + + async find(itemId = 1) { + const allItems = await this.findAll() + + return allItems.find(p => p.id == itemId) + } + + async saveAll(items) { + try{ + return await fsp.writeFile(this.dbPath, Flatted.stringify(items)) + }catch(err){ + return err + } + } } export default Service \ No newline at end of file From f566774130b7bd90af48ac9e0b306503fae8af22 Mon Sep 17 00:00:00 2001 From: aksuharun Date: Wed, 20 Sep 2023 15:32:15 +0300 Subject: [PATCH 3/3] Update flatted dependency to v3.2.9 --- week-3/week-3-models-and-services/package-lock.json | 8 ++++---- week-3/week-3-models-and-services/package.json | 2 +- .../week-3-models-and-services/services/base-service.js | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/week-3/week-3-models-and-services/package-lock.json b/week-3/week-3-models-and-services/package-lock.json index 25d85e4..1333b5c 100644 --- a/week-3/week-3-models-and-services/package-lock.json +++ b/week-3/week-3-models-and-services/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "chalk": "^2.4.2", - "flatted": "^2.0.1" + "flatted": "^3.2.9" } }, "node_modules/ansi-styles": { @@ -59,9 +59,9 @@ } }, "node_modules/flatted": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.1.tgz", - "integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==" + "version": "3.2.9", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", + "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==" }, "node_modules/has-flag": { "version": "3.0.0", diff --git a/week-3/week-3-models-and-services/package.json b/week-3/week-3-models-and-services/package.json index 97c513c..936b47a 100644 --- a/week-3/week-3-models-and-services/package.json +++ b/week-3/week-3-models-and-services/package.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "chalk": "^2.4.2", - "flatted": "^2.0.1" + "flatted": "^3.2.9" }, "type": "module" } diff --git a/week-3/week-3-models-and-services/services/base-service.js b/week-3/week-3-models-and-services/services/base-service.js index 909e70e..a7bbbe3 100644 --- a/week-3/week-3-models-and-services/services/base-service.js +++ b/week-3/week-3-models-and-services/services/base-service.js @@ -1,5 +1,5 @@ import {promises as fsp} from 'fs' -import Flatted from 'flatted' +import {parse as flattedParse, stringify as flattedStringify} from 'flatted' const Service = class { constructor(model, dbPath) { @@ -11,7 +11,7 @@ const Service = class { async findAll() { try{ const file = await fsp.readFile(this.dbPath, 'utf8'); - const items = Flatted.parse(file).map(this.model.create) + const items = flattedParse(file).map(this.model.create) return items }catch(err){ if(err.code == 'ENOENT') { @@ -54,7 +54,7 @@ const Service = class { async saveAll(items) { try{ - return await fsp.writeFile(this.dbPath, Flatted.stringify(items)) + return await fsp.writeFile(this.dbPath, flattedStringify(items)) }catch(err){ return err }