2021-06-17 16:17:15 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Database } = require('../lib/index');
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
const models = require('./models');
|
|
|
|
const connections = require('./connections');
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const orm = new Database({
|
|
|
|
connection: connections.mysql,
|
|
|
|
models: Database.transformContentTypes(models),
|
|
|
|
});
|
2021-06-17 16:17:15 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
try {
|
|
|
|
// await orm.schema.drop();
|
|
|
|
// await orm.schema.create();
|
|
|
|
|
|
|
|
console.log(orm.connection.client.config.client);
|
|
|
|
|
|
|
|
await orm.schema.reset();
|
|
|
|
// await orm.schema.sync();
|
|
|
|
} finally {
|
|
|
|
orm.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err);
|
2021-06-17 16:17:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// const entityService = uid => {
|
|
|
|
// // knows more about abstraction then the query layer
|
|
|
|
// // will be moved in the core services not the db
|
|
|
|
// // D&P should wrap some d&p logic
|
|
|
|
// // i18N should wrapp some i18n logic etc etc
|
|
|
|
|
|
|
|
// const repo = orm.query(uid);
|
|
|
|
|
|
|
|
// return {
|
|
|
|
// findById(id) {
|
|
|
|
// return repo.findOne({ where: { id } });
|
|
|
|
// },
|
|
|
|
|
|
|
|
// async update(id, data) {
|
|
|
|
// await repo.update({ where: { id }, data });
|
|
|
|
|
|
|
|
// return repo.findOne({ where: { id } });
|
|
|
|
// },
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
db.migration.create();
|
|
|
|
db.migration.up();
|
|
|
|
db.migration.down();
|
|
|
|
|
|
|
|
db.seed.run();
|
|
|
|
db.seed.create();
|
|
|
|
|
|
|
|
db.exporter.dump()
|
|
|
|
db.importer.restore()
|
|
|
|
|
|
|
|
db.schema.addField
|
|
|
|
db.schema.removeField
|
|
|
|
db.schema.addCollection
|
|
|
|
db.schema.removeCollection
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// const r = await orm.entityManager
|
|
|
|
// .createQueryBuilder('article')
|
|
|
|
// .select('*')
|
|
|
|
// .join({
|
|
|
|
// rootColumn: 'id',
|
|
|
|
// alias: 'ac',
|
|
|
|
// referencedTable: 'categories_articles_articles_links',
|
|
|
|
// referencedColumn: 'article_id',
|
|
|
|
// })
|
|
|
|
// .join({
|
|
|
|
// rootTable: 'ac',
|
|
|
|
// rootColumn: 'category_id',
|
|
|
|
// alias: 'c',
|
|
|
|
// referencedTable: 'categories',
|
|
|
|
// referencedColumn: 'id',
|
|
|
|
// })
|
|
|
|
// .where({
|
|
|
|
// $and: [],
|
|
|
|
// })
|
|
|
|
// .populate({
|
|
|
|
// category: true,
|
|
|
|
// })
|
|
|
|
// .execute();
|
|
|
|
|
|
|
|
// console.log(r);
|
|
|
|
|
|
|
|
// const catA = await entityService('category').findById(348);
|
|
|
|
// console.log(catA);
|
|
|
|
/*
|
2021-06-17 16:17:15 +02:00
|
|
|
orm.contentType('category').loadRelation()
|
|
|
|
=> query('category').load('xxx')
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// await orm.query('category').delete();
|
|
|
|
|
|
|
|
// const article = await orm.query('article').create({
|
|
|
|
// select: ['id', 'title'],
|
|
|
|
// populate: {
|
|
|
|
// category: {
|
|
|
|
// select: ['price'],
|
|
|
|
// where: {
|
|
|
|
// price: {
|
|
|
|
// $gte: 12,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// data: {
|
|
|
|
// title: 'my category',
|
|
|
|
// category_id: 1, // TODO: handle category: 1 too
|
|
|
|
// },
|
|
|
|
// });
|
2021-06-17 16:17:15 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// console.log(JSON.stringify(article, null, 4));
|
|
|
|
|
|
|
|
// await orm.query('category').createMany({
|
|
|
|
// // select: {},
|
|
|
|
// // populate: {},
|
|
|
|
// data: Array(5)
|
|
|
|
// .fill({})
|
|
|
|
// .map((v, idx) => ({
|
|
|
|
// title: `Category ${_.padStart(idx, 3, '0')}`,
|
|
|
|
// articles: [idx + 1, idx + 2],
|
|
|
|
// })),
|
|
|
|
// });
|
2021-06-23 15:37:20 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// await orm.query('article').createMany({
|
|
|
|
// // select: {},
|
|
|
|
// // populate: {},
|
|
|
|
// data: Array(5)
|
|
|
|
// .fill({})
|
|
|
|
// .map((v, idx) => ({
|
|
|
|
// title: `Article ${_.padStart(idx, 3, '0')}`,
|
|
|
|
// // category_id: idx + 1,
|
|
|
|
// category: idx+1
|
|
|
|
// })),
|
|
|
|
// });
|
2021-06-23 15:37:20 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// const cat = await orm.query('category').create({
|
|
|
|
// data: {
|
|
|
|
// articles: [1, 2, 3, 4, 5],
|
|
|
|
// },
|
|
|
|
// populate: ['articles'],
|
|
|
|
// });
|
2021-06-23 15:37:20 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// console.log(cat);
|
2021-06-23 15:37:20 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// const tag = await orm.query('tag').create({
|
|
|
|
// data: {
|
|
|
|
// articles: [1, 2, 3, 4, 5],
|
|
|
|
// },
|
|
|
|
// populate: ['articles'],
|
|
|
|
// });
|
2021-06-23 15:37:20 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// console.log(tag);
|
|
|
|
|
|
|
|
// const someArticles = await orm.query('article').findMany({
|
|
|
|
// where: {
|
|
|
|
// id: [1, 2, 3, 4, 5],
|
|
|
|
// },
|
|
|
|
// populate: ['tags', 'category'],
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(someArticles);
|
|
|
|
|
|
|
|
// await orm.query('category').updateMany({
|
|
|
|
// where: {
|
|
|
|
// // articles: {
|
|
|
|
// // title: {
|
|
|
|
// // $contains: 'Category',
|
|
|
|
// // },
|
|
|
|
// // },
|
|
|
|
// },
|
|
|
|
// data: {
|
|
|
|
// title: 'Category 007',
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// const r = await orm.query('category').findMany({
|
|
|
|
// where: {
|
|
|
|
// $and: [
|
|
|
|
// {
|
|
|
|
// title: {
|
|
|
|
// $ne: 'salut',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ],
|
|
|
|
// title: 'test',
|
|
|
|
// price: {
|
|
|
|
// $gt: 12,
|
|
|
|
// },
|
|
|
|
// articles: {
|
|
|
|
// title: {
|
|
|
|
// $startsWith: 'Test',
|
|
|
|
// // $mode: 'insensitive',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// compo: {
|
|
|
|
// key: 'x',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
2021-06-17 16:17:15 +02:00
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
// // escape stuff
|
|
|
|
// // const raw = (strings, )
|
|
|
|
|
|
|
|
// const params = {
|
|
|
|
// select: ['id', 'title'],
|
|
|
|
// where: {
|
|
|
|
// $not: {
|
|
|
|
// $or: [
|
|
|
|
// {
|
|
|
|
// articles: {
|
|
|
|
// category: {
|
|
|
|
// title: 'Category 003',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// title: {
|
|
|
|
// $in: ['Category 001', 'Category 002'],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// title: {
|
|
|
|
// $not: 'Category 001',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// orderBy: [{ articles: { title: 'asc' } }],
|
|
|
|
// limit: 10,
|
|
|
|
// offset: 0,
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const r = await orm.query('category').findMany(params);
|
|
|
|
|
|
|
|
// console.log(r);
|
|
|
|
|
|
|
|
// const r = await orm.query('category').findMany({
|
|
|
|
// where: {
|
|
|
|
// $or: [
|
|
|
|
// {
|
|
|
|
// compo: {
|
|
|
|
// value: {
|
|
|
|
// $gte: 3,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// articles: {
|
|
|
|
// title: {
|
|
|
|
// $contains: 'test',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ],
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(r);
|
|
|
|
|
|
|
|
// const r = await orm.query('user').findMany({
|
|
|
|
// where: {
|
|
|
|
// address: {
|
|
|
|
// name: 'A',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(r);
|
|
|
|
|
|
|
|
// const [results, count] = await orm.query('category').findWithCount(params);
|
|
|
|
|
|
|
|
// console.log({ results, count });
|
|
|
|
|
|
|
|
// await orm.query('category', {
|
|
|
|
// populate: ['articles'],
|
|
|
|
// });
|
|
|
|
|
|
|
|
// const address = await orm.query('address').findMany({
|
|
|
|
// populate: {
|
|
|
|
// user: {
|
|
|
|
// // select: ['id', 'address_id'],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(address);
|
|
|
|
|
|
|
|
// const user = await orm.query('user').findMany({
|
|
|
|
// populate: {
|
|
|
|
// address: {
|
|
|
|
// // select: ['id', 'name'],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(user);
|
|
|
|
|
|
|
|
// const article = await orm.query('article').findMany({
|
|
|
|
// populate: {
|
|
|
|
// category: true,
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
|
|
|
|
// console.log(article);
|
|
|
|
|
|
|
|
// await orm.query('category').findMany({
|
|
|
|
// populate: {
|
|
|
|
// compo: true,
|
|
|
|
// articles: {
|
|
|
|
// select: ['title'],
|
|
|
|
// populate: {
|
|
|
|
// category: {
|
|
|
|
// select: ['title'],
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// limit: 5,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// await orm.query('article').findMany({
|
|
|
|
// populate: {
|
|
|
|
// tags: true,
|
|
|
|
// },
|
|
|
|
// limit: 5,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// await orm.query('tag').findMany({
|
|
|
|
// populate: {
|
|
|
|
// articles: true,
|
|
|
|
// },
|
|
|
|
// limit: 5,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// const articleCategory = orm.query('article').load(article, 'category', {
|
|
|
|
// select: ['id', 'title'],
|
|
|
|
// limit: 5,
|
|
|
|
// offset: 2,
|
|
|
|
// orderBy: 'title',
|
|
|
|
// where: {},
|
|
|
|
// });
|
|
|
|
|
|
|
|
// const article = await orm.query('article').populate(article, {
|
|
|
|
// category: true,
|
|
|
|
// tags: true
|
|
|
|
// });
|
|
|
|
|
|
|
|
// await orm.query('article').findMany({
|
|
|
|
// limit: 5,
|
|
|
|
// where: {
|
|
|
|
// compo: {
|
|
|
|
// key: 'xx',
|
|
|
|
// },
|
|
|
|
|
|
|
|
// },
|
|
|
|
// populate: {
|
|
|
|
// category: {
|
|
|
|
// select: ['id', 'title'],
|
|
|
|
// limit: 5,
|
|
|
|
// offset: 2,
|
|
|
|
// orderBy: 'title',
|
|
|
|
// populate: {
|
|
|
|
// articles: {
|
|
|
|
// populate: {
|
|
|
|
// tags: true,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// compo: true,
|
|
|
|
// },
|
|
|
|
// orderBy: { compo: { key: 'DESC' } },
|
|
|
|
// });
|