0
votes

I am using StrongLoop to create REST style api from an existing MYSQL database which is a wordpress database. The existing MYSQL db has two table names 'wp_post' and 'wp_postmeta'

 wp_post table          |  wp_postmeta    |
------------------------|-----------------|
 ID                     |  meta_id        |
------------------------|-----------------|
 post_content           |  meta_key       |
------------------------|-----------------|
 post_title             |  meta_value     |
------------------------|-----------------|
 post_date_gmt          | post_id         |
------------------------|-----------------|
 post_status            |
------------------------|
 comment_status         |
------------------------|
 post_modified          |
------------------------|
 post_modified_gmt      |
------------------------|
 comment_count          |
------------------------|
 more feild ...         |
------------------------|

The wp_postmeta has meat_key and meta_value pair such as (vote_up, vote_down,featured, view_count) which are directly related to the post. Using these two table i want to create a model in StrongLoop with following schema named book:
Id, title, content, post_date, comment_count, vote_up, vote_down, featured, view_count

. Is it possible to created such model in StrongLoop? if yes how can i handle the CRUD operation on such a model?

I have been looking all over the internet for some example but did not find any. Any help is appreciate! Thanks!

2

2 Answers

1
votes

The simplest way would be to use the model discovery feature of Loopback. Give loopback access to your database via the mysql connector library, then generate a set of model files for the database in question.

Schema discovery: https://docs.strongloop.com/display/public/LB/Discovering+models+from+relational+databases

You could also use the Loopback model creator helper scripts to walk through recreating the model step by step, where the script will ask you questions about the models and data types: slc loopback:model [model-name], followed by slc loopback:relation command to relate two or more models together.

Model Generator: https://docs.strongloop.com/display/public/LB/Model+generator

Relation Generator: https://docs.strongloop.com/display/public/LB/Relation+generator

You'll end up with model.json files that describe your mysql tables in a pretty easy to understand structure. It's just the translation from SQL schema defs to .json model files.

0
votes

Another option is to create a updatable database view constructed in a required way: Id, title, content, post_date, comment_count, vote_up, vote_down, featured, view_count

And then generate model for this object in loopback, view can be seen as table in same way.