I'm creating a helper class for all my database functions with MongoDB. While I'm doing it, I thought if it's possible to create Generic database functions for MongoDB with mongoose.
I tested a little bit, but didn't come to a result.
I'm using like I mentioned MongoDB with mongoose in combination with Typescript.
export async function getByID<T>(id: string): Promise<T> {
const found: T = await T.findById(id);
return found;
}
My codeexample is not working right now, but it should demonstrate what I want to achive. I hope you guys can help me.
Related
I have five years experience in programming but I am very new to NodeJS and TypeScript.
According to their documentation, the normal way to use AJV for synchronous validation in TypeScript is to use the return of AJV#compile as both a function and an object.
import { AJV } from 'ajv';
// I am loading my schema from an adjacent file
import * as create_search_response from './create_search.schema.json';
const ajv = new AJV();
const validator = ajv.compile(create_search_response);
function throwUnlessObjectIsValid(obj: any) {
if (!validator(obj)) {
// assume at least one error
throw validator.errors?.pop();
}
}
My understanding is that we use the global value of validator.errors to store and retrieve the error info, similar to errno in C.
What if I perform validation in two threads simultaneously? Wouldn't the errors get mixed up? I have looked at their asynchronous validation system, and it would solve my problem with promise-based API, but I'm very confused about the details of keywords. It seems to require that I change the JSON schema itself.
What is the best practice here?
I am trying to run a query from the results of another using with in the objection orm
ex:
Model.query().with(alias, query).select(columns).from(alias);
according to the Knex documentation which is linked from the objection docs, this should work fine. However, when I run the code, objection prepends the schema name to the alias and I get an error stating that relation schema.alias does not exist. I tried using raw but this did not help either.
ex:
Model.query().with(alias, query).select(columns).from(raw(alias));
is there a way for me to select the table/alias defined in the with method without objection prepending the schema to it?
The query method of the model I was using was overridden with code that specified the schema
ex:
class MyModel extends BaseModel {
static query() {
return super.query().withSchema(schema);
}
}
To get around this issue I used the query method of the parent class directly rather than the overridden query method of the model I was using.
This solves my current problem, but does not answer the question of whether one could omit the prepended schema name in the from method.
In another stackoverflow post a master handling function that dispatches the processing to different functions was suggested.
functions.storage.object().onFinalize((object) => {
if (object.name.startsWith('User_Pictures/')) {
return handleUserPictures(object);
} else if (object.name.startsWith('MainCategoryPics/')) {
return handleMainCategoryPictures(object);
}
})
I have tried implementing this by having index.js as follows:
const handler = require('./handler');
exports.handler = handler.handler;
exports.userpictures = require('./userpictures');
exports.mainpictures = require('./mainpictures');
And in mainpictures.js having the following:
exports.handleMainCategoryPictures= async (object) => { ... code here ... }
When I ran firebase deploy no functions were detected. I was expecting 3. Is this type of structure possible, am I making some obvious mistake in terms of exporting correctly? When I tried exporting directly without the handler the functions were detected.
You still need to define your exported functions with the functions builder API. That's thing that goes like this in your first code bit:
export fun = functions.storage.object().onFinalize(...)
If you aren't using this API to build and export functions from index.js, then the Firebase CLI will find no function, and nothing will be deployed. You can use this API from a required file, if you want, but index.js must still ultimately export a function built like this.
If you are, in fact, using this and not showing it here, then I suggest you edit the question to show the complete, minimal example of all the files in play.
I have a class like the one shown here:
import {otherStuff} from 'somewhere'
const promiseFtp = require('promise-ftp');
export class processor(){
ftp: any;
public async connect(){
ftp = new promiseFtp();
await ftp.connect(myConnectionObject);
this.ftp = ftp;
}
}
My problem is that I have no way to mock promiseFtp. I cant find any documentation in Jasmine that supports this sort of testing. I would let it just call the connect function, but I dont want it to actually connect to any ftp site.
I might be able to use something like proxyquire, but my boss is against it since we moved to typescript, and that was supposed to get rid of proxyquire. I cannot use proxyquire Typescript should have a way around this somehow.
I even tried to my my own module that exports promiseFtp, then mocks that module, but I just cant do it. How do you mock a class so that it wont get called.
Is there some high level ORM for nodejs datastore client library? Because it becoming really hard to maintain relatively small application when entities referencing and objects versioning takes place. Just interested, should I start write my own bicycle or someone already wrote something like that?
If not, maybe there some separate libraries to implemented appropriate referencing mechanism?
Check out gstore-node:
gstore-node is a Google Datastore entities modeling library for Node.js inspired by Mongoose and built on top of the #google-cloud/datastore library.
It is not a replacement of #google-cloud/datastore but a tool built to help modeling Entities through Schemas and to help validating the data saved in the Datastore.
You may try on this one, very well structure, written in Typescript.
https://www.npmjs.com/package/ts-datastore-orm
import {BaseEntity, Column, Entity} from "ts-datastore-orm";
#Entity({kind: "user"})
export class User extends BaseEntity {
#Column({generateId: true})
public id: number = 0;
#Column({index: true})
public total: number = 0;
}
async function main() {
const user = User.create();
await user.save();
const id = user.id;
}