forked from feathersjs-ecosystem/feathers-objection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (51 loc) · 1.33 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import feathers from '@feathersjs/feathers'
import express from '@feathersjs/express'
import rest from '@feathersjs/express/rest'
import { errorHandler } from '@feathersjs/express'
import bodyParser from 'body-parser'
import { Model } from 'objection'
import createService from '../src'
import createModel from './todo'
// Initialize Knex
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
},
useNullAsDefault: false
})
// Bind Objection.js
Model.knex(knex)
// Create a feathers instance.
const app = express(feathers())
// Enable REST services
.configure(rest())
// Turn on JSON parser for REST services
.use(bodyParser.json())
// Turn on URL-encoded parser for REST services
.use(bodyParser.urlencoded({ extended: true }))
app.set('knex', knex)
// Create service
app.use('/todos', createService({
model: createModel(app),
id: 'id',
paginate: {
default: 2,
max: 4
}
}))
app.service("todos").hooks({
before: {
find(context) {
const query = context.service.createQuery(context.params);
query.where('complete', true);
context.params.objection = query;
return context;
}
}
});
// Handle Errors
app.use(errorHandler())
// Start the server
module.exports = app.listen(3030)
console.log('Feathers Todo Objection service running on 127.0.0.1:3030')