619 lines
12 KiB
JavaScript
Raw Normal View History

2021-06-17 16:17:15 +02:00
'use strict';
const _ = require('lodash');
const { Database } = require('../lib/index');
const category = {
2021-06-18 12:27:47 +02:00
modelName: 'category',
2021-06-17 16:17:15 +02:00
uid: 'category',
2021-06-18 12:27:47 +02:00
collectionName: 'categories',
2021-06-17 16:17:15 +02:00
attributes: {
title: {
type: 'string',
},
price: {
type: 'integer',
//
column: {
// unique: true,
nonNullable: true,
unsigned: true,
defaultTo: 12.0,
},
},
articles: {
type: 'relation',
relation: 'oneToMany',
target: 'article',
mappedBy: 'category',
2021-06-18 12:27:47 +02:00
// useJoinTable: false,
2021-06-17 16:17:15 +02:00
},
compo: {
type: 'component',
component: 'compo',
},
},
};
const article = {
2021-06-18 12:27:47 +02:00
modelName: 'article',
2021-06-17 16:17:15 +02:00
uid: 'article',
2021-06-18 12:27:47 +02:00
collectionName: 'articles',
2021-06-17 16:17:15 +02:00
attributes: {
title: {
type: 'string',
},
category: {
type: 'relation',
relation: 'manyToOne',
target: 'category',
inversedBy: 'articles',
2021-06-23 15:37:20 +02:00
// useJoinTable: false,
2021-06-17 16:17:15 +02:00
},
tags: {
type: 'relation',
relation: 'manyToMany',
target: 'tag',
inversedBy: 'articles',
},
compo: {
type: 'component',
component: 'compo',
repeatable: true,
},
// cover: {
// type: 'media',
// single: true,
// },
// gallery: {
// type: 'media',
// multiple: true,
// },
},
};
const tags = {
2021-06-18 12:27:47 +02:00
modelName: 'tag',
2021-06-17 16:17:15 +02:00
uid: 'tag',
2021-06-18 12:27:47 +02:00
collectionName: 'tags',
2021-06-17 16:17:15 +02:00
attributes: {
name: {
type: 'string',
},
articles: {
type: 'relation',
relation: 'manyToMany',
target: 'article',
mappedBy: 'tag',
},
},
};
const compo = {
2021-06-18 12:27:47 +02:00
modelName: 'compo',
2021-06-17 16:17:15 +02:00
uid: 'compo',
2021-06-18 12:27:47 +02:00
collectionName: 'compos',
2021-06-17 16:17:15 +02:00
attributes: {
key: {
type: 'string',
},
value: {
type: 'integer',
},
},
};
const user = {
2021-06-18 12:27:47 +02:00
modelName: 'user',
2021-06-17 16:17:15 +02:00
uid: 'user',
2021-06-18 12:27:47 +02:00
collectionName: 'users',
2021-06-17 16:17:15 +02:00
attributes: {
address: {
type: 'relation',
relation: 'oneToOne',
target: 'address',
inversedBy: 'user',
// useJoinTable: false,
},
},
};
const address = {
2021-06-18 12:27:47 +02:00
modelName: 'address',
2021-06-17 16:17:15 +02:00
uid: 'address',
2021-06-18 12:27:47 +02:00
collectionName: 'addresses',
2021-06-17 16:17:15 +02:00
attributes: {
name: {
type: 'string',
},
user: {
type: 'relation',
relation: 'oneToOne',
target: 'user',
mappedBy: 'address',
},
},
};
// const orm = new Database({
// connection: {
// client: 'sqlite',
// connection: {
// filename: 'test.sqlite',
// },
// useNullAsDefault: true,
// debug: true,
// },
// models: [category, article],
// });
const file = {
2021-06-18 12:27:47 +02:00
modelName: 'file',
2021-06-17 16:17:15 +02:00
uid: 'file',
2021-06-18 12:27:47 +02:00
collectionName: 'files',
2021-06-17 16:17:15 +02:00
attributes: {
name: {
type: 'string',
},
alternativeText: {
type: 'string',
},
caption: {
type: 'string',
},
width: {
type: 'integer',
},
height: {
type: 'integer',
},
formats: {
type: 'json',
},
hash: {
type: 'string',
},
ext: {
type: 'string',
},
mime: {
type: 'string',
},
size: {
type: 'decimal',
},
url: {
type: 'string',
},
previewUrl: {
type: 'string',
},
provider: {
type: 'string',
},
provider_metadata: {
type: 'json',
},
// related: {
// type: 'relation',
// relation: 'oneToMany',
// target: 'file_morph',
// mappedBy: 'file',
// },
// related: {
// type: 'relation',
// realtion: 'morphTo',
// },
},
};
const fileMorph = {
2021-06-18 12:27:47 +02:00
modelName: 'file-morph',
2021-06-17 16:17:15 +02:00
uid: 'file-morph',
2021-06-18 12:27:47 +02:00
collectionName: 'file_morphs',
2021-06-17 16:17:15 +02:00
attributes: {
// file: {
// type: 'relation',
// relation: 'manyToOne',
// target: 'file',
// inversedBy: 'related',
// useJoinTable: false,
// },
},
};
const orm = new Database({
connector: 'xx',
connection: {
client: 'postgres',
connection: {
database: 'strapi',
user: 'strapi',
password: 'strapi',
},
// debug: true,
},
2021-06-18 12:27:47 +02:00
models: Database.transformContentTypes([
2021-06-17 16:17:15 +02:00
category,
article,
compo,
tags,
user,
address,
file,
fileMorph,
]),
});
// 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
*/
async function main() {
// await orm.schema.drop();
// await orm.schema.create();
await orm.schema.reset();
// await orm.schema.sync();
// const r = await orm.entityManager
// .createQueryBuilder('article')
2021-06-18 12:27:47 +02:00
// .select('*')
2021-06-17 16:17:15 +02:00
// .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',
// })
2021-06-18 12:27:47 +02:00
// .where({
// $and: [],
// })
// .populate({
// category: true,
// })
2021-06-17 16:17:15 +02:00
// .execute();
// console.log(r);
// const catA = await entityService('category').findById(348);
// console.log(catA);
/*
orm.contentType('category').loadRelation()
=> query('category').load('xxx')
*/
// 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
// },
// });
// console.log(JSON.stringify(article, null, 4));
await orm.query('article').createMany({
// select: {},
// populate: {},
data: Array(100)
.fill({})
.map((v, idx) => ({
title: `Article ${_.padStart(idx, 3, '0')}`,
2021-06-23 15:37:20 +02:00
// category_id: idx + 1,
// category: idx+1
2021-06-17 16:17:15 +02:00
})),
});
2021-06-23 15:37:20 +02:00
// await orm.query('category').createMany({
// // select: {},
// // populate: {},
// data: Array(100)
// .fill({})
// .map((v, idx) => ({
// title: `Category ${_.padStart(idx, 3, '0')}`,
// articles: [idx + 1, idx + 2],
// })),
// });
const cat = await orm.query('category').create({
data: {
articles: [1, 2, 3, 4, 5],
},
populate: ['articles'],
});
console.log(cat);
const tag = await orm.query('tag').create({
data: {
articles: [1, 2, 3, 4, 5],
},
populate: ['articles'],
});
console.log(tag);
const someArticles = await orm.query('article').findMany({
where: {
id: [1, 2, 3, 4, 5],
},
populate: ['tags', 'category'],
});
console.log(someArticles);
2021-06-17 16:17:15 +02:00
// 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',
// },
// },
// });
// // 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);
2021-06-23 15:37:20 +02:00
// await orm.query('category').findMany({
// populate: {
// compo: true,
// articles: {
// select: ['title'],
// populate: {
// category: {
// select: ['title'],
// },
// },
// },
// },
// limit: 5,
// });
2021-06-17 16:17:15 +02:00
2021-06-23 15:37:20 +02:00
// await orm.query('article').findMany({
// populate: {
// tags: true,
// },
// limit: 5,
// });
2021-06-17 16:17:15 +02:00
2021-06-23 15:37:20 +02:00
// await orm.query('tag').findMany({
// populate: {
// articles: true,
// },
// limit: 5,
// });
2021-06-17 16:17:15 +02:00
2021-06-18 12:27:47 +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
// });
2021-06-23 15:37:20 +02:00
// await orm.query('article').findMany({
// limit: 5,
// where: {
// compo: {
// key: 'xx',
// },
2021-06-18 12:27:47 +02:00
2021-06-23 15:37:20 +02:00
// },
// populate: {
// category: {
// select: ['id', 'title'],
// limit: 5,
// offset: 2,
// orderBy: 'title',
// populate: {
// articles: {
// populate: {
// tags: true,
// },
// },
// },
// },
// compo: true,
// },
// orderBy: { compo: { key: 'DESC' } },
// });
2021-06-17 16:17:15 +02:00
}
main()
.catch(err => {
console.error(err);
})
.finally(() => orm.destroy());