DB Models for Image, Album, based on Data
This commit is contained in:
1 parent
b829885b00
commit
b69c19239e
5 files changed
+106
-16
No files matched your search
@@ -0,0 +1,40 @@
|
|||||||
|
const Data = require('./Data')
|
||||||
|
const Image = require('./Image')
|
||||||
|
const {find, findOne} = require('../lib/store')
|
||||||
|
|
||||||
|
const store = () => Data.getStore(Album)
|
||||||
|
|
||||||
|
class Album {
|
||||||
|
static async find(query, options = {}) {
|
||||||
|
const items = await find(store(), query, options)
|
||||||
|
|
||||||
|
return Promise.all(items.map(processAlbum))
|
||||||
|
}
|
||||||
|
|
||||||
|
static async findById(id) {
|
||||||
|
const item = await findOne(store(), {id})
|
||||||
|
|
||||||
|
return !item
|
||||||
|
? null
|
||||||
|
: processAlbum(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const previewFromImage = (imgs = []) => imgs.length ? imgs[0] : null
|
||||||
|
|
||||||
|
const processAlbum = async album => {
|
||||||
|
const previewImageId = album.preview ? album.preview : previewFromImage(album.images)
|
||||||
|
let preview
|
||||||
|
if (previewImageId) {
|
||||||
|
const image = await Image.findById(previewImageId)
|
||||||
|
preview = image.data.medium
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...album,
|
||||||
|
preview,
|
||||||
|
url: `/album/${album.id}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Album
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
const store = new Map()
|
||||||
|
|
||||||
|
class Data {
|
||||||
|
static setStore(Class, collection) {
|
||||||
|
store.set(Class, collection)
|
||||||
|
}
|
||||||
|
|
||||||
|
static getStore(Class) {
|
||||||
|
return store.get(Class)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Data
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
const Data = require('./Data')
|
||||||
|
const {findOne} = require('../lib/store')
|
||||||
|
|
||||||
|
const store = () => Data.getStore(Image)
|
||||||
|
|
||||||
|
class Image {
|
||||||
|
static async findById(id) {
|
||||||
|
return findOne(store(), {_id: id})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Image
|
||||||
+25
-16
@@ -1,24 +1,33 @@
|
|||||||
import {MongoClient, ObjectId} from 'mongodb';
|
const Data = require('./Data')
|
||||||
import co from 'co';
|
const Album = require('./Album')
|
||||||
|
const Image = require('./Image')
|
||||||
|
|
||||||
export var db;
|
const {MongoClient, ObjectId} = require('mongodb')
|
||||||
|
|
||||||
export function connect(uri){
|
export var db
|
||||||
return co(function*() {
|
|
||||||
db = yield MongoClient.connect(uri);
|
export async function connect(uri) {
|
||||||
return db;
|
const c = await MongoClient.connect(uri)
|
||||||
})
|
db = init(c)
|
||||||
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
export function collection(name){
|
export function collection(name) {
|
||||||
return db.collection(name);
|
return db.collection(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const c = collection;
|
export const c = collection
|
||||||
|
|
||||||
export function id(i){
|
export function id(i) {
|
||||||
if(typeof i === 'string')return ObjectId(i);
|
if (typeof i === 'string') return ObjectId(i)
|
||||||
else if(i instanceof ObjectId) return i;
|
else if (i instanceof ObjectId) return i
|
||||||
else if(i instanceof Object) return '_id' in i ? ObjectId(i._id) : null;
|
else if (i instanceof Object) return '_id' in i ? ObjectId(i._id) : null
|
||||||
return null;
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function init(db) {
|
||||||
|
Data.setStore(Album, db.collection('albums'))
|
||||||
|
Data.setStore(Image, db.collection('images'))
|
||||||
|
|
||||||
|
return db
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
async function find(collection, query, {sort, skip, limit}) {
|
||||||
|
const cursor = collection.find(query)
|
||||||
|
|
||||||
|
if (sort) cursor.sort(sort)
|
||||||
|
if (skip) cursor.skip(skip)
|
||||||
|
if (limit) cursor.limit(limit)
|
||||||
|
|
||||||
|
return cursor.toArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findOne(collection, query) {
|
||||||
|
return collection.findOne(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.find = find
|
||||||
|
exports.findOne = findOne
|
||||||
Reference in new issue
Block a user