-
Notifications
You must be signed in to change notification settings - Fork 0
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
15 changed files
with
411 additions
and
5 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module.exports = { | ||
host: "127.0.0.1", | ||
mongo: { | ||
host: "localhost", | ||
user: "teloal", | ||
password: "teloal", | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
const boolVal = (val, def) => (val ? val === "true" : def); | ||
|
||
// `undefined` means the default value is ignored and an environment variable is required | ||
module.exports = { | ||
host: undefined, | ||
}; |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './adventure-land.datasource'; | ||
export * from "./adventure-land.datasource"; | ||
export * from "./mongo.datasource"; |
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,31 @@ | ||
import { inject, lifeCycleObserver, LifeCycleObserver } from "@loopback/core"; | ||
import { juggler } from "@loopback/repository"; | ||
import config from "config"; | ||
|
||
const connectorConfig = { | ||
name: "mongo", | ||
connector: "mongodb", | ||
host: config.mongo.host, | ||
port: config.mongo.port, | ||
user: config.mongo.user, | ||
password: config.mongo.password, | ||
database: config.mongo.database, | ||
useNewUrlParser: true, | ||
}; | ||
|
||
// Observe application's life cycle to disconnect the datasource when | ||
// application is stopped. This allows the application to be shut down | ||
// gracefully. The `stop()` method is inherited from `juggler.DataSource`. | ||
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html | ||
@lifeCycleObserver("datasource") | ||
export class MongoDataSource extends juggler.DataSource implements LifeCycleObserver { | ||
static dataSourceName = "mongo"; | ||
static readonly defaultConfig = connectorConfig; | ||
|
||
constructor( | ||
@inject("datasources.config.mongo", { optional: true }) | ||
dsConfig: object = connectorConfig, | ||
) { | ||
super(dsConfig); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
apps/teloalapi/src/repositories/al-character.repository.ts
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,91 @@ | ||
import { inject } from "@loopback/core"; | ||
import { | ||
Count, | ||
DefaultCrudRepository, | ||
Entity, | ||
Options, | ||
Where, | ||
model, | ||
property, | ||
} from "@loopback/repository"; | ||
import { MongoDataSource } from "../datasources"; | ||
import { AlCharacter } from "@teloal/parse-character"; | ||
|
||
type Timestamps = { | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
}; | ||
|
||
type GConstructor<T = {}> = new (...args: any[]) => T; | ||
|
||
function extendClassWithTimestamps<T extends GConstructor<Entity>>(Base: T) { | ||
@model({ | ||
name: "AlCharacter", | ||
}) | ||
class Extended extends Base { | ||
constructor(...args: any[]) { | ||
super(...args); | ||
} | ||
|
||
@property({ | ||
type: "date", | ||
}) | ||
createdAt?: Date; | ||
|
||
@property({ | ||
type: "date", | ||
}) | ||
updatedAt?: Date; | ||
} | ||
|
||
return Extended; | ||
} | ||
|
||
type TimestampedAlCharacter = AlCharacter & Timestamps; | ||
|
||
export class AlCharacterRepository extends DefaultCrudRepository< | ||
TimestampedAlCharacter, | ||
typeof AlCharacter.prototype.name | ||
> { | ||
constructor(@inject("datasources.mongo") dataSource: MongoDataSource) { | ||
super(extendClassWithTimestamps(AlCharacter), dataSource); | ||
} | ||
|
||
async create(entity: TimestampedAlCharacter, options?: Options): Promise<TimestampedAlCharacter> { | ||
entity.createdAt = new Date(); | ||
entity.updatedAt = new Date(); | ||
return super.create(entity, options); | ||
} | ||
|
||
async update(data: TimestampedAlCharacter, options?: Options): Promise<void> { | ||
data.updatedAt = new Date(); | ||
return super.update(data, options); | ||
} | ||
|
||
async updateAll( | ||
data: TimestampedAlCharacter, | ||
where?: Where<TimestampedAlCharacter>, | ||
options?: Options, | ||
): Promise<Count> { | ||
data.updatedAt = new Date(); | ||
return super.updateAll(data, where, options); | ||
} | ||
|
||
async replaceById(id: string, data: TimestampedAlCharacter, options?: Options): Promise<void> { | ||
data.updatedAt = new Date(); | ||
return super.replaceById(id, data, options); | ||
} | ||
|
||
async updateById(id: string, data: TimestampedAlCharacter, options?: Options): Promise<void> { | ||
data.updatedAt = new Date(); | ||
return super.updateById(id, data, options); | ||
} | ||
|
||
async upsert(char: AlCharacter) { | ||
if (await this.exists(char.name)) { | ||
return await this.updateById(char.name, char); | ||
} | ||
|
||
return await this.create(char); | ||
} | ||
} |
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 @@ | ||
export * from "./al-character.repository"; |
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
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,32 @@ | ||
# This docker-compose is meant for local development. | ||
# It should not be used in production. | ||
|
||
version: "3" | ||
services: | ||
teloal-mongo: | ||
image: mongo | ||
container_name: teloal-mongo | ||
restart: unless-stopped | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: mongo-user | ||
MONGO_INITDB_ROOT_PASSWORD: mongo-password | ||
command: --port 27027 | ||
ports: | ||
- "27027:27027" | ||
volumes: | ||
- ./scripts/init-mongo.js:/docker-entrypoint-initdb.d/mongo-init.js:ro | ||
|
||
teloal-mongo-express: | ||
image: mongo-express | ||
container_name: teloal-mongo-express | ||
restart: unless-stopped | ||
environment: | ||
ME_CONFIG_BASICAUTH_USERNAME: "" | ||
ME_CONFIG_MONGODB_ADMINUSERNAME: mongo-user | ||
ME_CONFIG_MONGODB_ADMINPASSWORD: mongo-password | ||
ME_CONFIG_SITE_COOKIESECRET: teloal-cookie-secret | ||
ME_CONFIG_SITE_SESSIONSECRET: teloal-session-secret | ||
ME_CONFIG_MONGODB_URL: mongodb://mongo-user:mongo-password@teloal-mongo:27027/ | ||
ME_CONFIG_OPTIONS_EDITORTHEME: dracula | ||
ports: | ||
- "127.0.0.1:7937:8081" |
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
Oops, something went wrong.