Getting Started
Prerequisites
You have installed Nodejs with Version >= 14
Installation
Following steps will guide you to create the necessary files in your project directory.
├─ src
│ ├─ models # model files
│ │ ├─ shop.ts
│ │ └─ product.ts
│ ├─ index.ts # your program
│ └─ orm.ts # your orm configuration
├─ package.json
└─ tsconfig.json
├─ src
│ ├─ models # model files
│ │ ├─ shop.js
│ │ └─ product.js
│ ├─ index.js # your program
│ └─ orm.js # your orm configuration
└─ package.json
- Step 1: Create and change into a new directory
mdir my-project
cd my-project
1
2
2
- Step 2: Initialize your project
npm init
1
- Step 3: Install TaiChi ORM and its dependencies
npm install --save taichi-orm knex
1
- Step 4: Install the SQL client (mysql, postgresql or sqlite)
npm install --save mysql2
1
npm install --save pg
1
npm install --save sqlite3
1
- Step 5: Create
models
folder for your Model files.
mkdir src
cd src
mkdir models
1
2
3
2
3
- Step 6: Create Model files inside
models
folders
//@filename: src/models/shop.ts
import { Model, PrimaryKeyType, NumberType } from 'taichi-orm'
import Product from './product'
export default class Shop extends Model {
id = this.field(PrimaryKeyType)
products = Shop.hasMany(Product, 'shopId')
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
//@filename: src/models/shop.js
const { Model, PrimaryKeyType, NumberType } = require('taichi-orm')
module.exports = class Shop extends Model {
id = this.field(PrimaryKeyType)
products = Shop.hasMany(require('./product'), 'shopId')
}
1
2
3
4
5
6
7
2
3
4
5
6
7
//@filename: src/models/product.ts
import { Model, PrimaryKeyType, NumberType } from 'taichi-orm'
import Shop from './shop'
export default class Product extends Model {
id = this.field(PrimaryKeyType)
shopId = this.field(NumberType)
shop = Product.belongsTo(Shop, 'shopId')
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
//@filename: src/models/product.js
const { Model, PrimaryKeyType, NumberType } = require('taichi-orm')
module.exports = class Product extends Model {
id = this.field(PrimaryKeyType)
shopId = this.field(NumberType)
shop = Product.belongsTo(require('./shop'), 'shopId')
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- Step 7: Create a file named 'orm.ts'('orm.js'). It contains settings of your ORM. Here we use 'sqlite3' as example.
cd ..
1
//@filename: src/orm.ts
import { ORM } from 'taichi-orm'
import Shop from './models/shop'
import Product from "./models/product"
// configure your orm
export default new ORM({
// register the models here
models: {
Shop, Product
},
// knex config with client sqlite3 / mysql / postgresql
knexConfig: {
client: 'sqlite3',
connection: {
filename: ':memory:'
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//@filename: src/orm.js
const { ORM } = require('taichi-orm')
// configure your orm
module.exports = new ORM({
// register the models here
models: {
Shop: require('./models/shop'),
Product: require('./models/product')
},
// knex config with client sqlite3 / mysql / postgresql
knexConfig: {
client: 'sqlite3',
connection: {
filename: ':memory:'
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TIP
You can also register your Models with directory paths. Please see Register Model
- Step 8: Create a file named 'index.js'.
//@filename: src/index.ts
import orm from './orm'
(async() =>{
const {
createModels,
repos: {Shop, Product}
} = orm.getContext()
// create the tables (if necessary)
await createModels()
const [createdShop1, createdShop2] = await Shop.createEach([{ id: 1 }, {id: 2}])
const createdProducts = await Product.createEach([
{shopId: createdShop1.id },
{shopId: createdShop2.id }
])
//Find Shop with Id 2 and with related products
const foundShop2 = await Shop.find({
selectProps: ['products'],
where: {id: 2}
})
console.log('Found', foundShop2)
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//@filename: src/index.js
const orm = require('./orm');
(async() =>{
let {
createModels,
repos: {Shop, Product}
} = orm.getContext()
// create the tables (if necessary)
await createModels()
let [createdShop1, createdShop2] = await Shop.createEach([{ id: 1 }, {id: 2}])
let createdProducts = await Product.createEach([
{shopId: createdShop1.id },
{shopId: createdShop2.id }
])
//Find Shop with Id 2 and with related products
let foundShop2 = await Shop.find({
selectProps: ['products'],
where: {id: 2}
})
console.log('Found', foundShop2)
})()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- Step 9: Run your program
node index.js
1
Output:
Found [ { products: [ [Object] ], id: 2 } ]
1
TIP
You can find the full examples hereopen in new window.