-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task(core): add joigoose, refactor some models [#56]
- tag - topic - metadata - storage
- Loading branch information
Showing
15 changed files
with
178 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,36 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const { Schema } = mongoose; | ||
import { Joi, joiToMongoose, joiSchemas } from 'validation'; | ||
|
||
// ObjectMetadataSchema model is a general-purpose object to be used across the project. | ||
const ObjectMetadataSchema = new Schema({ | ||
createdAt: { | ||
type: Date, | ||
required: true, | ||
}, | ||
createdBy: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
updatedAt: { | ||
type: Date, | ||
required: true, | ||
}, | ||
updatedBy: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
export const joiMetadataSchema = Joi.object({ | ||
createdAt: Joi.date().required(), | ||
updatedAt: Joi.date().required(), | ||
createdBy: joiSchemas.userRef.required(), | ||
updatedBy: joiSchemas.userRef.required(), | ||
}); | ||
|
||
// ObjectMetadataSchema model is a general-purpose object to be used across the project. | ||
const ObjectMetadataSchema = joiToMongoose(joiMetadataSchema); | ||
|
||
export const ObjectMetadata = mongoose.model('ObjectMetadata', ObjectMetadataSchema); | ||
|
||
export const getInitObjectMetadata = userId => ({ | ||
export const getInitObjectMetadata = ({ _id }) => ({ | ||
createdAt: Date.now(), | ||
createdBy: userId, | ||
createdBy: _id, | ||
updatedAt: Date.now(), | ||
updatedBy: userId, | ||
updatedBy: _id, | ||
}); | ||
|
||
export const updateObjectMetadata = (oldMetadata, userId) => ({ | ||
export const updateObjectMetadata = (oldMetadata, { _id }) => ({ | ||
...oldMetadata, | ||
updatedAt: Date.now(), | ||
updatedBy: userId, | ||
updatedBy: _id, | ||
}); | ||
|
||
export const mergeWithUpdateMetadata = (data, userId) => ({ | ||
export const mergeWithUpdateMetadata = (data, { _id }) => ({ | ||
$set: { | ||
...data, | ||
'metadata.updatedBy': userId, | ||
'metadata.updatedBy': _id, | ||
'metadata.updatedAt': Date.now(), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,16 @@ | ||
import Joi from 'joi'; | ||
import joiObjectId from 'joi-objectid'; | ||
|
||
Joi.objectId = joiObjectId(Joi); | ||
|
||
const DATA_SCHEMA = entities => | ||
Joi.object().pattern(Joi.string().valid(entities), Joi.array().items(Joi.objectId())); | ||
import { Joi } from 'validation'; | ||
|
||
export const MAIN_PAGE_KEY = 'main-page-key'; | ||
export const SIDEBAR_KEY = 'sidebar-key'; | ||
|
||
// This is a list of Main Page Entities to be accepted by setMainPage() method. | ||
// Note: 'topics' are not accepted; get method always returns all topics available. | ||
// TODO: to add 'banners', 'diary'. | ||
const MAIN_PAGE_ENTITIES = ['articles', 'tags']; | ||
|
||
export const MAIN_PAGE_DATA_SCHEMA = DATA_SCHEMA(MAIN_PAGE_ENTITIES); | ||
|
||
export const SIDEBAR_KEY = 'sidebar-key'; | ||
|
||
const SIDEBAR_ENTITIES = ['tags']; | ||
|
||
export const SIDEBAR_DATA_SCHEMA = DATA_SCHEMA(SIDEBAR_ENTITIES); | ||
const getDataSchema = entities => | ||
Joi.object().pattern(Joi.string().valid(entities), Joi.array().items(Joi.objectId())); | ||
|
||
export const joiMainPageDataSchema = getDataSchema(MAIN_PAGE_ENTITIES); | ||
export const joiSidebarDataSchema = getDataSchema(SIDEBAR_ENTITIES); |
Oops, something went wrong.