-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,202 additions
and
2 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
07-creational-design-patterns/10-dependency-injection/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
data.sqlite |
21 changes: 21 additions & 0 deletions
21
07-creational-design-patterns/10-dependency-injection/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
07-creational-design-patterns/10-dependency-injection/blog.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
40 changes: 40 additions & 0 deletions
40
07-creational-design-patterns/10-dependency-injection/import-posts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
07-creational-design-patterns/10-dependency-injection/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
07-creational-design-patterns/10-dependency-injection/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.