Skip to content

Commit

Permalink
wip chapter 7
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Dec 24, 2024
1 parent e80828c commit 18296fd
Show file tree
Hide file tree
Showing 9 changed files with 1,202 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data.sqlite
21 changes: 21 additions & 0 deletions 07-creational-design-patterns/10-dependency-injection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 10-dependency-injection

This example shows how to wire dependencies using the dependency injection
pattern

## Dependencies

Install all necessary dependencies with:

```shell script
npm install
```

## Run

To run the example:

```shell script
node import-posts.js
node index.js
```
30 changes: 30 additions & 0 deletions 07-creational-design-patterns/10-dependency-injection/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export class Blog {
constructor(db) {
this.db = db
}

initialize() {
const initQuery = `CREATE TABLE IF NOT EXISTS posts (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`

return this.db.run(initQuery)
}

createPost(id, title, content, createdAt) {
return this.db.run(
'INSERT INTO posts VALUES (?, ?, ?, ?)',
id,
title,
content,
createdAt
)
}

getAllPosts() {
return this.db.all('SELECT * FROM posts ORDER BY created_at DESC')
}
}
9 changes: 9 additions & 0 deletions 07-creational-design-patterns/10-dependency-injection/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { open } from 'sqlite'
import sqlite3 from 'sqlite3'

export function createDb(filename) {
return open({
filename,
driver: sqlite3.Database,
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { join } from 'node:path'
import { Blog } from './blog.js'
import { createDb } from './db.js'

const posts = [
{
id: 'my-first-post',
title: 'My first post',
content: 'Hello World!\nThis is my first post',
// biome-ignore lint/style/useNamingConvention: db mapping
created_at: new Date('2020-02-03'),
},
{
id: 'iterator-patterns',
title: 'Node.js iterator patterns',
content: "Let's talk about some iterator patterns in Node.js\n\n...",
// biome-ignore lint/style/useNamingConvention: db mapping
created_at: new Date('2020-02-06'),
},
{
id: 'dependency-injection',
title: 'Dependency injection in Node.js',
content:
'Today we will discuss about dependency injection in Node.js\n\n...',
// biome-ignore lint/style/useNamingConvention: db mapping
created_at: new Date('2020-02-29'),
},
// ...
]

const db = await createDb(join(import.meta.dirname, 'data.sqlite'))
const blog = new Blog(db)
await blog.initialize()

await Promise.all(
posts.map(post =>
blog.createPost(post.id, post.title, post.content, post.created_at)
)
)
console.log('All posts imported')
21 changes: 21 additions & 0 deletions 07-creational-design-patterns/10-dependency-injection/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { join } from 'node:path'
import { Blog } from './blog.js'
import { createDb } from './db.js'

const db = await createDb(join(import.meta.dirname, 'data.sqlite'))
const blog = new Blog(db)
await blog.initialize()
const posts = await blog.getAllPosts()
if (posts.length === 0) {
console.log(
'No post available. Run `node import-posts.js`' +
' to load some sample posts'
)
}

for (const post of posts) {
console.log(post.title)
console.log('-'.repeat(post.title.length))
console.log(`Published on ${new Date(post.created_at).toISOString()}`)
console.log(post.content)
}
18 changes: 18 additions & 0 deletions 07-creational-design-patterns/10-dependency-injection/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "10-dependency-injection",
"version": "1.0.0",
"description": "This example shows how to wire dependencies using the dependency injection pattern",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {
"sqlite": "^5.1.1",
"sqlite3": "^5.1.7"
}
}
Loading

0 comments on commit 18296fd

Please sign in to comment.