Skip to content

Function NewQuery

basic create model

typescript
const model = await new FedacoTestUser().NewQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
ReferenceLooks LikeValue
model.emailexactly match'linbolen@gradii.com'

see also prerequisites

basic model collection retrieval

typescript
await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await FedacoTestUser.createQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
const models = await new FedacoTestUser().NewQuery().oldest('id').get();
ReferenceLooks LikeValue
isArray(models)exactly matchtrue
models[0]instance type exactly matchFedacoTestUser
models[1]instance type exactly matchFedacoTestUser
models[0].emailexactly match'linbolen@gradii.com'
models[1].emailexactly match'xsilen@gradii.com'

see also prerequisites

basic model retrieval

typescript
const factory = new FedacoTestUser();
await factory.NewQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await factory.NewQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
ReferenceLooks LikeValue
await factory.NewQuery().where('email', 'linbolen@gradii.com').doesntExist()exactly matchfalse
await factory.NewQuery().where('email', 'mohamed@laravel.com').doesntExist()exactly matchtrue
ReferenceLooks LikeValue
model.emailexactly match'linbolen@gradii.com'
model.email !== undefinedexactly matchtrue
ReferenceLooks LikeValue
friends !== undefinedexactly matchtrue
friendsmatch[]
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch1
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch2
ReferenceLooks LikeValue
missingexactly matchUndefined();
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match0
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match2
typescript
// .cursor();
for (const m of models) {
  expect(m.id).toEqual(1);
  expect(m.getConnectionName()).toBe('default');
}

see also prerequisites

fresh method on model

typescript
const now = new Date();
const nowSerialized = formatISO(startOfSecond(now));
const nowWithFractionsSerialized = now.toJSON();
// Carbon.setTestNow(now);
const storedUser1 = await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com',
  birthday: now
});
await storedUser1.NewQuery().update({
  email: 'dev@mathieutu.ovh',
  name: 'Mathieu TUDISCO'
});
const freshStoredUser1 = await storedUser1.Fresh();
const storedUser2 = await FedacoTestUser.createQuery().create({
  id: 2,
  email: 'linbolen@gradii.com',
  birthday: now
});
await storedUser2.NewQuery().update({
  email: 'dev@mathieutu.ovh'
});
const freshStoredUser2 = await storedUser2.Fresh();
const notStoredUser = FedacoTestUser.initAttributes({
  id: 3,
  email: 'linbolen@gradii.com',
  birthday: now
});
const freshNotStoredUser = await notStoredUser.Fresh();
ReferenceLooks LikeValue
freshStoredUser1.toArray()match`({
  'id'        : 1,
  'name'      : 'Mathieu TUDISCO',
  'email'     : 'dev@mathieutu.ovh',
  'birthday'  : nowWithFractionsSerialized,
  'created_at': nowSerialized,
  'updated_at': nowSerialized
});` |

| storedUser1 | instance type exactly match | FedacoTestUser | | storedUser2.toArray() | match | ({ 'id' : 2, 'email' : 'linbolen@gradii.com', 'birthday' : nowWithFractionsSerialized, 'created_at': nowSerialized, 'updated_at': nowSerialized }); | | freshStoredUser2.toArray() | match | ({ 'id' : 2, 'name' : null, 'email' : 'dev@mathieutu.ovh', 'birthday' : nowWithFractionsSerialized, 'created_at': nowSerialized, 'updated_at': nowSerialized }); | | storedUser2 | instance type exactly match | FedacoTestUser | | notStoredUser.toArray() | match | ({ 'id' : 3, 'email' : 'linbolen@gradii.com', 'birthday': nowWithFractionsSerialized }); | | freshNotStoredUser | exactly match | null |


see also prerequisites

paginated model collection retrieval when no elements and default per page

typescript
const models = await new FedacoTestUser().NewQuery().oldest('id').paginate();

see also prerequisites

paginated model collection retrieval when no elements

typescript
// Paginator.currentPageResolver(() => {
//   return 1;
// });
let models = await new FedacoTestUser().NewQuery().oldest('id').paginate(1, 2);
typescript
// expect(models).toInstanceOf(LengthAwarePaginator);
// Paginator.currentPageResolver(() => {
//   return 2;
// });
models = await new FedacoTestUser().NewQuery().oldest('id').paginate(2, 2);

see also prerequisites

paginated model collection retrieval

typescript
await new FedacoTestUser().NewQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await new FedacoTestUser().NewQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
await new FedacoTestUser().NewQuery().create({
  id: 3,
  email: 'foo@gmail.com'
});
// Paginator.currentPageResolver(() => {
//   return 1;
// });
let models = await new FedacoTestUser().NewQuery().oldest('id').paginate(1, 2);
ReferenceLooks LikeValue
models.items[0]instance type exactly matchFedacoTestUser
models.items[1]instance type exactly matchFedacoTestUser
models.items[0].emailexactly match'linbolen@gradii.com'
models.items[1].emailexactly match'xsilen@gradii.com'
ReferenceLooks LikeValue
models.items.lengthexactly match1
models.items[0]instance type exactly matchFedacoTestUser
models.items[0].emailexactly match'foo@gmail.com'

see also prerequisites

Released under the MIT License.