Search code examples
javascriptamazon-web-servicesexpressamazon-dynamodb

SchemaError: No map schema given for attribute: playlist


I am trying to create a DynamoDB schema and a user model. But whenever I call my User model my api always throws this error: SchemaError: No map schema given for attribute: playlist. Code is below:

/models/v2/schema.js

const dynamoose = require('dynamoose');

const schemaOpt = { saveUnknown: true, timestamps: true };

const playlistSchema = new dynamoose.Schema({
    "description": String,
    "id": String,
    "name": String,
    "image": String
}, schemaOpt);

const userSchema = new dynamoose.Schema({
    "pk": {
        type: String,
        hashKey: true
    },
    "sk": {
        type: String,
        rangeKey: true
    },
    "playlist": {
        type: Object,
        schema: playlistSchema
    }
}, schemaOpt);

module.exports = userSchema;

/models/v2/user

'use strict';

const dynamoose = require('dynamoose');
const userSchema = require('./schema');

const UserModel = dynamoose.model("User", userSchema);


exports.User = UserModel;

Usage example

'use strict';
const rfr = require('rfr');
const { User } = rfr('/models/v2/user')

Above usage code causes my api to throw an error No map schema given for attribute: playlist


Solution

  • saveUnknown opt will allow you store a property playlist with 1 level of unknown properties

    const schemaOpt = { saveUnknown: ["playlist.*"], timestamps: true };
    
    const userSchema = new dynamoose.Schema({
        "pk": {
            type: String,
            hashKey: true
        },
        "sk": {
            type: String,
            rangeKey: true
        },
        "playlist": Object
    }, schemaOpt);