Notadb is a shortened for "Not a database".
This library is you can do duck typing using a set schema.
yarn add notadb
import { Model } from "notadb";
const post = new Model([{ id: 2 }]);
const res = post.find(2);
/* res
{
id: 2,
key: "",
link: "",
type: "post",
author: {},
subject: "",
description: "",
thumbnail: "",
media: [],
properties: {},
metadata: [],
relations: [],
createdAt: "1977-01-01T00:00:00.0000",
updatedAt: "1977-01-01T00:00:00.0000",
}
*/
key | type | default | description |
---|---|---|---|
id | number | -1 | primary key |
key | string | "" | hash key |
link | string | "" | page link |
type | string | post | post type |
author | Author | {} | user |
subject | string | "" | subject |
description | string | "" | description |
thumbnail | string | "" | image url |
media | Media | [] | Media type |
metadata | Metadata[] | [] | |
properties | Properties | {} | |
relations | Record[] | [] | relations record |
createdAt | Date | current datetime | create datetime |
updatedAt | Date | current datetime | update datetime |
key | type | default | description |
---|---|---|---|
id | number | -1 | - |
name | string | "" | - |
avatar | string | "" | image url |
key | type | default | description |
---|---|---|---|
url | string | "" | - |
mimetype | string | "" | - |
key | type | default | description |
---|---|---|---|
label | string | "" | - |
name | string | "" | - |
value | any | "" | - |
key | type | default | description |
---|---|---|---|
name | string | "" | - |
value | any | "" | - |
const posts = new Model();
posts.insert({ id: 2 });
// or
const posts = new Model([{ id: 2 }]);
// => Model
const posts = new Model([{ id: 2 }]);
posts.update(2, { subject: "update" });
// => Model
const posts = new Model([{ id: 2 }, { id: 3 }]);
posts.destroy(2);
// => Model
const posts = new Model([
{ id: 2, subject: "a" },
{ id: 3, subject: "b" },
{ id: 4, subject: "b" },
]);
posts.all();
// => Record[] [ { id: 3, subject: "b", ...}, {id: 4, subject: "b", ...} ]
// find by id
const posts = new Model([
{ id: 2, subject: "a" },
{ id: 3, subject: "b" },
{ id: 4, subject: "b" },
]);
posts.find(2);
// => Record { id: 2, subject: "a", ...}
const posts = new Model([
{ id: 2, subject: "a" },
{ id: 3, subject: "b" },
{ id: 4, subject: "b" },
]);
posts.findBy("subject", "b");
// => Record { id: 3, subject: "b", ...}
const posts = new Model([
{ id: 2, subject: "a" },
{ id: 3, subject: "b" },
{ id: 4, subject: "b" },
]);
posts.findAll("subject", "b");
// => Record[] [ { id: 3, subject: "b", ...}, {id: 4, subject: "b", ...} ]
const posts = new Model(
Array(10)
.fill(0)
.map((v, index) => ({
id: v + index + 1,
subject: Number(index).toString(16),
}))
);
posts.head();
// => Record { id: 1, subject: "b", ...}
const posts = new Model(
Array(10)
.fill(0)
.map((v, index) => ({
id: v + index + 1,
subject: Number(index).toString(16),
}))
);
posts.last();
// => Record { id: 10, subject: "b", ...}
const posts = new Model(
Array(10)
.fill(0)
.map((v, index) => ({
id: v + index + 1,
subject: Number(index).toString(16),
}))
);
posts.drop(1); // count
// => Record [{ id: 2, subject: "b", ...}, { id: 3, subject: "b", ...}]
const posts = new Model([
{
id: 1,
subject: "title",
properties: { like: { label: "like", value: 11 } },
},
]);
const res = posts.find(1);
res.prop("like");
// => 11
const posts = new Model([
{
id: 1,
subject: "title",
properties: { like: { label: "like", value: 1 } },
},
]);
const res = posts.find(1);
res.prop("like", 11);
// => 11
If you don't put in the key, everything will be returned.
const posts = new Model([
{
id: 1,
subject: "title",
properties: {
like: { label: "like", value: 11 },
bookmark: { label: "bookmark", value: 12 },
},
},
]);
const res = posts.find(1);
const props = res.prop();
// => { like: 11, bookmark: 12 }
const posts = new Model([
{
id: 1,
subject: "title",
metadata: [{ label: "like", name: "like", value: 12 }],
},
]);
const res = posts.find(1);
res.meta("like");
// => 12
const posts = new Model([
{
id: 1,
subject: "title",
metadata: [{ label: "like", name: "like", value: 1 }],
},
]);
const res = posts.find(1);
res.meta("like", 12);
// => 12
If you don't put in the key, everything will be returned.
Metadata is good to apply to form.
const posts = new Model([
{
id: 1,
subject: "title",
metadata: [
{ label: "like", name: "like", value: 12 },
{ label: "bookmark", name: "bookmark", value: 13 },
],
},
]);
const res = posts.find(1);
res.meta();
// => [ { name: "like", value: 12 }, { name: "bookmark", value: 13 } ]
You can enumeration Metadata[]
or Property
format
const record = new Record({
id: 1,
subject: "title",
// like Metadata[]
array: [
{ label: "temp", name: "a", value: 1 },
{ label: "temp", name: "b", value: 2 },
],
// like Property
object: {
a: { label: "temp", value: 1 },
b: { label: "temp", value: 2 },
},
}) as Record & { array: Metadata[]; object: Property };
const metas = record.enum(record.array);
// => { a: 1, b: 2}
const props = record.enum(record.object);
// => { a: 1, b: 2}
Facade.author(1, 'b', 'http://www.domain.com');
// => { id: 1, name: 'b', avatar: 'http://www.domain.com' }
Facade.meta('a', 'b', 'c');
// => { label: 'a', name: 'b', value: 'c' }
Facade.prop('a', 'b', 'c');
// => { 'a': { name: 'b', value: 'c' } }
Facade.timestamps(new Date('2022-01-01'));
// => { createdAt: Date, updatedAt: Date }
example
{
...Facade.author(1, 'b', 'http://www.domain.com'),
properties: Object.assign.apply(Object, [
Facade.prop("like", "like", 1),
Facade.prop("bookmark", "bookmark", 0),
]),
metadata: [
Facade.meta("like", "like", 1),
Facade.meta("bookmark", "bookmark", 0),
],
...Facade.timestamps(new Date('2022-01-01'))
}
const posts = new Model([{ id: 2 }]);
posts.on("created", () => {}); // { target: Model }
posts.on("inserted", () => {}); // { record: Object, target: Model }
posts.on("updated", () => {}); // { record: Object, target: Model }
posts.on("destroyed", () => {}); // { record: Object, target: Model }
posts.on("truncated", () => {}); // { }
posts.on("changed", () => {}); // { type: string, target: Model }