# Importing a model in a TS file with v7

**URL:** <https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709>\
**Category:** Help me!\
**Created:** [June 15, 2021, 1:07pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709 "2021-06-15T13:07:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [June 15, 2021, 1:07pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/1 "2021-06-15T13:07:54Z")

</div>

Hi Forest,

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

Here is how we created models:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/ae093be14b18a4c831a6b6b3f8c2ec4ed472d0af.png)

Should do something like this ?

![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/e/e76007dc745ecf789180cd331fb23a971ee89296.png)

Best  
Guillaume

---

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [June 15, 2021, 1:32pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/2 "2021-06-15T13:32:18Z")

</div>

I’m not very found of that …

---

<div class="post-metadata">

**Author:** ![anon36130554](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon36130554](https://community.forestadmin.com/u/anon36130554)\
**Post date:** [June 15, 2021, 1:37pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/3 "2021-06-15T13:37:49Z")

</div>

Hello @Guiguijo 👋

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 🙌. 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):

```javascript
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.

---

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [June 15, 2021, 1:41pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/4 "2021-06-15T13:41:39Z")

</div>

Hi @anon36130554 ,

Thank your for the feedback.

I tell you if I have any suggestions.

Best  
Guillaume

---

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [June 21, 2021, 10:08am UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/5 "2021-06-21T10:08:02Z")

</div>

@anon36130554

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

```javascript
Model creation error: SyntaxError: Unexpected token ':'

```

See below:

```javascript
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

---

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [June 28, 2021, 1:28pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/6 "2021-06-28T13:28:50Z")

</div>

@anon36130554

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:

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

```

---

<div class="post-metadata">

**Author:** ![anon36130554](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon36130554](https://community.forestadmin.com/u/anon36130554)\
**Post date:** [July 1, 2021, 2:55pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/7 "2021-07-01T14:55:52Z")

</div>

Hello @Guiguijo 👋

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

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 🎊

To help you with your migration, we also created a documentation to guide you as much as possible, please refer to this [link](https://docs.forestadmin.com/woodshop/how-tos/translate-your-project-into-typescript).

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.

---

<div class="post-metadata">

**Author:** ![Guiguijo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guiguijo/32/2445_2.png) [@Guiguijo](https://community.forestadmin.com/u/Guiguijo)\
**Post date:** [July 2, 2021, 7:04am UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/9 "2021-07-02T07:04:40Z")

</div>

@anon36130554 did you look at that this issue? 👆 (just above your last post )

---

<div class="post-metadata">

**Author:** ![anon36130554](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon36130554](https://community.forestadmin.com/u/anon36130554)\
**Post date:** [July 2, 2021, 12:39pm UTC](https://community.forestadmin.com/t/importing-a-model-in-a-ts-file-with-v7/2709/10 "2021-07-02T12:39:36Z")

</div>

> [@Guiguijo](#):
>
> did you look at that this issue?

@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](https://docs.forestadmin.com/woodshop/how-tos/translate-your-project-into-typescript/v7/migrate-mongoose-files#connect-to-your-databases)).

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

Steve.
