433 lines
8.0 KiB
JavaScript
Raw Normal View History

2021-06-17 16:17:15 +02:00
'use strict';
2021-06-28 21:37:44 +02:00
const _ = require('lodash');
2021-06-17 16:17:15 +02:00
2021-06-28 21:37:44 +02:00
const { Database } = require('../lib/index');
2021-06-24 18:28:36 +02:00
const models = require('./models');
const connections = require('./connections');
2021-06-28 21:37:44 +02:00
async function main(connection) {
2021-06-28 12:34:29 +02:00
const orm = await Database.init({
2021-06-28 21:37:44 +02:00
connection,
2021-06-24 18:28:36 +02:00
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);
2021-06-28 21:37:44 +02:00
await orm.schema.sync();
2021-06-24 18:28:36 +02:00
await orm.schema.reset();
2021-06-28 12:34:29 +02:00
2021-07-07 18:04:39 +02:00
await orm.query('compo').create({
data: {
key: 'A',
value: 1,
},
});
2021-07-06 14:18:03 +02:00
await orm.query('article').create({
2021-07-01 13:52:01 +02:00
// select: {},
// populate: {},
2021-07-06 14:18:03 +02:00
data: {
2021-07-07 18:04:39 +02:00
compo: 1,
2021-07-06 14:18:03 +02:00
},
2021-07-01 13:52:01 +02:00
});
2021-07-07 18:04:39 +02:00
await orm.query('article').findMany({
populate: ['category.compo'],
2021-07-01 13:52:01 +02:00
});
2021-07-07 18:04:39 +02:00
// console.log(await orm.query('article').load(1, 'compo'));
// await orm.query('article').delete({ where: { id: 1 } });
2021-07-01 13:52:01 +02:00
// await tests(orm);
2021-06-24 18:28:36 +02:00
} finally {
orm.destroy();
}
}
2021-06-28 21:37:44 +02:00
// (async function() {
// for (const key in connections) {
// await main(connections[key]);
// }
// })().catch(err => {
// console.error(err);
// process.exit();
// });
main(connections.sqlite);
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));
2021-06-28 21:37:44 +02:00
const tests = async orm => {
// 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],
// })),
// });
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-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const cat = await orm.query('category').create({
data: {
articles: [1, 2, 3, 4, 5],
},
populate: ['articles'],
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(cat);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const tag = await orm.query('tag').create({
data: {
articles: [1, 2, 3, 4, 5],
},
populate: ['articles'],
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(tag);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const someArticles = await orm.query('article').findMany({
where: {
id: [1, 2, 3, 4, 5],
},
populate: ['tags', 'category'],
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(someArticles);
await orm.query('category').updateMany({
where: {
// articles: {
// title: {
// $contains: 'Category',
// },
// },
},
data: {
title: 'Category 007',
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
let 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-24 18:28:36 +02:00
2021-06-28 21:37:44 +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,
};
r = await orm.query('category').findMany(params);
console.log(r);
r = await orm.query('category').findMany({
where: {
$or: [
{
compo: {
value: {
$gte: 3,
},
},
},
{
articles: {
title: {
$contains: 'test',
},
},
},
],
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(r);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
r = await orm.query('user').findMany({
where: {
address: {
name: 'A',
},
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(r);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const [results, count] = await orm.query('category').findWithCount(params);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log({ results, count });
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
await orm.query('category', {
populate: ['articles'],
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const address = await orm.query('address').findMany({
populate: {
user: {
// select: ['id', 'address_id'],
},
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(address);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const user = await orm.query('user').findMany({
populate: {
address: {
// select: ['id', 'name'],
},
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(user);
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
const article = await orm.query('article').findMany({
populate: {
category: true,
},
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
console.log(article);
await orm.query('category').findMany({
populate: {
compo: true,
articles: {
select: ['title'],
populate: {
category: {
select: ['title'],
},
},
},
},
limit: 5,
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
await orm.query('article').findMany({
populate: {
tags: true,
},
limit: 5,
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
await orm.query('tag').findMany({
populate: {
articles: true,
},
limit: 5,
});
2021-06-24 18:28:36 +02:00
2021-06-28 21:37:44 +02:00
// 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({
where: {
2021-07-07 18:04:39 +02:00
$not: {
$or: [
{
category: {
title: 'Article 003',
},
},
{
title: {
$in: ['Article 001', 'Article 002'],
},
},
],
title: {
$not: 'Article 007',
},
2021-06-28 21:37:44 +02:00
},
},
2021-07-07 18:04:39 +02:00
orderBy: [{ category: { name: 'asc' } }],
offset: 0,
limit: 10,
2021-06-28 21:37:44 +02:00
populate: {
category: {
orderBy: 'title',
populate: {
2021-07-07 18:04:39 +02:00
categories: {
2021-06-28 21:37:44 +02:00
populate: {
tags: true,
},
},
},
},
2021-07-07 18:04:39 +02:00
seo: true,
2021-06-28 21:37:44 +02:00
},
});
};