0.0.1-alpha.4 • Published 4 years ago

@feathersjs/schema-sequelize v0.0.1-alpha.4

Weekly downloads
42
License
MIT
Repository
github
Last release
4 years ago

@feathersjs/schema-sequelize

Sequelize converter for @feathersjs/schema schemas:

import Joi from '@hapi/joi';
import { schema, property } from '@feathersjs/schema';

@schema({
  name: 'users'
})
export class User {
  @property<Joi.NumberSchema> (validator => validator.integer(), {
    sequelize: {
      primaryKey: true,
      autoIncrement: true
    }
  })
  id: number;

  @property<Joi.StringSchema> (validator => validator.email().required())
  email: string;

  @property<Joi.NumberSchema> (validator => validator.integer())
  age: number;
}

@schema({
  name: 'todos',
  sequelize (TodoModel: any, models: any) {
    TodoModel.belongsTo(models.users, { as: 'user' });
  }
})
export class Todo {
  @property<Joi.NumberSchema> (validator => validator.integer(), {
    sequelize: {
      primaryKey: true,
      autoIncrement: true
    }
  })
  id: number;
  
  @property(validator => validator.required())
  text: string;

  @property()
  userId: number;

  @property({
    async resolve (todo: any, context: any) {
      return context.users.findOne({
        raw: true,
        where: {
          id: todo.userId
        }
      });
    }
  })
  user: User;
}

const client = new Sequelize('sqlite://test-db.sqlite');

const UserModel = convert (User, client);
const TodoModel = convert(Todo, client);

associate(client);

await client.sync();

assert.ok(UserModel);
assert.ok(TodoModel);
0.0.1-alpha.4

4 years ago

0.0.1-alpha.3

4 years ago

0.0.1-alpha.2

4 years ago

0.0.1-alpha.1

4 years ago

0.0.1-alpha.0

4 years ago