Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Entities - store images as IDs, not plain objects" #559

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/demo-api/src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import config from "./configs";
import { registerPlugins } from "webiny-plugins";
import installer from "webiny-install";

import filesPlugins from "webiny-api-files/install/plugins";
import securityPlugins from "webiny-api-security/install/plugins";
import cmsPlugins from "webiny-api-cms/install/plugins";

registerPlugins(filesPlugins, securityPlugins, cmsPlugins);
registerPlugins(securityPlugins, cmsPlugins);

export const install = async (context = {}) => {
await installer({
Expand Down
37 changes: 10 additions & 27 deletions packages/demo-api/tasks/exportBlocks/exportBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { blue, green } from "chalk";
const pwd: string = (process.cwd(): any);

const copyImage = (srcFilename, targetFilename = null) => {
const src = `${pwd}/../../.files/${srcFilename}`;
const src = `${pwd}/static/${srcFilename}`;
const dest = `${pwd}/../webiny-api-cms/src/install/plugins/importData/blocks/images/${targetFilename ||
srcFilename}`;
fs.copySync(src, dest);
Expand All @@ -29,14 +29,12 @@ export default async () => {
.toArray();

const exportedBlocks = [];
const exportedFiles = [];

for (let i = 0; i < blocks.length; i++) {
const data = blocks[i];
// Copy images.
const regex = /(image|file|preview)":"([a-f0-9]{24})"/gm;

const str = JSON.stringify(data);
const regex = /\/files\/(.*?)"/gm;
const str = JSON.stringify(data.content);
let m;

console.log(`===========================\n> Block: ${data.name}`);
Expand All @@ -46,27 +44,13 @@ export default async () => {
regex.lastIndex++;
}

if (Array.isArray(m)) {
const [, , id] = m;
if (exportedFiles.find(item => item.id === id)) {
continue;
}
const file = await database.mongodb.collection("File").findOne({ id });
if (!file.meta) {
file.meta = {};
}
file.meta.private = true;

file.name = file.src.match(/\/files\/(.*)/);
file.name = file.name[1];

exportedFiles.push(file);
console.log(`${green("> Copy image:")} ${file.name}`);
copyImage(file.name);
}
const filename = m[1];

console.log(`${green("> Copy image:")} ${filename}`);
copyImage(filename);
}

/* console.log(`${blue("> Copy preview:")} ${data.preview.src}`);
console.log(`${blue("> Copy preview:")} ${data.preview.src}`);
const previewName = data.preview.src.match(/\/files\/(.*)/)[1];
let targetName = previewName;
if (!targetName.startsWith("cms-element-")) {
Expand All @@ -75,7 +59,7 @@ export default async () => {
data.preview.name = targetName;
data.preview.src = data.preview.src.replace(previewName, targetName);
}
copyImage(previewName, targetName);*/
copyImage(previewName, targetName);

exportedBlocks.push(data);
}
Expand All @@ -84,8 +68,7 @@ export default async () => {
const index = [
"// NOTE: THIS FILE IS AUTO-GENERATED. MANUAL CHANGES OF THIS FILE WILL BE LOST!\n",
"",
`export const blocks = [${exportedBlocks.map(b => JSON.stringify(b)).join(",\n")}];`,
`export const files = [${exportedFiles.map(f => JSON.stringify(f)).join(",\n")}];`
`export const blocks = [${exportedBlocks.map(b => JSON.stringify(b)).join(",\n")}];`
].join("\n");

console.log("\n> Writing index file...");
Expand Down
35 changes: 8 additions & 27 deletions packages/demo-api/tasks/exportPages/exportPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import inquirer from "inquirer";
const pwd: string = (process.cwd(): any);

const copyImage = (srcFilename, targetFilename = null) => {
const src = `${pwd}/../../.files/${srcFilename}`;
const src = `${pwd}/static/${srcFilename}`;
const dest = `${pwd}/../webiny-api-cms/src/install/plugins/importData/pages/images/${targetFilename ||
srcFilename}`;

Expand All @@ -26,7 +26,7 @@ const writeIndexFile = content => {
fs.writeFileSync(dest, content);
};

const omitPageAttributes = obj => {
const omitAttributes = obj => {
return omit(obj, [
"savedOn",
"createdOn",
Expand Down Expand Up @@ -61,27 +61,25 @@ export default async () => {
]);

// Filter pages
pages = pages.filter(p => answers.pages.includes(p.id)).map(omitPageAttributes);
pages = pages.filter(p => answers.pages.includes(p.id)).map(omitAttributes);

// Get categories
const categories = (await database.mongodb
.collection("CmsCategory")
.find()
.toArray()).map(omitPageAttributes);
.toArray()).map(omitAttributes);

// Get menus
const menus = (await database.mongodb
.collection("CmsMenu")
.find()
.toArray()).map(omitPageAttributes);

const exportedFiles = [];
.toArray()).map(omitAttributes);

// Copy page files
for (let i = 0; i < pages.length; i++) {
const data = pages[i];
const regex = /(image|file)":"([a-f0-9]{24})"/gm;
const str = JSON.stringify(data);
const regex = /\/files\/(.*?)"/gm;
const str = JSON.stringify(data.content);
let m;

console.log(`===========================\n> Page: ${data.title}`);
Expand All @@ -91,23 +89,7 @@ export default async () => {
regex.lastIndex++;
}

if (Array.isArray(m)) {
const [, , id] = m;
if (exportedFiles.find(item => item.id === id)) {
continue;
}
const file = await database.mongodb.collection("File").findOne({ id });
if (!file.meta) {
file.meta = {};
}
file.meta.private = true;

file.name = file.src.match(/\/files\/(.*)/);
file.name = file.name[1];

exportedFiles.push(file);
copyImage(file.name);
}
copyImage(m[1]);
}

// Copy page image from settings
Expand All @@ -129,7 +111,6 @@ export default async () => {
"",
`export const categories = [${categories.map(c => JSON.stringify(c)).join(", ")}];`,
`export const pages = [${pages.map(p => JSON.stringify(p)).join(", ")}];`,
`export const files = [${exportedFiles.map(f => JSON.stringify(f)).join(", ")}];`,
`export const menus = [${menus.map(m => JSON.stringify(m)).join(", ")}];`
].join("\n");

Expand Down
2 changes: 1 addition & 1 deletion packages/webiny-admin/src/components/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FileManagerPortal extends React.Component<*> {

const props = {
onChange: files => {
const fields = ["id", "name", "src", "size", "type"];
const fields = ["name", "src", "size", "type"];
if (Array.isArray(files)) {
onChange(files.map(file => pick(file, fields)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import gql from "graphql-tag";
const fileFields = /* GraphQL */ `
{
__typename
id
name
src
size
Expand Down
39 changes: 18 additions & 21 deletions packages/webiny-api-cms/src/entities/CmsSettings.entity.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// @flow
import { settingsFactory } from "webiny-api/entities";
import { EntityModel } from "webiny-entity";
import { Model } from "webiny-model";
import FileModel from "./File.model";

const createSocialMediaModel = context =>
class SocialMediaModel extends EntityModel {
constructor() {
super();
this.setParentEntity(context.settings);
this.attr("facebook").char();
this.attr("twitter").char();
this.attr("instagram").char();
this.attr("image").entity(context.files.entities.File);
}
};
class SocialMediaModel extends Model {
constructor() {
super();
this.attr("facebook").char();
this.attr("twitter").char();
this.attr("instagram").char();
this.attr("image").model(FileModel);
}
}

class CmsSettingsPagesModel extends Model {
constructor() {
Expand All @@ -25,23 +23,22 @@ class CmsSettingsPagesModel extends Model {
}
}

const createCmsSettingsModel = context => {
return class CmsSettingsModel extends EntityModel {
const cmsSettingsModelFactory = () => {
return class CmsSettingsModel extends Model {
constructor() {
super();
this.setParentEntity(context.settings);
this.attr("pages").model(CmsSettingsPagesModel);
this.attr("name").char();
this.attr("domain").char();
this.attr("favicon").entity(context.files.entities.File);
this.attr("logo").entity(context.files.entities.File);
this.attr("social").model(createSocialMediaModel(context));
this.attr("favicon").model(FileModel);
this.attr("logo").model(FileModel);
this.attr("social").model(SocialMediaModel);
}
};
};

export const cmsSettingsFactory = (context: Object) => {
return class CmsSettings extends settingsFactory(context) {
export const cmsSettingsFactory = (...args: Array<*>) => {
return class CmsSettings extends settingsFactory(...args) {
static key = "cms";
static classId = "CmsSettings";
static collectionName = "Settings";
Expand All @@ -51,7 +48,7 @@ export const cmsSettingsFactory = (context: Object) => {

constructor() {
super();
this.attr("data").model(createCmsSettingsModel({ ...context, settings: this }));
this.attr("data").model(cmsSettingsModelFactory());
}
};
};
12 changes: 8 additions & 4 deletions packages/webiny-api-cms/src/entities/Element.entity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { Entity } from "webiny-entity";
import createContentAttribute from "./Page/ContentAttribute";
import FileModel from "./File.model";

type ElementType = "element" | "block";

Expand All @@ -12,7 +12,7 @@ export interface IElement extends Entity {
preview: Object;
}

export function elementFactory(context): Class<IElement> {
export function elementFactory(): Class<IElement> {
return class Element extends Entity {
static classId = "CmsElement";

Expand All @@ -24,16 +24,20 @@ export function elementFactory(context): Class<IElement> {

constructor() {
super();

this.attr("name")
.char()
.setValidators("required");

this.attr("category").char();
this.attr("content").custom(createContentAttribute(context));

this.attr("content").object();

this.attr("type")
.char()
.setValidators("required,in:element:block");

this.attr("preview").entity(context.files.entities.File);
this.attr("preview").model(FileModel);
}
};
}
17 changes: 17 additions & 0 deletions packages/webiny-api-cms/src/entities/File.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @flow
import { Model } from "webiny-model";

export default class FileModel extends Model {
name: string;
size: number;
src: string;
type: string;
constructor() {
super();
this.attr("name").char();
this.attr("size").integer();
this.attr("src").char();
this.attr("type").char();
this.attr("meta").object();
}
}
5 changes: 2 additions & 3 deletions packages/webiny-api-cms/src/entities/Page.entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Entity, type EntityCollection } from "webiny-entity";
import type { ICategory } from "./Category.entity";
import pageSettingsFactory from "./PageSettings.model";
import mdbid from "mdbid";
import createContentAttribute from "./Page/ContentAttribute";

export interface IPage extends Entity {
createdBy: Entity;
Expand Down Expand Up @@ -77,11 +76,11 @@ export const pageFactory = (context: Object): Class<IPage> => {
.onSet(value => (this.locked ? this.url : value));

this.attr("content")
.custom(createContentAttribute(context))
.object()
.onSet(value => (this.locked ? this.content : value));

this.attr("settings")
.model(pageSettingsFactory({ ...context, page: this }))
.model(pageSettingsFactory({ entities: cms.entities, page: this }))
.onSet(value => (this.locked ? this.settings : value));

this.attr("version").integer();
Expand Down
Loading