
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@appolo/mongo
Advanced tools
Mongo module for appolo
built with appolo-mongo and mongoose
npm i @appolo/mongo
key | Description | Type | Default |
---|---|---|---|
id | ModelRepository injection id | string | modelRepository |
connection | mongo connection string | string | null |
config | mongose connection options | object | {} |
exitOnDisconnect | `call process.exit on disconnect | boolean | false ` |
key | Default |
---|---|
keepAlive | true |
useNewUrlParser | true |
useCreateIndex | true |
autoReconnect | true |
reconnectTries | Number.MAX_VALUE |
reconnectInterval | 500 |
in config/modules/all.ts |
import {MongoModule} from '@appolo/mongo';
export = async function (app: App) {
await app.module(new MongoModule({connection:"mongo://mongo-connection-string"}));
}
import {define, singleton} from 'appolo'
import {schema,prop,Model,model,Ref,injectModel,Doc} from "@appolo/mongo";
@model()
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
@prop(Address)
address:Address
}
@schema()
export class Address{
@prop({type:String})
street:string
@prop({type:String})
city:string
}
@model()
@schema()
export class Comment{
@prop({type:String})
text:string
@prop({ref:User})
user:Ref<User>
}
@define()
@singleton()
export class SomeManager {
@injectModel(User) userModel:Model<User>;
async getUser(id:string): Promise<Doc<User>> {
let user = await this.userModel.findById(id)
return user;
}
}
define new schema with collectionName and mongose schema options
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
@prop(Address)
address:Address
}
The prop decorator define class property to the Mongoose schema as a property
@prop({ type:String })
firstName: string;
define sumDocument
@schema()
export class Address{
@prop({type:String})
street:string
@prop({type:String})
city:string
}
@schema()
export class User{
@prop(Address)
address:Address
}
add ref to another mongoose schema
the ref schema must be defined as model with model
@model()
@schema()
export class User{
@prop()
name:string
}
@model()
@schema()
export class Comment{
@prop({ref:User})
user:Ref<User>
}
define any field as array using []
@prop([ type: String ])
names?: string[];
@prop([Address])
addresses:Address[]
@prop([{ref:User}])
user:Ref<User>[]
define required field
@prop({ type:String ,required:true})
firstName: string;
define index field
@prop({ index: true })
name?: string;
define unique field
@prop({ unique: true })
someId?: string;
define enum field
enum Gender {
MALE = 'male',
FEMALE = 'female',
}
@prop({ enum: Gender })
gender?: Gender;
define field with default
@prop({ type: String,default:"myName" })
name?: string;
validate using minlength / maxlength / match / min /max same as mongoose
@prop({ minlength: 5, maxlength: 10, match: /[0-9a-f]*/ })
phone: string;
or use custom validator same as mongoose
@prop({
type: String,
validate: {
validator: function(v) {
return /\d{3}-\d{3}-\d{4}/.test(v);
}
},
message: props => `${props.value} is not a valid phone number!`
})
phone: string;
define virtual getter setters as in mongoose
@prop()
firstName?: string;
@prop()
lastName?: string;
@virtual() // this will create a virtual property called 'fullName'
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
set fullName(full:string) {
const [firstName, lastName] = full.split(' ');
this.firstName = firstName;
this.lastName = lastName;
}
define static method as in mongoose the method will be created on the mongose model
@staticMethod()
static findByName(this: Model<User>, name: string) {
return this.findOne({ name });
}
@define()
@singleton()
export class SomeManager {
@injectModel(User) userModel:Model<User> & typeof User;
async getUser(name:string): Promise<Doc<User>> {
let user = await this.userModel.findByName(name)
return user;
}
}
you need added the typeof User
in order to use the static method findByName
define instance method as in mongoose instance methods are created on mongoose document
@method()
addAge(this: Doc<User>,age?:number) {
this.age = this.age + (age ||1 );
return this.save();
}
define mongoose pre hooks the pre function will be executed before the defined hook
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
@pre("save")
private preSave(this:Doc<User>,next){
if (this.name === 'aaa') {
this.name = 'bbb';
}
next();
}
}
define mongoose post hooks the post function will be executed after the defined hook
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
@post("save")
private preSave(doc:Doc<User>,next){
console.log('%s has been saved', doc._id);
next();
}
}
register new schema model
@model()
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
}
inject model to class
@define()
@singleton()
export class SomeManager {
@injectModel(User) userModel:Model<User>;
async getUser(id:string): Promise<Doc<User>> {
let user = await this.userModel.findById(id)
return user;
}
}
get the defined collection name on schema
@model()
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
}
User.collectionName // "user"
return mongoose schema object
@model()
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
}
let schema = User.getSchema()
define and mongoose model instance by mongoose connection
@model()
@schema("User",{strict:true})
export class User{
@prop({type:String})
name:string
}
let model = User.getModel<User>(mongoose.connection)
model.findById("someId")
with modelRepository you can access to the mongoose connection and all the models
getter return mongoose connection
return mongoose model by schema type
@define()
@singleton()
export class SomeManager {
@inject() modelRepository:ModelRepository;
async getUser(id:string): Promise<Doc<User>> {
let user = await this.modelRepository.getModel(User).findById(id)
return user;
}
}
FAQs
appolo mongo module
We found that @appolo/mongo demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.