A secondary JSON parser to turn fields into concrete objects.
After a JSON.parse(s)
, run this with parseModel(ob)
to turn fields into objects, like so:
let ob = JSON.parse(s)
ob = parseModel(ob, User)
npm install treeder/models
Or in your browser:
import {parseModel} from 'https://cdn.jsdelivr.net/gh/treeder/models@1/index.js'
First define your models. You may find this familiar if you use Lit. And these models can be used for migrations, for SQLite and Cloudflare D1 via flaregun, and for the handy api library.
export class User {
static properties = {
id: {
type: String,
primaryKey: true,
},
createdAt: {
type: Date,
},
updatedAt: {
type: Date,
},
name: {
type: String,
},
email: {
type: String,
},
data: {
type: Object,
// you can go deeper into Object properties, only required if you don't want JSON defaults.
birthday: {
type: Date,
}
},
}
}
Use standard JavaScript classes:
- String
- Number
- Boolean
- Date
- BigInt
- Object
To use a custom parser, use parse
instead of type
:
{
balance: {
parse: (n) => (n ? new Big(n) : new Big(0)),
},
}
Then call parseModel()
with your object and your new class.
let ob = JSON.parse(s)
ob = parseModel(ob, User)
That's it!