how to do getter to the root object in js? - javascript

In JavaScript, how to do getter to object in the root level?
I mean how to create getter for this object:
const userModel = {
get() { return { foo: true, ... }; }
}
and access to it by userModel.foo; // true. the code gives me userModel.get().. which not expect what I need.
The idea is go invoke get when I access to userModel.
Is it possible to do with js?
I am using the typeorm library and I have models for example User.
typeorm is able me to use this model by using this syntax:
import { getConnection } from 'typeorm';
const b = await getConnection().getRepository(User).findOne({...})...
now I want to create UserModel that do this:
getConnection().getRepository(User)
as:
export const userModel = {
get() { return getConnection().getRepository(User) }
}
so can I use it as:
userModel.findOne({...});
And I can't just use:
export const userModel = getConnection().getRepository(User);
Because the connection is not initialized yet, so it throws an error.

Yes, it is possible using Proxy:
const getConnection = (model) => ({
getRepository: (model) => {
return { findOne: () => (model) }
}
});
const User = {
//...user model,
name: "Jonh Smith"
}
class ProxyModel {
constructor(model) {
this.model = model;
return new Proxy({}, {get: (function(target, name) {
const repo = getConnection().getRepository(this);
if(name in repo) {
return repo[name];
}
}).bind(this.model)})
}
}
const userModel = new ProxyModel(User);
console.log(userModel.findOne());

Make sure you define getter properly:
const userModel = {
get foo() { return { foo: true }; }
}
console.log(userModel.foo)

Related

Unable to mock a class method in Javascript/Typescript

I am not getting any clue how to mock a method. I have to write a unit test for this function:
index.ts
export async function getTenantExemptionNotes(platform: string) {
return Promise.all([(await getCosmosDbInstance()).getNotes(platform)])
.then(([notes]) => {
return notes;
})
.catch((error) => {
return Promise.reject(error);
});
}
api/CosmosDBAccess.ts
import { Container, CosmosClient, SqlQuerySpec } from "#azure/cosmos";
import { cosmosdbConfig } from "config/Config";
import { Workload } from "config/PlatformConfig";
import { fetchSecret } from "./FetchSecrets";
export class CosmoDbAccess {
private static instance: CosmoDbAccess;
private container: Container;
private constructor(client: CosmosClient) {
this.container = client
.database(cosmosdbConfig.database)
.container(cosmosdbConfig.container);
}
static async getInstance() {
if (!CosmoDbAccess.instance) {
try {
const connectionString = await fetchSecret(
"CosmosDbConnectionString"
);
const client: CosmosClient = new CosmosClient(connectionString);
// Deleting to avoid error: Refused to set unsafe header "user-agent"
delete client["clientContext"].globalEndpointManager.options
.defaultHeaders["User-Agent"];
CosmoDbAccess.instance = new CosmoDbAccess(client);
return CosmoDbAccess.instance;
} catch (error) {
// todo - send to app insights
}
}
return CosmoDbAccess.instance;
}
public async getAllNotesForLastSixMonths() {
const querySpec: SqlQuerySpec = {
// Getting data from past 6 months
query: `SELECT * FROM c
WHERE (udf.convertToDate(c["Date"]) > DateTimeAdd("MM", -6, GetCurrentDateTime()))
AND c.IsArchived != true
ORDER BY c.Date DESC`,
parameters: [],
};
const query = this.container.items.query(querySpec);
const response = await query.fetchAll();
return response.resources;
}
}
export const getCosmosDbInstance = async () => {
const cosmosdb = await CosmoDbAccess.getInstance();
return cosmosdb;
};
index.test.ts
describe("getExemptionNotes()", () => {
beforeEach(() => {
jest.resetAllMocks();
});
it("makes a network call to getKustoResponse which posts to axios and returns what axios returns", async () => {
const mockNotes = [
{
},
];
const cosmosDBInstance = jest
.spyOn(CosmoDbAccess, "getInstance")
.mockReturnValue(Promise.resolve(CosmoDbAccess.instance));
const kustoResponseSpy = jest
.spyOn(CosmoDbAccess.prototype, "getAllNotesForLastSixMonths")
.mockReturnValue(Promise.resolve([mockNotes]));
const actual = await getExemptionNotes();
expect(kustoResponseSpy).toHaveBeenCalledTimes(1);
expect(actual).toEqual(mockNotes);
});
});
I am not able to get instance of CosmosDB or spyOn just the getAllNotesForLastSixMonths method. Please help me code it or give hints. The complexity is because the class is singleton or the methods are static and private

Am I supposed to pass around Pino child loggers?

This is rather a stylistic question. I'm using Pino in some of my Javascript/Typescript microservices. As they're running on AWS I'd like to propagate the RequestId.
When one of my functions is invoked, I'm creating a new child logger like this:
const parentLogger = pino(pinoDefaultConfig)
function createLogger(context) {
return parentLogger.child({
...context,
})
}
function createLoggerForAwsLambda(awsContext) {
const context = {
requestId: awsContext.awsRequestId,
}
return createLogger(context)
}
I'm then passing down the logger instance to all methods. That said, (... , logger) is in almost every method signature which is not too nice. Moreover, I need to provide a logger in my tests.
How do you do it? Is there a better way?
you should implement some sort of Dependency Injection and include your logger there.
if your using microservices and maybe write lambdas in a functional approach, you can handle it by separating the initialization responsibility in a fashion like this:
import { SomeAwsEvent } from 'aws-lambda';
import pino from 'pino';
const createLogger = (event: SomeAwsEvent) => {
return pino().child({
requestId: event.requestContext.requestId
})
}
const SomeUtil = (logger: pinno.Logger) => () => {
logger.info('SomeUtil: said "hi"');
}
const init(event: SomeAwsEvent) => {
const logger = createLogger(event);
someUtil = SomeUtil(logger);
return {
logger,
someUtil
}
}
export const handler = (event: SomeAwsEvent) => {
const { someUtil } = init(event);
someUtil();
...
}
The simplest way is to use some DI library helper to tackle this
import { createContainer } from "iti"
interface Logger {
info: (msg: string) => void
}
class ConsoleLogger implements Logger {
info(msg: string): void {
console.log("[Console]:", msg)
}
}
class PinoLogger implements Logger {
info(msg: string): void {
console.log("[Pino]:", msg)
}
}
interface UserData {
name: string
}
class AuthService {
async getUserData(): Promise<UserData> {
return { name: "Big Lebowski" }
}
}
class User {
constructor(private data: UserData) {}
name = () => this.data.name
}
class PaymentService {
constructor(private readonly logger: Logger, private readonly user: User) {}
sendMoney() {
this.logger.info(`Sending monery to the: ${this.user.name()} `)
return true
}
}
export async function runMyApp() {
const root = createContainer()
.add({
logger: () =>
process.env.NODE_ENV === "production"
? new PinoLogger()
: new ConsoleLogger(),
})
.add({ auth: new AuthService() })
.add((ctx) => ({
user: async () => new User(await ctx.auth.getUserData()),
}))
.add((ctx) => ({
paymentService: async () =>
new PaymentService(ctx.logger, await ctx.user),
}))
const ps = await root.items.paymentService
ps.sendMoney()
}
console.log(" ---- My App START \n\n")
runMyApp().then(() => {
console.log("\n\n ---- My App END")
})
it is easy to write tests too:
import { instance, mock, reset, resetCalls, verify, when } from "ts-mockito"
import { PaymentService } from "./payment-service"
import type { Logger } from "./logger"
const mockedLogger = mock<Logger>()
when(mockedLogger.info).thenReturn(() => null)
describe("Payment service: ", () => {
beforeEach(() => {
resetCalls(mockedLogger)
// reset(mockedLogger)
})
it("should call logger info when sending money", () => {
const paymentService = new PaymentService(instance(mockedLogger))
expect(paymentService.sendMoney()).toBe(true)
})
})
I would not use the requestId as part of the context of the logger, but use it as the payload of the logger, like logger.info({ requestId }, myLogMessage). This was you can have a simple function create a child logger that you can use for the entire module.

How to mongoose model based on dynamic schema passed as parameter?

I am new on mongoose and for expressjs
I want to retrieve a collection based on the doc and model.
I have multiple schema that inherits a common schema.
const extendSchema = (schema: mongoose.Schema<any>, definition: any): Schema<any> => {
return new mongoose.Schema({ ...schema.obj, ...definition, ...{ strict: false }});
};
const CommonSchema = new mongoose.Schema({ ... });
const OtherSchema = extendSchema(CommonSchema, { ... });
const OtherOtherSchema = extendSchema(CommonSchema, { ... });
Then, I want to retrieve the collection from the mongoose
const getCollectionObject = (collection: string, schema: Schema) => {
return collection.model(collection, schema);
};
// get the first collection
export const getOtherCollection = async (name: string, id: string) => {
try {
const model = getCollectionObject(name, OtherSchema);
const document = await model.findById(mongoose.Types.ObjectId(id)).lean();
return document;
} catch (error) {
return error;
}
};
// get the second collection
export const getOtherOtherCollection = async (name: string, id: string) => {
try {
const model = getCollectionObject(name, OtherOtherSchema);
const document = await model.findById(mongoose.Types.ObjectId(id)).lean();
return document;
} catch (error) {
return error;
}
};
I've got an error below
Is it possible?
Thank you in advance!
PS: I've already saw other posts which the solution is to make the properties optional.
This solved my issue.
Create a common schema plus the other schema
const CommonSchema = new mongoose.Schema({ ... });
const OtherSchema = { ... };
const OtherOtherSchema = { ... };
Then, I declared my based model.
const Base = mongoose.model('collection-name', CommonSchema);
Next, I created my other models based on the base model using discriminator
const OtherModel = Base.discriminator("Other", new mongoose.Schema(OtherSchema));
const OtherOtherModel = Base.discriminator("OtherOther", new mongoose.Schema(OtherOtherSchema));
You can now use the model on any scoped function, you may export it if you want to.
Other.create({ ... });
Other.findById()
OtherOther.create();
OtherOther.findById();
please let me know if this right approach
or you have any other suggestions
Thanks!

ES6 Class Inheritance Error Object(...) is not a function

I have a GenericDB class to interface with Firestore, I want to extend this class to work with multiple collections:
export default class GenericDB {
constructor(collectionPath) {
this.collectionPath = collectionPath
}
/**
* Create a document in the collection
* #param data
* #param id
*/
async create(data, id = null) {
const collectionRef = (await DB()).collection(this.collectionPath)
const serverTimestamp = firebase.firestore.FieldValue.serverTimestamp()
const dataToCreate = {
...data,
createTimestamp: serverTimestamp,
updateTimestamp: serverTimestamp
}
const createPromise = isNil(id)
? // Create doc with generated id
collectionRef.add(dataToCreate).then(doc => doc.id)
: // Create doc with custom id
collectionRef
.doc(id)
.set(dataToCreate)
.then(() => id)
const docId = await createPromise
return {
id: docId,
...data,
createTimestamp: new Date(),
updateTimestamp: new Date()
}
}
}
However, when I extend the GenericDB class to another class, I can not call the methods in the GenericDB class.
import GenericDB from './generic-db'
export default class OpenHouseDB extends GenericDB {
constructor(userId) {
super(`open-houses/${userId}/properties`)
}
test() {
// I can call this function successfully
console.log('test worked')
}
}
Trying to call the create function defined in the GenericDB class which is inherited by the OpenHouseDB class produces the following error:
TypeError: Object(...) is not a function
async setup({ commit, rootState }, property) {
try {
const { uid } = rootState.user
const openHouseDb = new OpenHouseDB(uid)
const { mlsId } = property
openHouseDb.test()
const createdOpenHouse = await openHouseDb.create(property, mlsId)
console.log(createdOpenHouse)
} catch (err) {
console.log(err)
}
}
I've omitted some code but I've confirmed that calling openHouseDb.test() in the above code does successfully console log the correct string. I'm not sure why calling openHouseDb.create(property, mlsId) produces TypeError: Object(...) is not a function. I've confirmed that openHouseDb does have a create method but I can't figure out how to call it.

Cannot call save() in ES6 mongoose extended model

Im trying to extend a Mongoose Model using ES6 syntax. While I can call successfully find({}) to retrieve data from mongo database, I am not able to call save() to save data. Both are executed inside the Model.
The error returned is Error: TypeError: this.save is not a function
const mongoose = require('mongoose')
const {Schema, Model} = mongoose
const PersonSchema = new Schema(
{
name: { type: String, required: true, maxlength: 1000 }
},
{ timestamps: { createdAt: 'created_at', updatedAt: 'update_at' } }
)
class PersonClass extends Model {
static getAll() {
return this.find({})
}
static insert(name) {
this.name = 'testName'
return this.save()
}
}
PersonSchema.loadClass(PersonClass);
let Person = mongoose.model('Persons', PersonSchema); // is this even necessary?
(async () => {
try {
let result = await Person.getAll() // Works!
console.log(result)
let result2 = await Person.insert() // FAILS
console.log(result2)
} catch (err) {
throw new Error(err)
}
})()
Im using:
Nodejs 7.10
mongoose 5.3.15
This is normal. You're trying to access a non static method from a static method.
You need to do something like this:
static insert(name) {
const instance = new this();
instance.name = 'testName'
return instance.save()
}
Some working example:
class Model {
save(){
console.log("saving...");
return this;
}
}
class SomeModel extends Model {
static insert(name){
const instance = new this();
instance.name = name;
return instance.save();
}
}
const res = SomeModel.insert("some name");
console.log(res.name);
Here is an example of what works and what doesn't work.
class SomeParentClass {
static saveStatic(){
console.log("static saving...");
}
save(){
console.log("saving...");
}
}
class SomeClass extends SomeParentClass {
static funcStatic(){
this.saveStatic();
}
func(){
this.save();
}
static funcStaticFail(){
this.save();
}
}
//works
SomeClass.funcStatic();
//works
const sc = new SomeClass();
sc.func();
//fails.. this is what you're trying to do.
SomeClass.funcStaticFail();

Categories

Resources