I tried to follow step to connect database with datasource concepts , but i am confuse. if am creating datasource through lb4 datasource. all datasource file come under src/datasource. In this url datasource.json created in server directory. I have not too much idea about loopback. How to fetch some data, Or fetch some data need to creating service for data lake.
@chayakornwc, from your description, it seems like there might be some mixed-up on the LB3 vs LB4 commands. In LoopBack 4, there is no longer server directory. I'd like to point you to this tutorial (https://loopback.io/doc/en/lb4/todo-tutorial.html), in case you haven't been there.
In short, to fetch data from database, you can run the commands below:
lb4 app to create your app scaffoldinglb4 model to add your model. This will be the model that corresponds to your data in the databaselb4 datasource to add a datasource. Select the corresponding connector for the database you're using. lb4 repository to add a repository that binds the model and the datasourcelb4 controller to add a controller that defines your REST APIs If you're using a sql-based database, you can create the model from discovering the schema in the database. We're in the process of updating the documentation to provide the instruction, but you can find a preliminary instruction in here. Hope it helps.
@chayakornwc you might also want to take a look at LoopBack 3 - https://loopback.io/doc/en/lb3/index.html. It is more matured and is still supported.
thank for help @hacksparrow @dhmlau 馃憤
Closing as resolved. Thanks.
i was connected my database
this my simple config below
{
"name": "posts",
"connector": "mongodb",
"url": "mongodb://127.0.0.1:27017",
"host": "localhost",
"port": 27017,
"user": "",
"password": "",
"database": "admin-artlampang"
}
i can post and get fake data on
http://explorer.loopback.io/?url=http://localhost:3000/openapi.json#/PostController/get_api_posts
but no data on my database
what's wrong ?
Sorry to close your issue, I thought it's been resolved.
Did you create a datasource and have the repository binds to it? Better yet, could you please share with us a sample application? Thanks.
i a has many relational collection
postcategories.repository.ts
import { DefaultCrudRepository, juggler, HasManyRepositoryFactory, repository } from '@loopback/repository';
import { postcategories, posts } from '../models';
import { PostsRepository } from './posts.repository';
import { MongoDataSource } from '../datasources';
import { inject, Getter } from '@loopback/core';
export class PostcategoriesRepository extends DefaultCrudRepository<postcategories, typeof postcategories.prototype._id> {
public readonly posts: HasManyRepositoryFactory<posts, typeof postcategories.prototype._id>;
constructor(@inject('datasources.mongo') dataSource: MongoDataSource, @repository.getter(PostsRepository) getPostsRepository: Getter<PostsRepository>, ) {
super(postcategories, dataSource);
this.posts = this._createHasManyRepositoryFactoryFor(
'posts',
getPostsRepository,
);
}
}
postcategories.model.ts
import { Entity, model, property, hasMany } from '@loopback/repository';
import { posts } from './posts.model'
@model()
export class postcategories extends Entity {
@property({
type: 'string',
id: true,
required: true,
})
_id: string;
@property({
type: 'string',
required: true,
})
key: string;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'number',
required: true,
})
_v: number;
@hasMany(() => posts, { keyTo: 'categories' })
posts?: posts[];
constructor(data?: Partial<postcategories>) {
super(data);
}
}
catoriespost-post.controller.ts
i wont to get post data where categories key contain the key value array type in posts collection has relate by _id key of categoirespost collection
@get('/api/categories/{categories}/posts')
async postsCategories(@param.path.string('categories') categories: typeof postcategories.prototype._id,
@param.query.object('filter', getFilterSchemaFor(posts)) filter: Filter): Promise<posts[]> {
return await this.PostcategoriesRepository.posts(categories).find()
}
my results is return empty array

Thanks @chayakornwc for the information. I'll try it out tomorrow. Thanks!
I've created the app based on the above description in this repo.
IIUC, here is what we're trying to do:
@get('/api/categories/{categories}/posts') that @chayakornwc added doesn't work. See https://github.com/dhmlau/hasmany-test/blob/master/src/controllers/category.controller.ts#L128@b-admike @bajtos @raymondfeng, I'm not quite sure how it should work. Could you please help?
@chayakornwc You have a wrong foreign key for posts to qualify for hasMany relations. It cannot be an array. A category has many posts means each post has a category.
To refer to other models by an array of FKs, use https://loopback.io/doc/en/lb3/Embedded-models-and-relations.html. We haven't supported it yet in LB4.
@raymondfeng i've trying without filter schema that's i can find this relations
@dhmlau @raymondfeng thanks for help 馃檹