Skip to content

Commit

Permalink
Update schemas at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Feb 12, 2025
1 parent cdf8e0f commit cb85c85
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 90 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added

- You can now specify `slugField` in model definitions to configure which field will be used to create slugs when [minting urls](./README.md#url-minting).
- Added the ability to update model schemas with `setSchema`.

### Changed

- `SolidTypeRegistration.forClass` is now declared as an array.
- Default `rdfContext` resolution changed to prioritize the vocab used in `rdfsClass` if present.

Also, check [soukai's release notes](https://github.com/NoelDeMartin/soukai/blob/main/CHANGELOG.md) for further changes.

## [v0.5.2](https://github.com/NoelDeMartin/soukai-solid/releases/tag/v0.5.2) - 2023-11-03

### Added
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"eslint": "^7.20.0",
"jest": "^26.6.3",
"jest-summary-reporter": "0.0.2",
"soukai": "0.5.2-next.8da33ba6f17b4fdf885c4a88ad6284bb35f04580",
"soukai": "0.5.2-next.43d0e6f4702a065b10328dcc42d10626e28ed79f",
"ts-jest": "^26.5.2",
"typescript": "^4.1.5"
}
Expand Down
71 changes: 69 additions & 2 deletions src/models/SolidModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { after, arrayWithout, range, stringToSlug, tap, toString, tt, urlParentDirectory, urlResolve, urlResolveDirectory, urlRoute, uuid } from '@noeldemartin/utils';
import { expandIRI as defaultExpandIRI } from '@noeldemartin/solid-utils';
import { fakeContainerUrl, fakeDocumentUrl, fakeResourceUrl } from '@noeldemartin/testing';
import { FieldType, InMemoryEngine, ModelKey, bootModels, setEngine } from 'soukai';
import { FieldType, InMemoryEngine, ModelKey, TimestampField, bootModels, setEngine } from 'soukai';
import dayjs from 'dayjs';
import { faker } from '@noeldemartin/faker';
import type { EngineDocument, Relation } from 'soukai';
Expand All @@ -22,6 +22,7 @@ import Group from '@/testing/lib/stubs/Group';
import Movie from '@/testing/lib/stubs/Movie';
import MoviesCollection from '@/testing/lib/stubs/MoviesCollection';
import Person from '@/testing/lib/stubs/Person';
import PersonSchema from '@/testing/lib/stubs/Person.schema';
import StubEngine from '@/testing/lib/stubs/StubEngine';
import WatchAction from '@/testing/lib/stubs/WatchAction';
import { assertInstanceOf } from '@/testing/utils';
Expand Down Expand Up @@ -695,6 +696,64 @@ describe('SolidModel', () => {
);
});

it('updates schema definitions', () => {
// Arrange
class StubPerson extends PersonSchema {}

bootModels({ StubPerson });

expect(StubPerson.primaryKey).toEqual('url');
expect(StubPerson.timestamps).toEqual([TimestampField.CreatedAt]);
expect(StubPerson.fields.name).toEqual({
type: FieldType.String,
required: false,
rdfProperty: 'http://xmlns.com/foaf/0.1/name',
rdfPropertyAliases: [],
});
expect(StubPerson.rdfContexts.default).toEqual('http://xmlns.com/foaf/0.1/');
expect(StubPerson.rdfContexts.vcard).toEqual('http://www.w3.org/2006/vcard/ns#');
expect(StubPerson.rdfsClasses).toEqual(['http://xmlns.com/foaf/0.1/Person']);

// Act
StubPerson.setSchema({
primaryKey: 'uuid',
rdfContext: 'https://schema.org/',
rdfsClass: 'Person',
fields: {
name: FieldType.String,
bornAt: {
type: FieldType.Date,
rdfProperty: 'birthDate',
},
},
});

// Assert
expect(StubPerson.primaryKey).toEqual('uuid');
expect(StubPerson.timestamps).toEqual([TimestampField.CreatedAt, TimestampField.UpdatedAt]);
expect(StubPerson.fields).toEqual({
uuid: {
type: FieldType.Key,
required: false,
},
name: {
type: FieldType.String,
required: false,
rdfProperty: 'https://schema.org/name',
rdfPropertyAliases: [],
},
bornAt: {
type: FieldType.Date,
required: false,
rdfProperty: 'https://schema.org/birthDate',
rdfPropertyAliases: [],
},
});
expect(StubPerson.rdfContexts.default).toEqual('https://schema.org/');
expect(StubPerson.rdfContexts.vcard).toBeUndefined();
expect(StubPerson.rdfsClasses).toEqual(['https://schema.org/Person']);
});

it('doesn\'t mint urls for new models if disabled', async () => {
// Arrange
class StubModel extends SolidModel {
Expand Down Expand Up @@ -1332,8 +1391,10 @@ describe('SolidModel', () => {
// Arrange
const containerUrl = fakeContainerUrl();
const name = faker.random.word();
const nickName = faker.random.word();
const person = new Person({
name,
nickName,
url: urlResolve(containerUrl, faker.datatype.uuid()),
friendUrls: [
urlResolve(containerUrl, faker.datatype.uuid()),
Expand All @@ -1354,9 +1415,11 @@ describe('SolidModel', () => {
'@vocab': 'http://xmlns.com/foaf/0.1/',
'crdt': 'https://vocab.noeldemartin.com/crdt/',
'metadata': { '@reverse': 'crdt:resource' },
'vcard': 'http://www.w3.org/2006/vcard/ns#',
},
'@type': 'Person',
'name': name,
'vcard:nickname': nickName,
'knows': friendUrls.map(url => ({ '@id': url })),
'metadata': {
'@id': person.url + '-metadata',
Expand Down Expand Up @@ -1428,6 +1491,7 @@ describe('SolidModel', () => {
'@vocab': 'http://xmlns.com/foaf/0.1/',
'crdt': 'https://vocab.noeldemartin.com/crdt/',
'metadata': { '@reverse': 'crdt:resource' },
'vcard': 'http://www.w3.org/2006/vcard/ns#',
},
'@type': 'Group',
'@id': mugiwara.url,
Expand Down Expand Up @@ -1494,7 +1558,10 @@ describe('SolidModel', () => {

// Assert
expect(jsonLd).toEqual({
'@context': { '@vocab': 'http://xmlns.com/foaf/0.1/' },
'@context': {
'@vocab': 'http://xmlns.com/foaf/0.1/',
'vcard': 'http://www.w3.org/2006/vcard/ns#',
},
'@type': 'Group',
'name': 'Mugiwara',
'member': [
Expand Down
Loading

0 comments on commit cb85c85

Please sign in to comment.