mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-01-24 14:26:58 +02:00
replaced DynamicList with a more generic (model) helper to allow creating pointer slice of any type
This commit is contained in:
parent
39accdba58
commit
051b3702b0
@ -16,6 +16,39 @@ const heading = `
|
|||||||
|
|
||||||
declare var $app: pocketbase.PocketBase
|
declare var $app: pocketbase.PocketBase
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $arrayOf creates a placeholder array of the specified models.
|
||||||
|
* Usually used to populate DB result into an array of models.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ` + "```" + `js
|
||||||
|
* const records = $arrayOf(new Record)
|
||||||
|
*
|
||||||
|
* $app.dao().recordQuery(collection).limit(10).all(records)
|
||||||
|
* ` + "```" + `
|
||||||
|
*/
|
||||||
|
declare function $arrayOf<T>(model: T): Array<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DynamicModel creates a new dynamic model with fields from the provided data shape.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ` + "```" + `js
|
||||||
|
* const model = new DynamicModel({
|
||||||
|
* name: ""
|
||||||
|
* age: 0,
|
||||||
|
* active: false,
|
||||||
|
* roles: [],
|
||||||
|
* meta: {}
|
||||||
|
* })
|
||||||
|
* ` + "```" + `
|
||||||
|
*/
|
||||||
|
declare class DynamicModel {
|
||||||
|
constructor(shape?: { [key:string]: any })
|
||||||
|
}
|
||||||
|
|
||||||
interface Record extends models.Record{} // merge
|
interface Record extends models.Record{} // merge
|
||||||
declare class Record implements models.Record {
|
declare class Record implements models.Record {
|
||||||
constructor(collection?: models.Collection, data?: { [key:string]: any })
|
constructor(collection?: models.Collection, data?: { [key:string]: any })
|
||||||
@ -244,23 +277,23 @@ declare class ApiError implements apis.ApiError {
|
|||||||
constructor(status?: number, message?: string, data?: any)
|
constructor(status?: number, message?: string, data?: any)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NotFoundError extends apis.NotFoundError{} // merge
|
interface NotFoundError extends apis.ApiError{} // merge
|
||||||
declare class NotFoundError implements apis.NotFoundError {
|
declare class NotFoundError implements apis.ApiError {
|
||||||
constructor(message?: string, data?: any)
|
constructor(message?: string, data?: any)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BadRequestError extends apis.BadRequestError{} // merge
|
interface BadRequestError extends apis.ApiError{} // merge
|
||||||
declare class BadRequestError implements apis.BadRequestError {
|
declare class BadRequestError implements apis.ApiError {
|
||||||
constructor(message?: string, data?: any)
|
constructor(message?: string, data?: any)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ForbiddenError extends apis.ForbiddenError{} // merge
|
interface ForbiddenError extends apis.ApiError{} // merge
|
||||||
declare class ForbiddenError implements apis.ForbiddenError {
|
declare class ForbiddenError implements apis.ApiError {
|
||||||
constructor(message?: string, data?: any)
|
constructor(message?: string, data?: any)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UnauthorizedError extends apis.UnauthorizedError{} // merge
|
interface UnauthorizedError extends apis.ApiError{} // merge
|
||||||
declare class UnauthorizedError implements apis.UnauthorizedError {
|
declare class UnauthorizedError implements apis.ApiError {
|
||||||
constructor(message?: string, data?: any)
|
constructor(message?: string, data?: any)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10462
plugins/jsvm/internal/docs/generated/types.d.ts
vendored
10462
plugins/jsvm/internal/docs/generated/types.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@ -67,6 +67,14 @@ func baseBinds(vm *goja.Runtime) {
|
|||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
vm.Set("$arrayOf", func(model any) any {
|
||||||
|
mt := reflect.TypeOf(model)
|
||||||
|
st := reflect.SliceOf(mt)
|
||||||
|
elem := reflect.New(st).Elem()
|
||||||
|
|
||||||
|
return elem.Addr().Interface()
|
||||||
|
})
|
||||||
|
|
||||||
vm.Set("DynamicModel", func(call goja.ConstructorCall) *goja.Object {
|
vm.Set("DynamicModel", func(call goja.ConstructorCall) *goja.Object {
|
||||||
shape, ok := call.Argument(0).Export().(map[string]any)
|
shape, ok := call.Argument(0).Export().(map[string]any)
|
||||||
if !ok || len(shape) == 0 {
|
if !ok || len(shape) == 0 {
|
||||||
@ -80,19 +88,6 @@ func baseBinds(vm *goja.Runtime) {
|
|||||||
return instanceValue
|
return instanceValue
|
||||||
})
|
})
|
||||||
|
|
||||||
vm.Set("DynamicList", func(call goja.ConstructorCall) *goja.Object {
|
|
||||||
shape, ok := call.Argument(0).Export().(map[string]any)
|
|
||||||
if !ok || len(shape) == 0 {
|
|
||||||
panic("missing shape data")
|
|
||||||
}
|
|
||||||
|
|
||||||
instance := newDynamicList(shape)
|
|
||||||
instanceValue := vm.ToValue(instance).(*goja.Object)
|
|
||||||
instanceValue.SetPrototype(call.This.Prototype())
|
|
||||||
|
|
||||||
return instanceValue
|
|
||||||
})
|
|
||||||
|
|
||||||
vm.Set("Record", func(call goja.ConstructorCall) *goja.Object {
|
vm.Set("Record", func(call goja.ConstructorCall) *goja.Object {
|
||||||
var instance *models.Record
|
var instance *models.Record
|
||||||
|
|
||||||
@ -406,24 +401,6 @@ func filesContent(dirPath string, pattern string) (map[string][]byte, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// newDynamicList creates a new dynamic slice of structs with fields based
|
|
||||||
// on the specified "shape".
|
|
||||||
//
|
|
||||||
// Example:
|
|
||||||
//
|
|
||||||
// m := newDynamicList(map[string]any{
|
|
||||||
// "title": "",
|
|
||||||
// "total": 0,
|
|
||||||
// })
|
|
||||||
func newDynamicList(shape map[string]any) any {
|
|
||||||
m := newDynamicModel(shape)
|
|
||||||
mt := reflect.TypeOf(m)
|
|
||||||
st := reflect.SliceOf(mt)
|
|
||||||
elem := reflect.New(st).Elem()
|
|
||||||
|
|
||||||
return elem.Addr().Interface()
|
|
||||||
}
|
|
||||||
|
|
||||||
// newDynamicModel creates a new dynamic struct with fields based
|
// newDynamicModel creates a new dynamic struct with fields based
|
||||||
// on the specified "shape".
|
// on the specified "shape".
|
||||||
//
|
//
|
||||||
|
@ -91,7 +91,7 @@ func TestBaseBindsCollection(t *testing.T) {
|
|||||||
vm := goja.New()
|
vm := goja.New()
|
||||||
baseBinds(vm)
|
baseBinds(vm)
|
||||||
|
|
||||||
v, err := vm.RunString(`new Collection({ name: "test", schema: [{name: "title", "type": "text"}] })`)
|
v, err := vm.RunString(`new Collection({ name: "test", createRule: "@request.auth.id != ''", schema: [{name: "title", "type": "text"}] })`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -105,6 +105,11 @@ func TestBaseBindsCollection(t *testing.T) {
|
|||||||
t.Fatalf("Expected collection with name %q, got %q", "test", m.Name)
|
t.Fatalf("Expected collection with name %q, got %q", "test", m.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expectedRule := "@request.auth.id != ''"
|
||||||
|
if m.CreateRule == nil || *m.CreateRule != expectedRule {
|
||||||
|
t.Fatalf("Expected create rule %q, got %v", "@request.auth.id != ''", m.CreateRule)
|
||||||
|
}
|
||||||
|
|
||||||
if f := m.Schema.GetFieldByName("title"); f == nil {
|
if f := m.Schema.GetFieldByName("title"); f == nil {
|
||||||
t.Fatalf("Expected schema to be set, got %v", m.Schema)
|
t.Fatalf("Expected schema to be set, got %v", m.Schema)
|
||||||
}
|
}
|
||||||
@ -796,7 +801,7 @@ func TestLoadingDynamicModel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadingDynamicList(t *testing.T) {
|
func TestLoadingArrayOf(t *testing.T) {
|
||||||
app, _ := tests.NewTestApp()
|
app, _ := tests.NewTestApp()
|
||||||
defer app.Cleanup()
|
defer app.Cleanup()
|
||||||
|
|
||||||
@ -806,10 +811,10 @@ func TestLoadingDynamicList(t *testing.T) {
|
|||||||
vm.Set("$app", app)
|
vm.Set("$app", app)
|
||||||
|
|
||||||
_, err := vm.RunString(`
|
_, err := vm.RunString(`
|
||||||
let result = new DynamicList({
|
let result = $arrayOf(new DynamicModel({
|
||||||
id: "",
|
id: "",
|
||||||
text: "",
|
text: "",
|
||||||
})
|
}))
|
||||||
|
|
||||||
$app.dao().db()
|
$app.dao().db()
|
||||||
.select("id", "text")
|
.select("id", "text")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user