unstable typescript

This commit is contained in:
tmshv committed 2019-10-14 23:42:43 +03:00
1 parent 274e1e7fc0
commit b35e4e36e2
19 files changed
+658 -286

No files matched your search

File renamed without changes.
-117
View File
@@ -1,117 +0,0 @@
const Data = require('./Data')
const Image = require('./Image')
const Config = require('./Config')
const {find, findOne, total} = require('../lib/store')
const store = () => Data.getStore(Article)
function make(C) {
return x => new C(x)
}
class Article {
static async find(query, options = {}) {
const items = await find(store(), query, options)
return Promise.all(items.map(processArticle))
}
static async findById(id) {
const item = await findOne(store(), {id})
return !item
? null
: processArticle(item)
}
static async total(query = {}) {
return total(store(), query)
}
static async findPinned() {
const now = new Date()
const query = {until: {$gte: now}}
return Article.find(query, {
sort: {date: -1},
})
}
constructor(data) {
this.id = data.id
this.images = data.images
this.post = data.post
this.folder = data.folder
this.date = data.date
this.title = data.title
this.until = data.until
this.tags = (data.tags || []).map(make(Tag))
this.content = data.content
this.hash = data.hash
this.type = data.type
this.origin = data.origin
this.file = data.file
this.version = data.version
this.preview = data.preview
this.url = `/article/${this.id}`
}
plain() {
return {
id: this.id,
images: this.images,
post: this.post,
folder: this.folder,
date: this.date,
title: this.title,
until: this.until,
tags: this.tags,
content: this.content,
hash: this.hash,
type: this.type,
origin: this.origin,
file: this.file,
version: this.version,
preview: this.preview,
url: this.url,
}
}
}
class Tag {
constructor(tag) {
this.name = tag;
}
}
const previewFromImage = (imgs = []) => imgs.length
? imgs[0]
: null
async function resolveDefaultPreview() {
const config = await Config.findConfig()
if (!config) return null
return Image.findByFile(config.articleCardDefaultPreview)
}
async function processArticle(article) {
let preview = null
const previewId = article.preview
? article.preview
: previewFromImage(article.images)
if (previewId) {
preview = await Image.findById(previewId)
} else {
preview = await resolveDefaultPreview()
}
return new Article({
...article,
preview,
})
}
module.exports = Article
+136
View File
@@ -0,0 +1,136 @@
import Data from './Data'
import Image from './Image'
import Config from './Config'
import { findOne, total } from '../lib/store'
import * as libStore from '../lib/store'
const store = () => Data.getStore(Article)
function make(C) {
return x => new C(x)
}
export default class Article {
static async find(query, options = {}): Promise<Article[]> {
return []
// const items = await libStore.find(store(), query, options as any)
// return Promise.all(items.map(processArticle))
}
static async findById(id) {
const item = await findOne(store(), { id })
return !item
? null
: processArticle(item)
}
static async total(query = {}) {
return total(store(), query)
}
static async findPinned() {
const now = new Date()
const query = { until: { $gte: now } }
return Article.find(query, {
sort: { date: -1 },
})
}
public id: any
public images: any
public post: any
public folder: any
public date: any
public title: any
public until: any
public tags: any
public content: any
public hash: any
public type: any
public origin: any
public file: any
public version: any
public preview: any
public url: any
constructor(data) {
this.id = data.id
this.images = data.images
this.post = data.post
this.folder = data.folder
this.date = data.date
this.title = data.title
this.until = data.until
this.tags = (data.tags || []).map(make(Tag))
this.content = data.content
this.hash = data.hash
this.type = data.type
this.origin = data.origin
this.file = data.file
this.version = data.version
this.preview = data.preview
this.url = `/article/${this.id}`
}
plain() {
return {
id: this.id,
images: this.images,
post: this.post,
folder: this.folder,
date: this.date,
title: this.title,
until: this.until,
tags: this.tags,
content: this.content,
hash: this.hash,
type: this.type,
origin: this.origin,
file: this.file,
version: this.version,
preview: this.preview,
url: this.url,
}
}
}
class Tag {
public name: string
constructor(tag) {
this.name = tag;
}
}
const previewFromImage = (imgs = []) => imgs.length
? imgs[0]
: null
async function resolveDefaultPreview() {
const config = await Config.findConfig()
if (!config) return null
return Image.findByFile(config.articleCardDefaultPreview)
}
async function processArticle(article) {
let preview = null
const previewId = article.preview
? article.preview
: previewFromImage(article.images)
if (previewId) {
preview = await Image.findById(previewId)
} else {
preview = await resolveDefaultPreview()
}
return new Article({
...article,
preview,
})
}
+1 -3
View File
@@ -2,7 +2,7 @@ const db = require('./db')
const store = new Map()
class Data {
export default class Data {
static setStore(Class, collection) {
store.set(Class, collection)
}
@@ -15,5 +15,3 @@ class Data {
return db.collection(name)
}
}
module.exports = Data
-93
View File
@@ -1,93 +0,0 @@
const Data = require('./Data')
const Image = require('./Image')
const Config = require('./Config')
const {find, findOne, total} = require('../lib/store')
const store = () => Data.getStore(Page)
class Page {
static async find(query, options = {}) {
const items = await find(store(), query, options)
return Promise.all(items.map(processPage))
}
static async findById(id) {
const item = await findOne(store(), {id})
return !item
? null
: processPage(item)
}
static async findByUrl(url) {
const item = await findOne(store(), {url})
return !item
? null
: processPage(item)
}
static async total(query = {}) {
return total(store(), query)
}
constructor(data) {
this.id = data.id
this.hash = data.hash
this.file = data.file
this.url = data.url
this.content = data.content
this.title = data.title
this.data = data.data
this.images = data.images
this.documents = data.documents
this.preview = data.preview
}
plain() {
return {
id: this.id,
images: this.images,
documents: this.documents,
data: this.data,
title: this.title,
content: this.content,
hash: this.hash,
file: this.file,
preview: this.preview,
url: this.url,
}
}
}
const previewFromImage = (imgs = []) => imgs.length
? imgs[0]
: null
async function resolveDefaultPreview() {
const config = await Config.findConfig()
if (!config) return null
return Image.findByFile(config.articleCardDefaultPreview)
}
async function processPage(article) {
let preview = null
const previewId = article.preview
? article.preview
: previewFromImage(article.images)
if (previewId) {
preview = await Image.findById(previewId)
} else {
preview = await resolveDefaultPreview()
}
return new Page({
...article,
preview,
})
}
module.exports = Page
+102
View File
@@ -0,0 +1,102 @@
import Data from './Data'
import { find as storeFind, findOne, total } from '../lib/store'
const Image = require('./Image')
const Config = require('./Config')
const store = () => Data.getStore(Page)
export default class Page {
static async find(query, options = {}): Promise<Page[]> {
const items = await storeFind(store(), query, options as any)
return Promise.all(items.map(processPage))
}
static async findById(id) {
const item = await findOne(store(), { id })
return !item
? null
: processPage(item)
}
static async findByUrl(url) {
const item = await findOne(store(), { url })
return !item
? null
: processPage(item)
}
static async total(query = {}) {
return total(store(), query)
}
public id: any
public hash: any
public file: any
public url: string
public content: any
public title: any
public data: any
public images: any
public documents: any
public preview: any
constructor(data) {
this.id = data.id
this.hash = data.hash
this.file = data.file
this.url = data.url
this.content = data.content
this.title = data.title
this.data = data.data
this.images = data.images
this.documents = data.documents
this.preview = data.preview
}
plain() {
return {
id: this.id,
images: this.images,
documents: this.documents,
data: this.data,
title: this.title,
content: this.content,
hash: this.hash,
file: this.file,
preview: this.preview,
url: this.url,
}
}
}
const previewFromImage = (imgs = []) => imgs.length
? imgs[0]
: null
async function resolveDefaultPreview() {
const config = await Config.findConfig()
if (!config) return null
return Image.findByFile(config.articleCardDefaultPreview)
}
async function processPage(article) {
let preview = null
const previewId = article.preview
? article.preview
: previewFromImage(article.images)
if (previewId) {
preview = await Image.findById(previewId)
} else {
preview = await resolveDefaultPreview()
}
return new Page({
...article,
preview,
})
}
+4 -9
View File
@@ -1,4 +1,4 @@
const Data = require('./Data')
import Data from './Data'
const Page = require('./Page')
const Article = require('./Article')
const Album = require('./Album')
@@ -12,7 +12,7 @@ const { MongoClient, ObjectId } = require('mongodb')
var db
async function connect() {
export async function connect() {
if (db) {
return db
}
@@ -22,11 +22,11 @@ async function connect() {
return db
}
function collection(name) {
export function collection(name) {
return db.collection(name)
}
function id(i) {
export function id(i) {
if (typeof i === 'string') return ObjectId(i)
else if (i instanceof ObjectId) return i
else if (i instanceof Object) return '_id' in i ? ObjectId(i._id) : null
@@ -45,8 +45,3 @@ function init(db) {
return db
}
exports.connect = connect
exports.collection = collection
exports.c = collection
exports.id = id
+3 -7
View File
@@ -1,4 +1,4 @@
async function find(collection, query, {sort, skip, limit}) {
export async function find(collection, query, {sort, skip, limit}) {
const cursor = collection.find(query)
if (sort) cursor.sort(sort)
@@ -8,16 +8,12 @@ async function find(collection, query, {sort, skip, limit}) {
return cursor.toArray()
}
async function findOne(collection, query) {
export async function findOne(collection, query) {
return collection.findOne(query)
}
async function total(collection, query) {
export async function total(collection, query) {
return collection
.find(query)
.count()
}
exports.find = find
exports.findOne = findOne
exports.total = total
+3 -4
View File
@@ -1,14 +1,13 @@
// import { MongoClient } from 'mongodb';
// const { connect, collection } = require('../../src/core/db')
// import {collection, connect} from '../core/db'
const core = require('../core/db')
import {collection, connect} from '../core/db'
// const client = new MongoClient(process.env.MONGODB_URI, { useNewUrlParser: true });
export const withCollection = handler => async (req, res) => {
await core.connect()
await connect()
req.collection = core.collection
req.collection = collection
// if (!client.isConnected()) {
// return client.connect().then(() => {
+9 -8
View File
@@ -1,8 +1,8 @@
const Koa = require('koa')
const next = require('next')
const { get } = require('koa-route')
const { connect } = require('./core/db')
const Page = require('./core/Page')
import Koa from 'koa'
import next from 'next'
import { get } from 'koa-route'
import { connect } from './core/db'
import Page from './core/Page'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
@@ -18,8 +18,9 @@ app.prepare().then(async () => {
process.exit(1)
}
const pages = await Page.find({})
const pagesUrls = pages.map(x => x.url)
// const pages = await Page.find({})
// const pagesUrls = pages.map(x => x.url)
const pagesUrls = []
const server = new Koa()
server.use(async (ctx, next) => {
@@ -29,7 +30,7 @@ app.prepare().then(async () => {
server.use(get('*', async ctx => {
const url = ctx.req.url
if (pagesUrls.includes(url)) {
await app.render(ctx.req, ctx.res, '/next', ctx.query)
} else {