js-data / js-data

Give your data the treatment it deserves with a framework-agnostic, datastore-agnostic JavaScript ORM built for ease of use and peace of mind. Works in Node.js and in the Browser. Main Site: http://js-data.io, API Reference Docs: http://api.js-data.io/js-data
MIT License
1.63k stars 139 forks source link

How to create table? #489

Closed DrSensor closed 6 years ago

DrSensor commented 6 years ago

Description

When I try to create a record, the table not created automatically.

Error: insert into `files` (`id`, `name`, `origin`, `type`, `uri`) values (NULL, '1200px-Arduino_Logo.svg.png', 'local-files', 'Electronics Schematic', 'file:///home/wildan/Pictures/1200px-Arduino_Logo.svg.png') - SQLITE_ERROR: no such table: files

Steps to reproduce

import { SqlAdapter } from 'js-data-sql'
import { DataStore } from 'js-data'
import path from 'path'

let adapter = new SqlAdapter({
  knexOpts: {
    client: 'sqlite3',
    connection: {
      filename: path.join('./', 'fide.db')
    },
    useNullAsDefault: true
  }
})

let store = new DataStore({ autoload: true })
store.registerAdapter('sql', adapter, { default: true })

const file = {
  type: 'object',
  required: ['name', 'type', 'origin', 'uri'],
  properties: {
    id: {
      description: 'The unique identifier for a file',
      type: 'integer'
    },
    name: {
      type: 'string'
    },
    type: {
      type: 'string',
      pattern: /Electronics Schematic|Design Blueprint/i
    },
    origin: {
      description: 'Where the file come from (e.g GDrive or local-files)',
      type: 'string',
      pattern: /local-files|gdrive|bim360|s3/i
    },
    uri: {
      description: 'URI with respect to project origin',
      type: 'string',
      pattern: /^(\w+:[/\\]{2}).+/
    }
  }
}

let fileService = store.defineMapper('file', {
  table: 'files',
  schema: new Schema(file)
})

store.create('file', {
  name: '1200px-Arduino_Logo.svg.png',
  type: 'local-files',
  origin: 'Electronics Schematic',
  uri: 'file:///home/wildan/Pictures/1200px-Arduino_Logo.svg.png'
})

I also try to create the table via knex but it doesn't work, fide.db still empty

adapter.knex.schema.createTableIfNotExists('files', table => {
console.log('attempt to create')
table.increments()
table.string('name')
table.string('type')
table.string('origin')
table.string('uri')
// table.timestamps()
})

It even doesn't output attempt to create in console and I also try createTable but its still same.

Thanks!

crobinson42 commented 6 years ago

Please checkout the docs and if you need further help go to the slack team channel. You can also post on stackoverflow with the #js-data tag.