Importing a model in a TS file with v7

Hi Forest,

How should we import a model in a TS file using the new database configuration?

Here is how we created models:

Should do something like this ?

image

Best
Guillaume

I’m not very found of that …

Hello @Guiguijo :wave:

First of all, I just want to let you know that I’m currently working on new types for our repository forest-express-mongoose version 7 :raised_hands:. They should be available this week, latest would be next week.

I have run into the same issue as yours. The best workaround I have found so far, is to put your models under db.models and export it alongside objectMapping and connections.

Then, wherever you need a model you could do the following (similar to your suggestion):

import { models } from '../models';
const Accounts = models['accounts'];

From there, and with the new types incoming, you will benefit from the typescript system and auto-completion from mongoose.

I’m not a big fan of this as you mentioned. Sadly, due to dynamic model imports I have not been able to find anything better so far.

If you have any suggestion, as I’m currently working on typescript projects/types for lianas version 7, I would be really glad to get them.

I hope this helps out,

Steve.

2 Likes

Hi @Steve_Bunlon ,

Thank your for the feedback.

I tell you if I have any suggestions.

Best
Guillaume

2 Likes

@Steve_Bunlon

when starting the project in TS with the new DB configuration I can see some wired logs:

Model creation error: SyntaxError: Unexpected token ':'

See below:

api_forest_1  | > ecotiz-api@2.0.2 start-forest:dev /srv/app
api_forest_1  | > tsc-watch --project ./joinly-forest/tsconfig.json --noClear --onSuccess "nodemon --config ./joinly-forest/nodemon.json"
api_forest_1  |
api_forest_1  |
api_forest_1  |
api_forest_1  | ----------------------
api_forest_1  | 10:02:48 AM - Starting compilation in watch mode...
api_forest_1  |
api_forest_1  |
api_forest_1  | 10:03:37 AM - Found 0 errors. Watching for file changes.
api_forest_1  |
api_forest_1  |
api_forest_1  | ----------------------
api_forest_1  | 10:03:39 AM - File change detected. Starting incremental compilation...
api_forest_1  |
api_forest_1  |
api_forest_1  | 10:03:40 AM - Found 0 errors. Watching for file changes.
api_forest_1  | [nodemon] 1.19.4
api_forest_1  | [nodemon] to restart at any time, enter `rs`
api_forest_1  | [nodemon] watching dir(s): dist/**/* dist/joinly-forest/server.js
api_forest_1  | [nodemon] watching extensions: ts,html
api_forest_1  | [nodemon] starting `node ./joinly-forest/dist/joinly-forest/server.js`
api_forest_1  | silly:   ┏ Logging initialized +0ms
api_forest_1  | silly:   ┃ [1] {
api_forest_1  | silly:   ┃ [2]   service: 'api_forest',
api_forest_1  | silly:   ┃ [3]   env: 'development',
api_forest_1  | silly:   ┃ [4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | silly:   ┃ [5]   context: { message: 'Logging initialized' },
api_forest_1  | silly:   ┃ [6]   timestamp: '2021-06-21T10:03:59.483Z'
api_forest_1  | silly:   β”— [7] }
api_forest_1  | debug:   ┏ MONGO URI = mongodb://db:27017/?retryWrites=true&w=majority +24s
api_forest_1  | debug:   ┃ [1] {
api_forest_1  | debug:   ┃ [2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [3]   env: 'development',
api_forest_1  | debug:   ┃ [4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [5]   context: { database: 'mongodb', message: 'MONGO URI = mongodb://db:27017/?retryWrites=true&w=majority' },
api_forest_1  | debug:   ┃ [6]   timestamp: '2021-06-21T10:04:23.951Z'
api_forest_1  | debug:   β”— [7] }
api_forest_1  | debug:   ┏ MONGO DB = joinly-dev +12ms
api_forest_1  | debug:   ┃ [1] {
api_forest_1  | debug:   ┃ [2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [3]   env: 'development',
api_forest_1  | debug:   ┃ [4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [5]   context: { database: 'mongodb', message: 'MONGO DB = joinly-dev' },
api_forest_1  | debug:   ┃ [6]   timestamp: '2021-06-21T10:04:23.973Z'
api_forest_1  | debug:   β”— [7] }
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | Model creation error: SyntaxError: Unexpected token ':'
api_forest_1  | info:    ┏ Your application is listening on port 3310. +15s
api_forest_1  | info:    ┃ [1] {
api_forest_1  | info:    ┃ [2]   service: 'api_forest',
api_forest_1  | info:    ┃ [3]   env: 'development',
api_forest_1  | info:    ┃ [4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | info:    ┃ [5]   context: { message: 'Your application is listening on port 3310.' },
api_forest_1  | info:    ┃ [6]   controller: 'joinly-forest server',
api_forest_1  | info:    ┃ [7]   controller_type: 'file',
api_forest_1  | info:    ┃ [8]   timestamp: '2021-06-21T10:04:38.485Z'
api_forest_1  | info:    β”— [9] }
api_forest_1  | debug:   ┏ mongo query +4s
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'collect',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.name': 'text',
api_forest_1  | debug:   ┃ [ 9]     'doc.background': true,
api_forest_1  | debug:   ┃ [10]     options: undefined,
api_forest_1  | debug:   ┃ [11]     message: 'mongo query'
api_forest_1  | debug:   ┃ [12]   },
api_forest_1  | debug:   ┃ [13]   timestamp: '2021-06-21T10:04:42.476Z'
api_forest_1  | debug:   β”— [14] }
api_forest_1  | debug:   ┏ mongo query +13ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'order',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.collect': 1,
api_forest_1  | debug:   ┃ [ 9]     'doc.background': true,
api_forest_1  | debug:   ┃ [10]     options: undefined,
api_forest_1  | debug:   ┃ [11]     message: 'mongo query'
api_forest_1  | debug:   ┃ [12]   },
api_forest_1  | debug:   ┃ [13]   timestamp: '2021-06-21T10:04:42.490Z'
api_forest_1  | debug:   β”— [14] }
api_forest_1  | debug:   ┏ mongo query +2ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'payment',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.orderId': 1,
api_forest_1  | debug:   ┃ [ 9]     'doc.background': true,
api_forest_1  | debug:   ┃ [10]     options: undefined,
api_forest_1  | debug:   ┃ [11]     message: 'mongo query'
api_forest_1  | debug:   ┃ [12]   },
api_forest_1  | debug:   ┃ [13]   timestamp: '2021-06-21T10:04:42.494Z'
api_forest_1  | debug:   β”— [14] }
api_forest_1  | debug:   ┏ mongo query +3ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'product',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query._id': 1,
api_forest_1  | debug:   ┃ [ 9]     'query.collect': 1,
api_forest_1  | debug:   ┃ [10]     'doc.background': true,
api_forest_1  | debug:   ┃ [11]     options: undefined,
api_forest_1  | debug:   ┃ [12]     message: 'mongo query'
api_forest_1  | debug:   ┃ [13]   },
api_forest_1  | debug:   ┃ [14]   timestamp: '2021-06-21T10:04:42.498Z'
api_forest_1  | debug:   β”— [15] }
api_forest_1  | debug:   ┏ mongo query +2ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'user',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.email': 1,
api_forest_1  | debug:   ┃ [ 9]     'doc.unique': 'Cet email - {VALUE} - est dΓ©jΓ  utilisΓ©!',
api_forest_1  | debug:   ┃ [10]     'doc.background': true,
api_forest_1  | debug:   ┃ [11]     options: undefined,
api_forest_1  | debug:   ┃ [12]     message: 'mongo query'
api_forest_1  | debug:   ┃ [13]   },
api_forest_1  | debug:   ┃ [14]   timestamp: '2021-06-21T10:04:42.502Z'
api_forest_1  | debug:   β”— [15] }
api_forest_1  | Your admin panel is available here: https://app.forestadmin.com/projects
api_forest_1  | debug:   ┏ mongo query +121ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'collect',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.name': 1,
api_forest_1  | debug:   ┃ [ 9]     'query.account': 1,
api_forest_1  | debug:   ┃ [10]     'doc.unique': true,
api_forest_1  | debug:   ┃ [11]     'doc.background': true,
api_forest_1  | debug:   ┃ [12]     options: undefined,
api_forest_1  | debug:   ┃ [13]     message: 'mongo query'
api_forest_1  | debug:   ┃ [14]   },
api_forest_1  | debug:   ┃ [15]   timestamp: '2021-06-21T10:04:42.627Z'
api_forest_1  | debug:   β”— [16] }
api_forest_1  | debug:   ┏ mongo query +7ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'order',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.orderItems.productId': 1,
api_forest_1  | debug:   ┃ [ 9]     'doc.background': true,
api_forest_1  | debug:   ┃ [10]     options: undefined,
api_forest_1  | debug:   ┃ [11]     message: 'mongo query'
api_forest_1  | debug:   ┃ [12]   },
api_forest_1  | debug:   ┃ [13]   timestamp: '2021-06-21T10:04:42.636Z'
api_forest_1  | debug:   β”— [14] }
api_forest_1  | debug:   ┏ mongo query +18ms
api_forest_1  | debug:   ┃ [ 1] {
api_forest_1  | debug:   ┃ [ 2]   service: 'api_forest',
api_forest_1  | debug:   ┃ [ 3]   env: 'development',
api_forest_1  | debug:   ┃ [ 4]   version: 'v2.0.2-4-g99e948a3',
api_forest_1  | debug:   ┃ [ 5]   context: {
api_forest_1  | debug:   ┃ [ 6]     collection: 'order',
api_forest_1  | debug:   ┃ [ 7]     method: 'createIndex',
api_forest_1  | debug:   ┃ [ 8]     'query.collect': 1,
api_forest_1  | debug:   ┃ [ 9]     'doc.background': true,
api_forest_1  | debug:   ┃ [10]     options: undefined,
api_forest_1  | debug:   ┃ [11]     message: 'mongo query'
api_forest_1  | debug:   ┃ [12]   },
api_forest_1  | debug:   ┃ [13]   timestamp: '2021-06-21T10:04:42.655Z'
api_forest_1  | debug:   β”— [14] }

It seems that some errors are raising in a first time but they disappeared.

Have you encounter the same case ?

Guillaume

@Steve_Bunlon

I think the problem of

Model creation error: SyntaxError: Unexpected token ':'

comes from the file-name.js.map that are produced by Typescript in the /model folder.

I added a condition in the function that filters the model files in the index.ts to avoid those files:

fs.readdirSync(modelsDir)
    .filter(file => file.indexOf('.') !== 0 && file !== 'index.js' && !file.includes('.map'))
    ... 

Hello @Guiguijo :wave:

I am glad to announce that the new Types implementation has just been released :tada:

To benefit from the new Types, you just have to upgrade your Liana to version 7.9.0. Yes, Types our now directly integrated in the forest-express-mongoose repository, you don’t need to install @types/forest-express-mongoose anymore :confetti_ball:

To help you with your migration, we also created a documentation to guide you as much as possible, please refer to this link.

Don’t hesitate to post any issue or frustration you encounter regarding Types, we will be glad to help you out and every feedback are appreciated.

I hope you will enjoy the new version !

Steve.

1 Like

@Steve_Bunlon did you look at that this issue? :point_up_2: (just above your last post )

@Guiguijo yes I did. The documentation has a section about it.

For the mongoose version of forest-express we decide to go from a dynamic loading approach to a static loading (please refer to this section of the documentation).

But indeed, I have your comment into account for the sequelize version (which still uses dynamic loading) and I thank you for it :slight_smile:

Steve.