-
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.
Merge pull request #1 from thenamespace/add-mongo-db-support
Add mongo db support
- Loading branch information
Showing
13 changed files
with
361 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GatewayModule } from './gateway/gateway.module'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { AppProperties } from './configuration/app-properties'; | ||
import { AppPropertiesModule } from './configuration/app-properties.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
isGlobal: true, | ||
envFilePath: ['.env'], | ||
}), | ||
GatewayModule], | ||
controllers: [], | ||
providers: [], | ||
MongooseModule.forRootAsync({ | ||
imports: [AppPropertiesModule], | ||
useFactory: async (configService: AppProperties) => ({ | ||
uri: configService.mongoConnectionString | ||
}), | ||
inject: [AppProperties] | ||
}), | ||
GatewayModule | ||
], | ||
}) | ||
export class AppModule {} |
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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppProperties } from './app-properties'; | ||
|
||
@Module({ | ||
exports: [AppProperties], | ||
providers: [AppProperties], | ||
}) | ||
export class AppPropertiesModule {} |
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,14 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
|
||
@Injectable() | ||
export class AppProperties { | ||
|
||
mongoConnectionString: string | ||
signerWallet: string | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
this.mongoConnectionString = this.configService.getOrThrow("MONGO_CONNECTION_STRING"); | ||
this.signerWallet = this.configService.getOrThrow("SIGNER_WALLET") | ||
} | ||
} |
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 { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import * as mongoose from 'mongoose'; | ||
|
||
@Schema({ timestamps: true, collection: "subnames", autoCreate: false, }) | ||
class Subname { | ||
@Prop({ unique: true, required: true, type: String }) | ||
fullName: string; | ||
|
||
@Prop({ required: true, type: String }) | ||
label: string; | ||
|
||
@Prop({ required: true, type: String }) | ||
domain: string; | ||
|
||
@Prop({ required: false, type: String }) | ||
ttl: number; | ||
|
||
@Prop({ | ||
required: true, | ||
type: Map, | ||
default: [], | ||
_id: false, | ||
}) | ||
addresses: Map<string, string>; | ||
|
||
@Prop({ | ||
required: false, | ||
type: Map, | ||
default: [], | ||
_id: false, | ||
}) | ||
textRecords: Map<string, string>; | ||
|
||
@Prop({ required: false, type: String }) | ||
contentHash: string; | ||
} | ||
|
||
export const SUBANME_DOMAIN = 'Subname'; | ||
export const SubnameSchema = SchemaFactory.createForClass(Subname); | ||
export type SubnameDocument = mongoose.HydratedDocument<Subname>; |
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,14 @@ | ||
import { InjectModel } from "@nestjs/mongoose"; | ||
import { SUBANME_DOMAIN, SubnameDocument } from "./resolved-ens.schema"; | ||
import { Model } from "mongoose"; | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
@Injectable() | ||
export class ResolvedEnsRepository { | ||
@InjectModel(SUBANME_DOMAIN) | ||
dao: Model<SubnameDocument> | ||
|
||
public async findOne(query: Record<string,any>): Promise<SubnameDocument | null> { | ||
return this.dao.findOne(query); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,9 +1,27 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { GatewayController } from "./gateway.controller"; | ||
import { GatewayService } from "./gateway.service"; | ||
import { Module } from '@nestjs/common'; | ||
import { GatewayController } from './gateway.controller'; | ||
import { GatewayService } from './gateway.service'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { SUBANME_DOMAIN, SubnameSchema } from './db/resolved-ens.schema'; | ||
import { GatewayDatabaseResolver } from './resolver/database.resolver'; | ||
import { AppPropertiesModule } from 'src/configuration/app-properties.module'; | ||
import { ResolvedEnsRepository } from './db/resovled-ens.repository'; | ||
|
||
@Module({ | ||
controllers: [GatewayController], | ||
providers: [GatewayService] | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ | ||
schema: SubnameSchema, | ||
name: SUBANME_DOMAIN, | ||
}, | ||
]), | ||
AppPropertiesModule, | ||
], | ||
controllers: [GatewayController], | ||
providers: [ | ||
GatewayService, | ||
GatewayDatabaseResolver, | ||
ResolvedEnsRepository | ||
], | ||
}) | ||
export class GatewayModule {} | ||
export class GatewayModule {} |
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,62 @@ | ||
import { | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { GatewayResolver, ResolverResult } from './gatway.resolver'; | ||
import { ResolvedEnsRepository } from '../db/resovled-ens.repository'; | ||
|
||
// 60 for ethereum address | ||
const DEFAULT_ADDRESS_COIN_TYPE = '60'; | ||
|
||
@Injectable() | ||
export class GatewayDatabaseResolver implements GatewayResolver { | ||
constructor(private readonly repository: ResolvedEnsRepository) {} | ||
|
||
public async getText(fullName: string, key: string): Promise<ResolverResult> {; | ||
const subname = await this.getSubname(fullName); | ||
|
||
if (subname.textRecords && subname.textRecords[key]) { | ||
return { | ||
value: subname.textRecords[key], | ||
ttl: subname.ttl || 0 | ||
} | ||
} | ||
return { | ||
ttl: 0, | ||
value: "" | ||
}; | ||
} | ||
|
||
public async getAddress(fullName: string, coinType?: string): Promise<ResolverResult> { | ||
const subname = await this.getSubname(fullName); | ||
const _coin = coinType || DEFAULT_ADDRESS_COIN_TYPE; | ||
if (subname.addresses && subname.addresses[_coin]) { | ||
return { | ||
value: subname.addresses[_coin], | ||
ttl: subname.ttl || 0 | ||
} | ||
} | ||
return { | ||
ttl: 0, | ||
value: "" | ||
}; | ||
} | ||
|
||
public async getContentHash(fullName: string): Promise<ResolverResult> { | ||
const subname = await this.getSubname(fullName); | ||
return { | ||
value: subname.contentHash || "", | ||
ttl: subname.ttl || 0 | ||
} | ||
} | ||
|
||
private getSubname = async (ensName: string) => { | ||
const subname = await this.repository.findOne({ | ||
fullName: ensName | ||
}); | ||
if (!subname && !subname.id) { | ||
throw new NotFoundException(`Subname not found`); | ||
} | ||
return subname; | ||
}; | ||
} |
Oops, something went wrong.