Skip to content

Instantly share code, notes, and snippets.

@daffl
daffl / hooks.ts
Created January 14, 2020 16:29
Async middleware with TypeScript
import { hooks, HookContext, NextFunction } from '@feathersjs/hooks';
const logRuntime = async (context: HookContext, next: NextFunction) => {
const start = new Date().getTime();
await next();
const end = new Date().getTime();
console.log(`Function '${context.method || '[no name]'}' returned '${context.result}' after ${end - start}ms`);
@daffl
daffl / hooks.js
Created January 14, 2020 16:28
Async middleware with JavaScript
const { hooks } = require('@feathersjs/hooks');
const logRuntime = async (context, next) => {
const start = new Date().getTime();
await next();
const end = new Date().getTime();
console.log(`Function '${context.method || '[no name]'}' returned '${context.result}' after ${end - start}ms`);
@daffl
daffl / models.js
Created November 6, 2019 18:06
Feathers Schema proposal ideas
const { setSchema } = require('@feathersjs/schema');
class User {}
setSchema(User, {
id: {
type: Number
},
email: {
description: 'The user email',
@daffl
daffl / app.ts
Last active April 25, 2019 17:53
Feathers Wings sketch outline
@Entity()
class User {
@PrimaryGeneratedColumn()
id: string;
@toLowerCase()
@validate('email')
@Column()
email: string;
@daffl
daffl / whitelist.js
Created January 4, 2019 21:31
Database adapters: Whitelisting
const service = require('feathers-<database>');
// Allow multi create, patch and remove
service({
whitelist: [ '$regex' ]
});
@daffl
daffl / multi-adapter.js
Created January 4, 2019 21:30
Database adapters: Multi updates
const service = require('feathers-<database>');
// Allow multi create, patch and remove
service({
multi: true
});
// Only allow create with an array
service({
multi: [ 'create' ]
@daffl
daffl / hookless.js
Created January 4, 2019 21:30
Database adapters: Hook-less methods
// Call `get` without running any hooks
const message = await app.service('/messages')._get('<message id>');
@daffl
daffl / querying.js
Created January 4, 2019 21:30
Database adapters: Querying by id
// Will throw `NotFound` if `companyId` does not match
// Even if the `id` is available
app.service('/messages').get('<message id>', {
query: { companyId: '<my company>' }
});
@daffl
daffl / app.js
Created January 3, 2019 03:54
Root level service
const feathers = require('@feathersjs/feathers');
const app = feathers();
app.use('/', myService);
@daffl
daffl / hooks.js
Last active November 17, 2018 02:51
Example service middleware
class MessageService {
// Find a list of resources
find(params) {},
// Get a single resource by its id
get(id, params) {},
// Create a new resource
create(data, params) {},
// Replace an existing resource by its id with data
update(id, data, params) {},
// Merge new data into a resource