Help needed when creating collection from nodejs into main data source

I’m struggling when creating collections from my nodejs code, they are created in the database, but do no appear within the main data source. Following the example here https://docs.nocobase.com/api/database is more than enough to get the table created, since it’s based off of sequelize I followed the normal pattern I use for sequelize.

I connected pgAdmin to the internal database so I could verify. The table is created upon initial run, the data is written into the table. The table just won’t display in the main data source. I can use the http api to do this, but this is far easier to do systematically.

import { Database } from ‘@nocobase/database’

// PostgreSQL database configuration parameters
export default const database = new Database({
dialect: ‘postgres’,
database: ‘database’,
username: ‘username’,
password: ‘password’,
host: ‘localhost’,
port: ‘port’
})

Model file:
import { database } from ‘database connection example above’
const UserCollection = database.collection({
name: ‘users’,
fields: [
{
name: ‘name’,
type: ‘string’,
},
{
name: ‘age’,
type: ‘integer’,
},
],
});

await UserCollection.sync() I’ve also used database.sync() here

export default UserCollection

Example in use
import { UserCollection } from ‘example above’

const UserRepository = UserCollection.repository();

await UserRepository.create({
name: ‘Mark’,
age: 18,
});

const create = await UserCollection.create({

Although the table is created in the database, it is not registered as a collection within NocoBase, and therefore will not appear in the main data source or UI.

To make it manageable in NocoBase, you need to define the collection and its fields using NocoBase’s db.collection() API.

Please refer to this section for details on how to define data structure:
https://docs.nocobase.com/api/database#define-data-structure

shleyZ,
Maybe I’m misunderstanding something, what you’re talking about is the equivalent to defining the model in sequelize. If you review my original post the section that says “Model File” does this. The link you sent me is even the example code I used to write out my initial question. What am I not understanding?

You’re right — db.sync() creates the table in the database.

But to make it appear in the NocoBase UI, you also need to register it in the internal metadata system:

const repo = database.getRepository<any>('collections');
if (repo) {
  await repo.db2cm('your_table_name');
}

This step tells NocoBase to display and manage the table in the admin panel.

Sorry shleyZ, not sure what I’m missing, but I haven’t been able to make this work through any amount of effort. I’ve dialed back to using the API to create the tables